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
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();
}
I know that it is not within the SearchView, but this is by far the simplest method to provide progress indication in the ActionBar found here http://blog.denevell.org/android-progress-spinner.html:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
setContentView(R.layout.activity_main);
// Turn it on
setProgressBarIndeterminateVisibility(true);
// And when you want to turn it off
setProgressBarIndeterminateVisibility(false);
}
If you really want the progress to appear inside the SearchView, I would assume that you could use a FrameLayout instead of LinearLayout (or atleast as a parent), putting the progress in bottom of the XML (which will place it on top of the SearchView).
If you are using a custom SearchView class that extends android.support.v7.widget.SearchView, you'll need to do this:
public void init(Context context){
EditText searchPlate = (EditText) findViewById(android.support.v7.appcompat.R.id.search_src_text);
//searchPlate.setHint("Search");
mSearchPlateView = findViewById(android.support.v7.appcompat.R.id.search_plate);
mSearchPlateView.setBackgroundColor(ContextCompat.getColor(context, android.R.color.transparent));
}
public void showProgressBar(Context context) {
if (mProgressBar != null) {
mProgressBar.animate()
.setDuration(200)
.alpha(1)
.start();
}
else {
RelativeLayout relativeLayout = (RelativeLayout) LayoutInflater.from(context).inflate(R.layout.invest_progress_loading, null);
mProgressBar = (ProgressBar) relativeLayout.findViewById(R.id.search_progress_bar);
((ViewGroup) mSearchPlateView).addView(relativeLayout, 1);
}
}
public void hideProgressBar() {
if(mProgressBar == null)
return;
mProgressBar.animate()
.setDuration(200)
.alpha(0)
.start();
}