How do I make a portion of a Checkbox's text clickable?
You may want only part of the text to be a clickable link, while the rest of the checkbox behaves as usual, i.e. you can click the other text to toggle the state.
You can set up your checkbox like so:
CheckBox checkBox = (CheckBox) findViewById(R.id.my_check_box);ClickableSpan clickableSpan = new ClickableSpan() { @Override public void onClick(View widget) { // Prevent CheckBox state from being toggled when link is clicked widget.cancelPendingInputEvents(); // Do action for link text... } @Override public void updateDrawState(TextPaint ds) { super.updateDrawState(ds); // Show links with underlines (optional) ds.setUnderlineText(true); }};SpannableString linkText = new SpannableString("Link text");linkText.setSpan(clickableSpan, 0, linkText.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);CharSequence cs = TextUtils.expandTemplate( "CheckBox text with link: ^1 , and after link", linkText);checkBox.setText(cs);// Finally, make links clickablecheckBox.setMovementMethod(LinkMovementMethod.getInstance());
The following code worked for me on KitKat. I am yet to test on below versions of Android.
String checkBoxText = "I agree to all the <a href='http://www.redbus.in/mob/mTerms.aspx' > Terms and Conditions</a>";checkBoxView.setText(Html.fromHtml(checkBoxText));checkBoxView.setMovementMethod(LinkMovementMethod.getInstance());
There actually is an elegant solution, using CheckBox
and single TextView
. Along with a combinations of TextView.setClickable()
, Intent Filter, and TextView.setMovementMethod()
.
You have main view (here, I called it ClickableTextViewExample):
package id.web.freelancer.example;import android.app.Activity;import android.os.Bundle;import android.text.Html;import android.text.method.LinkMovementMethod;import android.widget.CheckBox;import android.widget.TextView;public class ClickableTextViewExampleActivity extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); CheckBox checkbox = (CheckBox)findViewById(R.id.checkBox1); TextView textView = (TextView)findViewById(R.id.textView2); checkbox.setText(""); textView.setText(Html.fromHtml("I have read and agree to the " + "<a href='id.web.freelancer.example.TCActivity://Kode'>TERMS AND CONDITIONS</a>")); textView.setClickable(true); textView.setMovementMethod(LinkMovementMethod.getInstance()); }}
main.xml
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <LinearLayout android:id="@+id/linearLayout1" android:layout_width="match_parent" android:layout_height="wrap_content" > <CheckBox android:id="@+id/checkBox1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="CheckBox" /> <TextView android:id="@+id/textView2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="TextView" android:clickable="true" /> </LinearLayout></LinearLayout>
TCActivity.java
package id.web.freelancer.example;import android.app.Activity;import android.os.Bundle;public class TCActivity extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.tc); }}
tc.xml
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <TextView android:id="@+id/tcView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Terms and conditions" /></LinearLayout>
and the final piece of codes that glue it all, the AndroidManifest.xml:
<activity android:name="TCActivity"> <intent-filter> <category android:name="android.intent.category.DEFAULT" /> <action android:name="android.intent.action.VIEW" /> <data android:scheme="id.web.freelancer.example.TCActivity" /> </intent-filter> </activity>
Here comes, the explanations:
textView.setText(Html.fromHtml("I have read and agree to the " + "<a href='id.web.freelancer.example.TCActivity://Kode'>TERMS AND CONDITIONS</a>"));textView.setClickable(true);textView.setMovementMethod(LinkMovementMethod.getInstance());
setClickable
will allow you to click ontextView
. But not the HREF link. To do that, you will have to usesetMovementMethod()
and set it toLinkMovementMethod
.
After that, you need to catch the URL. I did this using intent-filter
in AndroidManifest.xml
<action android:name="android.intent.action.VIEW" /><data android:scheme="id.web.freelancer.example.TCActivity" />
It catch VIEW command and it only filter URL starting with
id.web.freelancer.example.TCActivity://
Here's the package for you to try it out and here's the github repository. Hope this helped