Alex
Alex

Reputation: 1659

How to make an Action Bar containing a title and 2 buttons?

I would like to make an Action Bar like this one
I tried the sample in the Android sdk but it's too complicated there are like 8 .java files for this Action Bar?


ActionBarCompat


I hope someone can provide me with an easier method and simpler way to do it. As I don't think i'm gonna copy all those 8 files to my project in order to make an Action Bar work.

Upvotes: 1

Views: 359

Answers (1)

Festus Tamakloe
Festus Tamakloe

Reputation: 11310

This is an approach.

Beginns add a meny layout in your res/menu folder action_bar_menu.xml

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >

    <item
        android:id="@+id/action_one"
        android:icon="@drawable/icon1.png"
        android:showAsAction="always"
        android:title="One">
    </item>
    <item
        android:id="@+id/action_two"
        android:icon="@drawable/icon2.png"
        android:showAsAction="always"
        android:title="Two">
    </item>

</menu>

in your activity

 @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    ActionBar actionBar = getActionBar();
    actionBar.setTitle("your title"); 
    // add the custom view to the action bar
    //actionBar.setCustomView(R.layout.actionbar_view);

    actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM
        | ActionBar.DISPLAY_SHOW_HOME);
  }


 @Override
   public boolean onCreateOptionsMenu(Menu menu) {
      getMenuInflater().inflate(R.menu.action_bar_menu, menu);
      return true;
   }

   @Override
   public boolean onOptionsItemSelected(MenuItem item) {
      switch (item.getItemId()) {
      case R.id.action_one:
         //put your business logic here
         break;
      case R.id.action_two:
         //put your business logic here
         break;
     case android.R.id.home:
        //put your business logic here
     break;
      default:
         // Nothing to do here 
      }
      return true;
   }

Upvotes: 2

Related Questions