spacing between buttons - android

Sorry as this is a repeated question , but posting Is necessary as everyone would have different setups
Here is my xml file .... Please suggest to bring proper spacing between the three buttons , Any further suggestions would be appreciated
MainMenu.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#drawable/mainbackground"
android:orientation="vertical"
android:paddingBottom="2dp"
android:paddingLeft="2dp"
android:paddingRight="2dp"
android:paddingTop="2dp" >
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#drawable/maintitle">
</LinearLayout>
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<Button
android:id="#+id/BtnDisplay"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="10dip"
android:layout_marginRight="10dip"
android:layout_marginTop="100dip"
android:background="#drawable/buttonshape"
android:text="#string/mem_sig_display"
android:textColor="#315683"
android:textSize="30px" />
</LinearLayout>
<Button
android:id="#+id/BtnSlave"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="10dip"
android:layout_marginRight="10dip"
android:background="#drawable/buttonshape"
android:gravity="center_vertical|center_horizontal"
android:paddingBottom="2dp"
android:paddingLeft="2dp"
android:paddingRight="2dp"
android:paddingTop="2dp"
android:text="#string/mes_mod_linked"
android:textColor="#315683"
android:textSize="30px" />
<Button
android:id="#+id/Histbutton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/buttonshape"
android:gravity="center"
android:paddingBottom="2dp"
android:paddingLeft="2dp"
android:paddingRight="2dp"
android:paddingTop="2dp"
android:text="#string/History"
android:textColor="#315683"
android:textSize="30sp" />
</LinearLayout>
I tried the margins and padding but seems like am not able to detect the right measurements required

Best way to deal with multiple buttons in linear layout is by using
weight property
. Also for spacing use padding or margin (carefully use one of them as padding is between components and margin is with refernce to boundaries).

Try this
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:padding="2dp" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<Button
android:id="#+id/BtnDisplay"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textColor="#315683"
android:textSize="30px" />
<Button
android:id="#+id/BtnSlave"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical|center_horizontal"
android:textColor="#315683"
android:textSize="30px" />
<Button
android:id="#+id/Histbutton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="#315683"
android:textSize="30sp" />
</LinearLayout>
</LinearLayout>

Related

Setting layouts to the size of their contents

