help me please. :(
how can i put the TextView in the center of the screen, dynamically?
i mean, any type of screen resolution its fix in the center of the screen.
thanks in advance. :)
This is a very basic Android principle. You should read through the Android Developers course. It will show you how to do things like this.
To answer your question, use a layout that has the layout parameters of match_parent and set the gravity to center like this:
<RelativeLayout android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center" />
Or programmatically:
relativeLayout.setGravity(Gravity.CENTER);
You could use a Relative Layout and in it add your Text View with the following attributes.
android:layout_centerInParent="true"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
Put it to Relative layout and add
android:layout_centerInParent="true"
Or in linear layout that matches the parent height and width use this code
android:gravity="center"
android:layout_gravity="center"
you can place the gravity for a textview as below:
<YourTextView>.setGravity(Gravity.CENTER);
you can use below code
<?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:gravity="center"
android:orientation="vertical" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:id="#+id/txt"
android:text="#string/hello" />
</LinearLayout>
Related
I have this android layout that I've created, now I wanted to add a new button but I find out that I can't put it in the center ! I can only put it on the left or right sides ..
I've tried
android:layout_gravity="center"
but still the same !
and btw, I'm using a linear layout
Apply android:layout_gravity="center" onto your LinearLayout and not your Button. This should center it. If you just want to center it vertically or horizontally, set layout_gravity to center_horizontal or center_vertical.
Try , this to ur LinearLayout. And tell us how it works.
android:layout_gravity="center"
android:gravity="center_horizontal"
Try to use RelativeLayout and set you button, and add to button parameters by your need:
android:layout_centerVertical="true"
or/and
android:layout_centerHorizontal="true"
or just
android:layout_centerInParent="true"
You need to wrap your button in a relative layout.
<?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" >
<RelativeLayout
android:id="#+id/wrapper"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button"
android:layout_centerHorizontal="true"/>
</RelativeLayout>
</LinearLayout>
I have an ImageView and TextView. I want to place the TextView below the image view but with the middle of the TextView aligned with the middle of the ImageView (along the horizontal axis). If the text in the TextView changes to something much larger or smaller, the middle of the text always needs to remain aligned with the middle of the ImageView. Is this possible in xml?
Yes, you are simply looking to contain both elements in a vertical LinearLayout which has android:gravity set to center_horizontal.
Something like this:
<?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:gravity="center_horizontal"
android:orientation="vertical"
... >
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
... />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
... />
</LinearLayout>
Because the TextView's width is wrap_content, setting its gravity shouldn't be necessary. I would do it just for safety (and additionally, I may set the width to match_parent as well).
You can make use of RealtiveLayout as follows..
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_horizontal"
android:orientation="vertical"
... >
<ImageView
android:id="#+id/imageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
... />
<TextView
android:layout_below="#+id/imageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
/>
</RelativeLayout>
If you want your textview to be of single line only then add following properties to TextView
android:ellipsezed="end"
android:singleLine="true"
Use a given layout and add it to any of your existing layout you get result what your looking for ..
Hope this explanation works for you..
set TextView android:layout_width="fill_parent" and set android:gravity="center" in TextView.
I think it help you.
Thanks
I have this layout:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<TextView
android:id="#+id/solution_title"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Label1"
/>
<TextView
android:id="#+id/solution_description"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Label2"
/>
<ListView
android:id="#android:id/list"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#+id/label"
android:textSize="20px" >
</ListView>
</LinearLayout>
I am combining two TextView elements with a ListView. For some reason, the TextView items render next to each other instead of one below the other. Why does that happen? And how do I make them line up one below the other?
Thanks!
in the LinearLayout you need to specify the orientation as follow:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical" >
That's because the LinearLayout has a horizontal orientation by default.
Specify android:orientation="vertical" for it.
See LinearLayout tutorial
But you want: android:orientation="vertical"
Have you tried to set the attribute:
android:orientation="vertical"
Hello See Vertical orientation
You may consider to have two linear layouts and have the vertical orientation within another linear layout which is horizontal if that helps.
I'm trying to position an image below a TextView in a RelativeLayout but it doesn't work. when i set its position using android:layout_below. This is my xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:background="#ccc"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView
android:id="#+id/app_details_1"
android:text="#string/app_details_1"
android:textColor="#347"
android:gravity="center"
android:textSize="9pt"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
<ImageView
android:id="#+id/logo"
android:src="#drawable/secs"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_below="#+id/appdetails_1" <!-- I'm setting it here -->
/>
</RelativeLayout>
Instead it just places the image at the top left of the screen. How do I position it? Is layout_below a wrong positioning code?
There's a mistake in your xml. Must be
android:layout_below="#id/app_details_1"
it should be ok, but first try to make sure the textView is positioned on top by using layout_alignParentTop="true" for the textView and see if it does the trick.
I have this code:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/layoutAnim"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/leftCornerImage"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="fill_parent">
<ImageButton
android:id="#+id/btnNight"
android:layout_width="62px"
android:layout_height="62px" />
<ImageButton
android:id="#+id/btnAlarm"
android:layout_width="62px"
android:layout_height="62px" />
<ImageButton
android:id="#+id/btnThermo"
android:layout_width="62px"
android:layout_height="62px"
android:layout_gravity="bottom" />
<ImageButton
android:id="#+id/btnDigital"
android:layout_width="62px"
android:layout_height="62px"
android:layout_gravity="bottom" />
</LinearLayout>
<LinearLayout .....
</LinearLayout>
</LinearLayout>
Can somebody helps me. Why android:layout_gravity="bottom" doesn't work? If you have the better solution, please help me.
A vertical LinearLayout will only honor horizontal gravity; a horizontal LinearLayout will only honor vertical gravity. Use RelativeLayout, or else use android:layout_weight to distribute extra vertical space where you want it.
android:layout_gravity="bottom" here goes for text inside button.
If you want buttons anchered to the bottom, you should provide android:gravity="bottom"> to first inner LinearLayout.
The parent controls the gravity so android:gravity="bottom" should be in the containing linear layout instead. Note this will affect all of the views. If you want only the last view to hug the bottom, either use a RelativeLayout or place a plain view in between the two images and set its layout_weight to 1.
Hope this helps!