6

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
        }           
    });
ChallengeAccepted
  • 1,684
  • 1
  • 16
  • 25
  • You can also [make a part of text clickable](https://stackoverflow.com/a/47166879/6131611) – Pavel Nov 25 '18 at 17:23

5 Answers5

6

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

tyczj
  • 71,600
  • 54
  • 194
  • 296
  • Yeah, I've considered that and intended that as my last solution. It would be more preferable to do it from the checkBox if possible. Was wondering if there was any possible way. – ChallengeAccepted Oct 14 '13 at 19:58
  • Thank you. I went ahead and did what you suggested. Might not be the answer but it was the best alternative. – ChallengeAccepted Oct 14 '13 at 20:16
4

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

Andrew Schuster
  • 3,229
  • 2
  • 21
  • 32
1

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.

Martin Sykes
  • 2,591
  • 1
  • 21
  • 19
  • Thank you. I went ahead and did what you and tyczj suggested. These didn't really answer the question but it got the job done in the end as an alternative. – ChallengeAccepted Oct 14 '13 at 20:16
1

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.

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
ChallengeAccepted
  • 1,684
  • 1
  • 16
  • 25
  • 1
    just align the text to the bottom of the checkbox – tyczj Oct 14 '13 at 20:17
  • Nice suggestion however that makes it look worse. It makes it at the bottom, exceeding the box. I guess alignTop should do for now. – ChallengeAccepted Oct 14 '13 at 20:21
  • 1
    add padding to the bottom, you can easily make it look like the checkbox with positioning and padding. `android:paddingBottom="5dp"` – tyczj Oct 14 '13 at 20:30
0

onclick is just not the right listener, its:

onCheckedChanged

EDIT: and to set the listener its:

setOnCheckedChangeListener
Tom
  • 1,203
  • 4
  • 21
  • 36