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>
Related
My app currently looks like this:
When I click on a button, the border color changes to green as shown above. I added a scrollview for my list of words as shown on the right side of the image. The problem is that this scrollview covers nearly the entire width of the screen and I am unable to click on the the first three rows of buttons.
A brief description of the layout:
I created a relative layout. The 25 buttons are in this relative layout. Then also within the relative layout I created a scrollable linear layout containing 20 textviews
Can someone please help me fix this layout? I want to be able to click on all the buttons and have the scrollview not block the buttons.
And I thought it might be a good idea to put the 25 buttons in its own relative layout within the main relative layout. Does anyone know how I can do that without redoing the entire layout?
Also, if you guys have any advice on the correct way to code the layout please let me know. I am fairly knew to android programming and don't know if I should use relative layout vs linear layout vs frame layout or match_parent vs fill_parent vs wrap_content. Please let me know if I am misusing any of them.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".Game" >
<Button
android:id="#+id/button1"
android:layout_width="#dimen/size"
android:layout_height="#dimen/size"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginTop="18dp"
android:background="#drawable/button"
android:onClick="ButtonOnClick" />
.
. (Buttons 2 - 24)
.
<Button
android:id="#+id/button25"
android:layout_width="#dimen/size"
android:layout_height="#dimen/size"
android:layout_alignBaseline="#+id/button24"
android:layout_alignBottom="#+id/button24"
android:layout_alignLeft="#+id/button20"
android:background="#drawable/button"
android:onClick="ButtonOnClick" />
<ScrollView
android:id="#+id/scrollable"
android:layout_width="wrap_content"
android:layout_height="150dp"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:scrollbars="vertical" >
<LinearLayout
android:id="#+id/ll"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="300dp"
android:layout_marginTop="20dp"
android:orientation="vertical" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/word1" />
.
. (TextViews 2 - 19)
.
<TextView
android:id="#+id/textView20"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/word20" />
</LinearLayout>
</ScrollView>
</RelativeLayout>
Use a LinearLayout instead of RelativeLayout as your top layout and make it android:orientation="horizontal".
Then put all of your buttons in a RelativeLayout with an android:layout_weight="1" and android:layout_width="0dp".
Also put the ScrollView in a second RelativeLayout with a width of "wrap_content".
Here is the structure:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<RelativeLayout
android:layout_widtht="0dp"
android:layout_heigh="fill_parent"
android:layout_weight="1">
All buttons go here...
</RelativeLayout>
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="fill_parent">
Scroll view goes here...
</RelativeLayout>
</LinearLayout>
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>
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.
Here is the XML:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#drawable/bg"
>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="4"
android:orientation="vertical"
>
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center_horizontal" //CENTER 01
>
<!--center button-->
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/img"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true" //CENTER 02
/>
//...
</RelativeLayout>
</LinearLayout>
<include android:id="#+id/banner" layout="#layout/banner"
android:layout_weight="1"
android:layout_gravity="bottom"/>
</LinearLayout>
The center button is centered, but not in the real center of the screen, but rather approx 50px to the right of the center. If I remove centerHorizontal CENTER01, then this does not happen.
Does anyone know why is this happening?
Please try for //CENTER 02:
android:layout_centerInParent="true"
This issue may be due to using of both Relative Layout and Linear Layout inside a xml file.
Change all Linear Layout to Relative Layouts so that Image View will be centered as you expected.
I too mingled both Linear Layout and Relative Layout in single XML and faces this kind of issue. Once i replaces all Linear Layout to Relative Layout my problem Resolved.
Try it.Thanks.Venky
Its because in Relative Layout center_horizontal isn't the command. that works for views wrapped in linear Layout. for RelativeLayout you need to use
android:layout_centerInParent="true";
as the center gravity command in linear
or
android:layout_centerHorizontal="true"
as the gravity center_horizontal in linear.
When you set android:gravity="center_horizontal" on a parent, it influences the behaviour of its children. Hence, your ImageView gets effected, creating the effect you see after already being placed in the center by other properties.
You can see this effect for yourself by placing a TextView in there with android:layout_width="fill_parent".