Rectangle 27 76

textview.setPaintFlags(textview.getPaintFlags()| Paint.UNDERLINE_TEXT_FLAG);

what about remove underline?

Sign up for our newsletter and get our top new questions delivered to your inbox (see an example).

Set underline text to TextView in android programmatically - Stack Ove...

android android-widget
Rectangle 27 74

textview.setPaintFlags(textview.getPaintFlags()| Paint.UNDERLINE_TEXT_FLAG);

what about remove underline?

Set underline text to TextView in android programmatically - Stack Ove...

android android-widget
Rectangle 27 2382

I'm assuming you're using XML layout.

<TextView  
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:gravity="center"
    android:text="@string/**yourtextstring**"
/>
.setGravity(Gravity.CENTER);

This doesn't work when used with a RelativeLayout where the layout's height & width are set to wrap_content

@Rob, if the width and height are wrap_content, then technically, the text is already centered.

If I want to align TextView relative to another view, but it's text centered in itself?

android:gravity="center"

Not to be confused with android:layout_gravity="center", which does something else.

Sign up for our newsletter and get our top new questions delivered to your inbox (see an example).

How do I center text horizontally and vertically in a TextView on Andr...

android text center textview
Rectangle 27 2376

I'm assuming you're using XML layout.

<TextView  
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:gravity="center"
    android:text="@string/**yourtextstring**"
/>
.setGravity(Gravity.CENTER);

This doesn't work when used with a RelativeLayout where the layout's height & width are set to wrap_content

@Rob, if the width and height are wrap_content, then technically, the text is already centered.

If I want to align TextView relative to another view, but it's text centered in itself?

android:gravity="center"

Not to be confused with android:layout_gravity="center", which does something else.

How do I center text horizontally and vertically in a TextView on Andr...

android text center textview
Rectangle 27 962

You should use:

For a sanity check, I just tried it, because I had a project open anyway, and yes, it's nice and red ;D

You can use various functions from the Color class to get the same effect of course.

Color.parseColor
text.setTextColor(Color.parseColor("#FFFFFF"));
Color.rgb
Color.argb
holder.text.setTextColor(Color.rgb(200,0,0));
holder.text.setTextColor(Color.argb(0,200,0,0));

And of course, if you want to define your color in an XML file, you can do this:

<color name="errorColor">#f00</color>

because the getColor() function is deprecated1, you need to use it like so:

ContextCompat.getColor(context, R.color.your_color);

You can also insert plain HEX, like so:

myTextView.setTextColor(0xAARRGGBB);

Where you have an alpha-channel first, then the color value.

Check out the complete manual of course, public class Color extends Object.

1This code used to be in here as well:

textView.setTextColor(getResources().getColor(R.color.errorColor));

This method is now deprecated in Android M. You can however use it from the contextCompat in the support library, as the example now shows.

In addition, if the text is a link you need to use text.setLinkTextColor(...); in code or android:textColorLink="..." in XML

@Nanne It would be nice if your answer also mentioned that R.color.XXX is a reference to the color. Meaning that it needs to be dereferenced (as it is in your example), for clarity.

I'm not sure what you mean? As in, dereferenced and so will use more resources or do you mean something else?

Is there any way of telling if a particular color value is going to let a text disappear?

getColor(int)

android - How to set the text color of TextView in code? - Stack Overf...

android colors textview
Rectangle 27 366

android:gravity="center"

The textview has to be match_parent or fill_parent for this to work. If its wrap content, then it will not center.

@KalelWade: centering text in a textview with wrap_content makes no sense at all. Maybe you're thinking of centering a textview in a parent view, in which case, you simiply put this line in the parent.

@Mooing Duck Thus the reason to not use wrap content - it doesn't make sense. But when you're either coping and pasting or just rushing, you may not put two and two together. That's why I mentioned it.

if TextView with width and height equal "wrap_content" is inside LinearLayout with width and height "match_parent" then you need to set the layout_gravity of Linear_layout.

How do I center text horizontally and vertically in a TextView on Andr...

android text center textview
Rectangle 27 366

