I had just started working on android. Actually I had created a calculator application.
The problem I have faced is to manage the layout. I have two rows of buttons. In each row are 4 buttons. When I run the application the space between the first row and the second row is quite large. So can anyone tell me how can I decrease the space between these two rows?
The code I have written:
<?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">
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1">
<TextView
android:id="#+id/label"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Calculator Application"/>
<EditText
android:id="#+id/entry"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#android:drawable/editbox_background"
android:layout_below="#id/label"/>
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1">
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1">
<Button
android:id="#+id/one"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_marginLeft="10dip"
android:text="1" />
<Button
android:id="#+id/two"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_marginLeft="10dip"
android:text="2" />
<Button
android:id="#+id/three"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_marginLeft="10dip"
android:text="3" />
<Button
android:id="#+id/plus"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_marginLeft="10dip"
android:text="+" />
</LinearLayout>
</LinearLayout>
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1">
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1">
<Button
android:id="#+id/four"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_marginLeft="10dip"
android:text="4" />
<Button
android:id="#+id/five"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_marginLeft="10dip"
android:text="5" />
<Button
android:id="#+id/six"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_marginLeft="10dip"
android:text="6" />
<Button
android:id="#+id/minus"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_marginLeft="10dip"
android:text="-" />
</LinearLayout>
</LinearLayout>
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1">
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1">
<Button
android:id="#+id/seven"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_marginLeft="10dip"
android:text="7" />
<Button
android:id="#+id/eight"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_marginLeft="10dip"
android:text="8" />
<Button
android:id="#+id/nine"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_marginLeft="10dip"
android:text="9" />
<Button
android:id="#+id/multiply"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_marginLeft="10dip"
android:text="*" />
</LinearLayout>
</LinearLayout>
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1">
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1">
<Button
android:id="#+id/zero"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_marginLeft="10dip"
android:text="0" />
<Button
android:id="#+id/clr"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_marginLeft="10dip"
android:text="c" />
<Button
android:id="#+id/equal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_marginLeft="10dip"
android:text="=" />
<Button
android:id="#+id/divide"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_marginLeft="10dip"
android:text="/" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
</LinearLayout>
Regards
Anshuman
Replace all of the
android:layout_height="fill_parent" calls to
android:layout_height="wrap_content"
(apart from the parent layout, which should be fill_parent)
I have verified that this works.
EDIT: To make the buttons fill the screen horizontally, (and all be equally sized), add this to each button:
android:layout_width="0dp"
android:layout_weight="1"
the better way is u can use grid layout.try this links
http://www.edumobile.org/android/android-beginner-tutorials/draw-menu-in-grid-view/
http://www.firstdroid.com/2011/02/06/android-tutorial-gridview-with-icon-and-text/
As CaspNZ suggested use android:layout_height="wrap_content" also using top padding may help.
Related
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>
Hi I am very new in android and present I am learning basic's in android and now I want to set EdittextViews and Buttons like my below image.
For this I have tried to write some code in XML file with my knowledge but I am not able to arrange fields like below image.
Please help me.
In below image "cyan" colors fields are EditTextViews and "red" color fields are Buttons.
my code:-
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#8cff55"
android:id="#+id/relative">
<TextView
android:layout_width="255dp"
android:layout_height="30dp"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Large Text"
android:id="#+id/textView"
android:layout_alignParentTop="true"
android:layout_alignParentStart="true"
android:layout_marginLeft="10dp"
android:layout_marginTop="10dp"
android:background="#cb32ff" />
<Button
android:layout_width="wrap_content"
android:layout_height="30dp"
android:text="New Button"
android:id="#+id/button"
android:layout_marginTop="10dp"
android:layout_marginRight="10dp"
android:layout_alignParentTop="true"
android:layout_alignParentEnd="true"
android:background="#ff2518" />
</RelativeLayout>
This is a layout I created for my android project.
You can solve your problem by changing some code.
Screenshot:
code:
<?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">
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="60dip"
android:background="#drawable/actionbar" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="text"
android:id="#+id/semesterHead"
android:layout_marginLeft="40dp"
android:textColor="#000000"
android:textSize="25sp"
android:gravity="center"
android:layout_gravity="center_vertical"
android:layout_marginBottom="5dp" />
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginTop="50dip">
<Button
android:layout_width="150dip"
android:layout_height="100dip"
android:text="semester-1"
android:id="#+id/button12"
android:layout_gravity="center_horizontal"
android:background="#drawable/button_main"
android:textColor="#ffffff" />
<Button
android:layout_width="150dip"
android:layout_height="100dip"
android:text="semester-2"
android:id="#+id/button11"
android:layout_gravity="center_horizontal"
android:background="#drawable/button_main"
android:textColor="#ffffff" />
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal" >
<Button
android:layout_width="150dip"
android:layout_height="100dip"
android:text="semester-3"
android:id="#+id/button15"
android:layout_gravity="center_horizontal"
android:background="#drawable/button_main"
android:textColor="#ffffff" />
<Button
android:layout_width="150dip"
android:layout_height="100dip"
android:text="semester-4"
android:id="#+id/button16"
android:layout_gravity="center_horizontal"
android:background="#drawable/button_main"
android:textColor="#ffffff" />
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal" >
<Button
android:layout_width="150dip"
android:layout_height="100dip"
android:text="semester-5"
android:id="#+id/button17"
android:layout_gravity="center_horizontal"
android:background="#drawable/button_main"
android:textColor="#ffffff" />
<Button
android:layout_width="150dip"
android:layout_height="100dip"
android:text="semester-6"
android:id="#+id/button18"
android:layout_gravity="center_horizontal"
android:background="#drawable/button_main"
android:textColor="#ffffff" />
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal" >
<Button
android:layout_width="150dip"
android:layout_height="100dip"
android:text="semester-7"
android:id="#+id/button13"
android:layout_gravity="center_horizontal"
android:background="#drawable/button_main"
android:textColor="#ffffff" />
<Button
android:layout_width="150dip"
android:layout_height="100dip"
android:text="semester-8"
android:id="#+id/button14"
android:layout_gravity="center_horizontal"
android:background="#drawable/button_main"
android:textColor="#ffffff" />
</LinearLayout>
Note: You can either follow my way or use other methods like GridLayout, TableLayout etc..
I'm trying to center a table row using XML in Android, but I'm having a problem where it's over to the right just a smidge too much for two of my rows that only contain two buttons. Here's my code, and I have a screen shot of how it looks
<TableLayout
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">
<TableRow
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center">
<ImageView
android:id="#+id/hiragana_multiple_choice_main_image"
android:gravity="center"
android:layout_width="fill_parent"
android:layout_height="match_parent"/>
</TableRow>
<TableRow
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center"
android:padding="30dp">
<Button
android:id="#+id/hiragana_multiple_choice_button_1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight="1" />
<Button
android:id="#+id/hiragana_multiple_choice_button_2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight="1" />
</TableRow>
<TableRow
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="30dp"
android:gravity="center">
<Button
android:id="#+id/hiragana_multiple_choice_button_3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_weight="1" />
<Button
android:id="#+id/hiragana_multiple_choice_button_4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_weight="1" />
</TableRow>
</TableLayout>
http://www.ptrprograms.com/multiple_choice_offset.png
Thanks!
try to use LinearLayout instead of tablelayout, it will fix it to center and also use fillparent and wrap content for best practice.
Try these Way....
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<ImageView
android:id="#+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="65dp"
android:src="#drawable/ic_launcher" />
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/imageView1"
android:layout_marginTop="59dp"
android:layout_toLeftOf="#+id/imageView1"
android:text="Button" />
<Button
android:id="#+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/button1"
android:layout_alignBottom="#+id/button1"
android:layout_alignParentRight="true"
android:layout_toRightOf="#+id/imageView1"
android:text="Button" />
<Button
android:id="#+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignRight="#+id/button1"
android:layout_below="#+id/button1"
android:layout_marginTop="68dp"
android:text="Button" />
<Button
android:id="#+id/button4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/button3"
android:layout_alignBottom="#+id/button3"
android:layout_alignLeft="#+id/button2"
android:layout_alignParentRight="true"
android:text="Button" />
</RelativeLayout>
I have a dialog box with two buttons at the bottom. How do I create a divider above these buttons? My XML is as follows:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="400dp"
>
<ImageView
android:id="#+id/imageView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/username"
android:layout_alignParentLeft="true" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:orientation="horizontal">
<Button
android:id="#+id/start"
android:textSize="18sp"
android:layout_marginRight="2dip"
android:layout_width="wrap_content"
android:layout_marginBottom="5dip"
android:padding="5dip"
android:layout_height="fill_parent"
android:text="Start" />
<Button
android:id="#+id/cancel"
android:textSize="18sp"
android:layout_marginLeft="2dip"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Cancel"
android:textColor="#000000" />
</LinearLayout>
</RelativeLayout>
Using View
<View android:layout_height="2px" android:layout_width="fill_parent"
android:background="#android:color/black"
/>
Simply add a ImageView and provide the following attribute,
<ImageView android:layout_width="fill_parent"
android:layout_height="3dip"
android:background="#color/white"
android:padding="5dip"/>
There is nothing like a Divider attribute that you could set for a Layout. Dividers are available for ListView only.
So you might have to change your linear layout like this,
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:orientation="horizontal">
<LinearLayout android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<ImageView android:layout_width="fill_parent"
android:layout_height="3dip"
android:background="#color/white"
android:padding="5dip"/>
</LinearLayout>
<LinearLayout android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:id="#+id/start"
android:textSize="18sp"
android:layout_marginRight="2dip"
android:layout_width="wrap_content"
android:layout_marginBottom="5dip"
android:padding="5dip"
android:layout_height="fill_parent"
android:text="Start" />
<Button
android:id="#+id/cancel"
android:textSize="18sp"
android:layout_marginLeft="2dip"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Cancel"
android:textColor="#000000" />
</LinearLayout>
</LinearLayout>
I just came up something based on Nirav's answer
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="254dp"
android:layout_height="wrap_content"
android:background="#drawable/dialogbox_bg"
android:orientation="vertical" >
<ImageView
android:layout_width="fill_parent"
android:layout_height="50dp"
android:gravity="center"
android:text="#string/selectattachment"
android:textColor="#color/white"
android:textSize="20sp"
android:textStyle="bold" />
<View
android:layout_width="fill_parent"
android:layout_height="2dp"
android:background="#color/blue" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:orientation="horizontal">
<Button
android:id="#+id/start"
android:textSize="18sp"
android:layout_marginRight="2dip"
android:layout_width="wrap_content"
android:layout_marginBottom="5dip"
android:padding="5dip"
android:layout_height="fill_parent"
android:text="Start" />
<Button
android:id="#+id/cancel"
android:textSize="18sp"
android:layout_marginLeft="2dip"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Cancel"
android:textColor="#000000" />
</LinearLayout>
</LinearLayout>
I have designed an XML layout as in this image:
I need to equally place the four buttons in the screen but I am having trouble getting it to work. I tried all possible changes that I can think of. Below is the layout's XML:
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="40dp"
android:id="#+id/buttonlayout"
android:layout_alignBottom="#id/framelayout">
<TableLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android">
<TableRow>
<Button
android:id="#+id/previous"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/previous_icon" />
<Button
android:id="#+id/button_startprint"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/print_icon"
android:padding="10dp" />
<Button
android:id="#+id/button_cam"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/cam_icon"
android:padding="10dp" />
<Button
android:id="#+id/next"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/next_icon"
android:hapticFeedbackEnabled="true"/>
</TableRow>
</TableLayout>
</RelativeLayout>
Can anyone help me out with this?
Hi jxgn use this code :
<?xml version="1.0" encoding="UTF-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<TableRow
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<LinearLayout
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="1" >
<Button
android:id="#+id/previous"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/ic_launcher" />
</LinearLayout>
<LinearLayout
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="1" >
<Button
android:id="#+id/button_startprint"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/ic_launcher" />
</LinearLayout>
<LinearLayout
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="1" >
<Button
android:id="#+id/button_cam"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/ic_launcher" />
</LinearLayout>
<LinearLayout
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="1" >
<Button
android:id="#+id/next"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/ic_launcher" />
</LinearLayout>
</TableRow>
</TableLayout>
use android:layout_weight=1, and Use android:layout_width="0dp" to make buttons of equal weights..
EDIT
for example define your button as
<Button
android:id="#+id/button_cam"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight=1
android:background="#drawable/cam_icon"
android:padding="10dp" />
If you set the attribute android:weight="1" for all four buttons they should be spaced out properly as TableRow is a subclass of LinearLayout. For better performance it is suggested to use android:layout_width="0dp" as well.
Try to replace all your buttons width with layout_width="0dp" and add layout_weight="1"
Try to use 9-patch image to avoid the fact that your image get stretched
<TableRow>
<Button
android:id="#+id/previous"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:background="#drawable/ic_menu_compass"
android:layout_weight="1" />
<Button
android:id="#+id/button_startprint"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:background="#drawable/ic_menu_mylocation"
android:padding="10dp"
android:layout_weight="1"/>
<Button
android:id="#+id/button_cam"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:background="#drawable/ic_menu_mapmode"
android:padding="10dp"
android:layout_weight="1" />
<Button
android:id="#+id/next"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:background="#drawable/ic_menu_about"
android:hapticFeedbackEnabled="true"
android:layout_weight="1"/>
</TableRow>
Try to set at each button:
android:layout_weight="0.25"
Hope this will already work for you
Try this it is usefull
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="#+id/buttonlayout" >
<TableLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android">
<TableRow>
<Button
android:id="#+id/previous"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/ic_launcher"
android:layout_weight="1"/>
<Button
android:id="#+id/button_startprint"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/ic_launcher"
android:layout_weight="1"
/>
<Button
android:id="#+id/button_cam"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/ic_launcher"
android:layout_weight="1" />
<Button
android:id="#+id/next"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/ic_launcher"
android:layout_weight="1" />
</TableRow>
</TableLayout>
</RelativeLayout>
Applying android:layout_weight="1" in your xml will stretch your button images.
If you dont want to stretch button images use ImageButton and set android:src to your drawables.
Refer below code:
<TableLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<TableRow>
<ImageButton
android:id="#+id/previous"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/previous_icon"
android:background="#android:color/transparent"
android:layout_weight="1" />
<ImageButton
android:id="#+id/button_startprint"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/print_icon"
android:background="#android:color/transparent"
android:padding="10dp"
android:layout_weight="1" />
<ImageButton
android:id="#+id/button_cam"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/cam_icon"
android:background="#android:color/transparent"
android:padding="10dp"
android:layout_weight="1" />
<ImageButton
android:id="#+id/next"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/next_icon"
android:background="#android:color/transparent"
android:hapticFeedbackEnabled="true"
android:layout_weight="1" />
</TableRow>
</TableLayout>
`