VanDir
VanDir

Reputation: 2030

No HomeAsUp button in my ActionBar when the menu is not setted

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:

  1. In the ActionBar doesn't appear any button to return to the previous activity.
  2. If I set a menu from a resource like R.menu.example then the homeAsUp button appears.
  3. If I set an empty menu (a menu without items) then the homeAsUp doesn't appear.

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

Answers (1)

Revathi
Revathi

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

Related Questions