Adeoni Oluwamayowa
Adeoni Oluwamayowa

Reputation: 9

How to make part of string text clickable?

How can I display dialog box when part of a String.xml text is clicked? I have a long string and I want to make part of the string to display a dialog box when it is clicked.

Now, I don't want the text to take me to a link; but it should appear like a link. Then, it should take me to a dialog box activity when the text is clicked. The text shouldn't appear like a button also. It should look like a link text but it should display a dialog box when clicked.Moreso, the dialog box contain series of String to display more information about the text that was clicked.

This is my String.xml

Upon the new birth, the believer is expected to grow (1 Pet. 2:2).This growth comes through the knowledge of our Lord and Saviour,Jesus Christ (2 Pet. 3:18) who is the essence, substance and testimony of the Scriptures (Jn. 5:39, Luk. 24:25-27, 44-47). We hope to teach the person of Jesus who is the word of God (Jn 1:1-17) and in so doing, find out what he has done for us, who we are in Him and what we have in Him. We also seek to find out truths about God by looking into Jesus who reveals God to us (Heb. 1:1-3, 2 Cor.4:3-4, Col. 1:12-15). In addition, the believer is expected to give glory to God in his conduct (1 Cor. 10:31), he is to be responsible and to walk in the reality of who he is. He is to be given to the service of God (Rom. 12:11). We pray that you receive the wisdom and revelation of the Spirit and that you increase in knowledge and understanding as we diligently study this manual together. Blessings!

I want to click on the (1 Pet. 2:2) to display a dialog box that contains information the bible reference.

Upvotes: 0

Views: 1393

Answers (1)

primo
primo

Reputation: 1472

Try like this

public void makeLinks(TextView textView, String[] links, ClickableSpan[] clickableSpans) {
        SpannableString spannableString = new SpannableString(textView.getText());
        for (int i = 0; i < links.length; i++) {
            ClickableSpan clickableSpan = clickableSpans[i];
            String link = links[i];

            int startIndexOfLink = textView.getText().toString().indexOf(link);
            spannableString.setSpan(clickableSpan, startIndexOfLink, startIndexOfLink + link.length(),
                    Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        }
        textView.setMovementMethod(LinkMovementMethod.getInstance());
        textView.setText(spannableString, TextView.BufferType.SPANNABLE);
    }

in onCreate() method

 ClickableSpan yourstringClick = new ClickableSpan() {
            @Override
            public void onClick(View view) {
              openyourdialoghere()
            }
        };

        makeLinks(tvLink, new String[]{"your string"}, new ClickableSpan[]{
                yourstringClick
        });

Edit

For multiple strings

ClickableSpan yourstringClick2 = new ClickableSpan() {
                @Override
                public void onClick(View view) {
                  openyourdialoghere()
                }
            };

            makeLinks(tvLink, new String[]{"your string","your string 2"}, new ClickableSpan[]{
                    yourstringClick,yourStringClick2
            });

Upvotes: 1

Related Questions