android:gravity="center"

The textview has to be match_parent or fill_parent for this to work. If its wrap content, then it will not center.

@KalelWade: centering text in a textview with wrap_content makes no sense at all. Maybe you're thinking of centering a textview in a parent view, in which case, you simiply put this line in the parent.

@Mooing Duck Thus the reason to not use wrap content - it doesn't make sense. But when you're either coping and pasting or just rushing, you may not put two and two together. That's why I mentioned it.

if TextView with width and height equal "wrap_content" is inside LinearLayout with width and height "match_parent" then you need to set the layout_gravity of Linear_layout.

How do I center text horizontally and vertically in a TextView on Andr...

android text center textview
Rectangle 27 13

Here is the simplest way

TextView theTextView = new TextView(this);

theTextView.setText(Html.fromHtml("<u>Text to underline</u>"));

Set underline text to TextView in android programmatically - Stack Ove...

android android-widget
Rectangle 27 13

Here is the simplest way

TextView theTextView = new TextView(this);

theTextView.setText(Html.fromHtml("<u>Text to underline</u>"));

Set underline text to TextView in android programmatically - Stack Ove...

android android-widget
Rectangle 27 243

textView.setGravity(Gravity.CENTER_VERTICAL | Gravity.CENTER_HORIZONTAL);

What is the criteria for judgement to decide which way to choose, to write in xml or write in Java file?

@Kenju if you want it to be easily editable and the value won't change during runtime: XML. If you must change the value realtime (for example, change the alignment when the user presses a button) then you can only do it via Java.

How do I center text horizontally and vertically in a TextView on Andr...

android text center textview
Rectangle 27 243

textView.setGravity(Gravity.CENTER_VERTICAL | Gravity.CENTER_HORIZONTAL);

What is the criteria for judgement to decide which way to choose, to write in xml or write in Java file?

@Kenju if you want it to be easily editable and the value won't change during runtime: XML. If you must change the value realtime (for example, change the alignment when the user presses a button) then you can only do it via Java.

How do I center text horizontally and vertically in a TextView on Andr...

android text center textview
Rectangle 27 125

If you still want to specify your colors in your XML file:

<color name="errorColor">#f00</color>

Then reference it in your code with one of these two methods:

textView.setTextColor(getResources().getColor(R.color.errorColor, null));

The first is probably preferable if you're compiling against Android M, however the theme you pass in can be null, so maybe that's easier for you?

And if you're using the Compat library you can do something like this

textView.setTextColor(ContextCompat.getColor(context, R.color.errorColor));

For setTextColor, why does it have to take the getResources().getColor() rather than the direct R.color.errorColor reference? The R.color.x works for almost every other method. Incredibly frustrating!

@Civilian: because the int param that the setXXXColor() methods require is taken as the actual ARGB value to use, NOT the value to lookup in the resources file. Strangely enough, the View class has both setBackgroundColor() and setBackgroundResource(), while TextView is missing a setTextResource() method.

getColor(int)
ContextCompat.getColor(getContext(), R.color.yourColor);

android - How to set the text color of TextView in code? - Stack Overf...

android colors textview
Rectangle 27 8

TextView myTextView = new TextView(this);
SpannableString mySpannableString = new SpannableString("My String");
mySpannableString.setSpan(new UnderlineSpan(), 0, mySpannableString.length(), 0);
myTextView.setText(mySpannableString);

Set underline text to TextView in android programmatically - Stack Ove...

android android-widget
Rectangle 27 15

The solution goes like this -

Call setLinks() with you textview and the text to be added.

setLinks(textView, text);

setLinks() function is as -