I have a simple XML I am trying to inflate, and struggling with the layout. What I want to achieve is quite simple (or so I thought), so I am sure I am going about it incorrectly. Basically I have three elements, 2 TextViews, and an Imageview. I want the two text boxes placed on top of one another, with the image on the right of them, aligned to the right side, and its view fitted it the size of the image. Like this:
Text 1 |==|
Text 2 would be below text 1 |==|
Heres my XML:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/entry_container"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:minHeight="?android:attr/listPreferredItemHeight"
android:orientation="horizontal"
android:paddingRight="?android:attr/scrollbarSize" >
<LinearLayout
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="#+id/entry1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ellipsize="marquee"
android:fadingEdge="horizontal"
android:gravity="left"
android:paddingLeft="#dimen/tab_host_default_height"
android:text="#string/test_string"
android:textStyle="bold" />
<TextView
android:id="#+id/entry2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ellipsize="marquee"
android:fadingEdge="horizontal"
android:gravity="left"
android:paddingLeft="5dp"
android:paddingTop="5dp"
android:text="#string/test_string" />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center"
>
<ImageButton
android:id="#+id/entry3"
style="#style/ImageButtonBDTheme"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#drawable/activated_background_holo_light"
android:contentDescription="#string/click_for_repair_report"
android:paddingRight="5dp"
android:src="#drawable/entry_link" />
</LinearLayout>
</LinearLayout>
Problem is that I cannot get the image to display without hard coding the size of the LinearLayout containing the two text boxes.
Any help is appreciated!
// try this
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/entry_container"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="5dp"
android:minHeight="?android:attr/listPreferredItemHeight"
android:paddingRight="?android:attr/scrollbarSize"
android:gravity="center">
<LinearLayout
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TextView
android:id="#+id/entry1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ellipsize="marquee"
android:fadingEdge="horizontal"
android:paddingLeft="#dimen/tab_host_default_height"
android:text="#string/test_string"
android:textStyle="bold" />
<TextView
android:id="#+id/entry2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ellipsize="marquee"
android:fadingEdge="horizontal"
android:text="#string/test_string" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:orientation="vertical">
<ImageButton
android:id="#+id/entry3"
style="#style/ImageButtonBDTheme"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#drawable/activated_background_holo_light"
android:contentDescription="#string/click_for_repair_report"
android:paddingRight="5dp"
android:src="#drawable/entry_link" />
</LinearLayout>
</LinearLayout>
Try this..
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/entry_container"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:minHeight="?android:attr/listPreferredItemHeight"
android:orientation="horizontal"
android:paddingRight="?android:attr/scrollbarSize" >
<LinearLayout
android:layout_width="0dp" //changes here
android:layout_weight="0.6" //changes here
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="#+id/entry1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ellipsize="marquee"
android:fadingEdge="horizontal"
android:gravity="left"
android:paddingLeft="#dimen/tab_host_default_height"
android:text="#string/test_string"
android:textStyle="bold" />
<TextView
android:id="#+id/entry2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ellipsize="marquee"
android:fadingEdge="horizontal"
android:gravity="left"
android:paddingLeft="5dp"
android:paddingTop="5dp"
android:text="#string/test_string" />
</LinearLayout>
<LinearLayout
android:layout_width="0dp" //changes here
android:layout_weight="0.4" //changes here
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center"
>
<ImageButton
android:id="#+id/entry3"
style="#style/ImageButtonBDTheme"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#drawable/activated_background_holo_light"
android:contentDescription="#string/click_for_repair_report"
android:paddingRight="5dp"
android:src="#drawable/entry_link" />
</LinearLayout>
</LinearLayout>
Always Use Relative Layout, it requires lesser no. of code as compared to other Layouts.
Try below, i guess this is what you want, set Padding/Margin according to your requirement.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/entry_container"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:minHeight="?android:attr/listPreferredItemHeight"
android:orientation="horizontal"
android:paddingRight="?android:attr/scrollbarSize" >
<TextView
android:id="#+id/entry1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ellipsize="marquee"
android:fadingEdge="horizontal"
android:gravity="left"
android:paddingLeft="#dimen/tab_host_default_height"
android:text="#string/test_string"
android:textStyle="bold" />
<TextView
android:id="#+id/entry2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/entry1"
android:ellipsize="marquee"
android:fadingEdge="horizontal"
android:gravity="left"
android:paddingLeft="5dp"
android:paddingTop="5dp"
android:text="#string/test_string" />
<ImageButton
android:id="#+id/entry3"
style="#style/ImageButtonBDTheme"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:background="#drawable/ic_launcher"
android:contentDescription="#string/click_for_repair_report"
android:paddingRight="5dp"
android:src="#drawable/entry_link" />
</RelativeLayout>

Why does the imageview disappear on the layout?

