How to have multiple TextView and EditText in one row each - android

I'm trying to get a form such a way that on each row the TextView is on the left and the EditText is on the right.
The below code works fine for one row, however,r if I have multiple rows then it doesn't draw TextView and EditText on each row but instead tries to jam everything together.
<?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="wrap_content"
android:orientation="horizontal"
android:padding="10dip"
android:background="#FFFFFF"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Full Name"
android:textSize="20dp"
android:layout_weight="1"
android:textColor="#000000"
/>
<EditText
android:id="#+id/name"
android:gravity="center"
android:hint="John Doe"
android:layout_width="fill_parent"
android:layout_weight="1"
android:layout_height="wrap_content"
/>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Age"
android:textSize="20dp"
android:layout_weight="1"
android:textColor="#000000"
/>
<EditText
android:id="#+id/age"
android:gravity="center"
android:hint="age"
android:layout_width="fill_parent"
android:layout_weight="1"
android:layout_height="wrap_content"
/>
</LinearLayout>
This is what it looks like

There are so many ways to achieve the Layout which you been looking for. You were on track by adding layout_weight="1" in Views. Here's the one which uses your Layout only with little bit of orientation changes.
Explaination:
LinearLayout //ParentView Orientation - Vertical
LinearLayout //childView Orientation - Horizontal
TextView & EditText // Taking equal space with help of layout_weight="1"
LinearLayout //childView Orientation - Horizontal
TextView & EditText // Taking equal space with help of layout_weight="1"
Layout:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="10dip"
android:background="#FFFFFF"
>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Full Name"
android:textSize="20dp"
android:layout_weight="1"
android:textColor="#000000"
/>
<EditText
android:id="#+id/name"
android:gravity="center"
android:hint="John Doe"
android:layout_width="fill_parent"
android:layout_weight="1"
android:layout_height="wrap_content"
/>
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Age"
android:textSize="20dp"
android:layout_weight="1"
android:textColor="#000000"
/>
<EditText
android:id="#+id/age"
android:gravity="center"
android:hint="age"
android:layout_width="fill_parent"
android:layout_weight="1"
android:layout_height="wrap_content"
/>
</LinearLayout>
</LinearLayout>

// try this way,hope this will help you...
<?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:padding="5dp"
android:background="#FFFFFF">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:gravity="center"
android:layout_weight="1">
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.40"
android:text="Full Name"
android:textSize="20sp"
android:gravity="right"
android:textColor="#000000"/>
<EditText
android:id="#+id/name"
android:gravity="center"
android:hint="John Doe"
android:layout_width="0dp"
android:layout_weight="0.60"
android:layout_height="wrap_content"/>
</LinearLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:gravity="center"
android:layout_weight="1"
android:layout_marginLeft="5dp">
<TextView
android:layout_width="0dp"
android:layout_weight="0.40"
android:layout_height="wrap_content"
android:text="Age"
android:gravity="right"
android:textSize="20sp"
android:textColor="#000000"/>
<EditText
android:id="#+id/age"
android:gravity="center"
android:hint="Age"
android:layout_width="0dp"
android:layout_weight="0.60"
android:layout_height="wrap_content"/>
</LinearLayout>
</LinearLayout>
</LinearLayout>

Related

Can't adjust weights in linear layout - android

