Reputation: 2030
I have an activity like that:
public class MapActivity extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_map);
ActionBar actionBar = getSupportActionBar();
actionBar.setCustomView(R.layout.action_bar_layout);
actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM | ActionBar.DISPLAY_SHOW_HOME
| ActionBar.DISPLAY_HOME_AS_UP);
actionBar.setDisplayHomeAsUpEnabled(true);
}
}
The layout action_bar_layout.xml simply contains two TextViews in the center.
PROBLEM:
WHAT I WANT:
I want to display an ActionBar with a custom layout and the homeAsUp button without any menu. How can I do that?
Upvotes: 0
Views: 166
Reputation: 172
Use this in your Activity
's onCreate()
ActionBar actionBar = getSupportActionBar();
actionBar.setCustomView(R.layout.custom_actionbar);
actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM|ActionBar.DISPLAY_SHOW_HOME |ActionBar.DISPLAY_HOME_AS_UP);
actionBar.setDisplayHomeAsUpEnabled(true);
create a new xml custom_actionbar.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical" >
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="custom" />
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android`enter code here`:layout_height="wrap_content"
android:text="custom" />
</LinearLayout>
in your activity remove all the over ridden methods like onCreateOptionsMenu() and onOptionsItemSelected()
. I have executed this code. It shows actionbar with only two textviews, no menu option and with homeAsUp button enabled.
Upvotes: 1