Matt
Matt

Reputation: 3912

More efficient way to initiate 100 buttons

I have a class that is going to need ~100 buttons. I know I can code it like this:

<ScrollView
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@drawable/scroll"
    android:fillViewport="true"
    android:scrollbars="none"
    android:paddingLeft="16dp"
    android:paddingRight="16dp"
    android:paddingTop="10dp"
    android:paddingBottom="65dp" >

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:paddingTop="5dp"
        android:paddingBottom="5dp"
        android:paddingRight="5dp"
        android:paddingLeft="5dp"
        android:background="@drawable/scrollviewborder"
        android:fillViewport="true" >

        <Button 
            android:id="@+id/b1"
            android:layout_width="fill_parent"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:layout_gravity="center"
            android:textSize="18sp" /> 

        <Button 
            android:id="@+id/b2"
            android:layout_width="fill_parent"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:layout_gravity="center"
            android:textSize="18sp" /> 

        <Button 
            android:id="@+id/b3"
            android:layout_width="fill_parent"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:layout_gravity="center"
            android:textSize="18sp" /> 

        <Button 
            android:id="@+id/b4"
            android:layout_width="fill_parent"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:layout_gravity="center"
            android:textSize="18sp" /> 

        <Button 
            android:id="@+id/b5"
            android:layout_width="fill_parent"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:layout_gravity="center"
            android:textSize="18sp" /> 

        <Button 
            android:id="@+id/b6"
            android:layout_width="fill_parent"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:layout_gravity="center"
            android:textSize="18sp" /> 

        <Button 
            android:id="@+id/b7"
            android:layout_width="fill_parent"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:layout_gravity="center"
            android:textSize="18sp" /> 

        <Button 
            android:id="@+id/b8"
            android:layout_width="fill_parent"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:layout_gravity="center"
            android:textSize="18sp" /> 

        <Button 
            android:id="@+id/b9"
            android:layout_width="fill_parent"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:layout_gravity="center"
            android:textSize="18sp" /> 

        <Button 
            android:id="@+id/b10"
            android:layout_width="fill_parent"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:layout_gravity="center"
            android:textSize="18sp" /> 

        <Button 
            android:id="@+id/b11"
            android:layout_width="fill_parent"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:layout_gravity="center"
            android:textSize="18sp" /> 

        <Button 
            android:id="@+id/b12"
            android:layout_width="fill_parent"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:layout_gravity="center"
            android:textSize="18sp" /> 

        <Button 
            android:id="@+id/b13"
            android:layout_width="fill_parent"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:layout_gravity="center"
            android:textSize="18sp" /> 

        <Button 
            android:id="@+id/b14"
            android:layout_width="fill_parent"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:layout_gravity="center"
            android:textSize="18sp" /> 

        <Button 
            android:id="@+id/b15"
            android:layout_width="fill_parent"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:layout_gravity="center"
            android:textSize="18sp" /> 

        <Button 
            android:id="@+id/b16"
            android:layout_width="fill_parent"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:layout_gravity="center"
            android:textSize="18sp" /> 

        <Button 
            android:id="@+id/b17"
            android:layout_width="fill_parent"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:layout_gravity="center"
            android:textSize="18sp" /> 

        <Button 
            android:id="@+id/b18"
            android:layout_width="fill_parent"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:layout_gravity="center"
            android:textSize="18sp" /> 

        <Button 
            android:id="@+id/b19"
            android:layout_width="fill_parent"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:layout_gravity="center"
            android:textSize="18sp" /> 

        <Button 
            android:id="@+id/b20"
            android:layout_width="fill_parent"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:layout_gravity="center"
            android:textSize="18sp" /> 
    </LinearLayout>
</ScrollView>

But that does not seem very performance efficient and I know if I keep doing that it will soon flag a warning that I have too many lines of code. Is there a more elegant way to do this? XML or in Java will do, it doesn't matter which way.

EDIT

I have caused an uproar about having 100 buttons in my design. Let me explain why.

Anyone here play the Android/Apple app Candy Crush Saga? You know how they have 400+ levels to play that you unlock one at a time? That is similar to what I am doing so that is why I need so many buttons.

After that explanation, if I still am doing this wrong, please let me know as I am still a beginner Android programmer.

Upvotes: 2

Views: 563

Answers (4)

Phil
Phil

Reputation: 36289

To do this correctly, use a two-step process. The first thing to note is that each of your buttons has a unique ID. It is best practice to declare all your ID's in XML, to ensure uniqueness. So step one is to setup your ids xml file (you can ignore this if you actually don't care about the id).

Under the res/values directory, create the file ids.xml, with the following contents:

