1

I am following tutorial from the book "Android Apps for Absolute Beginners 3rd Edition", and I am stuck with nested LinearLayout for android project in API 19 (4.4.2): Parent LinearLayout is horizontal and it is supposed to hold two child LinearLayout which are vertical and the end result should be like this:

enter image description here

But what I get is:

enter image description here

I tried several times, cross checked code in book, and explanations but I can't get it right. Here is content of activity_main.xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" tools:context=".MainActivity" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="horizontal" android:background="@drawable/galaxyinfoscreen" > <LinearLayout android:orientation="vertical" android:layout_margin="12dip" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="#00000000"> <TextView android:text="@string/hello_world" android:textColor="#FFFFFF" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="48dip" android:textStyle="bold" /> <TextView android:text="@string/galaxy_name" android:textColor="#FFFFFF" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <TextView android:text="@string/galaxy_solar" android:textColor="#FFFFFF" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <TextView android:text="@string/galaxy_habit" android:textColor="#FFFFFF" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <TextView android:text="@string/galaxy_colony" android:textColor="#FFFFFF" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <TextView android:text="@string/galaxy_pop" android:textColor="#FFFFFF" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <TextView android:text="@string/galaxy_fleet" android:textColor="#FFFFFF" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <TextView android:text="@string/galaxy_ships" android:textColor="#FFFFFF" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </LinearLayout> <LinearLayout android:orientation="vertical" android:layout_margin="33dip" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="#00000000" > <TextView android:text="@string/name_data" android:textColor="#FFFFFF" android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/name" /> <TextView android:text="@string/solar_data" android:textColor="#FFFFFF" android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/solar" /> <TextView android:text="@string/habit_data" android:textColor="#FFFFFF" android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/habit"/> <TextView android:text="@string/colony_data" android:textColor="#FFFFFF" android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/colony" /> <TextView android:text="@string/pop_data" android:textColor="#FFFFFF" android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/pop" /> <TextView android:text="@string/fleet_data" android:textColor="#FFFFFF" android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/fleet" /> <TextView android:text="@string/ships_data" android:textColor="#FFFFFF" android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/ships"/> </LinearLayout> </LinearLayout> 

What could be missing from this file? What should I do to get desired result?

4
  • you should be using a listview to display those. Commented Nov 22, 2014 at 18:21
  • @SarthakMittal Thank you, but it's not what I should/would like to use (I would take different approach with RelativeLayout) it is what author of book had on his mind, as this is just a beginning of complex application - later there will be menus, sliders etc., so if I mess up too much with this I won't be able follow further instructions in the book. It looks like that the author left out something from the code published in book (his working code at this stage obviously was different from published version) and downloaded (final) code is quite different and complex version. Commented Nov 22, 2014 at 19:45
  • ok, so maybe setting gravity to 1(say) in both of those linear layouts will work. Commented Nov 22, 2014 at 20:17
  • @SarthakMittal OK, I will check and let you know. Commented Nov 22, 2014 at 20:20

4 Answers 4

2

try this

<?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="match_parent" android:background="@drawable/galaxyinfoscreen" android:orientation="vertical" tools:context=".MainActivity" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="48dip" android:text="@string/hello_world" android:textColor="#FFFFFF" android:textStyle="bold" /> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="horizontal" > <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_margin="12dip" android:background="#00000000" android:orientation="vertical" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/galaxy_name" android:textColor="#FFFFFF" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/galaxy_solar" android:textColor="#FFFFFF" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/galaxy_habit" android:textColor="#FFFFFF" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/galaxy_colony" android:textColor="#FFFFFF" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/galaxy_pop" android:textColor="#FFFFFF" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/galaxy_fleet" android:textColor="#FFFFFF" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/galaxy_ships" android:textColor="#FFFFFF" /> </LinearLayout> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_margin="12dip" android:background="#00000000" android:orientation="vertical" > <TextView android:id="@+id/name" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/name_data" android:textColor="#FFFFFF" /> <TextView android:id="@+id/solar" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/solar_data" android:textColor="#FFFFFF" /> <TextView android:id="@+id/habit" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/habit_data" android:textColor="#FFFFFF" /> <TextView android:id="@+id/colony" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/colony_data" android:textColor="#FFFFFF" /> <TextView android:id="@+id/pop" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/pop_data" android:textColor="#FFFFFF" /> <TextView android:id="@+id/fleet" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/fleet_data" android:textColor="#FFFFFF" /> <TextView android:id="@+id/ships" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/ships_data" android:textColor="#FFFFFF" /> </LinearLayout> </LinearLayout> </LinearLayout> 

