Rows and columns alignment affecting each other [Android Studiuo] - android

I've got a problem with TableLayout. I'm trying to place my objects in columns but every next row affects previous ones and destroys them. Here's some code
<?xml version="1.0" encoding="utf-8"?>
<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"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context="com.example.dom.zadanie2.aktywnosc2"
android:orientation="vertical">
<TableRow
android:id="#+id/tableRow1"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="#+id/textView2"
android:text="Imie"
android:textAppearance="?android:attr/textAppearanceLarge"
android:layout_weight="1"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<EditText
android:id="#+id/editText2"
android:layout_weight="3"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</TableRow>
<TableRow
android:id="#+id/tableRow2"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView
android:id="#+id/textView3"
android:text="Płeć"
android:textAppearance="?android:attr/textAppearanceLarge"
android:layout_weight="1"/>
<RadioGroup
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="2"
android:orientation="horizontal">
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Kobieta"
android:id="#+id/radioButton"
android:layout_column="1"
android:paddingRight="40dp"/>
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text = "Mężczyzna"
android:id="#+id/radioButton2"/>
</RadioGroup>
</TableRow>
<TableRow
android:id="#+id/tableRow3"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<RatingBar
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/ratingBar"/>
</TableRow>
</TableLayout>
When I have placed the RatingBar, previous rows moved out of screen. Please give me some tips, I've tried various setting but nothing works properly. I don't know what do to so rows would appear independent. Thanks in advance.

Instead of using TableLayout and TableRow , Simply use LinearLayout
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<HorizontalScrollView
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<!-- your Views -->
</LinearLayout>
</HorizontalScrollView>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<!-- your Views -->
</LinearLayout>
<!-- goes on like this -->
</LinearLayout>

Suggest you to change to Linearlayout,
As in below code :
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:weightSum="10">
<LinearLayout
android:id="#+id/tableRow1"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:orientation="horizontal">
<TextView
android:id="#+id/textView2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0.75"
android:gravity="center"
android:text="Imie"
android:textAppearance="?android:attr/textAppearanceLarge" />
<EditText
android:id="#+id/editText2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0.25" />
</LinearLayout>
<LinearLayout
android:id="#+id/tableRow2"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:orientation="horizontal">
<TextView
android:id="#+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight="0.5"
android:gravity="center"
android:text="Plec"
android:textAppearance="?android:attr/textAppearanceLarge" />
<RadioGroup
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight="0.5"
android:gravity="center"
android:orientation="horizontal">
<RadioButton
android:id="#+id/radioButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Kobieta" />
<RadioButton
android:id="#+id/radioButton2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Mezczyzna" />
</RadioGroup>
</LinearLayout>
<LinearLayout
android:id="#+id/tableRow3"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1">
<RatingBar
android:id="#+id/ratingBar"
style="?android:attr/ratingBarStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center|right"
android:isIndicator="true"
android:numStars="5"
android:stepSize="1" />
</LinearLayout>
<LinearLayout
android:id="#+id/tableRow5"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="7"
android:orientation="horizontal">
</LinearLayout>
</LinearLayout>

Add
android:layout_weight="3"
to TableLayout, and in each TableRow, change
android:layout_height="wrap_content"
to
android:layout_height="0dp"
android:weightSum="1"
like the following
<TableRow
android:id="#+id/tableRow1"
android:layout_width="match_parent"
android:layout_height="0dp"
android:weightSum="6"
>
<TextView
android:id="#+id/textView2"
android:text="Imie"
android:textAppearance="?android:attr/textAppearanceLarge"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="wrap_content" />
<EditText
android:id="#+id/editText2"
android:layout_weight="5"
android:layout_width="0dp"
android:layout_height="wrap_content" />
</TableRow>
<TableRow
android:id="#+id/tableRow2"
android:layout_width="match_parent"
android:layout_height="0dp"
android:weightSum="5"
>
<TextView
android:id="#+id/textView3"
android:text="Płeć"
android:textAppearance="?android:attr/textAppearanceLarge"
android:layout_weight="2"
android:width="0dp"
/>
<RadioGroup
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="3"
android:orientation="horizontal">
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Kobieta"
android:id="#+id/radioButton"
android:layout_column="1"
android:paddingRight="40dp"
/>
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text = "Mężczyzna"
android:id="#+id/radioButton2"
/>
</RadioGroup>
</TableRow>
<TableRow
android:id="#+id/tableRow3"
android:layout_width="match_parent"
android:layout_height="0dp"
android:weightSum="1"
>
<RelativeLayout
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
>
<RatingBar
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:id="#+id/ratingBar"
/>
</RelativeLayout>
</TableRow>

