1

Right now I have the following code in "activity_main.xml" :

<Button
    android:id="@+id/Table_Button_1_1"
    android:layout_width="96dp"
    android:layout_height="96dp"
    android:layout_marginStart="5dp"
    android:layout_marginTop="1dp"
    android:text="@string/Table_Button_1_1"
    android:textAllCaps="false"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent" />

<Button
    android:id="@+id/Table_Button_1_2"
    android:layout_width="96dp"
    android:layout_height="96dp"
    android:layout_marginStart="102dp"
    android:layout_marginTop="1dp"
    android:text="@string/Table_Button_1_2"
    android:textAllCaps="false"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent" />

<Button
    android:id="@+id/Table_Button_1_3"
    android:layout_width="96dp"
    android:layout_height="96dp"
    android:layout_marginStart="199dp"
    android:layout_marginTop="1dp"
    android:text="@string/Table_Button_1_3"
    android:textAllCaps="false"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent" />

...

In the Java code it looks like this :

public class MainActivity extends AppCompatActivity
{
  @Override
  protected void onCreate(Bundle savedInstanceState)
  {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    Button button_1_1 = findViewById(R.id.Table_Button_1_1);
    button_1_1.setTypeface(Typeface.MONOSPACE);
    button_1_1.setTextSize(23);
    button_1_1.setText("2 \u24be\n\u2665 \u2709");

    Button button_1_2 = findViewById(R.id.Table_Button_1_2);
    button_1_2.setTypeface(Typeface.MONOSPACE);
    button_1_2.setTextSize(23);
    button_1_2.setText("a b\nc d");

With lots of buttons, I want to access them through an array of buttons, so I want my Java code to look like this :

public class MainActivity extends AppCompatActivity
{
  Button myButton[]=new Button[6];

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

    for (int i=0;i<6;i++)
    {
      Button myButton[i] = findViewById(R.id.Table_Button[i]);    <-- Get the button Like this
      myButton[i].setText("Text : "+i);
    }
  }

...

Yet, I don't know how to define the button array in activity_main.xml, so that in Java I can access them through findViewById(R.id.Table_Button[i]), I guess something like this :

<Button
        android:id="@+id/Table_Button[6]"                  <--- ?
        android:layout_width="96dp"
        android:layout_height="96dp"
        android:layout_marginStart="5dp"
        android:layout_marginTop="1dp"
        android:text="@string/Table_Button[6]"             <--- ?
        android:textAllCaps="false"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

This will also affect string.xml. What's the proper way to do it [ in both activity_main.xml & string.xml ] ?

Edit : Here is why my question is different from suggested previous question. The first half is similar [ how to access the buttons from java code ], but I want the buttons to be defined in the activity_main.xml, so at design time, I can see their layout, and locate which button is in which row,col. Therefore what I wanted the most is "How to define the button array in the activity_main.xml & string.xml" and be able to access them from the java code ?

4
  • U can define all the buttons programmatically, suppose have a LinearLayout in ur XML as root, and in ur java file add as many buttons as u want in that linearlayout. u can also loop through all the views in LinearLayout in future. Commented Dec 23, 2019 at 20:42
  • check out this stackoverflow.com/questions/15642104/… Commented Dec 23, 2019 at 20:46
  • Does this answer your question? Array of Buttons in Android Commented Dec 23, 2019 at 20:47
  • You can't do it this way. Commented Dec 23, 2019 at 21:17

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.