0

I am breaking my head dealing with variables types in my app. I try in a loop to increase a var and pass it to a listener, according to the name of the buttons defined in my XML layout. I would like to start from "jeton1" to "jeton2","jeton3"...., but cannot manage to do that in my code (errors arising), the vars do not point to the buttons stored in the XML and not showing up when calling the buttons listeners. I made a test with a defined array but the stuff failed. Test code below made upon only one button. A help would be greatly appreciated.

Here is my code The XML layout :

<GridLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:rowCount="20"
    android:columnCount="9">

    <Button
        android:id="@+id/jeton1"
        android:layout_column="0"
        android:layout_row="0"
        android:text="@string/boutona"
        android:layout_height="88dp"
        android:layout_width="88dp"
        android:textSize="40sp"
        android:backgroundTint="#eeceac"
        android:textStyle="bold" />

Main Java :

public class MainActivity extends AppCompatActivity {

int i;
String jeton = "";

@SuppressLint("ClickableViewAccessibility")
@Override

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    // Main loop

    for (i = 1; i < 2; i++) {

        jeton = "jeton" + i; // Should throw "jeton1", "jeton2".....
        final Button jetonnew;
        jetonnew = (Button) findViewById(R.id.jeton); // Error 'cannot resolve symbol

        // jetonnew = (Button)findViewById(R.id.jeton+i);
        // Step 4 Listener

        jetonnew.setOnTouchListener(
                new View.OnTouchListener() {

                    @Override
                    public boolean onTouch(View view, MotionEvent event) {

                        switch (event.getAction()) {

                            case MotionEvent.ACTION_UP:
                                jetonnew.getBackground().setAlpha(0); // Crash app
                                jetonnew.setTextColor(Color.parseColor("#2aa17b"));
                                break;
                        }
                        return true;
                    }

                });
    }
}

}

Many thanks for your replies and suggestions.

Saikrishna Rajaraman
  • 3,205
  • 2
  • 16
  • 29
alhambra60
  • 11
  • 3
  • I don't get your question fully? Do you want to build buttons dynamically? If yes, have a look at: https://stackoverflow.com/questions/1851633/how-to-add-a-button-dynamically-in-android – Christopher Aug 01 '18 at 06:21

4 Answers4

1

If you have a fixed number of buttons, you can store an array of integers

  int[] ids = {R.id.button1, R.id.button2, ...};

However, if you want to dynamically add buttons, you should try creating them programmatically

Button newButton = new Button(this);

or you can create some custom layout and inflate it

inflate(this, R.layout.customLayout, null);

Keep in mind that R.id.someId returns and integer not a string so you cannot append to it. Also adding a string after id does not work for the same reason.

0

jetonnew = (Button)findViewById(R.id.jeton+i);

This portion of code does not work because R.id.jeton is a generated integer not a string.

You should consider using findViewByTag instead of findViewById. Your code will look like this:

<GridLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:rowCount="20"
android:columnCount="9">

<Button
    android:tag="jeton1"
    android:layout_column="0"
    android:layout_row="0"
    android:text="@string/boutona"
    android:layout_height="88dp"
    android:layout_width="88dp"
    android:textSize="40sp"
    android:backgroundTint="#eeceac"
    android:textStyle="bold" />

So in your java file:

for (i = 1; i < 2; i++) {

    jeton = "jeton" + i; // Should throw "jeton1", "jeton2".....
    final Button jetonnew;
    //findViewByTag here
    jetonnew = (Button) findViewByTag(jeton)

Another way is to just iterate through GridLayout child, like this:

//suppose your gridLayout has id=@+id/gridparent
GridLayout gridParent = (GridLayout)findViewById(R.id.gridparent);
for(int index=0; index<gridParent.getChildCount(); ++index) {
     Button nextButton = (Button)gridParent.getChildAt(index);
     //attach listener here
}
maheryhaja
  • 1,617
  • 11
  • 18
0

If you want to find the id of the button by using its name:

jetonnew = findViewById(getResources().getIdentifier("jeton" + i, "id", getPackageName()));
0

You should create arrays of your grid button id's.

@IntegerRes int [] resourceButtonIds = new int[]{R.id.jeton1,R.id.jeton2}; 

Then modify the loop accordingly.

    for (i = 0; i < resourceButtonIds.length; i++) {
       final Button jetonnew;
       jetonnew = (Button) findViewById(resourceButtonIds[i]); 
       // now set all your listeners
    }
Krishna Sharma
  • 2,828
  • 1
  • 12
  • 23
  • Thank you Krishna, I tried this but for any reason it did not work. – alhambra60 Aug 01 '18 at 13:22
  • @alhambra60 didn't work means what ? errors are still there while calling findViewById ?. Can you please explain in details what issue you facing after using the solution I proposed. – Krishna Sharma Aug 01 '18 at 13:25