chanaka chanaka - 3 months ago 13
Android Question

How to add an actionbar in android studio for beginners?

I want to build my own app and i want to add an action bar to it.In my main layout i want an action bar with my app name only.In my other layouts/pages i want my page name and a back navigating symbol which navigates back to the previous page.Can anybody tell me how to do this and also can somebody tell me about the default actionbar in android what it does and what do i do with it.

Answer

Change your layout theme to "Theme.DeviceDefault.Light.DarkActionBar"

Make action bar menu layout under menu folder

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

<item android:id="@+id/b"
      android:icon="@drawable/b"
      android:showAsAction="always"
      android:title=""/>

<item android:id="@+id/c"
      android:icon="@drawable/c"
      android:showAsAction="always"
      android:title=""/>

<item android:id="@+id/d"
      android:icon="@drawable/d"
      android:showAsAction="always"
      android:title=""/>

<item android:id="@+id/e"
      android:icon="@drawable/e"
      android:showAsAction="always"
      android:title=""/>

Then write the following script to your activity class

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.actionbar_menu, menu);
    return super.onCreateOptionsMenu(menu);
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
        case R.id.a:
            //Write your code
            return true;
        case R.id.b:
            //Write your code
            return true;
        case R.id.c:
            //Write your code
            return true;
        case R.id.d:
            //Write your code
            return true;
        case R.id.e:
            //Write your code
            return true;
        default:
            return super.onOptionsItemSelected(item);
    }
}
Comments