Nesting RelativeLayouts within LinearLayout - android

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>

Related

vertical alignment of the text views of different linear layouts

I am trying to create a screen that shows the details of the students. The following is my xml code for that screen.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
tools:context=".PatientDetails">
<LinearLayout
android:id="#+id/ll1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:layout_weight="3"
android:background="#color/BackgroundColor">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1">
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Details"
android:textColor="#color/white"
android:textSize="30dp"
android:gravity="center"/>
</LinearLayout>
</LinearLayout>
<LinearLayout
android:id="#+id/ll2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:orientation="vertical"
android:background="#color/white">
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="16dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:orientation="horizontal"
android:layout_marginTop="16dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Student Name"
android:textSize="20dp"
android:layout_weight="1"
android:textColor="#color/BackgroundColor"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/PatientName"
android:text="Joseph"
android:layout_weight="1"
android:textSize="20dp"
android:textColor="#color/BackgroundColor"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:orientation="horizontal"
android:layout_marginTop="16dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Age"
android:textSize="20dp"
android:layout_weight="1"
android:textColor="#color/BackgroundColor"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/age"
android:text="21"
android:layout_weight="1"
android:textSize="20dp"
android:textColor="#color/BackgroundColor"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:orientation="horizontal"
android:layout_marginTop="16dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Sex"
android:textSize="20dp"
android:layout_weight="1"
android:textColor="#color/BackgroundColor"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/Sex"
android:text="Female"
android:layout_weight="1"
android:textSize="20dp"
android:textColor="#color/BackgroundColor"/>
</LinearLayout>
</LinearLayout>
</ScrollView>
</LinearLayout>
</LinearLayout>
The output of the code is as follows:
Can someone tell me how to modify the code so that the values on the right hand side of each row are aligned properly.
Row example:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="16dp"
android:orientation="horizontal">
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Student Name"
android:textColor="#color/BackgroundColor"
android:textSize="20dp" />
<TextView
android:id="#+id/PatientName"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Joseph"
android:textColor="#color/BackgroundColor"
android:textSize="20dp" />
</LinearLayout>
Remove android:layout_weight="1" from row root. Change android:layout_width="wrap_content" to android:layout_width="0dp"
Sample
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:orientation="horizontal"
android:weightSum="2">
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="left"
android:layout_weight="1"
android:text="Student Name"
android:textColor="#color/pika_bg"
android:textSize="20dp" />
<TextView
android:id="#+id/PatientName"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:paddingLeft="5dp"
android:text="Joseph"
android:textColor="#color/pika_bg"
android:textSize="20dp" />
</LinearLayout>
Here I am added android:weightSum="2"to the parent LinearLayout. So that the child views can get same width. Then we can adjust padding/gravity attributes according to your needs.
I think you use linear layout but linear layout have also one property its called layout_weight i think you should use this.
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="Test" />
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="Test" />
</LinearLayout>

Round Button and Flexible Margin in Layout in XML