TableRow has by default layout_width as match_parent and layout_hieght as wrap_content, so do not specify layout_width and layout_height in TableRow tag.
Now, The main answer of your question:- In Table layout, width of each cell(View) in a column is related to other cells(views) in the same column. The width of each cell will be equal to the highest width of the cell in the same column.
In your case the rating bar is in column zero and width of the rating bar is more then other cells of same column. To overcome from this problem use layout_span="total number of columns in table layout" attribute in ranting bar view. By using this attribute your rating bar will span in all the columns.
solution of your problem is this:-
<TableRow
android:id="#+id/tableRow3">
<RatingBar
android:layout_width="wrap_content"
android:layout_height="wrap_content"
layout_span="2"
android:id="#+id/ratingBar"/>
</TableRow>

Related

Android how take full screen with more LinearLayout orientation horizontal

i'm trying to do a school day and i use multiple linearLayout with days and hours.
With the layout_weight i can set the space for every elements inside the single LinearLayout.
But my problem is how can i set the layout_weightfor the LinearLayout because they are set with orientation horizontal and they don't take the entire screen.
It's like a table but i use LinearLayout because i need to create only rows.
This is the xml of the Activity
<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="com.ddz.diarioscolastico.OrarioActivityProva">
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:id="#+id/linearLayout">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_weight="2"
android:gravity="center"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Lun"
android:id="#+id/textView2"
/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="2"
android:gravity="center"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Mar"
android:id="#+id/textView" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="2"
android:gravity="center"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Mer"
android:id="#+id/textView3" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="2"
android:gravity="center"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Gio"
android:id="#+id/textView4" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="2"
android:gravity="center"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Ven"
android:id="#+id/textView5" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="2"
android:gravity="center"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Sab"
android:id="#+id/textView6" />
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:layout_below="#+id/linearLayout"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:id="#+id/linearLayout2">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="1"
android:id="#+id/textView7" />
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#+id/linearLayout2"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:id="#+id/linearLayout3">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="2"
android:id="#+id/textView8" />
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#+id/linearLayout3"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:id="#+id/linearLayout4">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="3"
android:id="#+id/textView9" />
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#+id/linearLayout4"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:id="#+id/linearLayout6">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="4"
android:id="#+id/textView10" />
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#+id/linearLayout6"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:id="#+id/linearLayout5">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="5"
android:id="#+id/textView11" />
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#+id/linearLayout5"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:id="#+id/linearLayout7">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="6"
android:id="#+id/textView12" />
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#+id/linearLayout7"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:id="#+id/linearLayout8">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="7"
android:id="#+id/textView13" />
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#+id/linearLayout8"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="8"
android:id="#+id/textView14" />
</LinearLayout>
As you can see if i set the android:layout_height="match_parent" yes all LinearLayout take full screen but only the first is visible, i wanna divide the screen for all.
Using Layout Weight is a little bit tricky task.
Suppose you want to show three buttons horizontally which share equal width in a linear layout. Then the xml code should be like this.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:weightSum="3"
android:orientation="horizontal" >
<Button
android:layout_height="wrap_content"
android:layout_width="0dp"
android:layout_weight="1"
android:text="Button 1"
/>
<Button
android:layout_height="wrap_content"
android:layout_width="0dp"
android:layout_weight="1"
android:text="Button 1"
/>
<Button
android:layout_height="wrap_content"
android:layout_width="0dp"
android:layout_weight="1"
android:text="Button 1"
/>
</LinearLayout>
As you can see,In the parent LinearLayout i used weightSum as 3 and in each child button i set the width to 0dp and weight to 1 and there are three buttons so it will share equal width.
By the way the root parent (RelativeLayout) is not closed in your given layout.
EDIT:2
As you said here is the code divide each LinearLayout equally with layout_weight
<?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:weightSum="3"
android:orientation="vertical" >
<LinearLayout
android:layout_height="0dp"
android:layout_weight="1"
android:layout_width="match_parent"
android:background="#00FF00"
>
</LinearLayout>
<LinearLayout
android:layout_height="0dp"
android:layout_weight="1"
android:layout_width="match_parent"
android:background="#FF0000"
>
</LinearLayout>
<LinearLayout
android:layout_height="0dp"
android:layout_weight="1"
android:layout_width="match_parent"
android:background="#0000FF"
>
</LinearLayout>
</LinearLayout>
OUTPUT :
Have you tried to replace your LinearLayout with a TableRow or use a GridLayout ? GridLayout requires API 14 but lower API can be use with the support library.

