I have created a checkBox within my xml file. I am attempting to set an onClickListener on the text of the checkBox however I'm finding no real solution. In other words I want to be able to click the checkbox(make the check box selectable) as well as click the text to open a new activity with the Term of Agreements. Thank you.
Main.xml
<CheckBox
android:id="@+id/checkBox1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="I agree to the Terms of Agreement."
android:textColor="#CC000000"
android:checked="true"/>
This is the following code i attempted, however it will just cause it to crash.
CheckBox checkBox1 = (CheckBox) findViewById(R.id.checkBox1);
Button termsOfAgreement = (Button) checkBox2.getText();
....
termsOfAgreement.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
//Launch activity
}
});
getText()
is not a button it is a string so you cannot cast it to a button.
you are better off not setting text for the checkbox and just using a regular textview next the to check box and putting a listener on the text.
then you have to manage the check box when the text is clicked. so you would have 2 click listeners, one for the checkbox and one for the textview
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