Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to center a textview inside a linearlayout

I have the following layout i'd like to make the textview appear in the center and middle of the view. How can i acheive this?

I've tried adjusting the various gravity attributes when looking at the layout in graphical view, but nothing seems to change it. I'd like the textview in the center which i've done but halfway down the view.

thanks matt.

<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"     android:layout_width="match_parent"     android:layout_height="match_parent"     android:orientation="vertical"     android:background="@drawable/carefreebgscaledalphajpg" >        <TextView         android:id="@+id/textviewdatefornocallspage"         android:layout_width="match_parent"         android:layout_height="wrap_content"         android:gravity="center"         android:text="You have no calls for "         android:textSize="25sp" />  </LinearLayout> 
like image 610
turtleboy Avatar asked Nov 20 '12 14:11

turtleboy


2 Answers

set the gravity to center in your linearLayout tag :

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"     android:layout_width="match_parent"     android:layout_height="match_parent"     android:orientation="vertical"      android:gravity="center"     android:background="@drawable/carefreebgscaledalphajpg" > 

or use a RelativeLayout like this :

<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"     android:layout_width="match_parent"     android:layout_height="match_parent"     android:background="@drawable/carefreebgscaledalphajpg" >        <TextView         android:id="@+id/textviewdatefornocallspage"         android:layout_width="match_parent"         android:layout_height="wrap_content"         android:layout_centerInParent="true"         android:text="You have no calls for "         android:textSize="25sp" />  </RelativeLayout> 
like image 138
Houcine Avatar answered Sep 20 '22 16:09

Houcine


Try this

simple i have tried to many answer but all answers are not working ......finally this methods works for me

                     <LinearLayout                         android:layout_below="@+id/iv_divider"                         android:layout_width="match_parent"                         android:layout_height="wrap_content"                         android:orientation="vertical"                         android:layout_marginTop="@dimen/_10dp">                         <TextView                             android:layout_width="match_parent"                             android:layout_height="wrap_content"                             android:text="@string/mycredits"                             android:textColor="@color/colorGray"                             android:gravity="center"                             android:textSize="@dimen/_25dp" />                     </LinearLayout> 
like image 34
Sunil Avatar answered Sep 21 '22 16:09

Sunil