Center a TextView in a TableRow in a TableLayout

I am trying to build something like a header. I tried using android:gravity="center" and android:textAlignment="center" on the TextView but it doesn't center the text. I don't know where the problem is.
<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"
tools:context=".MainActivity" >
<TableRow
android:id="#+id/tableRow1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#008000" >
<TextView
android:id="#+id/HeaderTextView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
<!--android:textAlignment="center"
android:gravity="center"-->
android:text="#string/header"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#FFF"
android:textStyle="italic" />
</TableRow>
</TableLayout>
<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"
tools:context=".MainActivity" >
<TableRow
android:id="#+id/tableRow1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:background="#008000" >
<TextView
android:id="#+id/HeaderTextView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="header"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#FFF"
android:textStyle="italic" />
</TableRow>
give android:gravity="center" to TableRow.
Set gravity to table row
<TableLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TableRow
android:gravity="center_horizontal"/>
</TableLayout>
If you have many TextView inside TableRow doing this solved my problem
<TableRow
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1">
<TextView
android:layout_width="0dp"
android:gravity="center"
android:layout_height="wrap_content"
android:layout_weight="0.33"
android:text="#string/feedback" />
<TextView
android:layout_width="0dp"
android:gravity="center"
android:layout_height="wrap_content"
android:layout_weight="0.33"
android:text="#string/football" />
<TextView
android:layout_width="0dp"
android:gravity="center"
android:layout_height="wrap_content"
android:layout_weight="0.33"
android:text="#string/about" />
</TableRow>

Issue with layout alignment

