How do you center a TextView inside a layout?

Put problem concerning Views, Layouts and other XML-Resources (like AndroidManifest) here.

How do you center a TextView inside a layout?

Postby boxy » Fri Aug 14, 2009 10:52 pm

Hey all,

I want to center a TextView inside of a layout. I've been playing
with the XML code below and can't get it to work. It just left aligns the TextView.
Please help!


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/
android"
android:orientation="horizontal"
android:gravity="center"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >

<TextView
android:id="@+id/txtMessage"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center" />

</LinearLayout>
boxy
Freshman
Freshman
 
Posts: 5
Joined: Fri Aug 14, 2009 2:30 am

Top

Postby gecu_121 » Sun Aug 16, 2009 10:25 am

hello,

use absolute layout for placing your widgets anywhere on the screen easily.

use droiddraw for GUI developement for android. This will make your work very easy.

here is the link

http://droiddraw.blogspot.com/
gecu_121
Junior Developer
Junior Developer
 
Posts: 17
Joined: Thu Jul 30, 2009 2:39 pm

Postby mad.dogg » Sun Aug 16, 2009 11:25 am

Ok you almost got it right.
You have
<LinearLayout>
<TextView
android:layout_width="fill_parent"
android:layout_gravity="center"
/>
</LinearLayout>
How do you want to center something that is taking the whole space?
If it's boring it ain't heavy enough!
mad.dogg
Developer
Developer
 
Posts: 42
Joined: Tue May 20, 2008 2:18 pm
Location: Bulgaria

Postby nmc » Sun Aug 16, 2009 1:57 pm

use absolute layout for placing your widgets anywhere on the screen easily.


Yeah, great idea, and on the upcoming devices (using different screen sizes) your app will look either very professional (with a half filled screen) or the user cant see parts of your ui...
nmc
Senior Developer
Senior Developer
 
Posts: 154
Joined: Thu Nov 27, 2008 8:30 pm
Location: Germany

Postby nmc » Sun Aug 16, 2009 2:09 pm

I want to center a TextView inside of a layout. I've been playing
with the XML code below and can't get it to work. It just left aligns the TextView.
Please help!


This works just fine for me:
Syntax: [ Download ] [ Hide ]
Using xml Syntax Highlighting
  1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  2.    android:orientation="vertical"
  3.    android:layout_width="fill_parent"
  4.    android:layout_height="fill_parent"
  5.    >
  6. <TextView android:id="@+id/txtView"
  7.    android:layout_width="fill_parent"
  8.    android:layout_height="fill_parent"
  9.    android:text="Some text"
  10.    android:gravity="center"
  11. />
  12. </LinearLayout>
  13.  
Parsed in 0.002 seconds, using GeSHi 1.0.8.4
nmc
Senior Developer
Senior Developer
 
Posts: 154
Joined: Thu Nov 27, 2008 8:30 pm
Location: Germany

Postby gecu_121 » Mon Aug 17, 2009 6:19 pm

nmc wrote:
Yeah, great idea, and on the upcoming devices (using different screen sizes) your app will look either very professional (with a half filled screen) or the user cant see parts of your ui...


yes nmc, you are absolutely right. I didn't realize that.
But one doubt I have, where absolute layout can be used if they are not so much of use.
gecu_121
Junior Developer
Junior Developer
 
Posts: 17
Joined: Thu Jul 30, 2009 2:39 pm

Top

Postby nmc » Mon Aug 17, 2009 9:36 pm

But one doubt I have, where absolute layout can be used if they are not so much of use.


Maybe this could be usefull when creating views at runtime (when the display size is known and taken into account for placing the views).
nmc
Senior Developer
Senior Developer
 
Posts: 154
Joined: Thu Nov 27, 2008 8:30 pm
Location: Germany

Postby gecu_121 » Tue Aug 18, 2009 5:45 am

nmc wrote:
But one doubt I have, where absolute layout can be used if they are not so much of use.


Maybe this could be usefull when creating views at runtime (when the display size is known and taken into account for placing the views).


But if I try to create an instance of the absolute layout in java code, it shows that the instance creation is depricated.
gecu_121
Junior Developer
Junior Developer
 
Posts: 17
Joined: Thu Jul 30, 2009 2:39 pm

Postby nmc » Tue Aug 18, 2009 9:51 am

But if I try to create an instance of the absolute layout in java code, it shows that the instance creation is depricated.


Never tested that, but it proves what i said, doesnt it? :wink:
nmc
Senior Developer
Senior Developer
 
Posts: 154
Joined: Thu Nov 27, 2008 8:30 pm
Location: Germany

Postby gecu_121 » Tue Aug 18, 2009 12:28 pm

Never tested that, but it proves what i said, doesnt it? :wink:


Ya, its true. :)
Thanks
gecu_121
Junior Developer
Junior Developer
 
Posts: 17
Joined: Thu Jul 30, 2009 2:39 pm

Postby boxy » Wed Aug 19, 2009 10:05 pm

nmc wrote:
I want to center a TextView inside of a layout. I've been playing
with the XML code below and can't get it to work. It just left aligns the TextView.
Please help!


This works just fine for me:
Syntax: [ Download ] [ Hide ]
Using xml Syntax Highlighting
  1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  2.    android:orientation="vertical"
  3.    android:layout_width="fill_parent"
  4.    android:layout_height="fill_parent"
  5.    >
  6. <TextView android:id="@+id/txtView"
  7.    android:layout_width="fill_parent"
  8.    android:layout_height="fill_parent"
  9.    android:text="Some text"
  10.    android:gravity="center"
  11. />
  12. </LinearLayout>
  13.  
Parsed in 0.002 seconds, using GeSHi 1.0.8.4


I haven't had a chance to go home and test this out yet. I'm sure it works but I don't understand why changing the layout's orientation from "horizontal" to "vertical" would suddenly horizontally center the textview. Am I missing something?
boxy
Freshman
Freshman
 
Posts: 5
Joined: Fri Aug 14, 2009 2:30 am

Postby nmc » Wed Aug 19, 2009 11:59 pm

A quick test in androids resource-editor showed that this also works using a horizontal layout...

How did you verify that this doesnt work?
nmc
Senior Developer
Senior Developer
 
Posts: 154
Joined: Thu Nov 27, 2008 8:30 pm
Location: Germany

Top

Return to View, Layout & Resource Problems

Who is online

Users browsing this forum: No registered users and 4 guests