9

I am using Linkify in my app, and visited link text is appearing as dark purple. My overall layout background color is dark blue so this is impossible to read. The text is set as white, but visited links are appearing as dark purple. How do I override this?

<TextView android:text="Website:" 
          android:layout_width="match_parent" 
          android:layout_height="wrap_content"
          android:textSize="14dip"
          android:paddingBottom="2dip"
          android:background="#ffffff"
          android:textColor="#577dbe" />              
<TextView android:text="http://www.mysite.com/"
          android:layout_width="match_parent" 
          android:layout_height="wrap_content"
          android:textSize="12dip"
          android:paddingBottom="6dip"
          android:textColor="#ffffff"
          android:id="@+id/contactWeb1" />  
CQM
  • 42,592
  • 75
  • 224
  • 366

4 Answers4

18

It turned out to be a simple solution!

However you won't be able to do the visited / not visited differentiation.

    TextView contactWeb1 = (TextView) findViewById(R.id.contactWeb1);
    noteView.setText("http://www.blablaasd.com/");
    noteView.setLinkTextColor(Color.red); //for example
    Linkify.addLinks(noteView, Linkify.ALL);

My attempts to catch visited states:

Use

    noteView.setLinkTextColor(getResources().getColorStateList(R.color.colors));

Instead of

    noteView.setLinkTextColor(Color.red);

In res/ create folder color and create colors.xml in res/color/

colors.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item
      android:state_window_focused="true" android:color="#00ff00">
        
    </item>
    <item
      android:state_window_focused="true" android:color="#00ff00">
        
    </item>
    <item android:color="#FF00ff"/>
</selector>

I have tried my best to catch visited states. I tried all the states a selector can take.

I might have missed In case you found out, share (:


ALTERNATE SOLUTION (works only for html links)

Set the font Color programatically

Drawback (Be carefull for this point)

  • You will have to catch whether it was visited or not (this is doable)

    This means that you are not overriding the visited links functionality.

CODE:

TextView contactWeb1 = (TextView) findViewById(R.id.contactWeb1);
String desc = "<font color=\"red\"><a href='http://www.mysite.com/'>Visit my site</a></font>";
contactWeb1.setText(Html.fromHtml(desc));
contactWeb1.setMovementMethod(LinkMovementMethod.getInstance());
Community
  • 1
  • 1
Sherif elKhatib
  • 45,786
  • 16
  • 89
  • 106
  • Hey, I won't be checking if the link was visited :) I am just using linkify to make clickable objects clickable, like addresses (which automatically goes to google maps) phone numbers and web links, kind of like how you can click those things in an email on android or how you can click a phone number on a webpage, they aren't html links but they are still clickable! so this might be the solution! – CQM Aug 24 '11 at 15:30
  • your code only checks for HTML links, how would that work for Phone numbers and Addresses which linkify is supposed to do. even setting the color afterwards with `phone1.setTextColor(Color.parseColor("#ffffff"));` does not work and is ignored – CQM Aug 24 '11 at 19:42
  • hi, i have one question regd the same, can we integrate the css file with the html tag – Aada Mar 21 '13 at 07:38
  • @Aada AFAIK you can not use css files with `Html.fromHtml` – Sherif elKhatib Mar 22 '13 at 10:06
0

Add:

android:textColor="#ffffff"

to TextView element in xml solves problem... it seems that overriding textcolor overrides other color styles related to element

see this question:

Android text view color doesn't change when disabled

Community
  • 1
  • 1
Anass
  • 6,132
  • 6
  • 27
  • 35
  • textcolor is already seen in the code I provided, try again with an original answer – CQM Aug 18 '11 at 17:28
0

I tried your code out and my TextView's color wasn't changing. Well a solution to your would be to add a onClick listener to the TextView and set the color of the TextView in it. So whenever the text is clicked it will be set to the color you specify.

blessanm86
  • 31,439
  • 14
  • 68
  • 79
  • that won't fix the problem of the textview being the wrong color after being visited – CQM Aug 24 '11 at 14:50
0

I ran into the same problem using Linkify. You can use LinkMovementMethod instead, and convert your text to a SpannableString.

TextView contactWeb1 = (TextView) findViewById(R.id.contactWeb1);
SpannableString str = SpannableString.valueOf(contactWeb1);
str.setSpan(new URLSpan(contactWeb1.getText()), 0, str.length() -1, Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
contactWeb1.setMovementMethod(LinkMovementMethod.getInstance());
Phil
  • 35,852
  • 23
  • 123
  • 164