13

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"?

3 Answers 3

6

Official documentation: https://developer.android.com/guide/topics/ui/controls/checkbox.html

<CheckBox android:id="@+id/checkbox_meat"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/meat"
    android:onClick="onCheckboxClicked"/>
<CheckBox android:id="@+id/checkbox_cheese"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/cheese"
    android:onClick="onCheckboxClicked"/>
Sign up to request clarification or add additional context in comments.

Comments

3

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

Comments

1

I believe using a lambda is more convenient:

<EditText
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/firstName"
    android:text="@={user.firstName}" />
<CheckBox
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:onCheckedChanged="@{()->handler.checked(firstName)}" />

For more details see this.

Also, I find the use of two way binding very useful for checkboxes:

<CheckBox
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:checked='@={user.isChecked}'/>

Note that onCheckedChanged is called before the two way binding sets the value.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.