0

I'm trying to add button to the Action Bar with no luck. There are no errors. Button is in the @drawable. But it doesn't show up. What am I doing wrong?

(It asks me to add more details, but I have nothing more to add)

test activity :

import android.content.Intent; import android.os.Bundle; import android.support.v7.app.ActionBarActivity; import android.view.Menu; import android.view.MenuInflater; import android.view.View; import android.widget.Button; public class test extends ActionBarActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.test); setupCREATEbut(); } private void setupCREATEbut() { Button CREATEbut = (Button) findViewById (R.id.appenter2); CREATEbut.setOnClickListener(new View.OnClickListener(){ @Override public void onClick(View v){ Intent intent = new Intent(v.getContext(), MainActivity.class); startActivityForResult(intent,0); } }); } @Override public boolean onCreateOptionsMenu(Menu menu) { MenuInflater inflater = getMenuInflater(); inflater.inflate(R.menu.topmenu, menu); return super.onCreateOptionsMenu(menu); } } 

test xml :

<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="rs.test"> <LinearLayout android:id="@+id/centerblock" android:layout_width="400dp" android:layout_height="wrap_content" android:layout_centerHorizontal="true" android:layout_centerVertical="true" android:orientation="vertical" > <RelativeLayout android:id="@+id/relcenterblock" android:layout_width="fill_parent" android:layout_height="415dp" > <Button android:id="@+id/appenter2" android:layout_width="160dip" android:layout_height="160dip" android:layout_centerHorizontal="true" android:layout_centerVertical="true" android:background="@drawable/bigroundbutton" android:gravity="center" android:text="@string/request" android:textColor="#ffffff" android:textSize="30sp" /> </RelativeLayout> </LinearLayout> </RelativeLayout> 

topmenu xml :

 <menu xmlns:android="http://schemas.android.com/apk/res/android" > <item android:id="@+id/action_search" android:icon="@drawable/ic_action_toprightmenu" android:title="@string/toprightmenu"/> </menu> 
3
  • Just to complement... i suggest you to use AppCompatActivity instead of ActionBarActivity that is deprecated. Commented Jul 30, 2015 at 1:20
  • Thanks ) Should I just change it here "public class test extends ActionBarActivity" or is this more complicated ? ) Commented Jul 30, 2015 at 2:32
  • 1
    Yes, just change to extends AppCompatActivity. Commented Jul 30, 2015 at 13:07

1 Answer 1

2

Try to use showAsAction=always or showAsAction=ifRoom.

<?xml version="1.0" encoding="utf-8"?> <menu xmlns:android="http://schemas.android.com/apk/res/android" xmlns:yourapp="http://schemas.android.com/apk/res-auto"> <item android:id="@+id/action_search" android:icon="@drawable/ic_action_toprightmenu" android:title="@string/toprightmenu" yourapp:showAsAction="always"/> </menu> 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.