2

I want to make substring of textview clickable sothat it will start next activity. I want to click "...More" in my TextView to open detail description of news, is there any solution??

 TextView description = (TextView) vi.findViewById(R.id.detail);

i want to set onclicklistener on specific word in TextView (here description).

Roshan Sharma
  • 513
  • 2
  • 7
  • 20

3 Answers3

1

Try this code

public class YourActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_test);

        ((TextView) findViewById(R.id.textView)).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // Do your stuff
            }
        });
    }

    @Override
    public void onPause() {
        super.onPause();
    }

    @Override
    protected void onResume() {
        super.onResume();
    }

}

This is your layout

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentStart="true"
        android:layout_alignParentTop="true"
        android:text="Do you want to see" />

    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_toEndOf="@+id/textView2"
        android:text="...more?" />

</RelativeLayout>

EDIT after comment

So you have to use the SpannableString class and ClickableSpan, something like this

SpannableString string = new SpannableString("YourStringHere");
ClickableSpan clickHandler = new ClickableSpan() {
    @Override
    public void onClick(View v) {
        //do your stuff here
    }
    @Override
    public void updateDrawState(TextPaint tp) {
            super.updateDrawState(tp);
            tp.setUnderlineText(false);
        }
};

//x and y are the start and end of the clickable substring
string.setSpan(clickHandler, x, y, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);

TextView tv = (TextView) findViewById(R.id.your_text_view);
tv.setText(string);
tv.setMovementMethod(LinkMovementMethod.getInstance());
tv.textView.setHighlightColor(Color.TRANSPARENT);

I hope this helps! ;-)

michoprogrammer
  • 1,159
  • 2
  • 18
  • 45
  • you donot understand what i am saying..i donot want to place listenser on textview ,What i am trying is to place listener on specific word in textView – Roshan Sharma May 12 '16 at 14:51
1

You don't give us much of a proof of work but still, the following should help you:

        description.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                 Intent intent = new Intent(this, myActivity.class);
                 startActivity(intent);
            }
        });

And don't forget to set in your TextView's XML:

android:clickable="true"

EDIT: I edited so my code now fills yours.

Amesys
  • 816
  • 2
  • 10
  • 19
  • you donot understand what i am saying..i donot want to place listenser on textview ,What i am trying is to place listener on specific word in textView – Roshan Sharma May 12 '16 at 14:53
  • Then what you need to do is 2 (or 3 if you need it wrapped) differents TextViews, one has the onclicklistener set on him, the other(s) are just placed aside. It's all about little layout modifications in your xml. – Amesys May 12 '16 at 14:55
  • but i donot know how to append textView one after another...two textview can be arranged in either vertical or horizontal...i tried this logic but cannot get solution – Roshan Sharma May 12 '16 at 14:59
  • Then your problem isn't about "How to place Onclicklistener in substring on Textview" but how to align three TextViews, it is a matter of layout. Go seek there. – Amesys May 12 '16 at 15:03
1

What you are looking for seems to be ClickableSpan you can have multiple spans with different formatting in your textview and only have one of the span with a callback on click.

Xavier Falempin
  • 1,186
  • 7
  • 19