SearchView widget not displaying underline

908 views Asked by At

I want to add underline to searchview widget like the blue underline in the given image below.But my searchview doesn't display any underline.Even when i type text no underline is displayed.Below is the code i am using.

menu_home.xml

  <menu xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        tools:context="com.peoplecloud.app.guggu.activities.HomeActivity">
        <item
            android:id="@+id/menu_item_search"

            android:icon="@drawable/icon_search"


            app:actionViewClass="android.support.v7.widget.SearchView"
            app:showAsAction="always"

            >
        </item>
    </menu>

Code-

  @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        menu.clear();
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.menu_home, menu);
        searchItem = menu.findItem(R.id.menu_item_search);
        searchItem.setVisible(false);
        mSearchView = (SearchView) MenuItemCompat.getActionView(menu.findItem(R.id.menu_item_search));
        mSearchView.setQueryHint("Search GIFs");

        setupSearchView(searchItem);
        return super.onCreateOptionsMenu(menu);
    }
private void setupSearchView(MenuItem searchItem) {
        searchItem.setShowAsActionFlags(MenuItem.SHOW_AS_ACTION_IF_ROOM
                | MenuItem.SHOW_AS_ACTION_COLLAPSE_ACTION_VIEW);

        mSearchView.setQuery("test", true);
        mSearchView.setOnQueryTextListener(this);
        mSearchView.setFocusable(true);
        mSearchView.requestFocusFromTouch();

        mSearchView.setOnQueryTextFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View view, boolean hasFocus)
            {
                if (hasFocus) {
                    showInputMethod(view.findFocus());
                }
            }
        });
        mSearchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {

            @Override
            public boolean onQueryTextSubmit(String s)
            {
                ...
                return true;
            }

            @Override
            public boolean onQueryTextChange(String s) {
                return false;
            }
        });

    }

enter image description here

1

There are 1 answers

1
Alok Nair On

Try the following, here a custom search view layout is added.

menu_home.xml

<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    tools:context="com.peoplecloud.app.guggu.activities.HomeActivity">
    <item
        android:id="@+id/menu_item_search"
        android:icon="@drawable/icon_search"
        app:actionLayout="@layout/search_layout"
        app:showAsAction="always">
    </item>
</menu>

search_layout.xml

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

    <EditText
        android:id="@+id/txt_search"
        android:textSize="16dp"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:hint="Search..."
        android:inputType="text"
        android:singleLine="true" />

</RelativeLayout>

onCreateOptionsMenu

/** Get the action view of the menu item whose id is search */
    View v = (View) menu.findItem(R.id. menu_item_search).getActionView();

 /** Get the edit text from the action view and add a text changed listener i.e a TextWatcher. 
 Here textWatcher is a TextWatcher object*/
     txtSearch = (EditText) v.findViewById(R.id.txt_search);
     txtSearch.addTextChangedListener(textWatcher);