I've searched but couldn't figure out the problem.
I want to make exactly like the above picture. I've used linear layout in vertical for the "Day Teams Score Status"
And then for the content containing the values of above headings, I've used a listview.
What I've achieved is that headings "Day Teams Score Status" are being shown with spaces but the contents ( which are in seperate .xml file ) are not being shown with spaces, so what I'm getting is:
The adapter xml file for the list view is:
adapter_my_matches.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
>
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Date"
android:id="#+id/textViewDate"
android:layout_weight="0.1"
android:layout_gravity="center_vertical" />
<LinearLayout
android:id="#+id/ll"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_weight="0.4">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Teams"
android:id="#+id/textViewTeams1"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Teams"
android:id="#+id/textViewTeams2"/>
</LinearLayout>
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Score"
android:id="#+id/textViewScore"
android:layout_weight="0.1"
android:layout_gravity="center_vertical" />
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Status"
android:id="#+id/textViewStatus"
android:layout_weight="0.2"
android:layout_gravity="center_vertical" />
</LinearLayout>
I think this is the layout you are looking for.Let me know if anything going bad. Here the weight i set for a sample, you can change it according to your need.
<?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="vertical"
>
<LinearLayout
android:id="#+id/ll"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:weightSum= "6">
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Date"
android:id="#+id/textViewDate"
android:layout_weight="1.2"
android:gravity="center" />
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Teams"
android:id="#+id/textViewTeams1"
android:gravity="center"
android:layout_weight="2"/>
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Score"
android:id="#+id/textViewScore"
android:layout_weight="1.4"
android:gravity="center"
/>
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Status"
android:id="#+id/textViewStatus"
android:layout_weight="1.4"
android:gravity="center"
/>
</LinearLayout>
<LinearLayout
android:id="#+id/ll1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:weightSum= "6">
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="12 Feb"
android:id="#+id/textViewDate1"
android:layout_weight="1.2"
android:gravity="center" />
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="IND VS PAK"
android:id="#+id/textViewTeams11"
android:gravity="center"
android:layout_weight="2"/>
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="256"
android:id="#+id/textViewScore1"
android:layout_weight="1.4"
android:gravity="center"
/>
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="WIN"
android:id="#+id/textViewStatus1"
android:layout_weight="1.4"
android:gravity="center"
/>
</LinearLayout>
</LinearLayout>
Add this to your linearlayout code
android:weightSum="0.8"
Use this One :
<?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="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:weightSum="8" >
<TextView
android:id="#+id/textViewDate"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_weight="1"
android:text="Date"
android:textAppearance="?android:attr/textAppearanceMedium" />
<LinearLayout
android:id="#+id/ll"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="4"
android:orientation="vertical" >
<TextView
android:id="#+id/textViewTeams1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Teams"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="#+id/textViewTeams2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Teams"
android:textAppearance="?android:attr/textAppearanceMedium" />
</LinearLayout>
<TextView
android:id="#+id/textViewScore"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_weight="1"
android:text="Score"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="#+id/textViewStatus"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_weight="2"
android:text="Status"
android:textAppearance="?android:attr/textAppearanceMedium" />
</LinearLayout>
give layout_weight to parent layout and give weight to child element and make sure that layout_weight should be sum of its all child's weight total. make width of child as warp_content.

Android limit a text view length

I have a layout containing two text views and an image button, like below figure.
This is the code of the layout
<LinearLayout
android:id="#+id/llGlobalControl"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:background="#color/gc_background"
android:gravity="center"
android:padding="6dp" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="left"
android:orientation="vertical"
android:weightSum="2" >
<TextView
android:id="#+id/tvGlobalControlTitle"
android:layout_width="wrap_content"
android:layout_height="0px"
android:layout_weight="1"
android:text="Text View"
android:textColor="#color/green"
android:textStyle="bold" />
<TextView
android:id="#+id/tvGlobalControlArtist"
android:layout_width="wrap_content"
android:layout_height="0px"
android:layout_weight="1"
android:text="Text View"
android:textColor="#color/green" />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="right"
android:orientation="vertical" >
<ImageButton
android:id="#+id/ibGlobalControlPlayPause"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_action_pause_over_video" />
</LinearLayout>
</LinearLayout>
My problem is that if the text in either of the text views gets too long, the button "gets pushed away", like the figure below.
Instead, I would like that the text view limits the text contained, something like the red underline in the figure below.
How do I do this?
The trick is to use android:ellipsize="end" and limit number of lines with android:maxLines="1".
Also, to prevent your ImageButton from getting pushed away, the easiest way is to set android:layout_weight="1" to the LinearLayout with TextViews and android:layout_width="wrap_content" to the LinearLayout containing the button.
This is the result:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/llGlobalControl"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:background="#color/gc_background"
android:gravity="center"
android:padding="6dp" >
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:gravity="left"
android:orientation="vertical"
android:layout_weight="1"
android:weightSum="2" >
<TextView
android:id="#+id/tvGlobalControlTitle"
android:layout_width="wrap_content"
android:layout_height="0px"
android:layout_weight="1"
android:text="This is a long text that will take up a lot of space and push the button away"
android:maxLines="1"
android:ellipsize="end"
android:textColor="#color/green"
android:textStyle="bold" />
<TextView
android:id="#+id/tvGlobalControlArtist"
android:layout_width="wrap_content"
android:layout_height="0px"
android:layout_weight="1"
android:text="Text View"
android:maxLines="1"
android:ellipsize="end"
android:textColor="#color/green" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:gravity="right"
android:orientation="vertical" >
<ImageButton
android:id="#+id/ibGlobalControlPlayPause"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_action_pause_over_video" />
</LinearLayout>
</LinearLayout>
try to set to your TextView set together
android:ellipsize="end"
android:singleLine="true"
try this
Add
android:ellipsize="end"
android:singleLine="true"
to TextView you want to be single line and with "..." at end.
try like this,
<LinearLayout
android:id="#+id/llGlobalControl"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:background="#color/gc_background"
android:gravity="center"
android:padding="6dp" >
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="left"
android:orientation="vertical"
android:weightSum="2" >
<TextView
android:id="#+id/tvGlobalControlTitle"
android:layout_width="wrap_content"
android:layout_height="0px"
android:layout_weight="1"
android:text="Text View"
android:textColor="#color/green"
android:textStyle="bold" />
<TextView
android:id="#+id/tvGlobalControlArtist"
android:layout_width="wrap_content"
android:layout_height="0px"
android:layout_weight="1"
android:text="Text View"
android:textColor="#color/green" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:gravity="right"
android:orientation="vertical" >
<ImageButton
android:id="#+id/ibGlobalControlPlayPause"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_action_pause_over_video" />
</LinearLayout>
</LinearLayout>
Try this way.. Added weight to layout..
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="left"
android:layout_weight="1"
android:orientation="vertical"
android:weightSum="2" >
<TextView
android:id="#+id/tvGlobalControlTitle"
android:layout_width="wrap_content"
android:layout_height="0px"
android:layout_weight="1"
android:text="Text View"
android:textColor="#color/green"
android:textStyle="bold" />
<TextView
android:id="#+id/tvGlobalControlArtist"
android:layout_width="wrap_content"
android:layout_height="0px"
android:layout_weight="1"
android:text="Text View"
android:textColor="#color/green" />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="right"
android:layout_weight="3"
android:orientation="vertical" >
<ImageButton
android:id="#+id/ibGlobalControlPlayPause"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_action_pause_over_video" />
</LinearLayout>
Try using this layout:
<RelativeLayout
android:id="#+id/llGlobalControl"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#color/gc_background"
android:padding="6dp" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:weightSum="2"
android:layout_toLeftOf="#id/ibGlobalControlPlayPause"
android:layout_toStartOf="#id/ibGlobalControlPlayPause" >
<TextView
android:id="#+id/tvGlobalControlTitle"
android:layout_width="wrap_content"
android:layout_height="0px"
android:layout_weight="1"
android:text="Text View"
android:textColor="#color/green"
android:textStyle="bold"
android:singleLine="true"
android:ellipsize="end"
android:maxLines="1"/>
<TextView
android:id="#+id/tvGlobalControlArtist"
android:layout_width="wrap_content"
android:layout_height="0px"
android:layout_weight="1"
android:text="Text View"
android:textColor="#color/green"
android:singleLine="true"
android:ellipsize="end"
android:maxLines="1" />
</LinearLayout>
<ImageButton
android:id="#+id/ibGlobalControlPlayPause"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:src="#drawable/ic_action_pause_over_video" />
</RelativeLayout>

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>

