Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Linkify links textColor ignored, css style overrides possible?

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" />  
like image 596
CQM Avatar asked Aug 18 '11 17:08

CQM


1 Answers

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());
like image 74
Sherif elKhatib Avatar answered Nov 08 '22 16:11

Sherif elKhatib