home.


Tagged: android-actionbar


Android: Actionbar setup

Create an options menu as normal. I.e. in your onCreate() method issue:

 setHasOptionsMenu(true);

And override methods to create the options menu

 @Override
 public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    inflater.inflate(R.menu.yourmenu, menu);
    super.onCreateOptionsMenu(menu, inflater);
 }

The yourmenu menu will live in res/menu/yourmenu.xml and look something like:

 <menu xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:id="@+id/menu_refresh"
        android:title="@string/menu_refresh"
        android:icon="@drawable/youricon"
        android:showAsAction="always"/>
 </menu>

The showAsAction attribute means you’ll always see it - it won’t be folded into a menu.

Et voila.

android android-actionbar

Android: ActionBar home up button

To create a little back or up icon near your icon on the action bar, call getActionBar().setDisplayHomeAsUpEnabled(true);

In the onMenuItemSelected method, listen for the android.R.id.home item. Then either finish() the activity to go back to previous one, or start a new activity if you want.

    @Override

    public boolean onMenuItemSelected(int featureId, MenuItem item) {

        switch (item.getItemId()) {

         case android.R.id.home:

          //finish();

          return true;        

         default:

          break;

       }

        return super.onMenuItemSelected(featureId, item);

    }
android android-actionbar

Page 1 of 1