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));
finish();
}
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)));
finish();
}
and would like to give the parameter i from the xml, is that possible?
Thank a lot for any help.
You can set Tags for a view. Tags are basically a way for views to have memories.
xml:
The line
android:tag="2"
set a tag value of2
(string data type) toButton2
Java file:
General Case:
Inside
GoToPageX(View v)
function, usev.getTag()
to get the tag value of corresponding view(From which ever view the method was called).Your case:
Add the method as follows
If you will create some layout element in xml you can use there
where
some_id_value
is kind of unique string which will be translate into id which is kept in R.java (better for you- don't change anything there) than in code you can get that id by usingread a little bit there that's really basics.
It is not directly possible. However, maybe you could use
android:tag
to get your parameter.You could also do this by enabling data binding and using a lambda expression for the
onClick
value. This way is especially useful if you plan to use multiple inputs of different types. Here's an example of a simple MainActivity.xml in which this strategy is used.and in MainActivity.java
UPDATE: For those who don't know how to set up data binding, I will explain it here so you don't have to google around for it. First, enable
dataBinding
in the build.gradle file:Also, make sure
jcenter()
is in your repositories.Then, go to the XML of the layout where
onClick
will be used and wrap its layout in alayout
tag with adata
section like this:For the
variable
tag'stype
parameter, you need to put the class that will contain the function whichonClick
points to. In this example, I will use the main activity class, which is namedMainActivity
in my test project.After you have your layout wrapped in a
layout
tag like in the example above, clean the project in Android Studio. You may also need to invalidate cache/restart or close and reopen Android Studio.Next, if the the layout with
onClick
you are trying to set up data binding for is the same layout set bysetContentView
in your main activity class, open the file that contains your main activity class. If the layout withonClick
you are trying to set up data binding for is inflated programmatically in a different file, open the file in which the layout is inflated instead.Add these imports to that file:
That first class you are importing is generated when you clean the project (and possibly have to invalidate cache/restart) and is automatically named after the XML file you added the
layout
wrapper to. If the layout file is named your_layout.xml, the import class will be namedYourLayoutBinding
. The exact import path will depend on your app name and structure, but it will always be within adatabinding
parent class.The next step depends on whether the layout you are adding data binding to is set with
setContentView
or is inflated withinflate
. Both versions of the following step make use of the methodsetMain
. ThesetMain
method is automatically generated and named using the value of thename
parameter in thelayout
wrapper we added. Since we putname="main"
, the method is calledsetMain
.If the layout you are adding data binding to is the same layout set by
setContentView
find the line in your main activity class that looks likesetContentView(R.layout.your_layout);
and change it to useDataBindingUtil.setContentView
instead ofsetContentView
, addingthis
as its first argument. Usebinding.setMain
to point the layout'smain
variable to the current activity.If the layout you are adding data binding to is not set by
setContentView
but rather inflated go to where it is inflated in your code. It should look something like this:Modify it to use
DataBindingUtil.inflate
, adding the previous inflater as its first argument. Usebinding.setMain
to point the layout'smain
variable to the main activity, and usebinding.getRoot()
to get the view. It should end up like this:Now the data binding is ready to use. Add a function for
onClick
to point to within your main activity class.You can call it from the layout you added data binding to using a lambda expression. This example function doesn't require a
View
, so it can be used like this:Make sure to use single quotes around the value for
onClick
if you plan on using aString
input.If you do need to pass the button's view to your function, simply add a
View
parameter to your function's required arguments and use a lambda expression like this instead: