Rectangle 27 9

If the TextView's height and width are wrap content then the text within the TextView always be centered. But if the TextView's width is match_parent and height is match_parent or wrap_content then you have to write the below code:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center" 
        android:text="Hello World" />

</RelativeLayout>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:text="Hello World" />

</LinearLayout>

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

android text center textview
Rectangle 27 9

If the TextView's height and width are wrap content then the text within the TextView always be centered. But if the TextView's width is match_parent and height is match_parent or wrap_content then you have to write the below code:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center" 
        android:text="Hello World" />

</RelativeLayout>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:text="Hello World" />

</LinearLayout>

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

android text center textview
Rectangle 27 6

The TextView's height and width are wrap content then the text within the textview always be centered, then make center in its parent layout by using:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:text="Hello.."/>
</RelativeLayout>

For LinearLayout also the code is same :

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello.."/>
</LinearLayout>

and pro-grammatically parent is RelativeLayout java code this at run time use something like this in your activity

TextView textView1 =(TextView)findViewById(R.id.texView1);
RelativeLayout.LayoutParams layoutParams = RelativeLayout.LayoutParams)textView1.getLayoutParams();
layoutParams.addRule(RelativeLayout.CENTER_IN_PARENT, RelativeLayout.TRUE);
textView1.setLayoutParams(layoutParams);

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

android text center textview
Rectangle 27 6

The TextView's height and width are wrap content then the text within the textview always be centered, then make center in its parent layout by using:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:text="Hello.."/>
</RelativeLayout>

For LinearLayout also the code is same :

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello.."/>
</LinearLayout>

and pro-grammatically parent is RelativeLayout java code this at run time use something like this in your activity

TextView textView1 =(TextView)findViewById(R.id.texView1);
RelativeLayout.LayoutParams layoutParams = RelativeLayout.LayoutParams)textView1.getLayoutParams();
layoutParams.addRule(RelativeLayout.CENTER_IN_PARENT, RelativeLayout.TRUE);
textView1.setLayoutParams(layoutParams);

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

android text center textview
Rectangle 27 249

There are three ways of underling the text in TextView.

Let me explain you all approaches :

For underling the text in TextView you have to use SpannableString

String udata="Underlined Text";
SpannableString content = new SpannableString(udata);
content.setSpan(new UnderlineSpan(), 0, udata.length(), 0);
mTextView.setText(content);

You can make use of setPaintFlags method of TextView to underline the text of TextView.

mTextView.setPaintFlags(mTextView.getPaintFlags() | Paint.UNDERLINE_TEXT_FLAG);
mTextView.setText("This text will be underlined");

You can refer constants of Paint class if you want to strike thru the text.

Html.fromHtml(htmlString);
String htmlString="<u>This text will be underlined</u>";
mTextView.setText(Html.fromHtml(htmlString));
txtView.setText(Html.fromHtml("<u>underlined</u> text"));

Glad to hear that..Mark this question as solved so that other users can refer it.

A third approach would be using Html.fromHtml("<u>This text will be underlined</u>"), but I have to admit I'm a much bigger fan of using SpannableStrings. @Kartik: You might as well use a StrikethroughSpan on the text to create the strikethrough effect. :)

Love the 3rd approach. Short, simple and concise and adds just one extra line of code to my program.

I know this is kinda old, but is there a way to set the thickness of the underline?

To draw an Underline below the TextView in Android - Stack Overflow

android textview underline
Rectangle 27 248

There are three ways of underling the text in TextView.

Let me explain you all approaches :

For underling the text in TextView you have to use SpannableString

String udata="Underlined Text";
SpannableString content = new SpannableString(udata);
content.setSpan(new UnderlineSpan(), 0, udata.length(), 0);
mTextView.setText(content);

You can make use of setPaintFlags method of TextView to underline the text of TextView.

mTextView.setPaintFlags(mTextView.getPaintFlags() | Paint.UNDERLINE_TEXT_FLAG);
mTextView.setText("This text will be underlined");

You can refer constants of Paint class if you want to strike thru the text.

Html.fromHtml(htmlString);
String htmlString="<u>This text will be underlined</u>";
mTextView.setText(Html.fromHtml(htmlString));
txtView.setText(Html.fromHtml("<u>underlined</u> text"));

Glad to hear that..Mark this question as solved so that other users can refer it.

A third approach would be using Html.fromHtml("<u>This text will be underlined</u>"), but I have to admit I'm a much bigger fan of using SpannableStrings. @Kartik: You might as well use a StrikethroughSpan on the text to create the strikethrough effect. :)

Love the 3rd approach. Short, simple and concise and adds just one extra line of code to my program.

I know this is kinda old, but is there a way to set the thickness of the underline?

To draw an Underline below the TextView in Android - Stack Overflow

android textview underline
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 2

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

This is does not make the underline the same color as text

How to create a Textview with underline text in android - Stack Overfl...

android
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 87

Three ways of underling the text in TextView

For underling the text in TextView you have to use SpannableString

String udata="Underlined Text";
SpannableString content = new SpannableString(udata);
content.setSpan(new UnderlineSpan(), 0, udata.length(), 0);//where first 0 shows the starting and udata.length() shows the ending span.if you want to span only part of it than you can change these values like 5,8 then it will underline part of it.
mTextView.setText(content);

You can make use of setPaintFlags method of TextView to underline the text of TextView.

mTextView.setPaintFlags(mTextView.getPaintFlags() | Paint.UNDERLINE_TEXT_FLAG);
mTextView.setText("This text will be underlined");

You can refer constants of Paint class if you want to strike thru the text.

String htmlString="<u>This text will be underlined</u>";
mTextView.setText(Html.fromHtml(htmlString));

The word is SpannableString not what you said Pedro Varela

Loved the 2nd approach, neat and clean

Can I underline text in an android layout? - Stack Overflow

android android-layout fonts
Rectangle 27 84

Three ways of underling the text in TextView

For underling the text in TextView you have to use SpannableString

String udata="Underlined Text";
SpannableString content = new SpannableString(udata);
content.setSpan(new UnderlineSpan(), 0, udata.length(), 0);//where first 0 shows the starting and udata.length() shows the ending span.if you want to span only part of it than you can change these values like 5,8 then it will underline part of it.
mTextView.setText(content);

You can make use of setPaintFlags method of TextView to underline the text of TextView.

mTextView.setPaintFlags(mTextView.getPaintFlags() | Paint.UNDERLINE_TEXT_FLAG);
mTextView.setText("This text will be underlined");

You can refer constants of Paint class if you want to strike thru the text.

String htmlString="<u>This text will be underlined</u>";
mTextView.setText(Html.fromHtml(htmlString));

The word is SpannableString not what you said Pedro Varela

Loved the 2nd approach, neat and clean

Can I underline text in an android layout? - Stack Overflow

android android-layout fonts
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 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 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