<resources>
    <item type="id" name="b1" />
    <item type="id" name="b2" />
    <item type="id" name="b3" />
    <item type="id" name="b4" />
    <item type="id" name="b5" />
    <item type="id" name="b6" />
    <item type="id" name="b7" />
    <item type="id" name="b8" />
    <item type="id" name="b9" />
    <item type="id" name="b10" />
    <item type="id" name="b11" />
    <item type="id" name="b12" />
    <item type="id" name="b13" />
    <item type="id" name="b14" />
    <item type="id" name="b15" />
    <item type="id" name="b16" />
    <item type="id" name="b17" />
    <item type="id" name="b18" />
    <item type="id" name="b19" />
    <item type="id" name="b20" />
    <item type="id" name="b21" />
    <item type="id" name="b22" />
    <item type="id" name="b23" />
    <item type="id" name="b24" />
    <item type="id" name="b25" />
    <item type="id" name="b26" />
    <item type="id" name="b27" />
    <item type="id" name="b28" />
    <item type="id" name="b29" />
    <item type="id" name="b30" />
    <item type="id" name="b31" />
    <item type="id" name="b32" />
    <item type="id" name="b33" />
    <item type="id" name="b34" />
    <item type="id" name="b35" />
    <item type="id" name="b36" />
    <item type="id" name="b37" />
    <item type="id" name="b38" />
    <item type="id" name="b39" />
    <item type="id" name="b40" />
    <item type="id" name="b41" />
    <item type="id" name="b42" />
    <item type="id" name="b43" />
    <item type="id" name="b44" />
    <item type="id" name="b45" />
    <item type="id" name="b46" />
    <item type="id" name="b47" />
    <item type="id" name="b48" />
    <item type="id" name="b49" />
    <item type="id" name="b50" />
    <item type="id" name="b51" />
    <item type="id" name="b52" />
    <item type="id" name="b53" />
    <item type="id" name="b54" />
    <item type="id" name="b55" />
    <item type="id" name="b56" />
    <item type="id" name="b57" />
    <item type="id" name="b58" />
    <item type="id" name="b59" />
    <item type="id" name="b60" />
    <item type="id" name="b61" />
    <item type="id" name="b62" />
    <item type="id" name="b63" />
    <item type="id" name="b64" />
    <item type="id" name="b65" />
    <item type="id" name="b66" />
    <item type="id" name="b67" />
    <item type="id" name="b68" />
    <item type="id" name="b69" />
    <item type="id" name="b70" />
    <item type="id" name="b71" />
    <item type="id" name="b72" />
    <item type="id" name="b73" />
    <item type="id" name="b74" />
    <item type="id" name="b75" />
    <item type="id" name="b76" />
    <item type="id" name="b77" />
    <item type="id" name="b78" />
    <item type="id" name="b79" />
    <item type="id" name="b80" />
    <item type="id" name="b81" />
    <item type="id" name="b82" />
    <item type="id" name="b83" />
    <item type="id" name="b84" />
    <item type="id" name="b85" />
    <item type="id" name="b86" />
    <item type="id" name="b87" />
    <item type="id" name="b88" />
    <item type="id" name="b89" />
    <item type="id" name="b90" />
    <item type="id" name="b91" />
    <item type="id" name="b92" />
    <item type="id" name="b93" />
    <item type="id" name="b94" />
    <item type="id" name="b95" />
    <item type="id" name="b96" />
    <item type="id" name="b97" />
    <item type="id" name="b98" />
    <item type="id" name="b99" />
    <item type="id" name="b100" />
</resources>

Next, create your buttons in code:

for (int i = 0; i < 100; i++)
{
    Button button = new Button(this);
    int _id = getResources().getIdentifier("b" + (i+1), "id", this.getPackageName());
    button.setTag(_id);
    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, 0);
    params.weight = 1;
    params.gravity = Gravity.CENTER;
    button.setLayoutParams(params);
    button.setTextSize(18);
    myLinearLayout.addView(button);//myLinearLayout is your parent LinearLayout.
}

Upvotes: 2

Timothy T.
Timothy T.

Reputation: 602

It's way more proper to do it in java.

LinearLayout lay = (LinearLayout) findViewById(R.id.your_liner_layout); // Get the liner layout

  for (i=0; i <= 100; i++) {
     Button btn = new Button(this); // Create a button

     btn.setGravity(Gravity.CENTER); // setting the gravity

     btn.setText("button number " + i); //setting the text

     LinearLayout.LayoutParams layout = new LayoutParams(LayoutParams.MATCH_PARENT, 0, 1); // Creating a LayoutParams : first args is the width, seconds the height and the last one the weight.

     lay.addView(btn, layout); // Adding the button to the linear layout
}

Upvotes: 0

Ken Wolf
Ken Wolf

Reputation: 23279

Try something like this:

for (int i=0; i<100; i++) {
    Button button = (Button) getLayoutInflater().inflate(R.layout.button_view, null);
    // Do stuff to your button, add onClickListeners, etc.
    rootView.addView(button);
}

Where rootView is a LinearLayout or similar that you have instantiated earlier.

and res/layout/button_view.xml is something like:

<Button xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />

Upvotes: 3

Erik Ghonyan
Erik Ghonyan

Reputation: 431

Well, if you need that many buttons there's nothing you can do about it. If you want to keep your code short, you can have a loop in your Java code, but I personally prefer the XML version because then you see your layout right in your IDE.

The only thing I think is worth doing is creating a new style for those buttons. They all have 18sp text size and you might want to add some other attributes later.

Also, remove layout_gravity="center" and layout_weight=1, they have no effect in your case.

And don't use fill_parent, use match_parent.

Upvotes: 0

Related Questions