I have a relative layout with two linear layout inside it with two textviews in each of them.
I wanted the textviews to appear horizontally as a row.
The code is given below:
<RelativeLayout
android:id="#+id/linear2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<LinearLayout
android:id="#+id/txtlinear"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:id="#+id/esms_parentfees_classtext"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="#+id/esms_parentfees_classdetais"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
<LinearLayout
android:layout_gravity="center_horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="#+id/esms_parentfees_classdetais"
android:orientation="horizontal" >
<TextView
android:id="#+id/esms_parentfees_datetext"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="#+id/esms_parentfees_datedetais"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
</RelativeLayout>
Notes on your XML:
You can't use toRightOf, etc, within a LinearLayout
RelativeLayout's don't have an orientation
This is needed for some reason to show all the XML? Ignore this line
<RelativeLayout
android:id="#+id/relativeLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<LinearLayout
android:id="#+id/linearLayout1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:id="#+id/esms_parentfees_classtext"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.5" />
<TextView
android:id="#+id/esms_parentfees_classdetais"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.5" />
</LinearLayout>
<LinearLayout
android:id="#+id/linearLayout2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:id="#+id/esms_parentfees_datetext"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.5" />
<TextView
android:id="#+id/esms_parentfees_datedetais"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.5" />
</LinearLayout>
</RelativeLayout>
The easiest way would be to make the outer RelativeLayout a LinearLayout
<LinearLayout
android:id="#+id/linear2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
Then remove any tag like
android:layout_toRightOf="#+id/esms_parentfees_classdetais"
Or, you could just change the tag mentioned above to
android:layout_below="#+id/esms_parentfees_classdetais"
if you need the four textviews in a row its easy to just use a linear layout with the four text views in it (or a table layout) instead of using two linear layouts in a relative layout.
Have a look at this :
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignRight="#+id/txtlinear"
>
<TextView
android:id="#+id/esms_parentfees_classtext"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="hi" />
<TextView
android:id="#+id/esms_parentfees_classdetais"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="hi" />
<TextView
android:id="#+id/esms_parentfees_datetext"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="hi" />
<TextView
android:id="#+id/esms_parentfees_datedetais"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="hi" />
</LinearLayout>
or
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignRight="#+id/txtlinear"
>
<TableRow
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<TextView
android:id="#+id/esms_parentfees_classtext"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="hi" />
<TextView
android:id="#+id/esms_parentfees_classdetais"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="hi" />
<TextView
android:id="#+id/esms_parentfees_datetext"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="hi" />
<TextView
android:id="#+id/esms_parentfees_datedetais"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="hi" />
</TableRow>
</TableLayout>
or if its a compulsion that you need a relative layout with two linear layouts in it you can follow any of the solutions above
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
>
<TextView
android:id="#+id/esms_parentfees_classtext"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<TextView
android:id="#+id/esms_parentfees_classdetais"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toLeftOf="#+id/esms_parentfees_classtext"
/>
</RelativeLayout>

Two columns layout with first column wrap_content, second fill_parent

I have to design UI to contain two columns.
First column should wrap contained label and second in the same row should fill whole space. Below some example:
http://temp.chrzanowski.info/so_layout.png
How can I do this ?
Use TableLayout with strechColumns = "1"
It stretches the 2nd column occupying the remaining space.
<TableLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:stretchColumns="1" >
<TableRow
android:id="#+id/tableRow1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView" />
</TableRow>
<TableRow
android:id="#+id/tableRow2"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView" />
</TableRow>
<TableRow
android:id="#+id/tableRow3"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView" />
</TableRow>
</TableLayout>
Following TableLayout would give you the desired output:
<TableLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:stretchColumns="1">
<TableRow>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Bla bla bla"
android:singleLine="true" /> <!-- first column's view -->
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content" /> <!-- second column's view -->
</TableRow>
...
</TableLayout>
As an alternative, you can do a LinearLayout like so:
<?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="wrap_content"
android:orientation="horizontal"
android:weightSum="100"
android:gravity="center_vertical">
<LinearLayout
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="40">
<TextView
android:layout_weight="1"
android:layout_width="0dip"
android:layout_height="wrap_content"
android:text="A very very very long text"
android:singleLine="true"
android:scrollHorizontally="true"
android:ellipsize="end">
</TextView>
</LinearLayout>
<LinearLayout
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="60">
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="TextBox">
</EditText>
</LinearLayout>
</LinearLayout>
To adjust width of columns, modify the layout_weight of each LinearLayout containers. Lesser value means smaller width, and both weights must equal 100.
Try this:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal" >
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="vertical">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Label 1"
android:layout_marginLeft="7dp"
android:layout_marginTop="20dp"/>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Label Loooong2"
android:layout_marginLeft="7dp"
android:layout_marginTop="30dp"/>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Label 3"
android:layout_marginLeft="7dp"
android:layout_marginTop="30dp"/>
</LinearLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="2"
android:orientation="vertical">
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="TextBox"/>
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="TextBox"/>
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="TextBox"/>
</LinearLayout>
</LinearLayout>

Center TableLayout row in android xml