The imageview is on the right of the layout. It doesn't display if the content of messagedetail_row_text is too long. The imageview does display if the content of messagedetail_row_text is not too long.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="15dp"
android:gravity="right"
android:orientation="horizontal" >
<LinearLayout
android:id="#+id/dfdwasdfds"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/outgoing"
android:orientation="vertical" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="22dip"
android:orientation="horizontal" >
<TextView
android:id="#+id/messagedetail_row_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingTop="1dp"
android:textColor="#000000"
android:textSize="18sp" />
<TextView
android:id="#+id/messagedetail_row_date"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="20dip"
android:paddingTop="1dp"
android:textColor="#000000"
android:textSize="16sp" />
</LinearLayout>
<TextView
android:id="#+id/messagedetail_row_text"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:paddingLeft="2dp"
android:textColor="#000000"
android:textSize="20sp" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_toRightOf="#id/dfdwasdfds"
>
<ImageView
android:id="#+id/messagegedetail_rov_icon2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="top"
android:gravity="right"
android:src="#drawable/retouxiang" />
</LinearLayout>
</RelativeLayout>
It is not clear what you are looking for exactly. This is from what I understand:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="15dp"
android:gravity="right"
android:orientation="horizontal" >
<LinearLayout
android:id="#+id/xyz"
android:layout_alignParentRight="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
>
<ImageView
android:id="#+id/messagegedetail_rov_icon2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="top"
android:gravity="right"
android:src="#drawable/retouxiang" />
</LinearLayout>
<LinearLayout
android:id="#+id/dfdwasdfds"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toLeftOf="#+id/xyz"
android:background="#drawable/outgoing"
android:orientation="vertical" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="22dip"
android:orientation="horizontal" >
<TextView
android:id="#+id/messagedetail_row_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingTop="1dp"
android:textColor="#000000"
android:text="messagedetail_row_name"
android:textSize="18sp" />
<TextView
android:id="#+id/messagedetail_row_date"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="20dip"
android:paddingTop="1dp"
android:text="messagedetail_row_date"
android:textColor="#000000"
android:textSize="16sp" />
</LinearLayout>
<TextView
android:id="#+id/messagedetail_row_text"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:paddingLeft="2dp"
android:text="messagedetail_row_text and this is a long text"
android:textColor="#000000"
android:textSize="20sp" />
</LinearLayout>
</RelativeLayout>
Try to change
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_toRightOf="#id/dfdwasdfds"
>
to
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_below="#id/dfdwasdfds"
>
I think it is happening because you have given your textView width fill_parent and it takes up as much space is its length is, then its parent width is wrap_content, so it goes upto its child occupies space(i.e TextView), so you dont have space to display the ImageView.
You can try using LinearLayout as root layout instead of relativeLayout..and manage your views by giving them weights..

Parent linear layout not taking up entire screen despite having fill_parent

I have a strange issue right now with a parent linear layout, I have its height set to fill_parent yet it seems intent on wrapping the content instead, what really is puzzling me even more is that in the graphical layout view its showing up the way Im intending for it to look but yet its still not doing what It should in the emulator?
here is my code:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#color/title_color_dark_transparent" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_gravity="center_vertical|center_horizontal|center"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginTop="5dp"
android:orientation="vertical"
android:paddingTop="5dp" >
<EditText
android:id="#+id/menutweetedittext"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#drawable/edit_text"
android:gravity="top"
android:hint="#string/edittext_hint"
android:inputType="textCapSentences|textMultiLine|textAutoCorrect|textAutoComplete"
android:lines="4"
android:textAppearance="#style/TextAppearance.EditText" />
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:gravity="right"
android:orientation="horizontal" >
<TextView
android:id="#+id/menucharactersremaining"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/characters"
android:textColor="#FFFFFF"
android:textSize="20dp" />
<Button
android:id="#+id/menuposttweetbutton"
style="#style/TextAppearance.Button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="2dp"
android:background="#drawable/button_background"
android:onClick="posttweetbuttonClicked"
android:text="#string/postbuttonstext"
android:textSize="15dp" />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="8dp"
android:orientation="vertical" >
</LinearLayout>
</LinearLayout>
<com.google.ads.AdView
android:id="#+id/menuadView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
ads:adSize="BANNER"
ads:adUnitId="a14f5be094d0328"
ads:loadAdOnCreate="true" />
</LinearLayout>
I took a shot at simplifying your layout. I ditched all your styling, but maybe the objects are in the right places. Is this what you're looking for?
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#android:color/darker_gray" >
<EditText
android:id="#+id/menutweetedittext"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:gravity="top"
android:hint="Hint"
android:inputType="textCapSentences|textMultiLine|textAutoCorrect|textAutoComplete"
android:lines="4"
android:textAppearance="#android:style/TextAppearance.Medium" />
<Button
android:id="#+id/menuposttweetbutton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_below="#+id/menutweetedittext"
android:layout_marginLeft="8dp"
android:layout_marginTop="8dp"
android:onClick="posttweetbuttonClicked"
android:text="Tweet"
android:textSize="15dp" />
<TextView
android:id="#+id/menucharactersremaining"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/menuposttweetbutton"
android:layout_toLeftOf="#+id/menuposttweetbutton"
android:text="This is the text view"
android:textColor="#FFFFFF"
android:textSize="20dp" />
<ImageView
android:id="#+id/menuadView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:background="#android:color/white"
android:src="#drawable/icon" />
</RelativeLayout>

