Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Put a progress spinner in a SearchView?

Tags:

android

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

like image 939
user3203425 Avatar asked Jan 10 '23 16:01

user3203425


1 Answers

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();
}
like image 164
Pedro Oliveira Avatar answered Jan 12 '23 05:01

Pedro Oliveira