Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change the position of menu items on actionbar

I'm developing one application in which I have to add a custom layout on actionbar. Adding the custom layout is done, but when I'm adding the menu items on actionbar my custom layout changes it's position from right of the actionbar to center of the actionbar. Below is the image what I have achieved so far.

Done till here

I want something like this on the actionbar.

Like this

Custom layout(yellow button) at the right most part of actionbar and menu items in the middle.

Adding my code to achieve this custom layout using native android actionbar:

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

    getActionBar().setDisplayHomeAsUpEnabled(true);
    getActionBar().setHomeButtonEnabled(true);

    actionBar = getActionBar();
    actionBar.setDisplayShowTitleEnabled(false);
    actionBar.setDisplayUseLogoEnabled(false);
    actionBar.setDisplayHomeAsUpEnabled(false);
    actionBar.setDisplayShowCustomEnabled(true);
    cView = getLayoutInflater().inflate(R.layout.header, null);
    actionBar.setCustomView(cView);

}

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

}

@Override
public boolean onOptionsItemSelected(MenuItem item) {

    switch (item.getItemId()) {

    case android.R.id.home:
        finish();

    default:
        return super.onOptionsItemSelected(item);
    }
}

How can this be achieved?

Any kind of help will be appreciated.

like image 602
Anupam Avatar asked Apr 18 '13 11:04

Anupam


People also ask

How do I customize my action bar?

This example demonstrate about how to create a custom action bar in Android. Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project. Step 2 − Add the following code to res/layout/activity_main. xml.

Which method is used to create menu items on ActionBar?

The Android tooling automatically creates a reference to menu item entries in the R file, so that the menu resource can be accessed. An activity adds entries to the action bar in its onCreateOptionsMenu() method. The showAsAction attribute allows you to define how the action is displayed.

How will you replace a menu with the action bar?

To replace an app's default action bar with a Toolbar : Create a new custom theme and modify the app's properties so that it uses this new theme. Disable the windowActionBar attribute in the custom theme and enable the windowNoTitle attribute. Define a layout for the Toolbar .


1 Answers

Just use orderInCategory in your xml file and it will order from less number to greater (from left side of your action bar). Such as:

<item
    android:id="@+id/Refresh"
    android:icon="@drawable/ic_action_refresh"
    android:showAsAction="ifRoom"
    android:title="@string/refresh"
    android:orderInCategory="2"/>
<item
    android:id="@+id/Search"
    android:icon="@drawable/ic_action_action_search"
    android:showAsAction="ifRoom|collapseActionView"
    android:title="@string/search"
    android:orderInCategory="1"/>

Even though, Refresh item is coming first in xml file, it won't be first. But Search icon from left to rigth in your action bar will be first according to its orderInCategory attribute.

like image 157
Rashid Avatar answered Sep 21 '22 08:09

Rashid