J. Natael J. Natael - 4 months ago 62
Android Question

Android Relative Layout is running off screen

I have a problem in which a relative layout seems to be running off the screen in the emulator but showing properly in the designer.

I have a LinearLayout and a button both inside a RelativeLayout. The LinearLayout contains some TextViews and then a ScrollView with radio buttons (number to be dynamically allocated).

Putting the LinearLayout and the button in the RelativeLayout gets the button to stay at the screen bottom regardless of radio button count. This allows me to anchor the button to the bottom of the screen and the bottom of the LinearLayout to the top of the button.

In the Android Studio designer this works great! The button is at the bottom, and if the radios extend beyond the screen you can scroll them. When I run the emulator though the screen gets cut off before the button! I've tried this in both the Microsoft and Android (so slow) emulators and gotten the same result.

I found a similar question at Android: Relative layout content goes below screen which pinpointed adding this line to my button:

android:layout_marginBottom="@dimen/activity_horizontal_margin"


That brought it partway back on screen but not all the way, and the rest of that thread is about removing the LinearLayout which I want to keep in this use case. Seems something more is going on here. What am I doing wrong? Here's my XML:

<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:paddingBottom="16dp"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.complexityresolved.lifetracker.RadioInputsActivity$PlaceholderFragment"
android:gravity="center_horizontal">

<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"
tools:context="com.complexityresolved.lifetracker.RadioInputsActivity$PlaceholderFragment"
android:orientation="vertical"
android:layout_centerHorizontal="true"
android:layout_above="@+id/radioInputsActivityDoneButton">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Text With Trait Name"
android:id="@+id/RadioScreenTextViewTraitName"
android:layout_gravity="center_horizontal"
android:clickable="true" />

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="Text With Short Description"
android:id="@+id/RadioScreenTextViewShortDescription"
android:layout_gravity="center_horizontal"
android:visibility="gone" />

<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/scrollView">
<RadioGroup
android:layout_width="match_parent"
android:layout_height="wrap_content">
<RadioButton android:id="@+id/radio_pirates"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="pirates"/>
<RadioButton android:id="@+id/radio_ninjas"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="ninjas"/>
<RadioButton android:id="@+id/radio_irates"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="pirates"/>
<RadioButton android:id="@+id/radio_injas"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="ninjas"/>
<RadioButton android:id="@+id/radio_rates"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="pirates"/>
<RadioButton android:id="@+id/radio_njas"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="ninjas"/>
<RadioButton android:id="@+id/radio_ates"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="pirates"/>
<RadioButton android:id="@+id/radio_jas"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="ninjas"/>
<RadioButton android:id="@+id/adio_pirates"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="pirates"/>
<RadioButton android:id="@+id/adio_ninjas"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="ninjas"/>
<RadioButton android:id="@+id/dio_pirates"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="pirates"/>
<RadioButton android:id="@+id/dio_ninjas"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="ninjas"/>
<RadioButton android:id="@+id/raio_pirates"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="pirates"/>
<RadioButton android:id="@+id/raio_ninjas"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="ninjas"/>
<RadioButton android:id="@+id/rao_pirates"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="pirates"/>
<RadioButton android:id="@+id/rao_ninjas"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="ninjas"/>
</RadioGroup>
</ScrollView>

</LinearLayout>

<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/radioInputsActivityDoneButton"
android:id="@+id/radioInputsActivityDoneButton"
android:layout_centerHorizontal="true"
android:layout_alignParentBottom="true"
android:layout_marginBottom="@dimen/activity_horizontal_margin"/>
</RelativeLayout>


Picture in the designer

Picture in the emulator

Also, my activity layout as requested:

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/main_content"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context="com.complexityresolved.lifetracker.RadioInputsActivity">

<android.support.design.widget.AppBarLayout
android:id="@+id/appbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="@dimen/appbar_padding_top"
android:theme="@style/AppTheme.AppBarOverlay">

<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:layout_scrollFlags="scroll|enterAlways"
app:popupTheme="@style/AppTheme.PopupOverlay">

</android.support.v7.widget.Toolbar>

</android.support.design.widget.AppBarLayout>

<android.support.v4.view.ViewPager
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior" />

</android.support.design.widget.CoordinatorLayout>

Answer

Try this:-

I have tested your you, i saw the issue that you mentioned above. I have updated your code ande posted below. It is working good.

In your Activity.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/main_content"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"      tools:context="com.complexityresolved.lifetracker.RadioInputsActivity">

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

        <android.support.design.widget.AppBarLayout
            android:id="@+id/appbar"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:paddingTop="@dimen/appbar_padding_top"
            android:theme="@style/AppTheme.AppBarOverlay">

            <android.support.v7.widget.Toolbar
                android:id="@+id/toolbar"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                android:background="?attr/colorPrimary"
                app:layout_scrollFlags="scroll|enterAlways"
                app:popupTheme="@style/AppTheme.PopupOverlay">

            </android.support.v7.widget.Toolbar>

        </android.support.design.widget.AppBarLayout>

        <android.support.v4.view.ViewPager
            android:id="@+id/container"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:layout_behavior="@string/appbar_scrolling_view_behavior" />

    </LinearLayout>
</android.support.design.widget.CoordinatorLayout>