Snake
Snake

Reputation: 14658

Center text in textview is changing the view size based on text length

I am trying to create equally sized boxes with centered text. I am running into weird issue. When I create the boxed without the gravity.center then I am getting the following

enter image description here

When I add the the attribute center, I get the following

enter image description here

The text is nicely centered but the views are shifted for some reason when text size changes. Even if I cut the text down to one word then the box is bigger then everything else

Here is my layout.. What is it going on? How can I solve it?

   <LinearLayout android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
           <TextView
             android:id="@+id/tvPlayer1"
             android:layout_width="80dp"
             android:layout_height="80dp"
             android:background="@drawable/bg"
             android:padding="5dp"
             android:gravity="center"
             android:layout_marginRight="7dp"
             android:text="Name of Player Added" />

          <TextView
             android:id="@+id/tvPlayer1"
             android:layout_width="80dp"
             android:layout_height="80dp"
             android:background="@drawable/bg"
             android:layout_gravity="top"
             android:padding="5dp"
             android:gravity="center"
             android:layout_marginRight="7dp"
             android:text="Name of Player Added" />           
          <TextView
             android:id="@+id/tvPlayer1"
             android:layout_width="80dp"
             android:layout_height="80dp"
             android:background="@drawable/bg"
             android:padding="5dp"
             android:gravity="center"
             android:layout_marginRight="7dp"
             android:text="blah blah blah blah blah blah blah blah blah blah" /> 
          <TextView
             android:id="@+id/tvPlayer1"
             android:layout_width="80dp"
             android:layout_height="80dp"
             android:background="@drawable/bg"
             android:padding="5dp"
             android:gravity="center"
             android:layout_marginRight="7dp"
             android:text=" blah blah blah blah blah blah blah blah blah blah" /> 

        </LinearLayout>   

Upvotes: 3

Views: 159

Answers (3)

Naitik
Naitik

Reputation: 816

try like this. Hope you will get your desired output.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout android:layout_width="wrap_content"
        android:layout_height="80dp"
        android:orientation="horizontal">
           <TextView
             android:id="@+id/tvPlayer1"
             android:layout_width="80dp"
             android:layout_height="match_parent"
             android:background="@drawable/bg"
             android:padding="5dp"
             android:gravity="center"
             android:layout_marginRight="7dp"
             android:text="Name of Player Added" />

          <TextView
             android:id="@+id/tvPlayer1"
             android:layout_width="80dp"
             android:layout_height="match_parent"
             android:background="@drawable/bg"
             android:layout_gravity="top"
             android:padding="5dp"
             android:gravity="center"
             android:layout_marginRight="7dp"
             android:text="Name of Player Added" />           
          <TextView
             android:id="@+id/tvPlayer1"
             android:layout_width="80dp"
             android:layout_height="match_parent"
             android:background="@drawable/bg"
             android:padding="5dp"
             android:gravity="center"
             android:layout_marginRight="7dp"
             android:text="blah blah blah blah blah blah blah blah blah blah" /> 
          <TextView
             android:id="@+id/tvPlayer1"
             android:layout_width="80dp"
             android:layout_height="match_parent"
             android:background="@drawable/bg"
             android:padding="5dp"
             android:gravity="center"
             android:layout_marginRight="7dp"
             android:text=" blah blah blah blah blah blah blah blah blah blah" /> 

        </LinearLayout>  

Upvotes: 0

kevz
kevz

Reputation: 2737

try below layout

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
xmlns:android="http://schemas.android.com/apk/res/android"
android:weightSum="4">

<TextView
    android:id="@+id/tvPlayer1"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:background="@drawable/bg"
    android:text="Name of Player Added"
    android:layout_weight="1"
    android:padding="5dp"
    android:gravity="center"
    />

<TextView
    android:id="@+id/tvPlayer1"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:background="@drawable/bg"
    android:text="Name of Player Added"
    android:layout_weight="1"
    android:padding="5dp"
    android:gravity="center" />
<TextView
    android:id="@+id/tvPlayer1"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:background="@drawable/bg"
    android:text="blah blah blah blah blah blah blah blah blah blah"
    android:layout_weight="1"
    android:padding="5dp"
    android:gravity="center" />
<TextView
    android:id="@+id/tvPlayer1"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:background="@drawable/bg"
    android:text=" blah blah blah blah blah blah blah blah blah blah"
    android:layout_weight="1"
    android:gravity="center"/>

</LinearLayout>

Upvotes: 0

Bhawna Raheja
Bhawna Raheja

Reputation: 619

I just added android:gravity="center" in LinearLayout. Try this code:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
xmlns:android="http://schemas.android.com/apk/res/android"
android:background="@android:color/black"
android:gravity="center">
<TextView
    android:id="@+id/tvPlayer1"
    android:layout_width="80dp"
    android:layout_height="80dp"
    android:background="@android:color/holo_blue_bright"
    android:layout_marginRight="7dp"
    android:text="Name of Player Added"
    android:gravity="center"/>

<TextView
    android:id="@+id/tvPlayer2"
    android:layout_width="80dp"
    android:layout_height="80dp"
    android:background="@android:color/holo_red_light"
    android:layout_marginRight="7dp"
    android:text="Name of Player Added"
    android:gravity="center"/>
<TextView
    android:id="@+id/tvPlayer3"
    android:layout_width="80dp"
    android:layout_height="80dp"
    android:background="@android:color/darker_gray"
    android:layout_marginRight="7dp"
    android:gravity="center"
    android:text="blah blah blah blah blah blah blah blah blah blah" />
<TextView
    android:id="@+id/tvPlayer4"
    android:layout_width="80dp"
    android:layout_height="80dp"
    android:background="@android:color/holo_green_dark"
    android:layout_marginRight="7dp"
    android:text=" blah blah blah blah blah blah blah blah blah blah"
    android:gravity="center"/>

</LinearLayout>

Upvotes: 4

Related Questions