I am wondering, what is the best approach to achieve this kind of layout?
I have been trying various approaches, but my button keeps sticking to each other. and I have no idea to create a layout in XML that totally respect all screen sizes.
Any ideas please?
Here is what i thought to give you some hint.
<LinearLayout
android:id="#+id/numericPad"
android:layout_width="300dp"
android:layout_height="match_parent"
android:layout_below="#+id/statusMessage"
android:layout_centerHorizontal="true"
android:layout_marginLeft="5dip"
android:layout_marginRight="5dip"
android:layout_marginTop="20dip"
android:orientation="vertical" >
<LinearLayout
android:id="#+id/tableRow1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<Button
android:id="#+id/button1"
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_margin="3dip"
android:layout_weight="1"
android:gravity="center"
android:padding="6dip"
android:text="1"
android:textColor="#ffffff"
android:textSize="25sp" >
</Button>
<Button
android:id="#+id/button2"
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_margin="3dip"
android:layout_weight="1"
android:gravity="center"
android:padding="6dip"
android:text="2"
android:textColor="#ffffff"
android:textSize="25sp" >
</Button>
<Button
android:id="#+id/button3"
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_margin="3dip"
android:layout_weight="1"
android:gravity="center"
android:padding="6dip"
android:text="3"
android:textColor="#ffffff"
android:textSize="25sp" >
</Button>
</LinearLayout>
This is how I created this layout but this was in landscape mode and this is for only one row
You can use both Linearlayout with weightsum as well as circle shapes. That will be perfect in all mobile screens. Like this,
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:showIn="#layout/activity_main">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:weightSum="3">
<RelativeLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:layout_centerInParent="true"
android:background="#drawable/back"
android:padding="15dp"
android:textSize="20dp"
android:text="1" />
</RelativeLayout>
<RelativeLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:layout_centerInParent="true"
android:background="#drawable/back"
android:padding="15dp"
android:textSize="20dp"
android:text="2" />
</RelativeLayout>
<RelativeLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:layout_centerInParent="true"
android:background="#drawable/back"
android:padding="15dp"
android:textSize="20dp"
android:text="3" />
</RelativeLayout>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_marginTop="15dp"
android:weightSum="3">
<RelativeLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:layout_centerInParent="true"
android:background="#drawable/back"
android:padding="15dp"
android:textSize="20dp"
android:text="4" />
</RelativeLayout>
<RelativeLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:layout_centerInParent="true"
android:background="#drawable/back"
android:padding="15dp"
android:textSize="20dp"
android:text="5" />
</RelativeLayout>
<RelativeLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:layout_centerInParent="true"
android:background="#drawable/back"
android:padding="15dp"
android:textSize="20dp"
android:text="6" />
</RelativeLayout>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_marginTop="15dp"
android:weightSum="3">
<RelativeLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:layout_centerInParent="true"
android:background="#drawable/back"
android:padding="15dp"
android:textSize="20dp"
android:text="7" />
</RelativeLayout>
<RelativeLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:layout_centerInParent="true"
android:background="#drawable/back"
android:padding="15dp"
android:textSize="20dp"
android:text="8" />
</RelativeLayout>
<RelativeLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:layout_centerInParent="true"
android:background="#drawable/back"
android:padding="15dp"
android:textSize="20dp"
android:text="9" />
</RelativeLayout>
</LinearLayout>
back.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval">
<stroke
android:width="1dp"
android:color="#android:color/black" />
</shape>

how to set in textview same sizes

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>

How to fill the layout to entire available width in Android

