ChallengeAccepted
ChallengeAccepted

Reputation: 1704

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
        }           
    });

Upvotes: 6

Views: 11114

Answers (5)

ChallengeAccepted
ChallengeAccepted

Reputation: 1704

Result with TextView beside the CheckBox

It appears there is no real solution to that. Only possibility is to set it aside to the checkBox. The purpose of this was to avoid any miss alignments between the textView and the checkBox however this is the best possible solution offered. Here is the code.

    <CheckBox
    android:id="@+id/checkBox"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/checkBox"
    android:layout_margin="5dp"
    android:textColor="#CC000000"
    android:checked="true"/>

    <TextView
     android:id="@+id/termsOfAgreement"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:text="I agree to the Terms of Agreement."
     android:textColor="#CC000000"
     android:layout_toRightOf="@id/checkBox"
     android:layout_alignTop="@id/checkBox"
     android:textSize="16sp"/>

Then in the java code set an onClickListener for the TextView and a separate listener for the CheckBox itself.

Upvotes: 1

Andrew Schuster
Andrew Schuster

Reputation: 3229

Seeing as none of the previous answers really gave you what you wanted, potentially you could just wrap both your CheckBox and your TextView within a larger view group (like LinearLayout).

You're implementation in xml would look as follows:

<LinearLayout
    android:id="@+id/check_box_and_text"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:clickable="true"
    android:orientation="horizontal">

    <CheckBox
        android:id="@+id/checkBox"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="5dp"
        android:textColor="#CC000000"
        android:checked="true"/>

    <TextView
        android:id="@+id/termsOfAgreement"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="I agree to the Terms of Agreement."
        android:textColor="#CC000000"
        android:textSize="16sp"/>

</LinearLayout>

With this, you can now just set the OnClickListener to the LinearLayout instead of having one for both your CheckBox and your TextView. Notice that I set clickable to true under LinearLayout. This is an important thing to note because by default, clickable is false on a LinearLayout.

Implementation in your java code would be as follows:

LinearLayout myCheckbox = (LinearLayout)findViewById(R.id.check_box_and_text);
myCheckbox.setOnClickListener(yourListener);

As a quick final note, if you find that the alignment is wonky with your CheckBox and TextView, look into using the xml attributes layout_gravity and gravity to get them the way you want them.

Hope this helps! Good luck

Upvotes: 4

Martin Sykes
Martin Sykes

Reputation: 2641

Might be a bit of a cheat but how about putting a textview (with your text) next to a checkbox (without text) that way you can use the ontouch/onclick event of the textview to set the checkbox and open the activity, and pressing the checkbox will only check/uncheck it.

Upvotes: 1

Tom
Tom

Reputation: 1273

onclick is just not the right listener, its:

onCheckedChanged

EDIT: and to set the listener its:

setOnCheckedChangeListener

Upvotes: 0

tyczj
tyczj

Reputation: 73753

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

Upvotes: 6

Related Questions