Android Linkify links textColor ignored, css style overrides possible?

后端 未结 4 1827
耶瑟儿~
耶瑟儿~ 2020-12-31 18:55

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.

相关标签:
4条回答
  • 2020-12-31 19:23

    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.

    0 讨论(0)
  • 2020-12-31 19:38

    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

    0 讨论(0)
  • 2020-12-31 19:38

    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());
    
    0 讨论(0)
  • 2020-12-31 19:44

    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());
    
    0 讨论(0)
提交回复
热议问题