Nested Linear Layouts not showing up

Only the first nested linear layout is showing up, any idea why? The first textView inside the 2nd linear layout shows up, and the layout is the proper height, but none if it's contents can be viewed.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:weightSum="100">
<LinearLayout
android:id="#+id/linearLayout3"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<TextView
android:layout_width="fill_parent"
android:layout_height="42dp"
android:background="#FAFA25"
android:height="200dp"
android:text="#string/hello"
android:textColor="#222222"
android:textSize="20dp" />
<LinearLayout
android:id="#+id/linearLayout1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/width" />
<EditText
android:id="#+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:inputType="number" >
<requestFocus />
</EditText>
</LinearLayout>
<TextView
android:id="#+id/textView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:text="#string/totalWidth"
android:layout_marginRight="10dp" />
<LinearLayout
android:id="#+id/linearLayout2"
android:layout_height="wrap_content"
android:background="#999999"
android:layout_width="fill_parent"
android:layout_margin="10dp"
android:padding="4dp"
android:weightSum="100" >
<TextView
android:id="#+id/textView5"
android:layout_width="fill_parent"
android:layout_height="50dp"
android:text="#string/numOne"
android:layout_weight="62"
android:background="#ffffff"
android:gravity="center"
android:textColor="#222222"
android:layout_marginRight="1dp" />
<TextView
android:id="#+id/textView6"
android:layout_width="fill_parent"
android:layout_height="50dp"
android:text="#string/numTwo"
android:background="#ffffff"
android:layout_weight="38"
android:gravity="center"
android:textColor="#222222"
/>
</LinearLayout>
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:text="Calculate" />
</LinearLayout>
</LinearLayout>
linearLayout3 , orientation is horizontal, in case you are wondering why, default orientation is horizontal. and since textView has layout_width="fill_parent", the next child will not show up.
change the orientation to vertical.

Directly Nesting Linear Layouts only shows the first child view

