Terence Chow
Terence Chow

Reputation: 11153

textview below textview linearlayout

I'm using LinearLayout because it's the only way I can use layout_weight and I'm not familiar enough with aligning textviews evenly in RelativeLayout. (I'm new to android). I'm making a calendar app and can't seem to figure out how to get a textview below my 7 textviews. (The 7 textviews are the days of the week). They end up showing all on the same row.

How do I get the final textview below my 7 days? Please note I've searched this question on stackoverflow but I haven't found anything that made sense to me or worked.

<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:layout_gravity="top" >

<TextView
    android:id="@+id/day1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:gravity="center"
    android:text="@string/day1"
    android:textAppearance="?android:attr/textAppearanceSmall" />

<TextView
    android:id="@+id/day2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:gravity="center"
    android:layout_weight="1"
    android:text="@string/day2"
    android:textAppearance="?android:attr/textAppearanceSmall" />

<TextView
    android:id="@+id/day3"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:gravity="center"
    android:layout_weight="1"
    android:text="@string/day3"
    android:textAppearance="?android:attr/textAppearanceSmall" />

<TextView
    android:id="@+id/day4"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:gravity="center"
    android:layout_weight="1"
    android:text="@string/day4"
    android:textAppearance="?android:attr/textAppearanceSmall" />

<TextView
    android:id="@+id/day5"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:gravity="center"
    android:layout_weight="1"
    android:text="@string/day5"
    android:textAppearance="?android:attr/textAppearanceSmall" />

<TextView
    android:id="@+id/day6"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:gravity="center"
    android:layout_weight="1"
    android:text="@string/day6"
    android:textAppearance="?android:attr/textAppearanceSmall" />
<TextView
    android:id="@+id/day7"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:gravity="center"
    android:layout_weight="1"
    android:text="@string/day7"
    android:textAppearance="?android:attr/textAppearanceSmall" />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="a text string below" 
    android:textAppearance="?android:attr/textAppearanceSmall" />


</LinearLayout>

Upvotes: 2

Views: 25764

Answers (6)

mudrak patel
mudrak patel

Reputation: 605

From what i understand, you want to do is this:

  • TextView1
  • TextView2
  • (Some more TextViews)
  • TextViewLast

To achieve this in the VERTICAL ORIENTATION, i did this in my .xml layout file. Hope it works for you too.

NOTE: You DO NOT need nested Linear Layouts.This can be achieved by one Linear Layout.

<?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">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/fullNameTextView"
        android:text="@string/fullNameString"
        android:textSize="@dimen/mainActivityFontSize"
        android:layout_marginLeft="@dimen/mainActivityMarginLeft"/>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/programNameTextView"
        android:textSize="@dimen/mainActivityFontSize"
        android:layout_marginLeft="@dimen/mainActivityMarginLeft"
        android:text="@string/programName"/>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/semesterTextView"
        android:textSize="@dimen/mainActivityFontSize"
        android:layout_marginLeft="@dimen/mainActivityMarginLeft"
        android:text="@string/semesterTextView"/>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/courseNameTextView"
        android:textSize="@dimen/mainActivityFontSize"
        android:layout_marginLeft="@dimen/mainActivityMarginLeft"
        android:text="@string/courseName"/>

</LinearLayout>

Three things that made my layout the way I want (and hope you also want the same type of layout):

  1. android:layout_width="match_parent"
  2. android:layout_height="wrap_content"
  3. android:orientation="vertical"

And the result is this:

Upvotes: 0

Marcin S.
Marcin S.

Reputation: 11191

To place a view with the summary below the rest of the views you can change the orientation of the main Linear Layout to vertical and enclose your 7 textViews with another Linear Layout with horizontal orientation. Here is a code:

<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="vertical"
android:layout_gravity="top" >

<LinearLayout android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">

<TextView
android:id="@+id/day1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:text="@string/day1"
android:textAppearance="?android:attr/textAppearanceSmall" />

<TextView
android:id="@+id/day2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:layout_weight="1"
android:text="@string/day2"
android:textAppearance="?android:attr/textAppearanceSmall" />

<TextView
android:id="@+id/day3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:layout_weight="1"
android:text="@string/day3"
android:textAppearance="?android:attr/textAppearanceSmall" />

<TextView
android:id="@+id/day4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:layout_weight="1"
android:text="@string/day4"
android:textAppearance="?android:attr/textAppearanceSmall" />

<TextView
android:id="@+id/day5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:layout_weight="1"
android:text="@string/day5"
android:textAppearance="?android:attr/textAppearanceSmall" />

<TextView
android:id="@+id/day6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:layout_weight="1"
android:text="@string/day6"
android:textAppearance="?android:attr/textAppearanceSmall" />

<TextView
android:id="@+id/day7"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:layout_weight="1"
android:text="@string/day7"
android:textAppearance="?android:attr/textAppearanceSmall" />

</LinearLayout>

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="a text string below" 
android:textAppearance="?android:attr/textAppearanceSmall" />


</LinearLayout>

Upvotes: 1

Scott
Scott

Reputation: 185

An even simpler explanation: You are using a LinearLayout with orientation set to horizontal; this is telling Android "All of these items should be placed next to each other in a single row". What you want is a vertical linear layout which contains the linear layout you currently have. Your final textView can then be a child of the vertical layout rather than the horizontal one (which is what the other answers are showing).

Upvotes: 0

Yellos
Yellos

Reputation: 1657

Simplified:

<LinearLayout android:orientation="vertical">    
    <LinearLayout android:orientation="horizontal" android:layout_weight="1">
        <!-- TextView 1,2,3,4,5,6,7 here -->
    </LinearLayout>
    <TextView android:layout_weight="1"/><!-- (TextView 8) -->
</LinearLayout>

Upvotes: 8

yildirimyigit
yildirimyigit

Reputation: 3022

If you use RelativeLayout, you will be able to use *android:layout_below* attribute.

All of the textviews you use have the same weight. As far as I know, you may just delete those lines.

If I were you, I'd consider a hierarchy as follows:

<RelativeLayout>
    <LinearLayout android:id="@+id/blabla">
        *7 textviews*
    </LinearLayout>
    <TextView>
        android:layout_below= @+id/blabla
    </TextView>
</RelativeLayout>

Upvotes: 3

Chris
Chris

Reputation: 360

Don´t know if I unserstand it right, but what you can try ist to encapsulate the days 1-7 in an extra Linear Layout and the last TextView in the RootLayout.

For Example:

<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="vertical"
android:layout_gravity="top" >


<LinearLayout 
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_gravity="top" >

  <!-- TextView for Days 1 to 7 in here -->

</LinearLayout>

<!-- Last TextView in here -->

</LinearLayout>

Edit: And try to set the android:orientation Tag to "Vertical"

Upvotes: 0

Related Questions