void setLinks(TextView tv, String text) {
        String[] linkPatterns = {
                "([Hh][tT][tT][pP][sS]?:\\/\\/[^ ,'\">\\]\\)]*[^\\. ,'\">\\]\\)])",
                "#[\\w]+", "@[\\w]+" };
        for (String str : linkPatterns) {
            Pattern pattern = Pattern.compile(str);
            Matcher matcher = pattern.matcher(tv.getText());
            while (matcher.find()) {
                int x = matcher.start();
                int y = matcher.end();
                final android.text.SpannableString f = new android.text.SpannableString(
                        tv.getText());
                InternalURLSpan span = new InternalURLSpan();
                span.text = text.substring(x, y);
                f.setSpan(span, x, y,
                        android.text.Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
                tv.setText(f);
                // tv.setOnLongClickListener(span.l);

            }
        }
        tv.setLinkTextColor(Color.BLUE);
        tv.setLinksClickable(true);
        tv.setMovementMethod(LinkMovementMethod.getInstance());
        tv.setFocusable(false);
    }

and the InternalURLSpan class goes like this -

class InternalURLSpan extends android.text.style.ClickableSpan {
        public String text;

        @Override
        public void onClick(View widget) {
            handleLinkClicked(text);
        }

    }

handleLinkClicked() is as -

public void handleLinkClicked(String value) {
    if (value.startsWith("http")) {     // handle http links
    } else if (value.startsWith("@")) { // handle @links
    } else if (value.startsWith("#")) { // handle #links
    }
}

How can I separate links' onClickListener from the remaining text's onClickListener. I have onCLicklistener for textview as well as links within the textview. Your solution works fine, but links onClick also invokes textview's onClick listener. How can I separate both?

Get the value of link text when clicked in a textview in android - Sta...

android text textview hyperlink
Rectangle 27 59

You have to use SpannableString for that :

String mystring=new String("Hello.....");
SpannableString content = new SpannableString(mystring);
content.setSpan(new UnderlineSpan(), 0, mystring.length(), 0);
yourtextview.setText(content);

Update : You can refer my answer on Underling TextView's here in all possible ways.

android - How to set underline text on textview? - Stack Overflow

android textview
Rectangle 27 7

TextView myTextView = new TextView(this);
SpannableString mySpannableString = new SpannableString("My String");
mySpannableString.setSpan(new UnderlineSpan(), 0, mySpannableString.length(), 0);
myTextView.setText(mySpannableString);

Set underline text to TextView in android programmatically - Stack Ove...

android android-widget
Rectangle 27 221

Just build your String in HTML and set it:

String sourceString = "<b>" + id + "</b> " + name; 
mytextview.setText(Html.fromHtml(sourceString));

As fromHtml( sourceString) is deprecated in API 24 , you have to use next code : Spanned durationSpanned; if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.N) { durationSpanned = Html.fromHtml(durationFormatted,Html.FROM_HTML_MODE_LEGACY); } else { durationSpanned = Html.fromHtml(durationFormatted); }

This is the only option that will work if you are localizing your app.

Its a good solution but I found out it doesn't work for < and > @wtsang02 solution is more optimal

It don't work with < IF you use String.format, UNLESS you also escape the bracket, as described here.

android - how to make a specific text on TextView BOLD - Stack Overflo...

android android-textview android-textattributes
Rectangle 27 18

Use center for the gravity property in the TextView (not the layout_gravity).

android - TextView with Background and centered text - Stack Overflow

android background textview
Rectangle 27 214

While you can use Html.fromHtml() you can use a more native approach which is SpannableStringBuilder , this post may be helful.

SpannableStringBuilder str = new SpannableStringBuilder("Your awesome text");
str.setSpan(new android.text.style.StyleSpan(android.graphics.Typeface.BOLD), INT_START, INT_END, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
TextView tv=new TextView(context);
tv.setText(str);

For people coming from Google with no prior knowledge: INT_START/INT_END are the location on where the bold should start and end.

@wtsang02 INT_START and INT_END will be different positions in different localizations.

Try to understand what the code is doing when in doubt. The int start is a number you define, which is the start index of the span. Try 0 for start, string.().size() for end.

android - how to make a specific text on TextView BOLD - Stack Overflo...

android android-textview android-textattributes
Rectangle 27 62

You can also use the combination:

android:gravity="left|center"

Then, if textview width is more than "fill_parent" the text will still be aligned to left (not centered as with gravity set only to "center").

How do I center text horizontally and vertically in a TextView on Andr...

android text center textview