I am trying to nest two Linear Layouts as direct children of another Linear Layout. Eventually the intention is for these two layouts to be nested within a ViewFlipper, so each of the two Linear Layouts will be flipped in and out of the screen.
The Layouts work fine until I nest the two Linear Layouts within a parent element, in which case only the first widget in the first child is displayed. So I have something like this:
<LinearLayout (parent)...
<LinearLayout (child1)...
<child1's Widgets... <---only the first widget here is being shown
<LinearLayout (child2)...
<child2's widgets...
I have no idea why this could be happening. Can anybody shed any light??
Full code:
<?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"
android:background="#FFFFFF">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="22sp"
android:layout_gravity="center"
android:gravity="center"
android:text="Hello, is this a great app?"
android:textColor="#000000"
android:layout_marginBottom="10dip"
android:layout_marginTop="8dip"/>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<Button
android:id="#+id/btnYes"
android:text="Yes"
android:textSize="20sp"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_marginBottom="7dip"
android:layout_marginLeft="4dip"
android:layout_marginRight="4dip"/>
<Button
android:id="#+id/btnNo"
android:text="No"
android:textSize="20sp"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_marginBottom="7dip"
android:layout_marginLeft="4dip"
android:layout_marginRight="4dip"/>
</LinearLayout>
<!-- <ViewFlipper
android:layout_width="fill_parent"
android:layout_height="fill_parent">
-->
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<LinearLayout <!-- ONLY THE CONTENT FROM HERE IS BEING SHOWN -->
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="0px"
android:layout_height="wrap_content"
android:text="Lat:"
android:textSize="16sp"
android:layout_weight="1"
android:layout_marginLeft="12dip"/>
<TextView
android:id="#+id/lblLat"
android:layout_width="0px"
android:layout_height="wrap_content"
android:text=""
android:textSize="16sp"
android:layout_weight="3"
android:gravity="right"
android:layout_marginRight="12dip"/>
</LinearLayout> <!--NO MORE CONTENT Is SHOWN AFTER HERE -->
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="0px"
android:layout_height="wrap_content"
android:text="Lon:"
android:textSize="16sp"
android:layout_weight="1"
android:layout_marginLeft="12dip"/>
<TextView
android:id="#+id/lblLon"
android:layout_width="0px"
android:layout_height="wrap_content"
android:text=""
android:textSize="16sp"
android:layout_weight="3"
android:gravity="right"
android:layout_marginRight="12dip"/>
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="0px"
android:layout_height="wrap_content"
android:text="Number of satellites:"
android:textSize="16sp"
android:layout_weight="3"
android:layout_marginLeft="12dip"/>
<TextView
android:id="#+id/lblSats"
android:layout_width="0px"
android:layout_height="wrap_content"
android:text=""
android:textSize="16sp"
android:layout_weight="1"
android:gravity="right"
android:layout_marginRight="12dip"/>
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="0px"
android:layout_height="wrap_content"
android:text="Signal To Noise:"
android:textSize="16sp"
android:layout_weight="3"
android:layout_marginTop="25dip"
android:layout_marginLeft="12dip"/>
<TextView
android:id="#+id/lblSNR"
android:layout_width="0px"
android:layout_height="wrap_content"
android:text=""
android:textSize="16sp"
android:layout_weight="1"
android:gravity="right"
android:layout_marginTop="25dip"
android:layout_marginRight="12dip"/>
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<TextView
android:id="#+id/lblAddresses"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text=""
android:textSize="16sp"
android:layout_marginLeft="12dip"
android:minLines="3"/>
</LinearLayout>
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<TextView
android:id="#+id/lblAbout"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Stuff About App"
android:textSize="16sp"
android:layout_marginLeft="12dip"
android:minLines="3"/>
</LinearLayout>
<!-- </ViewFlipper> -->
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<Button
android:id="#+id/btnLookup"
android:text="LookUp"
android:textSize="18sp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="20dip"
android:paddingTop="1dip"
android:paddingBottom="5dip"
android:layout_alignParentBottom="true"
android:layout_marginBottom="8dip"
android:layout_marginLeft="8dip" />
<Button
android:id="#+id/btnQuit"
android:text="Quit"
android:textSize="18sp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:padding="20dip"
android:paddingTop="1dip"
android:paddingBottom="5dip"
android:layout_marginBottom="8dip"
android:layout_marginRight="8dip" />
</RelativeLayout>
</LinearLayout>
Make sure to have on all your LinearLayout tags an android:orientation property.
<LinearLayout
..
android:orientation="vertical|horizontal">
...
</LinearLayout>
If you want to display the child components in vertical, you need to indicate it explicitly because by default it is set to horizontal.

Categories

Resources