Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting an onClickListener on a CheckBox Text

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
        }           
    });
like image 907
ChallengeAccepted Avatar asked Oct 14 '13 19:10

ChallengeAccepted


1 Answers

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

like image 198
tyczj Avatar answered Nov 08 '22 11:11

tyczj