nmvictor
nmvictor

Reputation: 1129

Customizing SearchView Programmatically

I want to customize the 'edittext' of the Search View widget in my application.I want to programmatically set its background drawable. I also want to programmatically add the flag android:imeOptions="flagNoExtractUi" so that on land scape mode, soft keyboard will only cover half the screen.

Anyone know how I can make this customizations on the search views 'edittext'?

The search view

Upvotes: 3

Views: 2088

Answers (1)

nmvictor
nmvictor

Reputation: 1129

This is what I did. I studied abs_search_view.xml in action bar sherlock library and learnt that R.id.search_plate represents a LinearLayour. The first child of the LinearLayout is a view whose class is com.actionbarsherlock.widget.SearchView$SearchAutoComplete. So I retrieved the first child and cast it to com.actionbarsherlock.widget.SearchView.SearchAutoComplete, did a null check and later set the ime options with searchText.setImeOptions(EditorInfo.IME_FLAG_NO_EXTRACT_UI) as adviced by Ahmend in the comments. Hope this helps someone.

public static void styleSearchView(SearchView searchView, Context context) {
    LinearLayout searchPlate = (LinearLayout) searchView
            .findViewById(R.id.abs__search_plate);
    // TODO
    // searchPlate.setBackgroundResource(R.drawable.your_custom_drawable);
    if (searchPlate != null)
        ((com.actionbarsherlock.widget.SearchView.SearchAutoComplete) searchPlate
                .getChildAt(0))
                .setImeOptions(EditorInfo.IME_FLAG_NO_EXTRACT_UI);
    AutoCompleteTextView searchText = (AutoCompleteTextView) searchView
            .findViewById(R.id.abs__search_src_text);
    if (searchText != null)
        searchText.setImeOptions(EditorInfo.IME_FLAG_NO_EXTRACT_UI);
    // TODO
    // searchText.setHintTextColor(context.getResources().getColor(R.color.your_custom_color));
}

enter image description here

Upvotes: 1

Related Questions