Waza_Be Waza_Be - 2 months ago 8
Android Question

Possibility to add parameters in button xml?

I currently have an activity with some buttons.

In my xml, buttons are defined like this:

<ImageButton (...) android:onClick="GoToPageX"/>

and I have in my activity:

public void GotoPageX() {
startActivity(new Intent(this, PageX.class));


The problem is that I have hundreds of buttons and do not want to write

<ImageButton (...) android:onClick="GoToPage1"/>
<ImageButton (...) android:onClick="GoToPage2"/>
<ImageButton (...) android:onClick="GoToPage3"/>
<ImageButton (...) android:onClick="GoToPage100"/>

and all the scripts.

I am now using

public void GotoPage( int i) {
startActivity(new Intent(getBaseContext(), activities.get(i)));

and would like to give the parameter i from the xml, is that possible?

Thank a lot for any help.


It is not directly possible. However, maybe you could use android:tag to get your parameter.

<ImageButton (...) android:onClick="goToPage" android:tag="25"/>

public void goToPage(View v) {
    String pageNumber = v.getTag().toString(); 
    /* ... */