Note that there are some explanatory texts on larger screens.

plurals
  1. POMore efficient way to initiate 100 buttons
    text
    copied!<p>I have a class that is going to need ~100 buttons. I know I can code it like this:</p> <pre><code>&lt;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" &gt; &lt;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" &gt; &lt;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" /&gt; &lt;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" /&gt; &lt;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" /&gt; &lt;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" /&gt; &lt;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" /&gt; &lt;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" /&gt; &lt;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" /&gt; &lt;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" /&gt; &lt;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" /&gt; &lt;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" /&gt; &lt;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" /&gt; &lt;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" /&gt; &lt;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" /&gt; &lt;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" /&gt; &lt;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" /&gt; &lt;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" /&gt; &lt;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" /&gt; &lt;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" /&gt; &lt;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" /&gt; &lt;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" /&gt; &lt;/LinearLayout&gt; &lt;/ScrollView&gt; </code></pre> <p>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.</p> <p><strong>EDIT</strong></p> <p>I have caused an uproar about having 100 buttons in my design. Let me explain why.</p> <p>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.</p> <p>After that explanation, if I still am doing this wrong, please let me know as I am still a beginner Android programmer.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload