0

I have about 100 views (Buttons,Textviews and ImageButton) in my activity that i create in a for loop. My app runs slow on older versions of android ( 5 and below) and sometimes it does not even load the views.

My question is, what is the proper way of handling such views and loading them without putting too much load on the main thread?

I couldn't use a separate thread since i cant reference UI elements from any other thread than the main one.

Thanks.

for (int i = 0; i < arr.size(); i++) {


            String name = arr.get(i).getName();
            final int songPath = arr.get(i).getSongPath();
            int iconPath = arr.get(i).getIconPath();

            RelativeLayout relativeLayout = new RelativeLayout(this);
            RelativeLayout.LayoutParams rlp = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
            relativeLayout.setLayoutParams(rlp);



            buttons[i] = new ImageButton(this);
            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, 350);
            params.setMargins(0, 0, 0, 8);

            buttons[i].setLayoutParams(params);
            buttons[i].setBackgroundResource(iconPath);
            buttons[i].setTag(name + "button");
            buttons[i].setId(id++);



            TextView emoteName = new TextView(this);
            RelativeLayout.LayoutParams textParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
            textParams.addRule(RelativeLayout.BELOW, buttons[i].getId());
            textParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
            textParams.setMargins(20,10,0,0);

            emoteName.setText(name.toUpperCase());
            emoteName.setId(id++);
            emoteName.setTextSize(20f);

            emoteName.setLayoutParams(textParams);


            Button threedots = new Button(this);
            RelativeLayout.LayoutParams threedotsParams = new RelativeLayout.LayoutParams(50, RelativeLayout.LayoutParams.WRAP_CONTENT);
            threedotsParams.addRule(RelativeLayout.BELOW, buttons[i].getId());
            threedotsParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
            threedotsParams.addRule(RelativeLayout.ALIGN_RIGHT,buttons[i].getId());
            threedotsParams.setMargins(0,0,0,0);

            threedots.setText(getString(R.string.vertical_ellipsis));
            threedots.setTextSize(25f);
            threedots.setBackgroundColor(Color.TRANSPARENT);
            threedots.setLayoutParams(threedotsParams);

            final int counterForOnClick = i;
            threedots.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    ShowDialog(arr.get(counterForOnClick));
                }
            });
            relativeLayout.addView(buttons[i]);
            relativeLayout.addView(emoteName);
            relativeLayout.addView(threedots);
            if (i % 2 == 0) {

                leftLayout.addView(relativeLayout);
            } else {

                rightLayout.addView(relativeLayout);

            }

            final int index = i;
            buttons[i].setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                        playEmoteSound(songPath);


                }
            });


        }
Tamir Abutbul
  • 7,301
  • 7
  • 25
  • 53
ibrahim
  • 573
  • 1
  • 6
  • 20

1 Answers1

1

As you said - adding all those views is not the best idea.

From your code, I can see that we are talking about the same views with different data.

You will want to use RecyclerView in order to create a list of the same view.

From the documentation:

If your app needs to display a scrolling list of elements based on large data sets (or data that frequently changes), you should use RecyclerView as described on this page.

Tamir Abutbul
  • 7,301
  • 7
  • 25
  • 53
  • Thanks,I will look into it now – ibrahim May 10 '19 at 18:35
  • I used recyclerviewer to dispaly all 100 views and its working fine. I have implemented a search and filter features in my app. How can i delete all the views in the recyclerviewer and replace them with the only views that match the search? or should i just delete the whole recyclerviewer and create another with new adapter? thanks very much – ibrahim May 11 '19 at 16:58
  • check [this](https://stackoverflow.com/questions/30398247/how-to-filter-a-recyclerview-with-a-searchview) – Tamir Abutbul May 11 '19 at 17:00