1

I want to use progressbar in my android project, where user select multiple files to upload to server.

I want to show progressbar for each file upload progress. I don't want to use ProgressDialog to do this task. I am planning to show all the selected files upload progress in a recyclerview(Each progressbar in a row), So that the user scroll through all the progressbars.

I did not find any good tutorial for this. I have worked on recyclerview but putting a progressbar neatly is matters here. any suggestions?

Dhaval Parmar
  • 18,812
  • 8
  • 82
  • 177

1 Answers1

1

You can just set a ProgressBar on the recycler_item.xml (replace recycler_item with your actual item file name) that will be inflated on a custom Adapter.

Then set the visibility of the ProgressBar as VISIBLE every time the onBindViewHolder() gets called.

Quick example:

ProgressBarAdapter.java (Where the real magic goes).

class ProgressBarAdapter extends RecyclerView.Adapter<ProgressBarAdapter.ViewHolder>{

    public class ViewHolder extends RecyclerView.ViewHolder{
        public TextView textView;
        public ProgressBar progressBar;

        public ViewHolder(View itemView){
            super(itemView);

            textView = (TextView) itemView.findViewById(R.id.text1);
            progressBar = (ProgressBar) itemView.findViewById(R.id.progress1);
        }
    }

    private List<String> mFiles;
    private Context mContext;

    public ProgressBarAdapter(List<String> files){
        mFiles = files;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        mContext = parent.getContext();
        LayoutInflater inflater = LayoutInflater.from(mContext);

        View viewInflated = inflater.inflate(R.layout.recycler_item, parent, false);

        return new ViewHolder(viewInflated);
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        String file = mFiles.get(position);

        TextView textView = holder.textView;
        textView.setText(file);

        holder.progressBar.setVisibility(View.VISIBLE);
        holder.progressBar.setIndeterminate(true);
    }

    @Override
    public int getItemCount() {
        return mFiles.size();
    }
}

MainActivity.java

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        ArrayList<String> files = new ArrayList<>();
        files.add("File 1");
        files.add("File 2");
        files.add("File 3");

        RecyclerView recyclerView = (RecyclerView) findViewById(R.id.recycler1);
        recyclerView.setAdapter(new ProgressBarAdapter(files));
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
    }
}

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:orientation="vertical"
    tools:context=".MainActivity">

    <android.support.v7.widget.RecyclerView
        android:id="@+id/recycler1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

    </android.support.v7.widget.RecyclerView>

</LinearLayout>

recycler_item.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <TextView
        android:id="@+id/text1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

    <!-- This ProgressBar will get inflated for each ReyclerView item -->
    <ProgressBar 
        android:id="@+id/progress1"
        android:visibility="gone"
        style="@style/Base.Widget.AppCompat.ProgressBar.Horizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />
</LinearLayout>

The result:

Code working

Evin1_
  • 12,292
  • 9
  • 45
  • 47