Sarah Sami
Sarah Sami

Reputation: 577

make text as hyper link in android

I want to make that textView which is created dynamically as hyper link I tried many thing but all failed any suggestions ?

for(int i=0;i<tags.size();i++){
        TextView txt= new TextView(this);
        txt.setText(tags.get(i).getName()+" ");
        txt.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
        final String url = tags.get(i).getValue();
        if(url.length() > 0){
            txt.setClickable(true);
            txt.setLinksClickable(true);
            txt.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {
                    startActivity(new Intent(Intent.ACTION_VIEW,Uri.parse(url)));
                }
            });
        }
        ((ViewGroup) view).addView(txt);
    }

Upvotes: 0

Views: 128

Answers (3)

Constantin Cerberus
Constantin Cerberus

Reputation: 301

i hope i get your right. you want text will look like hipper link. use this code

TextView text = new TextView(context); text.setText("Your Text");

    SpannableStringBuilder ssb = new SpannableStringBuilder();
    ssb.append(text.getText());
    ssb.setSpan(new URLSpan("#"), 0, ssb.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
    text.setText(ssb, TextView.BufferType.SPANNABLE);

Upvotes: 1

cVoronin
cVoronin

Reputation: 1350

Try this:

TextView txtLink = (TextView) findViewById(R.id.txtLink);
String html = "<a href=\"http://google.com\">Open Google</a>";
txtLink.setText(Html.fromHtml(html));
txtLink.setMovementMethod(LinkMovementMethod.getInstance());

Upvotes: 1

cania
cania

Reputation: 858

Check out the Linkify class:

-> http://developer.android.com/reference/android/text/util/Linkify.html

Upvotes: 0

Related Questions