[ACCEPTED]-Possibility to add parameters in button xml?-android-activity

Accepted answer
Score: 113

It is not directly possible. However, maybe 1 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(); 
    /* ... */
}
Score: 3

You could also do this by enabling data 68 binding and using a lambda expression for 67 the onClick value. This way is especially useful 66 if you plan to use multiple inputs of different 65 types. Here's an example of a simple MainActivity.xml in 64 which this strategy is used.

<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android">
    <data>
        <variable name="main" type="com.example.android.myapp.MainActivity" />
    </data>
    <LinearLayout android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
        <ImageButton (...) android:onClick='@{() -> main.GotoPage(1,"one")}'/>
        <ImageButton (...) android:onClick='@{() -> main.GotoPage(2,"two")}'/>
        <ImageButton (...) android:onClick='@{() -> main.GotoPage(3,"three")}'/>
        ...
        <ImageButton (...) android:onClick='@{() -> main.GotoPage(100,"one hundred")}'/>
    </LinearLayout>
</layout>

and in MainActivity.java

public void GotoPage(int i, String otherVariable) {
    /** code using i and otherVariable **/
}

UPDATE: For 63 those who don't know how to set up data 62 binding, I will explain it here so you don't 61 have to google around for it. First, enable 60 dataBinding in the build.gradle file:

android {
    ...
    dataBinding {
        enabled = true
    }
    ...
}

Also, make sure jcenter() is in your 59 repositories.

Then, go to the XML of the 58 layout where onClick will be used and wrap its 57 layout in a layout tag with a data section like this:

<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android">
    <data>
        <variable name="main" type="com.example.android.yourapp.MainActivity" />
    </data>
    <YourLayout>
        ...
    </YourLayout>
</layout>

For 56 the variable tag's type parameter, you need to put the 55 class that will contain the function which 54 onClick points to. In this example, I will use 53 the main activity class, which is named 52 MainActivity in my test project.

After you have your 51 layout wrapped in a layout tag like in the example 50 above, clean the project in Android Studio. You 49 may also need to invalidate cache/restart 48 or close and reopen Android Studio.

Next, if 47 the the layout with onClick you are trying to set 46 up data binding for is the same layout set 45 by setContentView in your main activity class, open the 44 file that contains your main activity class. If 43 the layout with onClick you are trying to set up 42 data binding for is inflated programmatically 41 in a different file, open the file in which 40 the layout is inflated instead.

Add these 39 imports to that file:

import com.example.android.yourapp.databinding.YourLayoutBinding;
import android.databinding.DataBindingUtil;

That first class you 38 are importing is generated when you clean 37 the project (and possibly have to invalidate 36 cache/restart) and is automatically named 35 after the XML file you added the layout wrapper 34 to. If the layout file is named your_layout.xml, the import 33 class will be named YourLayoutBinding. The exact import path 32 will depend on your app name and structure, but 31 it will always be within a databinding parent class.

The 30 next step depends on whether the layout 29 you are adding data binding to is set with 28 setContentView or is inflated with inflate. Both versions of 27 the following step make use of the method 26 setMain. The setMain method is automatically generated 25 and named using the value of the name parameter 24 in the layout wrapper we added. Since we put name="main", the 23 method is called setMain.


If the layout you are adding data binding to is the same layout set by setContentView find the line in your 22 main activity class that looks like setContentView(R.layout.your_layout); and 21 change it to use DataBindingUtil.setContentView instead of setContentView, adding this as 20 its first argument. Use binding.setMain to point the layout's 19 main variable to the current activity.

YourLayoutBinding binding = DataBindingUtil.setContentView(this, R.layout.your_layout);
binding.setMain(this);

If the layout you are adding data binding to is not set by setContentView but rather inflated go to 18 where it is inflated in your code. It should 17 look something like this:

return inflater.inflate(R.layout.your_layout, container, false);

Modify it to use 16 DataBindingUtil.inflate, adding the previous inflater as its first 15 argument. Use binding.setMain to point the layout's main variable 14 to the main activity, and use binding.getRoot() to get the 13 view. It should end up like this:

YourLayoutBinding binding = DataBindingUtil.inflate(inflater, R.layout.your_layout, container, false);
binding.setMain((MainActivity) getActivity());
return binding.getRoot();

Now the 12 data binding is ready to use. Add a function 11 for onClick to point to within your main activity 10 class.

public void exampleFunction(int number, String text) {
    System.out.println("Number: " + number + ", Text: " + text);
}

You can call it from the layout you 9 added data binding to using a lambda expression. This 8 example function doesn't require a View, so 7 it can be used like this:

<Button android:id="@+id/buttonID"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:textSize="26sp"
    android:text="Test"
    android:onClick='@{() -> main.exampleFunction(123, "test")}'/>

Make sure to use 6 single quotes around the value for onClick if you 5 plan on using a String input.

If you do need to 4 pass the button's view to your function, simply 3 add a View parameter to your function's required 2 arguments and use a lambda expression like 1 this instead:

<Button android:id="@+id/buttonID"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:textSize="26sp"
    android:text="Test"
    android:onClick='@{(view) -> main.exampleFunction(view, 123, "test")}'/>
Score: 2

If you will create some layout element in 6 xml you can use there

<ImageButton
    android:id="@+id/some_id_value" />

where some_id_value is kind of unique 5 string which will be translate into id which 4 is kept in R.java (better for you- don't 3 change anything there) than in code you 2 can get that id by using

R.id.some_id_value

read a little bit 1 there that's really basics.

Score: 1

You can set Tags for a view. Tags are basically 6 a way for views to have memories.

xml:

<ImageButton
    ...Other Parameters...
    android:id="@+id/Button2"
    android:tag="2"
    android:onClick="GoToPageX"/>
<ImageButton
    ...Other Parameters...
    android:id="@+id/Button3"
    android:tag="3"
    android:onClick="GoToPageX"/>

The 5 line android:tag="2" set a tag value of 2(string data type) to 4 Button2

Java file:

General Case:
Inside GoToPageX(View v) function, use v.getTag() to get the tag 3 value of corresponding view(From which ever 2 view the method was called).

Your case:
Add the method 1 as follows

public void GoToPageX(View v){
    int i = Integer.parseInt(v.getTag()); //parseInt converts string to integer
    startActivity(new Intent(getBaseContext(), activities.get(i)));
    finish();
}

More Related questions