align text center with android

JavaAndroidXmlTextCenter

Java Problem Overview


I know it sounds easy. I need to put a text in center, but when the text is too long it needs to go below, but still align in the center of my xml.

Here's my code :

 <LinearLayout
	android:layout_width="wrap_content"
	android:layout_height="wrap_content"
	android:id="@+id/showdescriptioncontenttitle"
	android:paddingTop="10dp"
	android:paddingBottom="10dp"
	android:layout_centerHorizontal="true"
>
	<TextView 
		android:id="@+id/showdescriptiontitle"
		android:text="Title"
		android:textSize="35dp"
		android:layout_width="wrap_content"
		android:layout_height="wrap_content"
	/>
</LinearLayout>

I put paddingTop and Bottom because I need some space. PS: My code is bigger; it's in a RelativeLayout.

Java Solutions


Solution 1 - Java

Set also android:gravity parameter in TextView to center.

For testing the effects of different layout parameters I recommend to use different background color for every element, so you can see how your layout changes with parameters like gravity, layout_gravity or others.

Solution 2 - Java

use this way

txt.setGravity(Gravity.CENTER);

Solution 3 - Java

use this way in xml

   <TextView
        android:id="@+id/myText"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Time is precious, so love now."
        android:gravity="center"
        android:textSize="30dp"
        android:textColor="#fff"
		/>
      
        

Solution 4 - Java

You can use the following:

<LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/showdescriptioncontenttitle"
    android:paddingTop="10dp"
    android:paddingBottom="10dp">

 <TextView
        android:id="@+id/textview1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:text="Your text"
        android:typeface="serif" />
</LinearLayout>

The layout needs to be relative for the "center" property to be used.

Solution 5 - Java

Adding android:gravity="center" in your TextView will do the trick (be the parent layout is Relative/Linear)!

Also, you should avoid using dp for font size. Use sp instead.

Solution 6 - Java

Just add these 2 lines;

android:gravity="center_horizontal"
android:textAlignment="center"

Solution 7 - Java

add layout_gravity and gravity with center value on TextView

<TextView
    android:text="welcome text"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_gravity="center"
    android:gravity="center"
    />

Solution 8 - Java

android:layout_gravity="center"

or

android:gravity="center"

both works for me.

Solution 9 - Java

Or check this out this will help align all the elements at once.

 <LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/showdescriptioncontenttitle"
    android:paddingTop="10dp"
    android:paddingBottom="10dp"
    android:layout_gravity="center"
    android:gravity="center_horizontal"
>
    <TextView 
        android:id="@+id/showdescriptiontitle"
        android:text="Title"
        android:textSize="35dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
    />
</LinearLayout>

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionTsunazeView Question on Stackoverflow
Solution 1 - Javapeter.bartosView Answer on Stackoverflow
Solution 2 - Javabhargavkumar040View Answer on Stackoverflow
Solution 3 - JavasakshiView Answer on Stackoverflow
Solution 4 - JavagsbView Answer on Stackoverflow
Solution 5 - JavaDivyaView Answer on Stackoverflow
Solution 6 - JavaRamandeep SinghView Answer on Stackoverflow
Solution 7 - JavaMubinView Answer on Stackoverflow
Solution 8 - JavaDinithView Answer on Stackoverflow
Solution 9 - JavaMilan PatelView Answer on Stackoverflow