"Glaxy Information Screen" TextView is inside first verical LinearLayout. As this text view is taking more space due to its margin and text length hence leaving less space for second linearlayout, resulting into wrap of text for sencond verical layout.

To solve the problem, add one more linear layout with orientation as horizontal as parent of both the vertical layout and add the "Glaxy Information Screen" text view as direct child of top parent. you may need to make some adjustment to the margin values to aling the layouts.

Sign up to request clarification or add additional context in comments.

1 Comment

I accept this as solution as it makes the least possible change to the code from book, so probably I can continue building example from book with minimum alterations. If you care, please add few words why this works compared to version from book - just to make your answer more complete (I understand it totally, but for someone in future who might find this question). Thank you.
2

In the second LinearLayout, the margin parameter is android:layout_marginTop="33dip

You forgot to put Top after margin.

;)

Comments

1

The second LinearLayout doesn't have enough width to accommodate the string and it gets wrapped. If you absolutely must use nested LinearLayout, you should use layout_weight instead of using width with wrapped content. In the example below, I have also moved the title out of the nested LinearLayout to another vertical one.

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" tools:context=".MainActivity" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:background="@drawable/galaxyinfoscreen" > <TextView android:text="@string/hello_world" android:textColor="#FFFFFF" android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center" android:textStyle="bold" /> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" > <LinearLayout android:orientation="vertical" android:layout_margin="12dip" android:layout_width="0dp" android:layout_weight="3" android:layout_height="wrap_content" android:background="#00000000"> <TextView android:text="@string/galaxy_name" android:textColor="#FFFFFF" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <TextView android:text="@string/galaxy_solar" android:textColor="#FFFFFF" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <TextView android:text="@string/galaxy_habit" android:textColor="#FFFFFF" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <TextView android:text="@string/galaxy_colony" android:textColor="#FFFFFF" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <TextView android:text="@string/galaxy_pop" android:textColor="#FFFFFF" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <TextView android:text="@string/galaxy_fleet" android:textColor="#FFFFFF" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <TextView android:text="@string/galaxy_ships" android:textColor="#FFFFFF" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </LinearLayout> <LinearLayout android:orientation="vertical" android:layout_margin="12dip" android:layout_width="0dp" android:layout_weight="1" android:layout_height="wrap_content" android:background="#00000000" > <TextView android:text="@string/name_data" android:textColor="#FFFFFF" android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/name" /> <TextView android:text="@string/solar_data" android:textColor="#FFFFFF" android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/solar" /> <TextView android:text="@string/habit_data" android:textColor="#FFFFFF" android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/habit"/> <TextView android:text="@string/colony_data" android:textColor="#FFFFFF" android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/colony" /> <TextView android:text="@string/pop_data" android:textColor="#FFFFFF" android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/pop" /> <TextView android:text="@string/fleet_data" android:textColor="#FFFFFF" android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/fleet" /> <TextView android:text="@string/ships_data" android:textColor="#FFFFFF" android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/ships"/> </LinearLayout> </LinearLayout> </LinearLayout> 

2 Comments

Great, this works. There was only one </LinearLayout> missing tag at the end - but when I click to edit your answer it shows correctly all three closing </LinearLayout> tags?. If no one posts anything better than this I will accept this as solution. Thanks.
The last </LinearLayout> was missing due to spacing issues. Corrected. Hope you understood the reason why this works.
0

With your current code i have two suggestions:

First:

android:singleLine="true" 

add this tag to all your list views, so this will maintain symmetry in all your textView placement.

Secondly:

Keep a single Parent LinearLayout with orientation vertical, then have multiple LinearLayout with horizontal orientation and two TextView inside it.

2 Comments

1st solution didn't work (completely) here is result: i968.photobucket.com/albums/ae168/nenadbulatovic/… . For second solution, as this is just a beginning of complex application - later there will be menus, sliders etc., so if I mess up too much with this I won't be able follow further instructions in the book. It looks like that the author left out something from the code published in book (his working code at this stage obviously was different from published version) and downloaded (final) code is quite different and complex version.
As for 1st solution, it moved name of galaxy (first field) totally out of screen, but other field are positioned OK) as you can see from screenshot. i968.photobucket.com/albums/ae168/nenadbulatovic/…

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.