I am trying to align in the center 2 buttons in a table row, no matter the screen resolution, so i am trying to avoid using specified margins.
This is my code, it works only with declared margins, and i am using gravity and layout gravity...nothing worked for me. They did not align in the center.
Does anyone have an idea? Thanks anyway.
<ScrollView
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TableLayout
android:id="#+id/TableLayout01"
android:layout_gravity="center"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="100px"
android:stretchColumns="1" >
<TableRow
android:id="#+id/TableRow01"
android:layout_gravity="center"
android:paddingLeft="50dip"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<Button
android:id="#+id/image1Btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#android:color/transparent"
android:drawablePadding="-5sp"
android:drawableTop="#drawable/image1"
android:gravity="center"
android:src="#drawable/image1"
android:text="#string/image1text"
android:textColor="#color/darkgrey" />
<Button
android:id="#+id/image2Btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#android:color/transparent"
android:drawablePadding="-5sp"
android:drawableTop="#drawable/image2"
android:gravity="center"
android:src="#drawable/image2"
android:text="#string/image2text"
android:textColor="#color/darkgrey" />
</TableRow>
<TableRow
android:id="#+id/TableRow02"
android:paddingLeft="50dip"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<Button
android:id="#+id/image3Btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#android:color/transparent"
android:drawablePadding="-5sp"
android:drawableTop="#drawable/image3"
android:gravity="center"
android:src="#drawable/image3"
android:text="#string/image3text"
android:textColor="#color/darkgrey" />
<Button
android:id="#+id/image4Btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#android:color/transparent"
android:drawablePadding="-5sp"
android:drawableTop="#drawable/image4"
android:gravity="center"
android:src="#drawable/image4"
android:text="#string/image4text"
android:textColor="#color/darkgrey" />
</TableRow>
</TableLayout>
</RelativeLayout>
</ScrollView>
Put your buttons in a couple of LinearLayouts instead of a TableRow.
<TableLayout
...
>
<LinearLayout
android:orientation="vertical"
>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_gravity="center"
>
<Button
android:id="#+id/image1Btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#android:color/transparent"
android:drawablePadding="-5sp"
android:drawableTop="#drawable/image1"
android:src="#drawable/image1"
android:text="#string/image1text"
android:textColor="#color/darkgrey" />
<Button
android:id="#+id/image2Btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#android:color/transparent"
android:drawablePadding="-5sp"
android:drawableTop="#drawable/image2"
android:src="#drawable/image2"
android:text="#string/image2text"
android:textColor="#color/darkgrey" />
</LinearLayout>
</LinearLayout>
...
</TableLayout>
The outer LinearLayout will be stretched to the width of the TableLayout and have its height set to wrap_content by TableLayout. The inner LinearLayout will be centered horizontally in the outer LinearLayout because the latter is vertically oriented.
<LinearLayout android:id="#+id/LinearLayout01" android:layout_width="fill_parent" android:layout_height="fill_parent" xmlns:android="http://schemas.android.com/apk/res/android" android:background="#drawable/wood">
<ScrollView android:id="#+id/ScrollView01" android:layout_width="fill_parent" android:layout_height="fill_parent">
<AbsoluteLayout android:id="#+id/absoluteLayout1" android:layout_width="match_parent" android:layout_height="fill_parent" xmlns:android="http://schemas.android.com/apk/res/android" android:paddingLeft="10dp" android:paddingRight="10dp">
<TableLayout android:id="#+id/tableLayout0" android:background="#drawable/my_border" android:layout_width="fill_parent" android:layout_y="150dp" android:layout_height="match_parent">
<TableRow android:layout_height="wrap_content" android:id="#+id/tableRow1" android:layout_width="wrap_content">
<TextView android:id="#+id/EmailID" android:text="EmailID" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="#drawable/my_border2" android:textStyle="bold" android:layout_weight="1"></TextView>
<EditText android:id="#+id/txtEmailID" android:layout_width="150dp" android:background="#drawable/my_border1" android:layout_height="wrap_content" android:layout_weight="1">
<requestFocus></requestFocus>
</EditText>
</TableRow>
<TableRow android:layout_height="wrap_content" android:id="#+id/tableRow6" android:layout_width="wrap_content" android:paddingTop="20dp">
<Button android:id="#+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:text="Cancel"></Button>
<Button android:id="#+id/button2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:text="Login"></Button>
</TableRow>
</TableLayout>
</AbsoluteLayout>
</ScrollView>
</LinearLayout>

Categories

Resources