Android linear layout align center and right

This is what I have:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout android:id="#+id/LinearLayout01"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:background="#FFFFFF">
<LinearLayout android:id="#+id/LinearLayout01"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:background="#E30000"
android:layout_gravity="center_horizontal|center_vertical"
android:gravity="center_horizontal|center_vertical">
<TextView
android:id="#+id/TextView00"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#FFFFFF"
android:textSize="20px"
android:textStyle="bold"
android:text="Something"
android:gravity="right|center_vertical"
/>
<LinearLayout android:id="#+id/LinearLayout01"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:gravity="right|center_vertical"
android:layout_gravity="center_vertical"
android:background="#E30000">
<Button
android:id="#+id/btnCalls"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:drawableLeft="#drawable/icon"
android:gravity="right|center_vertical"
/>
</LinearLayout>
</LinearLayout>
</LinearLayout>
And I want this:
So horizontal center align the text and vertical center align the button.
What am I missing? I haven't tried RelativeLayout and I would prefer LinearLayout to work this out.
android:gravity="right|center_vertical"
aligns your TextView to the right. How about center_horizontal?
And your LinearLayouts have the same ID, if there is not more code, then I would delete one of them.
try this one
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout android:id="#+id/LinearLayout01"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:background="#FFFFFF">
<LinearLayout android:id="#+id/LinearLayout01"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:background="#E30000"
android:layout_gravity="center_horizontal|center_vertical"
android:gravity="center_vertical">
<TextView
android:id="#+id/TextView00"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#FFFFFF"
android:textSize="20px"
android:textStyle="bold"
android:text="Something"
android:layout_weight="1"
android:gravity="center"/>
<LinearLayout android:id="#+id/LinearLayout01"
android:layout_height="wrap_content"
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:gravity="right|center_vertical"
android:layout_gravity="center_vertical"
android:background="#E30000"
android:layout_width="wrap_content">
<Button
android:id="#+id/btnCalls"
android:layout_width="wrap_content"
android:drawableLeft="#drawable/icon"
android:gravity="right|center_vertical"
android:layout_height="wrap_content"
android:layout_margin="5dip"/>
</LinearLayout>
</LinearLayout>
</LinearLayout>
I found none of the answers work exactly as the OP intended, but the RelativeLayout solution was the closest one. Except that the TextView wasn't centered. But, with layout_centerInParent=true, works like a charm:
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="#+id/TextView00"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#FFFFFF"
android:textSize="20px"
android:textStyle="bold"
android:text="Something"
android:layout_centerInParent="true" />
<Button
android:id="#+id/btnCalls"
android:drawableLeft="#drawable/icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"/>
</RelativeLayout>
you have used android:gravity="right|center_vertical" in your textview. Use android:gravity="center".
Try to add empty View inside horizontal LinearLayout between element that you want see left and element that you want to see right, e.g.:
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<View
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_weight="1"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
define layout weight for TextView and LinearLayout
set android:layout_weight="1" to TextView
set android:layout_weight="0" to LinearLayout
and also change the layout_width="wrap_content" to LinearLayout
and set layout_gravity="center" to TextView
I think,gravity means the position of things inside the component(for textview,it means the text you have given)in component where layout_gravity means position of component itself in the whole layout.So...
Try Using:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout android:id="#+id/LinearLayout01"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:background="#FFFFFF"
>
<LinearLayout android:id="#+id/LinearLayout01"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:background="#E30000"
android:layout_gravity="right"
android:gravity="center_vertical|right"
>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right|center_vertical"
android:gravity="center_vertical|center"
android:layout_weight="0.9"
>
<TextView
android:id="#+id/TextView00"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#FFFFFF"
android:textSize="20px"
android:textStyle="bold"
android:text="Something"
android:gravity="center_horizontal"
android:layout_gravity="center_horizontal|center_vertical"
/>
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right|center_vertical"
android:gravity="center_vertical|right"
android:paddingTop="5dip"
android:layout_weight="0.1"
>
<Button
android:id="#+id/btnCalls"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:drawableLeft="#drawable/icon"
android:layout_gravity="center_vertical"
android:gravity="center"
/>
</LinearLayout>
</LinearLayout>
</LinearLayout>
Just use RelativeLayout, and wallaaa!!!
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="#+id/TextView00"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#FFFFFF"
android:textSize="20px"
android:textStyle="bold"
android:text="Something"
android:gravity="center" />
<Button
android:id="#+id/btnCalls"
android:drawableLeft="#drawable/icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"/>
</RelativeLayout>
The solution for this is-
android:gravity="center_vertical"
Normally you'll also have to take care of its parent's position too to make sure it works.

