0

I am trying to show a rectangular box with a centered TextView as a footer bar at the bottom of a UI (just like the toolbar/actionbar is shown as a header bar above the main UI). The TextView should be below the main UI (which is a ScrollView) and the TextView doesn't show at all. Everything else shows correctly. Any ideas on what I'm doing wrong?

layout.xml

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout

xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#FFFFFF"
android:focusableInTouchMode="true"
tools:context=".CardViewActivity">

<include
    android:id="@+id/toolbar"
    layout="@layout/toolbar" >
</include>

<ScrollView
    android:id="@+id/ScrollView1"
    android:layout_width="match_parent"
    android:layout_height="match_parent" 
    ...>
...
</ScrollView>

<TextView
    android:id="@+id/skychilltext2"
    android:text="skychill"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:gravity="center"
    android:textStyle="bold"
    android:textColor="#FFFFFF"
    android:background="@color/colorPrimary"
    android:textAppearance="?android:attr/textAppearanceMedium"
    android:clickable="true"  />

</LinearLayout>    
AJW
  • 1,578
  • 3
  • 36
  • 77

5 Answers5

1

Try below

<RelativeLayout
  (...)>

    <LinearLayout android:id="@+id/ll1"
        android:layout_alignParentTop="true"
        (...)/>

    <TextView android:id="@+id/button"
        android:layout_alignParentBottom="true"
        (...)/>

    <ScrollView
         android:layout_above="@id/button"
         android:layout_below="@id/ll1"
         (...)/>

</RelativeLayout>

For reference see here How to make a static button under a ScrollView? or How can I place new items under scrollview

Community
  • 1
  • 1
Mohammad Tauqir
  • 1,817
  • 1
  • 18
  • 53
  • Ok is there any disadvantage to switching from a LinearLayout to a RelativeLayout? – AJW Dec 30 '15 at 01:46
1

because you set the height to match_parent, it will take up all the room. I'm not sure exactly how you want it, but you could add a weight to it to expand to as much room as possible.

<ScrollView
    android:id="@+id/ScrollView1"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="1" 
    ...>
    ...
</ScrollView>

Weight specifies how much percentage of the screen the view will take, so if every view has the same weight they will equally take up the same amount of space on the screen.

view 1 - weight 1
view 2 - weight 1
view 3 - weight 1
view 4 - weight 3 since this is weight 3 and all the weights above add up to
                | 3, this view will take up exactly half the screen.
                | the other ones will take 1/6 of the screen because (1+1+1+3 = 6)
WalterM
  • 2,686
  • 1
  • 19
  • 26
  • Ok, then do I need to set any layout_weight for the TextView or just leave out? – AJW Dec 30 '15 at 01:41
  • leave it out. no need. keep it to wrap content – WalterM Dec 30 '15 at 01:47
  • So that means the View for the TextView will only be big enough to wrap the text content and all of the remaining vertical space will be allotted to the ScrollView, correct? – AJW Dec 30 '15 at 01:52
1

The problem is android:layout_height="match_parent" in ScrollView. It directs LinearLayout to give all the height to it, leaving nothing for TextView.

Change ScrollView:

android:layout_height="wrap_content"
android:layout_weight="1"

Change TextView:

android:layout_weight="0"
Rediska
  • 1,392
  • 10
  • 14
  • Ok, what does setting the TextView layout_weight to zero do? versus not setting a weight at all? – AJW Dec 30 '15 at 01:39
  • Views with layout_weight="0" do not expand to consume available room in LinearLayout – Rediska Dec 30 '15 at 01:41
  • I don't understand. Setting the TextView's layout_weight to zero would then mean that the TextView wouldn't show at all because there is no available room in the Linear Layout? – AJW Dec 30 '15 at 01:48
  • No. the value "0" means that it will not participate in distribution of extra available space, and it will take only as much space as needed for its content. – Rediska Dec 30 '15 at 02:00
0

set android:layout:weight on TextView.

KasoGG
  • 1
0

You need weightsum to scale layout. Your code set match_parent for height scrollView

<ScrollView
android:id="@+id/ScrollView1"
android:layout_width="match_parent"
android:layout_height="wrap_content" 
...>

...