How do I style autoLink="web" links? - android

I have the following TextView:
<TextView android:id="#+id/theFooBar"
android:autoLink="web"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:text="#string/fooBar"
android:textColor="#android:color/black"
android:textSize="20sp"/>
And the string:
<string name="fooBar">Foo <u>bar</u>.</string>
This gives my black, underlined text. What if I want blue, non-underlined text for the link (the "bar" part), but I want the rest (the "foo" part) to be black? How could I achieve that?

Add android:textColorLink in TextView to define text color for links

I recommended you to use WebView instead of TextView in this situation:
WebView web = (WebView) findViewById(R.id.theFooBar);
String str = "<font color='blue'>bar</font><font color='black'><u>foo</u></font>";
web.setBackgroundColor(0);
// It will sets the background color from white to transparent.
web.loadData(str, "text/html", "utf8");

Related

Making Button text partially bold doesn't work

I want to make text inside Button partially bold but this doesn't work.
I have my text placed in string values. String styling is completely ignored and text appeared as "normal" style inside button.
<string name="gps_yes_button_text"><b>Yes, </b>confirm this location!</string>
Button:
<Button
android:id="#+id/gps_confirm_button"
android:layout_width="#dimen/gps_button_width"
android:layout_height="#dimen/gps_button_height"
android:layout_gravity="center"
android:elevation="#dimen/gps_button_elevation"
android:text="#string/gps_yes_button_text"
android:textColor="#color/colorText"
android:textSize="#dimen/gps_text_large"
android:textAllCaps="false"
android:background="#drawable/gps_rounded_button"/>
Please try to set the button text by coding as below.
String buttonText = "<b>Yes, </b>confirm this location!";
button.setText(Html.fromHtml(buttonText));

TextView anchor link space

I want to make a TextView with a link. I made it with combination of html and bit of java:
// used to enable link navigation on TextView
setMovementMethod(LinkMovementMethod.getInstance())
// TextView with link
<TextView
android:id="#+id/textView5"
android:layout_width="wrap_content"
android:layout_height="19dp"
android:layout_marginTop="8dp"
android:gravity="center"
android:linksClickable="true"
android:text="#string/link"/>
// #string/link
<string name="link">Test link</string>
However there is still one issue, the space before actual link text is underlined like this:
Why is that and how could it be fixed?
// #string/link
<string name="link1">Test link</string>
You can use the white space in xml as string use  . XML won't take white space as it is. it will trim the white space before setting it. So use   instead of single white space.
Use CDATA in string to use HTML tags and use Html.fromHtml() method to set the text.
Implementation below:
Set the text using Html.fromHtml() in your Activity class.
TextView textView = (TextView) findViewById(R.id.textView);
textView.setText(Html.fromHtml(getString(R.string.link)));
textView.setMovementMethod(LinkMovementMethod.getInstance());
In strings.xml modify as below:
<string name="link">Test <![CDATA[link]]></string>

Text view with different colored texts in xml code

I need my textview to have different colored texts. Also I need to do this from xml code, non from java code. Is there anyone who knows some way for doing this?
Thanks
e.g. I have sentence "This is red". I need words to be green, and word red to be red.
There are three ways to change the color of some text inside a textview.
through strings.xml file in (res>values), using the tag (<![CDATA[<p>This is green <font color='hexvalue of red'>and this is red</font>.</p> ]]>) and then declaring the textview in java code as myTextView.setText(Html.fromHtml(getString(R.string.myText));
through java code, using the HTML tag String text = "<font color='hexvalue of green'>This is green</font> <font color='hexvalue of red'>and this is red</font>."; myTextView.setText(Html.fromHtml((text));
through Spannable text using java code.
Spannable span = new SpannableString("My String");
span.setSpan(new ForegroundColorSpan(Color.RED), start_position, end_position,Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
myTextView.setText(span);
If there are other ways to do it then I'm not aware of them.
Hope this helps
Refer your text to string.xml and using html font tag , by using that way you can change each letter color also .
just add this in java for that string:
TextView tv=(TextView)findViewById(R.id.tv);
tv.setText(Html.fromHtml(getString(R.string.any_text)));
and
In string.xml:
<string name="any_text">
<![CDATA[ <b><font color=#ff0000>write</b> your <b><font color=#0000ff>text</b> here .
]]>
</string>
hope help you
If you want to give text color in strings.xml then check out the below code:
<string name="by_continuing_i_confirm_that_i_have_nread_the_privacy_policy">
<font fgcolor='#ffffff' >By continuing, I confirm that I have \nread the</font> <font fgcolor='#2DBBDD' >Privacy Policy</font>
In Java class define TextView like this:
TextView tv = (TextView) findViewById(R.id.text1);
String text = "<font color=#cc0029>write any thing here</font> "+
"<font color=#ffcc00>write any thing here 2</font>";
tv.setText(Html.fromHtml(text));
<TextView
android:id="#+id/yourUniqueTextViewID"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World"
android:textColor="#color/RED" />
Where "RED" is a named constant you have to define under res/values/ in an xml file. Typically i create "colors.xml".
Or see this for a good set of predefined colors: Web colors in an Android color xml resource file

Android - Hyperlink missing underline

I use linkify to make a textview work as hyperlink, and it does work nicely. The only issue is the underline is missing, could anyone point me out what could cause the problem? shouldn't the underline come by default?
Thanks!
Take a look at the Spannable params
addLinks(Spannable text,...)
linkify class
SpannableStringBuilder class
-replying to comment-
SpannableStringbuilder implements CharSequence, which can be used in TextView.setText();
So once you finish making your underlined text, you can use TextView.setText() and still use the method your are using.
Or refer to this: How to set underline text on textview?
You can use the xml attribute autoLink="web" for the TextView widget to automatically detect if the content is a web address. Here is an example:
<TextView
android:id="#+id/text_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="#android:color/black"
android:textColorLink="#color/hyperlink_blue"
android:autoLink="web"
android:textSize="16sp"/>
The attribute textColor is for text other than hyperlinks, which will be 'black' in the above example and the attribute textColorLink is for any text that takes a form of a hyperlink - which will be blue per above.
You can also append other autoLink values by 'piping' them together:
android:autoLink="web|email|map|phone"
This works for TextView, AppCompatTextView (SupportV7/AppCompat), AppCompatTextView (androidx/AppCompat)

TextView , Text is grayed out

Text in the textView seems like it is grayed out.
I want so set the text color as when you click textView then It seems in black color as it is enabled.
For changing the text color of TextView you need to set its textColor property.
In XML file you can do like :
<TextView
android:text="Some text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#ffff00"
android:background="#ff0000"
/>
Programatically you can set TextColor and its Background in TextView using :
text_view.setBackgroundColor(Color.RED);
text_view.setTextColor(Color.BLUE);
Hope this helps. !
try this code
text.setBackgroundColor(56565);
text.setEnabled(true);
it will helpful for u
you can change just alpha value of the textview like as following
from https://gist.github.com/Skamp/1330169
final ColorStateList colors = textview.getTextColors();
...
textview.setTextColor(colors.withAlpha(255));

Categories

Resources