felipepo
felipepo

Reputation: 57

Progress Bar inside Spinner in Android

I am trying to add a ProgressBar inside a Spinner when there are 0 items on the Spinner on my Android app, but no success so far. My Spinner uses a simple ArrayAdapter since it shows only text.

I have tried to change to a custom adapter as follows, but no luck.

public class SpinnerAdapter  extends BaseAdapter {

    ArrayList<String> result;

Context context;
private static LayoutInflater inflater = null;

public SpinnerAdapter(Context context, ArrayList<String> items) {
    result = items;
    this.context = context;
    inflater = (LayoutInflater)context.
            getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}

@Override
public int getCount() {
    return result.size();
}

@Override
public Object getItem(int position) {
    return position;
}

@Override
public long getItemId(int position) {
    return position;
}

@Override
public View getView(final int position, View convertView, ViewGroup parent) {

    ProgressBar bar = (ProgressBar) ((Activity)context).findViewById(R.id.spinner_item_progress_bar);

    if(result.size() > 0) {
        bar.setVisibility(View.INVISIBLE);
    } else {
        bar.setIndeterminate(true);
        bar.setVisibility(View.VISIBLE);
    }

    View rowView;

    if(convertView == null) {
        rowView = inflater.inflate(R.layout.spinner_item, null);
        TextView tv = (TextView) ((Activity)context).findViewById(R.id.spinner_item_text_view);
        tv.setText(result.get(position));
    }
    else {
        rowView = convertView;
        }

        return rowView;
    }
}

And my SpinnerItem:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">

    <ProgressBar
        android:id="@+id/spinner_item_progress_bar"
        style="?android:attr/progressBarStyle"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

    <TextView
        android:id="@+id/spinner_item_text_view"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="TextView" />
</LinearLayout>

I have done the same on my iOS app and I am going to add an image so I can show the behavior I want on my Android app.

enter image description here

So, this is a "Loading ..." UIView with a UIActivityIndicatorView. This view is added to the UIPickerView when there are 0 itens within the UIPickerView. Whenever an item is added to the picker, this UIView is removed. Works perfectly on iOS.

Any clue on how to do this on Android?

Thank you.

Upvotes: 1

Views: 1124

Answers (2)

felipepo
felipepo

Reputation: 57

Solved the issue. So, I kept the basic ArrayAdapter instead of overloading the BaseAdapter. Then, I went to the layout file of the Fragment that holds the Spinner and added the following:

<RelativeLayout
    android:padding="5dp">

    <Spinner
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/search_spinner" />

        <LinearLayout
            android:orientation="horizontal"
            android:gravity="center"
            android:layout_width="match_parent"
            android:layout_height="match_parent">

            <ProgressBar
                android:id="@+id/progressBar"
                style="?android:attr/progressBarStyleSmall"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" />

                <TextView
                    android:id="@+id/spinner_item_progress_bar_label"
                    android:text="Carregando..."
                    android:paddingLeft="8dp"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"/>
    </LinearLayout>
</RelativeLayout>

And then, on my Fragment class when I want the bar to be visible:

ProgressBar bar = (ProgressBar) getActivity().findViewById(R.id.progressBar);
bar.setIndeterminate(true);
bar.setVisibility(View.VISIBLE);

and when I want it to dissapear:

bar.setVisibility(View.INVISIBLE);

This is how it looks at the end and it is satisfactory to me:

enter image description here

Upvotes: 0

mmmatey
mmmatey

Reputation: 684

So for example you can do it with "ViewSwitcher". Put both inside viewswitcher and then in code when you have data just call method on switcher "showNext()" or "showPrevius()"...

<ViewSwitcher
        android:id="@+id/switcher"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

<ProgressBar
        android:id="@+id/spinner_item_progress_bar"
        style="?android:attr/progressBarStyle"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />
<Spinner
       android:id="@+id/spinner_item_progress_bar"
        style="?android:attr/progressBarStyle"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"


</ViewSwitcher>

Upvotes: 2

Related Questions