While developing a layout today I found some weird thing my Linear layout is not filling the available space .
//Image removed
My left layout is re-sizing according to content but i don't want that. What I want is the right edge of left hand side layout should touch the left edge of right layout.
I have tried both fill parent and wrap content with layout_weight but nothing is happening what i want is something like this.
// Image removed
My layout code :
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingTop="6dp"
android:paddingBottom="6dp"
android:paddingLeft="7dp"
android:paddingRight="7dp">
<RelativeLayout
android:id="#+id/total_top_layout"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:paddingBottom="10dp"
android:layout_centerHorizontal="true">
<TextView
android:id="#+id/ammount_view"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="First Text Here"
android:textSize="20sp"
android:layout_centerVertical="true" />
<EditText
android:id="#+id/total_ammount_input"
android:layout_height="wrap_content"
android:layout_width="150dp"
android:singleLine="true"
android:layout_alignParentRight="true"
android:background="#color/hologreennewdark"
android:text="second Text"
android:layout_centerVertical="true"
android:padding="8dp"
android:gravity="center"
android:textColor="#color/WhiteSmoke" />
</RelativeLayout>
<View
android:id="#+id/divideaftertotal"
android:layout_width="#dimen/divide_width"
android:layout_height="#dimen/divider_height"
android:background="#color/YellowGreen"
android:layout_below="#+id/total_top_layout"
/>
<RelativeLayout
android:id="#+id/tens_view_top"
android:layout_below="#id/divideaftertotal"
android:layout_height="75dp"
android:layout_width="wrap_content"
>
<LinearLayout
android:id="#+id/tens_view_left"
android:layout_alignParentLeft="true"
android:orientation="vertical"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_centerInParent="true"
>
<LinearLayout
android:id="#+id/firstcoloumn"
android:layout_height="fill_parent"
android:layout_width="fill_parent"
android:orientation="vertical"
android:layout_weight="1"
>
<LinearLayout
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:gravity="center"
android:orientation="horizontal"
>
<TextView
android:id="#+id/amount_rupee"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:text="Num1"
android:layout_centerVertical="true"
android:textSize="25sp" />
<TextView
android:id="#+id/multiply_sign"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="X"
android:layout_centerVertical="true"
android:textSize="26sp"
android:layout_toRightOf="#id/amount_rupee"
android:layout_marginLeft="40dp" />
<TextView
android:id="#+id/multiple_digit"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="Num2"
android:layout_centerVertical="true"
android:textSize="25sp"
android:layout_toRightOf="#+id/multiply_sign"
android:layout_marginLeft="40dp" />
</LinearLayout>
<SeekBar
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:layout_below="#+id/multiple_digit"
android:layout_marginTop="8dp"
/>
</LinearLayout>
</LinearLayout>
<LinearLayout
android:layout_height="100dp"
android:layout_width="wrap_content"
android:background="#color/hologreendark"
android:layout_alignParentRight="true">
<TextView
android:id="#+id/amount_rupee"
android:layout_height="fill_parent"
android:layout_width="100dp"
android:text="Num3"
android:layout_centerVertical="true"
android:textSize="25sp"
android:gravity="center"
/>
</LinearLayout>
</RelativeLayout>
<View
android:id="#+id/divideaftertens"
android:layout_width="#dimen/divide_width"
android:layout_height="#dimen/divider_height"
android:background="#color/YellowGreen"
android:layout_below="#+id/tens_view_top"
/>
</RelativeLayout>
On the layout android:id="#+id/tens_view_left", you could add this 2 atributes:
android:layout_alignParentLeft="true"
so that its left side is anchored in its parent's left border, and
android:layout_toLeftOf="#id/id_from_the_linear_layout_on_the_right"
And that layout is:
<LinearLayout
android:id="#+id/id_from_the_linear_layout_on_the_right"
android:layout_height="100dp"
android:layout_width="wrap_content"
android:background="#color/hologreendark"
android:layout_alignParentRight="true">
<TextView
android:id="#+id/amount_rupee"
android:layout_height="fill_parent"
android:layout_width="100dp"
android:text="Num3"
android:layout_centerVertical="true"
android:textSize="25sp"
android:gravity="center"
/>
</LinearLayout>
By the way, you might need to set android:orientation="horizontal"
on the layout android:id="#+id/tens_view_top"
When using layout_weight, set the layout_width to 0dp (for horizontal orientation).
it could be like,
<LinearLayout
android:id="#+id/tens_view_left"
android:layout_alignParentLeft="true"
android:orientation="vertical"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_centerInParent="true"
>
<LinearLayout
android:id="#+id/firstcoloumn"
android:layout_height="fill_parent"
android:layout_width="fill_parent"
android:orientation="horizontal"
>
<LinearLayout
android:layout_height="fill_parent"
android:layout_width="0dp"
android:orientation="vertical"
android:layout_weight="1">
<LinearLayout
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:gravity="center"
android:orientation="horizontal"
>
<TextView
android:id="#+id/amount_rupee"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:text="Num1"
android:layout_centerVertical="true"
android:textSize="25sp" />
<TextView
android:id="#+id/multiply_sign"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="X"
android:layout_centerVertical="true"
android:textSize="26sp"
android:layout_toRightOf="#id/amount_rupee"
android:layout_marginLeft="40dp" />
<TextView
android:id="#+id/multiple_digit"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="Num2"
android:layout_centerVertical="true"
android:textSize="25sp"
android:layout_toRightOf="#+id/multiply_sign"
android:layout_marginLeft="40dp" />
</LinearLayout>
<SeekBar
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:layout_below="#+id/multiple_digit"
android:layout_marginTop="8dp"
/>
</LinearLayout>
<LinearLayout
android:layout_height="100dp"
android:layout_width="wrap_content"
android:background="#color/hologreendark"
android:layout_alignParentRight="true">
<TextView
android:id="#+id/amount_rupee"
android:layout_height="fill_parent"
android:layout_width="100dp"
android:text="Num3"
android:layout_centerVertical="true"
android:textSize="25sp"
android:gravity="center"
/>
</LinearLayout>
</LinearLayout>
</LinearLayout>
Change the RelativeLayout to LinearLayout that contain two layout you want to have close, and add layout_weight=1 to first LinearLayout with layout_width=0dip like
<LinearLayout
android:id="#+id/tens_view_top"
android:layout_below="#id/divideaftertotal"
android:layout_height="75dp"
android:layout_width="wrap_content"
>
<LinearLayout
android:layout_weight="1"
android:id="#+id/tens_view_left"
android:orientation="vertical"
android:layout_height="wrap_content"
android:layout_width="0dip"
>
<LinearLayout
android:id="#+id/firstcoloumn"
android:layout_height="fill_parent"
android:layout_width="fill_parent"
android:orientation="vertical"
android:layout_weight="1"
>
<LinearLayout
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:gravity="center"
android:orientation="horizontal"
>
<TextView
android:id="#+id/amount_rupee"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:text="Num1"
android:layout_centerVertical="true"
android:textSize="25sp" />
<TextView
android:id="#+id/multiply_sign"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="X"
android:layout_centerVertical="true"
android:textSize="26sp"
android:layout_toRightOf="#id/amount_rupee"
android:layout_marginLeft="40dp" />
<TextView
android:id="#+id/multiple_digit"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="Num2"
android:layout_centerVertical="true"
android:textSize="25sp"
android:layout_toRightOf="#+id/multiply_sign"
android:layout_marginLeft="40dp" />
</LinearLayout>
<SeekBar
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:layout_below="#+id/multiple_digit"
android:layout_marginTop="8dp"
/>
</LinearLayout>
</LinearLayout>
<LinearLayout
android:layout_height="100dp"
android:layout_width="wrap_content"
android:background="#FFCCDD"
android:layout_alignParentRight="true">
<TextView
android:id="#+id/amount_rupee"
android:layout_height="fill_parent"
android:layout_width="100dp"
android:text="Num3"
android:layout_centerVertical="true"
android:textSize="25sp"
android:gravity="center"
/>
</LinearLayout>
// try this i have used LinearLayout rather than RelativeLayout
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="7dp">
<LinearLayout
android:id="#+id/total_top_layout"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:gravity="center"
android:paddingBottom="10dp">
<TextView
android:id="#+id/ammount_view"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_width="0dp"
android:text="First Text Here"
android:textSize="20sp"/>
<EditText
android:id="#+id/total_ammount_input"
android:layout_height="wrap_content"
android:layout_width="150dp"
android:singleLine="true"
android:text="second Text"
android:padding="8dp"
android:gravity="center"/>
</LinearLayout>
<View
android:id="#+id/divideaftertotal"
android:layout_width="match_parent"
android:layout_height="1dp"/>
<LinearLayout
android:id="#+id/tens_view_left"
android:layout_height="wrap_content"
android:layout_width="match_parent">
<LinearLayout
android:id="#+id/firstcoloumn"
android:layout_height="wrap_content"
android:layout_width="0dp"
android:layout_weight="1"
android:orientation="vertical">
<LinearLayout
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:gravity="center">
<LinearLayout
android:layout_height="match_parent"
android:gravity="center"
android:layout_width="0dp"
android:layout_weight="1">
<TextView
android:id="#+id/amount_rupee"
android:layout_height="wrap_content"
android:layout_width="0dp"
android:layout_weight="1"
android:text="Num1"
android:textSize="25sp" />
<TextView
android:id="#+id/multiply_sign"
android:layout_height="wrap_content"
android:layout_width="0dp"
android:layout_weight="1"
android:text="X"
android:layout_centerVertical="true"
android:textSize="26sp"
android:layout_marginLeft="40dp" />
<TextView
android:id="#+id/multiple_digit"
android:layout_height="wrap_content"
android:layout_width="0dp"
android:layout_weight="1"
android:text="Num2"
android:textSize="25sp"
android:layout_marginLeft="40dp" />
</LinearLayout>
</LinearLayout>
<SeekBar
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:layout_below="#+id/multiple_digit"
android:layout_marginTop="8dp"/>
</LinearLayout>
<TextView
android:id="#+id/amount_rupee"
android:layout_height="match_parent"
android:layout_width="100dp"
android:text="Num3"
android:textSize="25sp"
android:gravity="center"/>
</LinearLayout>
<View
android:id="#+id/divideaftertens"
android:layout_width="match_parent"
android:layout_height="1dp"/>
</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