It's possible, to do things like this
XML:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/addContactButton"
android:text="@string/addContactButtonLabel"
android:onClick="launchContactAdder"/><!-- here -->
</LinearLayout>
Java:
public void launchContactAdder(View v)
{
Intent i = new Intent(this, ContactAdder.class);
startActivity(i);
}
but there is a requirement, that the method must be public, void and the most important take View as an argument.
Now i'd like to do exactly the same thing but with Checkbox button. Checkbox has android:onclick attribute, but in Android tutorial (http://developer.android.com/resources/samples/ContactManager/index.html) I can see this code
showInvisibleControl.setOnCheckedChangeListener(new OnCheckedChangeListener()
{
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
{
Log.d(TAG, "mShowInvisibleControl changed: " + isChecked);
showInvisible = isChecked;
populateContactList();
}
});
So there is a onCheckedChanged(CompoundButton buttonView, boolean isChecked) method. Is there any way to do this by XML? There's no android:onCheckedChange attribute, only android:onClick attribute, but as I wrote above, name of that attribute must have corresponding method name, which takes View as an argument, but from the code above I understand that i must have a method with CompoundButton and boolean arguments.
Any way to do that in "XML way"?
It is now possible to do this with the use of data binding library. First you need to create a handler class (in this example, I call it MainActivityHandlers). Inside this handler class, define a method (for example the name is method1) with the corresponding implementation Then in your layout file, you just need to implement :
...
<data>
<variable
name="handler"
type="com.test.android.testapp.MainActivityHandlers" />
</data>
...
<RadioGroup
...
android:onCheckedChanged="@{handler.method1}"
...
>
and you're good to go. More info : https://developer.android.com/topic/libraries/data-binding/index.html
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With