How to make a part of text clickable like button in Android?

26,308

Solution 1

make three textView ex: textView1, textView2 and textView3.

<RelativeLayout android:id="@+id/textLay"
    android:layout_width="fill_parent" android:layout_height="wrap_content"
    >

    <TextView android:id="@+id/textView1" android:layout_width="wrap_content"
        android:layout_height="wrap_content" 
        android:text="Please press this"/>

    <TextView android:id="@+id/textView2" android:layout_width="wrap_content"
        android:layout_height="wrap_content" 
         android:text="here" 
        android:layout_toRightOf="@+id/textView1"  />

    <TextView android:id="@+id/textView3" android:layout_width="wrap_content"
        android:layout_height="wrap_content" 
         android:text="to refresh the set of values." 
        android:layout_toRightOf="@+id/textView2"  />

</RelativeLayout>

Solution 2

Set background of Button as null by android:background="@null" give the same text color as given to other TextView's.

or You can take 3 TextView's and setOnClickListener to middle one.

Solution 3

Set style of Button with borderless android attribut to remove background and border:

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    style="?android:attr/borderlessButtonStyle"
    android:text="here" />

Solution 4

Just add below attribute to TextView in XML layout file to make it clickable.

android:clickable="true"

In your Java file add OnClickListener to complete click action on text view.

Solution 5

Add android:clickable="true" and android:onClick="yourclickname" for the TextView in the XML. Then define yourclickname in your activity. This should make your TextView clickable and triggers the specified method when clicked.

Share:
26,308
Karthik
Author by

Karthik

An Android enthusiast.

Updated on July 09, 2022

Comments

  • Karthik
    Karthik almost 2 years

    I would like to add button myButton to TextView. How to get it in a single line so that it looks like one set of lines. Here is sample of the text to be added

    Please press this here to refresh the set of values.

    I want to have "here" as clickable button. How can I do it. Any snippet on it would be helpful.

  • Karthik
    Karthik over 12 years
    This doesnt comes well when we take into multi line text.How to solve it in due with modifying the snippet.
  • Karthik
    Karthik over 12 years
    how to have three textviews followed by one another in a multi line
  • user1387035
    user1387035 over 11 years
    Hey ur suggestion was very useful to me...thanx
  • Randika Vishman
    Randika Vishman over 8 years
    Simple and Elegant! I needed the link text color to be same as default color of hyperlinks in web. So I put RGB(0, 122, 255), and looks the same! (Y)
  • Jason K.
    Jason K. about 7 years
    easiest solution and answers the original question exactly.