How I can add Edit Text field and button in ActionBar?

15,774

Solution 1

Putting a regular Button in the action bar will look fairly awful, IMHO.

That being said, you can use android:actionLayout on your <item> element in your menu XML resource. This should point to the name of a layout XML resource. This resource will be inflated into the action bar. You can get the widgets by calling getActionView() on the MenuItem object corresponding to this <item> element.

Here is a sample project demonstrating this. Here is the documentation on this technique.

Solution 2

So you should check out the documentation for the action bar sherlock found at the website.

Basically to add something to the action bar sherlock you have to add it in terms of Menu items like as follows :

    @Override
public boolean onCreateOptionsMenu(Menu menu) {
            //inflate with your particular xml
    MenuInflater inflater = getSupportMenuInflater();
    inflater.inflate(R.menu.child_add_menu, menu);
    return super.onCreateOptionsMenu(menu);
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    //your code here

}

You can also check out the question that is asked at : ActionBarSherlock with multiple MenuItems?

Share:
15,774
Manzur Husain
Author by

Manzur Husain

Updated on July 22, 2022

Comments

  • Manzur Husain
    Manzur Husain almost 2 years

    I am using actionbarsherlock and I want to use an Edit Text and a Button in the action bar. I I have gone through many documents and posts but haven't found a solution yet.

    How do I add an Edit Text in the Android action bar?