how to set in textview same sizes - android

I need to implement textview with equal sizes for 3 character, but i have got some problem if i add:
This four buttons have got diferent sizes ...but i dont know why, and how can i change
MOD
_X_
_(_
_5_
_ is white spaces..
There is xml code:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="1dp" >
<TextView
android:id="#+id/item_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="12sp" >
</TextView>
<TextView
android:id="#+id/item_text_Main"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="15sp" >
</TextView>
End there is picture of my problem:

Try this way,hope this will help you to solve your problem.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:lines="4"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1">
<TextView
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:padding="10dp"
android:gravity="center"
android:text="MC_"/>
<TextView
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:padding="5dp"
android:gravity="center"
android:layout_marginLeft="5dp"
android:text="MS_"/>
<TextView
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:padding="5dp"
android:gravity="center"
android:layout_marginLeft="5dp"
android:text="M+_"/>
<TextView
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:padding="5dp"
android:gravity="center"
android:layout_marginLeft="5dp"
android:text="_(_"/>
<TextView
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:padding="5dp"
android:gravity="center"
android:layout_marginLeft="5dp"
android:text="_)_"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:layout_marginTop="5dp">
<TextView
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:padding="10dp"
android:gravity="center"
android:text="MC_"/>
<TextView
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:padding="5dp"
android:gravity="center"
android:layout_marginLeft="5dp"
android:text="MS_"/>
<TextView
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:padding="5dp"
android:gravity="center"
android:layout_marginLeft="5dp"
android:text="M+_"/>
<TextView
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:padding="5dp"
android:gravity="center"
android:layout_marginLeft="5dp"
android:text="_(_"/>
<TextView
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:padding="5dp"
android:gravity="center"
android:layout_marginLeft="5dp"
android:text="_)_"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:layout_marginTop="5dp">
<TextView
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:padding="10dp"
android:gravity="center"
android:text="MC_"/>
<TextView
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:padding="5dp"
android:gravity="center"
android:layout_marginLeft="5dp"
android:text="MS_"/>
<TextView
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:padding="5dp"
android:gravity="center"
android:layout_marginLeft="5dp"
android:text="M+_"/>
<TextView
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:padding="5dp"
android:gravity="center"
android:layout_marginLeft="5dp"
android:text="_(_"/>
<TextView
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:padding="5dp"
android:gravity="center"
android:layout_marginLeft="5dp"
android:text="_)_"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:layout_marginTop="5dp">
<TextView
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:padding="10dp"
android:gravity="center"
android:text="MC_"/>
<TextView
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:padding="5dp"
android:gravity="center"
android:layout_marginLeft="5dp"
android:text="MS_"/>
<TextView
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:padding="5dp"
android:gravity="center"
android:layout_marginLeft="5dp"
android:text="M+_"/>
<TextView
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:padding="5dp"
android:gravity="center"
android:layout_marginLeft="5dp"
android:text="_(_"/>
<TextView
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:padding="5dp"
android:gravity="center"
android:layout_marginLeft="5dp"
android:text="_)_"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:layout_marginTop="5dp">
<TextView
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:padding="10dp"
android:gravity="center"
android:text="MC_"/>
<TextView
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:padding="5dp"
android:gravity="center"
android:layout_marginLeft="5dp"
android:text="MS_"/>
<TextView
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:padding="5dp"
android:gravity="center"
android:layout_marginLeft="5dp"
android:text="M+_"/>
<TextView
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:padding="5dp"
android:gravity="center"
android:layout_marginLeft="5dp"
android:text="_(_"/>
<TextView
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:padding="5dp"
android:gravity="center"
android:layout_marginLeft="5dp"
android:text="_)_"/>
</LinearLayout>
</LinearLayout>

Give your TextView a fix size width by changing layout_width from wrap_content to some fix dp size like 30dp
Example:
<TextView
android:id="#+id/item_text"
android:layout_width="32dp"
android:layout_height="wrap_content"
android:text="MOD"
android:textSize="12sp" >
</TextView>

look like you try to make button in table like in XML???
don`t know if this can help you, but when i try to make same precise width i use this...
here`s the explanation
create 1 linear layout with vertical orientation (so, everytime you add new item, it will placed to down)
inside it, insert linear layout with horizontal orientation... (insert as many row you want, in you case looks like you need 5 linear layout with horizontal orientation)
after that, insert you button / textview inside the linear layout (horizontal orientation), so your button / text will placed horizontaly.
if you want to change row, just insert the button / textview inside another linear layout (horizontal orientation).
after you insert all, set the linear layout (horizontal value) with this code
android:weightSum="5"
why 5? because in your case, u make 5 column.
and dont forget to set the width = fill parent.
after that set the button /textview inside the linearlayout (horizontal) with add this code
android:layout_weight="1"
and dont forget to change the width with fill parent.
with this, i can make precise width in every row. next step you can set the margin to make your button / textview looks good.
hope this can help you...
=====================================
here`s the code if you confuse with my explanation
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.example.tes.MainActivity"
tools:ignore="MergeRootFrame" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:weightSum="5" >
<Button
android:id="#+id/Button22"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Button" />
<Button
android:id="#+id/Button21"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Button" />
<Button
android:id="#+id/Button24"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Button" />
<Button
android:id="#+id/Button23"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Button" />
<Button
android:id="#+id/Button20"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Button" />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:weightSum="5" >
<Button
android:id="#+id/Button17"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Button" />
<Button
android:id="#+id/Button16"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Button" />
<Button
android:id="#+id/Button19"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Button" />
<Button
android:id="#+id/Button18"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Button" />
<Button
android:id="#+id/Button15"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Button" />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:weightSum="5" >
<Button
android:id="#+id/Button12"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Button" />
<Button
android:id="#+id/Button11"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Button" />
<Button
android:id="#+id/Button14"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Button" />
<Button
android:id="#+id/Button13"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Button" />
<Button
android:id="#+id/Button10"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Button" />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:weightSum="5" >
<Button
android:id="#+id/Button07"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Button" />
<Button
android:id="#+id/Button06"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Button" />
<Button
android:id="#+id/Button09"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Button" />
<Button
android:id="#+id/Button08"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Button" />
<Button
android:id="#+id/Button05"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Button" />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:weightSum="5">
<Button
android:id="#+id/Button04"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Button" />
<Button
android:id="#+id/Button03"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Button" />
<Button
android:id="#+id/Button02"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Button" />
<Button
android:id="#+id/Button01"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Button" />
<Button
android:id="#+id/button1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Button"
android:layout_weight="1"/>
</LinearLayout>

Related

Android RelativeLayout : Placing buttons side by side next to one another

I am trying to place buttons side by side next to one another in three buttons to one row using a RelativeLayout.
This is the relative layout placed inside a linear layout with the group of buttons
main.xml
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:orientation="vertical" >
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#CC8FD8D8"
android:gravity="center"
android:orientation="vertical"
android:paddingBottom="20px" >
</RelativeLayout>
</LinearLayout>
These are the group of button found inside the layouts
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:drawableTop="#drawable/snap"
android:textColor="#FFFFFF"
android:background="#00FFFFFF"
android:id="#+id/shutterButton"
android:text="SNAP"></Button>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:drawableTop="#drawable/up"
android:textColor="#FFFFFF"
android:background="#00FFFFFF"
android:id="#+id/Up"
android:text="xxxx"></Button>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:drawableTop="#drawable/ic_action_borrow"
android:textColor="#FFFFFF"
android:background="#00FFFFFF"
android:id="#+id/xxxx"
android:text="xxxxx"></Button>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:drawableTop="#drawable/xxxx"
android:textColor="#FFFFFF"
android:background="#00FFFFFF"
android:id="#+id/xxxx"
android:text="xxxx"></Button>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:drawableTop="#drawable/xxxx"
android:textColor="#FFFFFF"
android:background="#00FFFFFF"
android:id="#+id/xxxx"
android:text="xxxx"></Button>
OUTPUT
Please how can I place the buttons one after another in 3 buttons to one row.
Kindly assist!
Try this:
XML:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:orientation="vertical">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#CC8FD8D8"
android:gravity="center"
android:orientation="horizontal"
android:paddingBottom="20px">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#00FFFFFF"
android:text="SNAP"
android:textColor="#FFFFFF"></Button>
<!--android:drawableTop="#drawable/snap"-->
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#00FFFFFF"
android:text="xxxx"
android:textColor="#FFFFFF"></Button>
<Button
android:id="#+id/xxxx"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#00FFFFFF"
android:text="xxxxx"
android:textColor="#FFFFFF"></Button>
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#CC8FD8D8"
android:gravity="center"
android:orientation="horizontal"
android:paddingBottom="20px">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#00FFFFFF"
android:text="xxxx"
android:textColor="#FFFFFF"></Button>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#00FFFFFF"
android:text="xxxx"
android:textColor="#FFFFFF"></Button>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#00FFFFFF"
android:text="xxxx"
android:textColor="#FFFFFF"
android:visibility="invisible"></Button>
</LinearLayout>
</LinearLayout>
Output will be:
the following code creates 4 buttons side by side horizontally
<LinearLayout
android:layout_marginTop="1dp"
android:layout_marginBottom="1dp"
android:id="#+id/lay"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="50dp">
<Button
android:layout_width="0dp"
android:layout_height="match_parent"
android:id="#+id/button1"
android:text="Button1"
android:layout_weight="1"
android:padding="5dp" />
<Button
android:layout_width="0dp"
android:layout_height="match_parent"
android:id="#+id/button2"
android:text="Button2"
android:layout_weight="1"
android:padding="5dp" />
<Button
android:layout_width="0dp"
android:layout_height="match_parent"
android:id="#+id/button3"
android:text="Button3"
android:layout_weight="1"
android:padding="5dp" />
<Button
android:layout_width="0dp"
android:layout_height="match_parent"
android:id="#+id/button4"
android:text="Button4"
android:layout_weight="1"
android:padding="5dp" />
</LinearLayout>
You should linear layout for this kind of horizontal buttons and don't forget to make the orientation of inside LinearLayout to horizontal.
Like
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#CC8FD8D8"
android:gravity="center"
android:orientation="horizontal"
android:paddingBottom="20px" >
Then add buttons inside this linear layout, all the buttons will come side by side.
Try LinearLayout with android:orientation="horizontal". Check this below -
<?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:background="#CC8FD8D8"
android:gravity="center"
android:orientation="horizontal"
android:paddingBottom="20px">
<Button
android:id="#+id/shutterButton"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#00FFFFFF"
android:drawableTop="#drawable/snap"
android:text="SNAP"
android:textColor="#FFFFFF" />
<Button
android:id="#+id/Up"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#00FFFFFF"
android:drawableTop="#drawable/up"
android:text="xxxx"
android:textColor="#FFFFFF" />
<Button
android:id="#+id/xxxx"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#00FFFFFF"
android:drawableTop="#drawable/ic_action_borrow"
android:text="xxxxx"
android:textColor="#FFFFFF" />
<Button
android:id="#+id/xxxx"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#00FFFFFF"
android:drawableTop="#drawable/xxxx"
android:text="xxxx"
android:textColor="#FFFFFF" />
<Button
android:id="#+id/xxxx"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#00FFFFFF"
android:drawableTop="#drawable/xxxx"
android:text="xxxx"
android:textColor="#FFFFFF" />
</LinearLayout>
Try this
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#CC8FD8D8"
android:gravity="center"
android:orientation="vertical"
android:paddingBottom="20px" >
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:drawableTop="#drawable/snap"
android:textColor="#FFFFFF"
android:background="#00FFFFFF"
android:id="#+id/shutterButton"
android:text="SNAP"></Button>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:drawableTop="#drawable/up"
android:textColor="#FFFFFF"
android:background="#00FFFFFF"
android:id="#+id/Up"
android:text="xxxx"
android:layout_alignParentTop="true"
android:layout_toLeftOf="#+id/xxxxx"
android:layout_toStartOf="#+id/xxxxx"
android:layout_marginRight="27dp"
android:layout_marginEnd="27dp"></Button>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:drawableTop="#drawable/ic_action_borrow"
android:textColor="#FFFFFF"
android:background="#00FFFFFF"
android:id="#+id/xxxxx"
android:text="xxxxx"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"></Button>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:drawableTop="#drawable/xxxx"
android:textColor="#FFFFFF"
android:background="#00FFFFFF"
android:id="#+id/xxxxxx"
android:text="xxxx"
android:layout_alignParentTop="true"
android:layout_toLeftOf="#+id/xxxx"
android:layout_toStartOf="#+id/xxxx"
android:layout_marginRight="50dp"
android:layout_marginEnd="50dp"></Button>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:drawableTop="#drawable/xxxx"
android:textColor="#FFFFFF"
android:background="#00FFFFFF"
android:id="#+id/xxxx"
android:text="xxxx"
android:layout_centerVertical="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"></Button>
</RelativeLayout>
</LinearLayout>

Layout is streching while cicking on InputView (Showing the keybad)

I have an little issue related to the layout which is stretching when the keypad appears clicking on an InputView.
This is how the layout looks before clicking on ImageView:
Before
This how the layout looks after :
After
As you can the layout moves.
[EDIT] above XML layout :
<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"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin"
tools:context=".MainActivity">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_margin="10dp"
android:orientation="vertical"
android:weightSum ="10">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_margin="3dp"
android:orientation="horizontal"
android:layout_weight="1">
<ImageView
android:id="#+id/imgGameStatus"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1" />
<TextView
android:id="#+id/txtGameStatus"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="7"
android:layout_gravity="center_vertical"
android:text="Status..."/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_margin="3dp"
android:orientation="horizontal"
android:layout_weight="1">
<TextView
android:id="#+id/txtInvite"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="#string/lib_choose_number"
android:layout_gravity="center_vertical"
android:layout_weight="7"/>
<EditText
android:id="#+id/edtNumber"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:maxLength="4"
android:inputType="number"
android:paddingRight="4dp"
android:layout_gravity="center_vertical"
android:layout_weight="2" />
<Button
android:id="#+id/btnSend"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_weight="3"
android:text="#string/lib_send"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_margin="3dp"
android:gravity="center"
android:orientation="horizontal"
android:layout_weight="1">
<Button
android:id="#+id/imgBtn0"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="0"
android:layout_weight="1"
android:background="#color/green"/>
<Button
android:id="#+id/imgBtn1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="1"
android:layout_weight="1"
android:background="#color/green"/>
<Button
android:id="#+id/imgBtn2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="2"
android:layout_weight="1"
android:background="#color/green"/>
<Button
android:id="#+id/imgBtn3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="3"
android:layout_weight="1"
android:background="#color/green"/>
<Button
android:id="#+id/imgBtn4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="4"
android:layout_weight="1"
android:background="#color/green"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_margin="3dp"
android:gravity="center"
android:orientation="horizontal"
android:layout_weight="1">
<Button
android:id="#+id/imgBtn5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="5"
android:layout_weight="1"
android:background="#color/green"/>
<Button
android:id="#+id/imgBtn6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="6"
android:layout_weight="1"
android:background="#color/green"/>
<Button
android:id="#+id/imgBtn7"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="7"
android:layout_weight="1"
android:background="#color/green"/>
<Button
android:id="#+id/imgBtn8"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="8"
android:layout_weight="1"
android:background="#color/green"/>
<Button
android:id="#+id/imgBtn9"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="9"
android:layout_weight="1"
android:background="#color/green"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_margin="3dp"
android:gravity="center"
android:orientation="horizontal"
android:layout_weight="5">
<ListView
android:id="#+id/listView1"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</ListView>
</LinearLayout>
</LinearLayout>
</RelativeLayout>
your help please.
My guess is that your empty listview pushes up the buttons, when it has been pushed up by the keyboard.
Insert
android:windowSoftInputMode="stateHidden|adjustNothing"
in your activity in the manifest like so:
<activity
android:name=".activities.MyActivity"
android:windowSoftInputMode="stateHidden|adjustNothing">
</activity>
Insert
android:windowSoftInputMode="stateHidden|adjustNothing"
in your activity in the manifest like so:
<activity
android:name=".activities.MyActivity"
android:windowSoftInputMode="stateHidden|adjustNothing">
</activity>

Nesting RelativeLayouts within LinearLayout

I am trying to achieve the following layout in Android with the main heading at the top and three subheadings spaced equally apart below, with some buttons below that:
I was able to accomplish this with the following XML, but I feel like I am not using the RelativeLayout and LinearLayout properly:
<LinearLayout 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"
android:background="#cfff"
android:orientation="vertical"
tools:context=".MainActivity">
<TextView
android:id="#+id/text_title"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:layout_weight="1"
android:text="Title"
android:textSize="80sp"/>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_weight=".5">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1">
<TextView
android:id="#+id/text_caption1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Subheading"
android:gravity="center"
android:textSize="18sp"/>
<TextView
android:id="#+id/text_number1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#id/text_caption1"
android:text="1"
android:layout_weight="1"
android:textSize="80sp"
android:gravity="center"/>
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1">
<TextView
android:id="#+id/text_caption2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Subheading"
android:gravity="center"
android:textSize="18sp"/>
<TextView
android:id="#+id/text_number2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#id/text_caption2"
android:text="10"
android:layout_weight="1"
android:textSize="80sp"
android:gravity="center"/>
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1">
<TextView
android:id="#+id/text_caption3"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Subheading"
android:gravity="center"
android:textSize="18sp"/>
<TextView
android:id="#+id/text_sets"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#id/text_caption3"
android:text="3"
android:layout_weight="1"
android:textSize="80sp"
android:gravity="center"/>
</RelativeLayout>
</LinearLayout>
<!-- Button1 -->
<Button
android:id="#+id/button1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="0"
android:text="Button1"
android:textSize="20sp"/>
<!-- Button2 -->
<Button
android:id="#+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0"
android:text="Button2"
android:textSize="20sp" />
</LinearLayout>
I'm focusing somewhat on the Subheading portion, although any suggestions on the overall layout would be welcome. So I used 3 different RelativeLayouts to put the "Subheading" captions with their respective numbers, and nested those within a horizontal LinearLayout to get them spaced next to each other horizontally.
This seems to be creating a bit of a problem, since Android Studio is complaining that my use of layout_weight within nested layouts is bad for performance. When I get rid of the layout_weight however, everything falls apart and I only see the "Title" and one subheading. I'm also wondering whether I could have accomplished this more elegantly with just one RelativeLayout and no nesting, but I can't see how to code something like this without the use of a LinearLayout.
Thanks in advance for any suggestions!
Yes,you can accomplish this more elegantly with just one RelativeLayout and no nesting
I hope this is what you are looking for
<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"
android:background="#cfff"
tools:context=".MainActivity">
<TextView
android:id="#+id/text_title"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="Title"
android:textSize="80sp" />
<TextView
android:id="#+id/text_caption1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_above="#id/text_number1"
android:text="Subheading"
android:textSize="18sp" />
<TextView
android:id="#+id/text_number1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:gravity="left"
android:text="1"
android:textSize="80sp" />
<TextView
android:id="#+id/text_caption2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_above="#id/text_number2"
android:gravity="center"
android:text="Subheading"
android:textSize="18sp" />
<TextView
android:id="#+id/text_number2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:gravity="center"
android:text="2"
android:textSize="80sp" />
<TextView
android:id="#+id/text_caption3"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_above="#id/text_number3"
android:gravity="right"
android:text="Subheading"
android:textSize="18sp" />
<TextView
android:id="#+id/text_number3"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:gravity="right"
android:text="3"
android:textSize="80sp" />
<Button
android:id="#+id/button1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_above="#+id/button2"
android:text="Button1"
android:textSize="20sp" />
<Button
android:id="#+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:text="Button2"
android:textSize="20sp" />
</RelativeLayout>
try this one...
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:weightSum="10">
<TextView
android:layout_width="wrap_content"
android:layout_height="0dip"
android:layout_weight="5"
android:text="TITLE"
android:layout_gravity="center"
android:gravity="center"
android:textSize="45sp"
android:id="#+id/textView64" />
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="3"
android:weightSum="10">
<LinearLayout
android:orientation="vertical"
android:layout_width="0dip"
android:layout_weight="3.25"
android:gravity="center"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Subheading"
android:id="#+id/textView66" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="1"
android:textSize="50sp"
android:id="#+id/textView67" />
</LinearLayout>
<LinearLayout
android:orientation="vertical"
android:layout_width="0dp"
android:layout_weight="3.5"
android:gravity="center"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="SubHeading"
android:id="#+id/textView68" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="2"
android:textSize="50sp"
android:id="#+id/textView69" />
</LinearLayout>
<LinearLayout
android:orientation="vertical"
android:layout_width="0dip"
android:layout_weight="3.25"
android:gravity="center"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="SubHeading"
android:id="#+id/textView70" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="3"
android:textSize="50sp"
android:id="#+id/textView71" />
</LinearLayout>
</LinearLayout>
<Button
android:layout_width="match_parent"
android:layout_height="0dip"
android:layout_weight="0.8"
android:text="New Button"
android:id="#+id/button"
android:layout_gravity="center_horizontal" />
<Button
android:layout_width="wrap_content"
android:layout_height="0dip"
android:layout_weight="0.8"
android:text="New Button"
android:id="#+id/button1"
/>
Change android:weightSum and android:layout_weight acoording to your required ratio...
<LinearLayout 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"
android:background="#cfff"
android:orientation="vertical"
tools:context=".MainActivity"
android:weightSum="4">
<TextView
android:id="#+id/text_title"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:layout_weight="1.5"
android:text="Title"
android:textSize="80sp"/>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_weight="1.5"
android:weightSum="3">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1">
<TextView
android:id="#+id/text_caption1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Subheading"
android:gravity="center"
android:textSize="18sp"/>
<TextView
android:id="#+id/text_number1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#id/text_caption1"
android:text="1"
android:layout_weight="1"
android:textSize="80sp"
android:gravity="center"/>
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1">
<TextView
android:id="#+id/text_caption2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Subheading"
android:gravity="center"
android:textSize="18sp"/>
<TextView
android:id="#+id/text_number2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#id/text_caption2"
android:text="10"
android:layout_weight="1"
android:textSize="80sp"
android:gravity="center"/>
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1">
<TextView
android:id="#+id/text_caption3"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Subheading"
android:gravity="center"
android:textSize="18sp"/>
<TextView
android:id="#+id/text_sets"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#id/text_caption3"
android:text="3"
android:layout_weight="1"
android:textSize="80sp"
android:gravity="center"/>
</RelativeLayout>
</LinearLayout>
<!-- Button1 -->
<Button
android:id="#+id/button1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Button1"
android:layout_weight=".5"
android:textSize="20sp"/>
<!-- Button2 -->
<Button
android:id="#+id/button2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight=".5"
android:text="Button2"
android:textSize="20sp" />
</LinearLayout>

Evenly spaced out row of buttons required

Using 3 rows and 4 columns I have tried to evenly space out all the buttons horizontally but have failed. vertically is fine.
Below are 3 different attempts to space out the buttons, 1 for each of my 3 rows. The first row stretches the graphic (which is unwanted), the second row does not stretch but there is no spacing (spacing between the graphics is required), while the third stretches without any spacing. Please help me to evenly space these out. Other peoples solutions on this same site have not worked. Should I try using a table instead?
Visual summary= without stretching the graphic:
I want | x x x x |
and not | xxxx |
<?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" >
<LinearLayout
android:id="#+id/row1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight="1"
android:gravity="center"
android:orientation="horizontal" >
<View
android:layout_width="0dp"
android:layout_height="1dp"
android:layout_weight=".1" >
</View>
<Button
android:id="#+id/a1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#drawable/blanktilesml"
android:gravity="center_horizontal|center_vertical"
android:src="#drawable/clear64" />
<View
android:layout_width="0dp"
android:layout_height="1dp"
android:layout_weight=".1" >
</View>
<Button
android:id="#+id/a2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#drawable/blanktilesml"
android:gravity="center_horizontal|center_vertical"
android:src="#drawable/clear64" />
<View
android:layout_width="0dp"
android:layout_height="1dp"
android:layout_weight=".1" >
</View>
<Button
android:id="#+id/a3"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#drawable/blanktilesml"
android:gravity="center_horizontal|center_vertical"
android:src="#drawable/clear64" />
<View
android:layout_width="0dp"
android:layout_height="1dp"
android:layout_weight=".1" >
</View>
<Button
android:id="#+id/a4"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#drawable/blanktilesml"
android:gravity="center_horizontal|center_vertical"
android:src="#drawable/clear64" />
<View
android:layout_width="0dp"
android:layout_height="1dp"
android:layout_weight=".1" >
</View>
</LinearLayout>
<LinearLayout
android:id="#+id/row2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight="1"
android:gravity="center"
android:orientation="horizontal" >
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:orientation="horizontal" >
<Button
android:id="#+id/b1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#drawable/blanktilesml"
android:gravity="center"
android:src="#drawable/clear64" />
</LinearLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:orientation="horizontal" >
<Button
android:id="#+id/b2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#drawable/blanktilesml"
android:gravity="center"
android:src="#drawable/clear64" />
</LinearLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:orientation="horizontal" >
<Button
android:id="#+id/b3"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#drawable/blanktilesml"
android:gravity="center"
android:src="#drawable/clear64" />
</LinearLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:orientation="horizontal" >
<Button
android:id="#+id/b4"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#drawable/blanktilesml"
android:gravity="center"
android:src="#drawable/clear64" />
</LinearLayout>
</LinearLayout>
<LinearLayout
android:id="#+id/row3"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight="1"
android:gravity="center"
android:orientation="horizontal"
android:weightSum="1" >
<Button
android:id="#+id/c1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight=".25"
android:background="#drawable/blanktilesml"
android:paddingLeft="10dp"
android:src="#drawable/clear64" />
<Button
android:id="#+id/c2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight=".25"
android:background="#drawable/blanktilesml"
android:paddingLeft="10dp"
android:src="#drawable/clear64" />
<Button
android:id="#+id/c3"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight=".25"
android:background="#drawable/blanktilesml"
android:paddingLeft="10dp"
android:src="#drawable/clear64" />
<Button
android:id="#+id/c4"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight=".25"
android:background="#drawable/blanktilesml"
android:paddingLeft="10dp"
android:src="#drawable/clear64" />
</LinearLayout>
</LinearLayout>
Try this:
<LinearLayout
android:id="#+id/row1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight="1"
android:gravity="center"
android:orientation="horizontal" >
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center" >
<ImageButton
android:id="#+id/a1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#android:drawable/arrow_up_float"
android:gravity="center_horizontal|center_vertical"
android:src="#drawable/ic_launcher" />
</LinearLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center" >
<ImageButton
android:id="#+id/a2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#android:drawable/arrow_up_float"
android:gravity="center_horizontal|center_vertical"
android:src="#drawable/ic_launcher" />
</LinearLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center" >
<ImageButton
android:id="#+id/a3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#android:drawable/arrow_up_float"
android:gravity="center_horizontal|center_vertical"
android:src="#drawable/ic_launcher" />
</LinearLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center" >
<ImageButton
android:id="#+id/a4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#android:drawable/arrow_up_float"
android:gravity="center_horizontal|center_vertical"
android:src="#drawable/ic_launcher" />
</LinearLayout>
</LinearLayout>
try this for your first row. Similarly can be done for all the row,
So unless you don't want the images to be stretched you need to rap them in a layout. Doing so you will manage the spacing and the drawable will look just fine. If you stretch the buttons then the drawable will also be stretched accordingly.
Here is what I did with a calculator project I was working on. Requiring buttons in rows all the same size. You will need to add padding to your buttons to suit your needs.:
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:padding="5dp"
android:id="#+id/NumberPad">
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="#+id/NumberRow123"
android:layout_weight="1">
<Button
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:id="#+id/button1"
android:layout_weight="1"
android:background="#drawable/unpressed7"/>
<Button
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:id="#+id/button2"
android:layout_weight="1"
android:background="#drawable/unpressed8"/>
<Button
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:id="#+id/button3"
android:layout_weight="1"
android:background="#drawable/unpressed9"/>
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="#+id/NumerRow456"
android:layout_weight="1">
<Button
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:id="#+id/button4"
android:layout_weight="1"
android:background="#drawable/unpressed4"/>
<Button
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:id="#+id/button5"
android:layout_weight="1"
android:background="#drawable/unpressed5"/>
<Button
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:id="#+id/button6"
android:layout_weight="1"
android:background="#drawable/unpressed6"/>
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:id="#+id/NumberRow789">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/button7"
android:layout_weight="1"
android:background="#drawable/unpressed1"/>
<Button
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:id="#+id/button8"
android:layout_weight="1"
android:background="#drawable/unpressed2"/>
<Button
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:id="#+id/button9"
android:layout_weight="1"
android:background="#drawable/unpressed3"/>
</LinearLayout>

Android Grid with LinearLayout

I'm trying to build a grid with some linear layouts.
Unfortunately the buttons inside the "grid" fill all the space and the "grid" isn't always the same size.
<ScrollView
xmlns:tools="http://schemas.android.com/tools"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="wrap_content"
android:id="#+id/scrollView1"
android:layout_width="fill_parent"
android:layout_weight="1">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:orientation="vertical" >
<ImageView
android:id="#+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/banner"
android:background="#80000000"
android:contentDescription="#string/about"/>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_marginTop="15dp"
android:gravity="center_horizontal"
android:text="#string/modi1"
tools:context=".MenuActivity" />
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_marginTop="15dp"
android:gravity="center_horizontal"
android:text="#string/modi2"
tools:context=".MenuActivity" />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<Button
android:layout_marginTop="10dp"
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:text="#string/szen_diagramm" />
<Button
android:layout_marginTop="10dp"
android:id="#+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:text="#string/szen_countdown" />
</LinearLayout>
<Button
android:layout_marginTop="10dp"
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:text="#string/szen_diagramm" />
<Button
android:layout_marginTop="10dp"
android:id="#+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:text="#string/szen_countdown" />
</LinearLayout>
<Button
android:layout_marginTop="10dp"
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:text="#string/szen_diagramm" />
<Button
android:layout_marginTop="10dp"
android:id="#+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:text="#string/szen_countdown" />
</LinearLayout>
</LinearLayout>
</ScrollView>
It should look something like
this
with the buttons centered and at the size they need with the text inside.
Could anyone help me?
Try this.
<ScrollView xmlns:tools="http://schemas.android.com/tools"
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/scrollView1"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<TableLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TableRow>
<ImageView
android:id="#+id/imageView1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#80000000"
android:contentDescription="ImageView"
android:layout_span="2"
android:src="#drawable/banner" />
</TableRow>
<TableRow>
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginTop="15dp"
android:layout_weight="2"
android:gravity="center_horizontal"
android:text="modi1" />
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginTop="15dp"
android:layout_weight="2"
android:gravity="center_horizontal"
android:text="modi2" />
</TableRow>
<TableRow>
<Button
android:id="#+id/button1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:layout_weight="2"
android:gravity="center"
android:text="szen_diagramm" />
<Button
android:id="#+id/button2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:layout_weight="2"
android:gravity="center"
android:text="szen_countdown" />
</TableRow>
<TableRow>
<Button
android:id="#+id/button1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:layout_weight="2"
android:gravity="center"
android:text="szen_diagramm" />
<Button
android:id="#+id/button2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:layout_weight="2"
android:gravity="center"
android:text="szen_countdown" />
</TableRow>
<TableRow>
<Button
android:id="#+id/button1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:layout_weight="2"
android:gravity="center"
android:text="szen_diagramm" />
<Button
android:id="#+id/button2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:layout_weight="2"
android:gravity="center"
android:text="szen_countdown" />
</TableRow>
</TableLayout>
this is how it shows to me
Remove the android:layout_weight="1" to avoid the button filling all the space.
You should try using a TableLayout and several TableRow to replace your LinearLayouts containing the buttons, thus creating a well formated grid
I had a similar problem, solved inserting a LinearLayout in TableRow. Have a look to the posted xml code and verify if it fits your requirements.
<TableRow
android:layout_gravity="fill_horizontal"
android:paddingTop="#dimen/table_row_padding_top" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="2"
android:gravity="center_horizontal"
android:orientation="vertical" >
<ImageButton
android:contentDescription="#string/str_phone"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:background="#drawable/icon_phone" />
<TextView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="center_horizontal"
android:text="#string/str_phone"
android:textColor="#000000"
android:textSize="12sp" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="2"
android:gravity="center_horizontal"
android:orientation="vertical" >
<ImageButton
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:background="#drawable/icon_car"
android:contentDescription="#string/str_car" />
<TextView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="center_horizontal"
android:text="#string/str_car"
android:textColor="#000000"
android:textSize="12sp" />
</LinearLayout>
</TableRow>

Categories

Resources