I'm using a SearchView in my Activity. As the user types, I am performing search requests to a server. I want to indicate that some activity is happening. Is it possible to display a progress spinner within the SearchView?
Otherwise, how are people handling this - do we create a custom actionbar parent layout, and within that embed the SearchView?
<LinearLayout orientation="horizontal">
    <SearchView />
    <ProgressBar />
</LinearLayout>
Thank you
First create the layout for the progressbar. A XML like this should do the job:
R.layout.loading_icon
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <ProgressBar
        style="?android:attr/progressBarStyleLarge"
        android:layout_width="25dp"
        android:layout_height="25dp"
        android:id="@+id/search_progress_bar"
        android:layout_marginTop="5dp"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true" />
</RelativeLayout>
Next create two functions. One to show the progress bar on your searchview and other to hide it:
public void showProgressBar(SearchView searchView, Context context)
{
    int id = searchView.getContext().getResources().getIdentifier("android:id/search_plate", null, null);
    if (searchView.findViewById(id).findViewById(R.id.search_progress_bar) != null)
        searchView.findViewById(id).findViewById(R.id.search_progress_bar).animate().setDuration(200).alpha(1).start();
    else
    {
        View v = LayoutInflater.from(context).inflate(R.layout.loading_icon, null);
        ((ViewGroup) searchView.findViewById(id)).addView(v, 1);
    }
}
public void hideProgressBar(SearchView searchView)
{
    int id = searchView.getContext().getResources().getIdentifier("android:id/search_plate", null, null);
    if (searchView.findViewById(id).findViewById(R.id.search_progress_bar) != null)
        searchView.findViewById(id).findViewById(R.id.search_progress_bar).animate().setDuration(200).alpha(0).start();
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With