Android Create aligned fields Layout

Here's my issue I have a login and want to line up the EditText fields. See the mockup picture attached .
I have tried a few ways and cannot figure out how to make the fields lineup.
I really hate layouts in Android I spend more time just messing with things to line up right. Wish they would just scrap it and use something more intuitive, as its more like using tables in HTML. So in advance thanks for your help and cheers!
You could probably play around with RelativeLayout and get just what you want.
Here's an approximation with TableLayout.
<?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">
<TableLayout
android:layout_marginTop="40dip"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center">
<TableRow>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="right"
android:padding="10dip"
android:text="UserID:" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="10dip"
android:text="USER ID" />
</TableRow>
<TableRow>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="right"
android:padding="10dip"
android:text="Password:" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="10dip"
android:text="PASSWORD" />
</TableRow>
</TableLayout>
<LinearLayout
android:layout_marginTop="50dip"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:weightSum="3"
android:gravity="center">
<Button
android:layout_width="0dip"
android:layout_height="fill_parent"
android:layout_weight="1"
android:text="LOGIN" />
<Button
android:layout_width="0dip"
android:layout_height="fill_parent"
android:layout_weight="1"
android:text="REGISTER" />
</LinearLayout>
</LinearLayout>
See if this works for you. It doesnt center quite perfectly but it does align well across multiple screen sizes.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" android:background="#FFFF0000">
<LinearLayout android:id="#+id/top_row"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:orientation="vertical" android:layout_centerHorizontal="true" android:layout_width="wrap_content" android:layout_marginTop="25dip">
<LinearLayout android:layout_height="wrap_content" android:orientation="horizontal" android:layout_width="fill_parent">
<TextView android:layout_height="wrap_content" android:text="User Id" android:gravity="right" android:layout_marginRight="10dip" android:layout_width="fill_parent" android:layout_weight="2" android:textColor="#FF000000"></TextView>
<EditText android:layout_height="wrap_content" android:layout_width="fill_parent" android:ems="10" android:layout_weight="1"></EditText>
</LinearLayout>
<LinearLayout android:layout_height="wrap_content" android:orientation="horizontal" android:layout_width="fill_parent">
<TextView android:layout_height="wrap_content" android:text="Password" android:gravity="right" android:layout_marginRight="10dip" android:layout_width="fill_parent" android:layout_weight="2" android:textColor="#FF000000"></TextView>
<EditText android:layout_height="wrap_content" android:layout_width="fill_parent" android:ems="10" android:layout_weight="1"></EditText>
</LinearLayout>
</LinearLayout>
<LinearLayout
android:layout_height="wrap_content"
android:orientation="horizontal" android:layout_width="wrap_content" android:layout_below="#+id/top_row" android:layout_centerHorizontal="true" android:layout_marginTop="20dip">
<Button android:layout_width="wrap_content" android:text="LOGIN" android:layout_height="wrap_content" android:layout_marginRight="10dip"></Button>
<Button android:text="REGISTER" android:layout_height="wrap_content" android:layout_width="wrap_content" android:layout_marginLeft="10dip"></Button>
</LinearLayout>
</RelativeLayout>
Here's what it looks like in my IDE...
You can do this quick and dirty using TableLayout, just add a TableRow with a TextView and a EditText for the userid and repeat the same for the password.
See http://developer.android.com/resources/tutorials/views/hello-tablelayout.html

Categories

Resources