Ali Shah lakhani
Ali Shah lakhani

Reputation: 105

Is it a good practice to add 50 buttons programmatically in android?

I am just testing some stuff out. I wanted to know is it a good practice to add 50 or more Buttons, using a for loop, in a linear layout? and is it a good practice to use OnClickListeners on it? The code lags after I add 80+ Buttons. I tried to use the technique of Recyclerview to load only visible views but It still lags and formatting gets lost too.

Activity:

public class ButtonsTest extends Activity implements View.OnClickListener{
final ArrayList<Button> Buttons = new ArrayList<>();
//I want this text to be at least 800-1500 words.
String DemoText = "Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.";

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.buttonstext);
    FlowLayout fl = (FlowLayout) findViewById(R.id.layouttest);
    List<String> t = Arrays.asList(DemoText.split(" "));

    for (int x = 0; x < t.size(); x++) {
        Buttons.add(new Button(this));
        Buttons.get(x).setText(t.get(x));
        Buttons.get(x).setTextColor(Color.BLACK);
        Buttons.get(x).setBackgroundColor(Color.WHITE);
        Buttons.get(x).setId(x);
        Buttons.get(x).setOnClickListener(this);
        fl.addView(Buttons.get(x));
    }
}

@Override
public void onClick(View v) {
    Button t = (Button) v;
    t.setBackgroundColor(Color.YELLOW);
    t.setTextColor(Color.BLACK);
}
}

XML code:

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

<ScrollView
    android:id="@+id/scrollView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">

    <org.apmem.tools.layouts.FlowLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/layouttest"
        android:layout_width="fill_parent"
        android:layout_height="match_parent">

    </org.apmem.tools.layouts.FlowLayout>
</ScrollView>

Upvotes: 0

Views: 225

Answers (3)

Sachin
Sachin

Reputation: 176

If you are trying to make something like a grid of minesweeper with onclick listener on each button then use either relative layout technique or grid layout in android using java file and resize the button with layoutParams property. I have done this and adding 90-150 button is smooth. I Can give you my implementation if this helps.

Upvotes: 0

Juan Cort&#233;s
Juan Cort&#233;s

Reputation: 21112

Let me be the one to say no. If you're going to need so many buttons, specially inside a scrollview, they should instead be held inside some sort of container that recycles the views. A RecyclerView ideally, where you specify the data, and create the relationship between that data and the views, that will in turn be created programmatically.

So yes but just not in a loop and adding them like that


Some useful links

Upvotes: 1

Langusten Gustel
Langusten Gustel

Reputation: 11002

I think it is a good practice to add them programmatically but I would consider using a ListView or RecyclerView.

Upvotes: 0

Related Questions