0

I want to have an action bar with AutoCompleteTextView(Collapsible) as a menuItem along with other menuItems like Delete. I am using SherlockActionBar's library to make it compatible with lower version(like 2.2, 2.3).

My requirement is to display

  1. a searchIcon (for collapsible AutoCompleteTextView) at extreme right of the actionBar and

  2. a delete icon to left of above mentioned searchIcon.

Now when user taps on searchIcon, it should expand itself to cover the entire actionBar (hidinig delete icon).

I had tried wrapping both the iconsearch(for AutoCompleteTextView) and deleteButton in a layout and providing this layout as a view for action bar provided by SherlockActionBar library but it didn't displayed anything.

Then I tried adding iconSearch (with attribute value as collapsible) and deleteButton as individual menu items (shown in the SherlockActionBar Samples) but it just displays actionSearch.

I have no idea how to implement the same using SherlockActionBar library. I have tried to implement the same by following the samples given but couldn't achieve this.

Any help would be appreciated...

Thanks in advance.

AndoAiron
  • 694
  • 5
  • 19
  • 2
    check http://stackoverflow.com/a/15826444/1168654 and http://stackoverflow.com/q/15804805/1168654 read all question and answer before rpl here.. – Dhaval Parmar Apr 11 '13 at 11:30
  • @DhavalSodhaParmar I have checked the above mentioned links but these links doesn't provide any help for my problem. – AndoAiron Apr 11 '13 at 12:08

3 Answers3

2

First create your xml layout from search (layout_search.xml):

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" >

    <AutoCompleteTextView
        android:id="@+id/edit_search"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="Search" >

        <requestFocus />
    </AutoCompleteTextView>

    <ImageView
        android:id="@+id/img_clear"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="right"
        android:src="@drawable/ic_menu_clear" />

</FrameLayout>

In your menu xml:

<item
    android:id="@+id/menu_search"
    android:icon="@drawable/ic_menu_search"
    android:showAsAction="always|collapseActionView"
    android:title="Search">
</item>

Creating menus:

    @Override
        public boolean onCreateOptionsMenu(Menu menu) {
            getSupportMenuInflater().inflate(R.menuyour_menu, menu);        
            menu.getItem(0).setActionView(R.layout.layout_search);
            AutoCompleteTextView search = (AutoCompleteTextView) mI.getActionView().findViewById(R.id.edit_search); 
            ImageView imgClear = (ImageView) menu.getItem(0).getActionView().findViewById(R.id.img_clear).setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                search.setText("");
            }
        });

            return super.onCreateOptionsMenu(menu);
        }
andrehsouza
  • 479
  • 1
  • 5
  • 14
1

I think you should use ready-made solution com.actionbarsherlock.widget.SearchView.

menu.xml

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
    <item
        android:id="@+id/menu_remove"
        android:icon="@drawable/content_remove"
        android:showAsAction="ifRoom"
        android:title="@string/menu_remove" />
    <item
        android:id="@+id/menu_search"
        android:actionViewClass="com.actionbarsherlock.widget.SearchView"
        android:icon="@drawable/action_search"
        android:showAsAction="always|collapseActionView"
        android:title="@string/search" />
</menu>

MainActivity.java

@Override
public boolean onCreateOptionsMenu(Menu menu) {

    // Inflate the options menu from XML
    MenuInflater inflater = getSupportMenuInflater();
    inflater.inflate(R.menu.main, menu);

    return super.onCreateOptionsMenu(menu);
}

result: Android ActionBarSherlock Search http://img6.imageshack.us/img6/7448/androidsearchactionbar.png

Rafal Gałka
  • 1,022
  • 1
  • 10
  • 16
  • The problem with this SearchView is that it accepts only CursorAdapter but I have an arraylist of POJO objects that needs to be displayed as search results. – AndoAiron Apr 16 '13 at 10:13
0

I am done with the required implementation. Thanks to Rafal Galka & andrehsouza. I had to combine both the solutions (provided by Rafal Galka & andrehsouza) to achieve the desired effect.

What I did is, I created a menu.xml with two action items. One of them is a standard action item with normal icon and title and another one is with the custom view. And this custom view is nothing but just a simple AutoCompleteTextView which has been defined under res/layout folder.

So here is my res/menu/menu.xml

<menu xmlns:android="http://schemas.android.com/apk/res/android" >
    <item
        android:id="@+id/menu_xxx"
        android:showAsAction="ifRoom|withText"
        android:icon="@drawable/drawablexxx"
        android:title="@string/xxx"/>

    <item
        android:id="@+id/menu_search"
        android:actionLayout="@layout/collapsibleactionview"
        android:showAsAction="always|collapseActionView"
        android:title="@string/action_search"/>
</menu>

and collapsibleactionview.xml has been defined under res/layout as :

<?xml version="1.0" encoding="utf-8"?>
<AutoCompleteTextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/edittextcollapsible"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:hint="@string/hint search"
    android:imeOptions="actionSearch"
    android:inputType="text" />

Bingo !!!

Community
  • 1
  • 1
AndoAiron
  • 694
  • 5
  • 19