Layout occupying the entire screen

Asked

Viewed 916 times

1

I’m working on a project where I have a screen that contains some elements separated by different layout types. All these elements needed to be grouped within a single LinearLayout so that I could "envelop them" in a ScrollView. The problem is that one of these layouts is simply 'throwing' out of sight another, occupying all available space. The result is this:

Erro do além

Notice in the blue part that has a layout (in case, a RelativeLayout) smashed out of the screen. This layout should be fixed at the bottom of the screen, but if I change the type of the Linearlayout fill immediately above it, this is the result:

Tá certo mas tá errado

The buttons CANCEL and SAVE should be fixed at the bottom of the screen. This is the XML used in the layout:

<?xml version="1.0" encoding="utf-8"?>
<ScrollView 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/content_new_color"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:clipToPadding="false"
    android:fillViewport="true"
    android:scrollbarStyle="insideOverlay"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:context="com.ions.colorcodes.activities.NewColorActivity"
    tools:showIn="@layout/activity_new_color">

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

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            android:paddingBottom="@dimen/activity_vertical_margin"
            android:paddingLeft="@dimen/activity_horizontal_margin"
            android:paddingRight="@dimen/activity_horizontal_margin"
            android:paddingTop="@dimen/activity_vertical_margin">

            <android.support.design.widget.TextInputLayout
                android:id="@+id/input_layout_name"
                android:layout_width="match_parent"
                android:layout_height="wrap_content">

                <EditText
                    android:id="@+id/input_color_name"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:hint="@string/hint_color_name" />

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

            <android.support.design.widget.TextInputLayout
                android:id="@+id/input_layout_code"
                android:layout_width="match_parent"
                android:layout_height="wrap_content">

                <EditText
                    android:id="@+id/input_color_code"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:hint="@string/hint_color_code" />

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

            <android.support.v4.widget.Space
                android:layout_width="match_parent"
                android:layout_height="16dp" />

            <TextView
                android:id="@+id/info_preview"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="@string/info_preview" />

            <android.support.v4.widget.Space
                android:layout_width="match_parent"
                android:layout_height="8dp" />

            <LinearLayout
                android:id="@+id/card_preview_wrapper"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="vertical"
                android:weightSum="1">

                <android.support.v7.widget.CardView
                    android:id="@+id/card_preview"
                    android:layout_width="160dp"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center"
                    android:layout_margin="@dimen/card_margin"
                    android:elevation="4dp"
                    app:cardCornerRadius="@dimen/card_album_radius">

                    <RelativeLayout
                        android:id="@+id/preview_card"
                        android:layout_width="match_parent"
                        android:layout_height="match_parent">

                        <TextView
                            android:id="@+id/new_color_name"
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content"
                            android:layout_alignParentStart="true"
                            android:layout_below="@+id/new_color_preview"
                            android:paddingLeft="@dimen/album_title_padding"
                            android:paddingRight="@dimen/album_title_padding"
                            android:paddingTop="@dimen/album_title_padding"
                            android:textStyle="bold" />

                        <TextView
                            android:id="@+id/new_color_hex_code"
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content"
                            android:layout_alignParentStart="true"
                            android:layout_below="@+id/new_color_name"
                            android:paddingBottom="@dimen/songs_count_padding_bottom"
                            android:paddingLeft="@dimen/album_title_padding"
                            android:paddingRight="@dimen/album_title_padding" />

                        <ImageView
                            android:id="@+id/new_color_preview"
                            android:layout_width="match_parent"
                            android:layout_height="128dp"
                            android:layout_alignParentStart="true"
                            android:layout_alignParentTop="true"
                            android:background="#ffffff"
                            android:clickable="true"
                            android:scaleType="fitXY" />

                    </RelativeLayout>
                </android.support.v7.widget.CardView>
            </LinearLayout>
        </LinearLayout>

        <RelativeLayout
            android:id="@+id/preview_button_bar"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

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

                <Button
                    android:id="@+id/btn_cancel_new_color"
                    style="@style/Widget.AppCompat.Button.Borderless"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:text="@string/btn_cancel" />

                <Button
                    android:id="@+id/btn_save_new_color"
                    style="@style/Widget.AppCompat.Button.Borderless.Colored"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:text="@string/btn_save" />
            </LinearLayout>
        </RelativeLayout>
    </LinearLayout>
</ScrollView>

Where am I going wrong? I’m looking for solutions for a long time and can’t find anything that helps me.

2 answers

1

Try to leave the Buttons layout out of Scrollview +- like 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"
        xmlns:app="http://schemas.android.com/apk/res-auto" 
        android:layout_width="match_parent"
        android:layout_height="match_parent"         
        android:paddingLeft="@dimen/activity_horizontal_margin"
        android:paddingRight="@dimen/activity_horizontal_margin"
        android:paddingTop="@dimen/activity_vertical_margin"
        android:paddingBottom="@dimen/activity_vertical_margin"
        app:layout_behavior="@string/appbar_scrolling_view_behavior"
        tools:showIn="@layout/activity_segunda"
        tools:context="br.myapplication.SegundaActivity"
        android:orientation="vertical"
        android:weightSum="1">

<ScrollView
    android:layout_width="match_parent"
    android:layout_height="416dp"
    android:id="@+id/scrollView">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:paddingBottom="@dimen/activity_vertical_margin"
        android:paddingLeft="@dimen/activity_horizontal_margin"
        android:paddingRight="@dimen/activity_horizontal_margin"
        android:paddingTop="@dimen/activity_vertical_margin">

        <android.support.design.widget.TextInputLayout
            android:id="@+id/input_layout_name"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <EditText
                android:id="@+id/input_color_name"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:hint="Nome da Cor" />

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

        <android.support.design.widget.TextInputLayout
            android:id="@+id/input_layout_code"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <EditText
                android:id="@+id/input_color_code"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:hint="Código da Cor" />

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

        <android.support.v4.widget.Space
            android:layout_width="match_parent"
            android:layout_height="16dp" />

        <TextView
            android:id="@+id/info_preview"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Preview" />

        <android.support.v4.widget.Space
            android:layout_width="match_parent"
            android:layout_height="8dp" />

        <RelativeLayout
            android:id="@+id/card_preview_wrapper"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            android:weightSum="1">

            <android.support.v7.widget.CardView
                android:id="@+id/card_preview"
                android:layout_width="160dp"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:elevation="4dp"
                app:cardCornerRadius="5dp"
                android:layout_centerHorizontal="true">

                <RelativeLayout
                    android:id="@+id/preview_card"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent">

                    <TextView
                        android:id="@+id/new_color_name"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_alignParentStart="true"
                        android:layout_below="@+id/new_color_preview"
                        android:paddingLeft="10dp"
                        android:paddingRight="10dp"
                        android:paddingTop="10dp"
                        android:textStyle="bold" />

                    <TextView
                        android:id="@+id/new_color_hex_code"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_alignParentStart="true"
                        android:layout_below="@+id/new_color_name"
                        android:paddingBottom="10dp"
                        android:paddingLeft="10dp"
                        android:paddingRight="10dp" />

                    <ImageView
                        android:id="@+id/new_color_preview"
                        android:layout_width="match_parent"
                        android:layout_height="128dp"
                        android:layout_alignParentStart="true"
                        android:layout_alignParentTop="true"
                        android:background="#ffffff"
                        android:clickable="true"
                        android:scaleType="fitXY" />

                </RelativeLayout>
            </android.support.v7.widget.CardView>
        </RelativeLayout>
    </LinearLayout>
</ScrollView>

<LinearLayout
   android:layout_width="match_parent"
   android:layout_height="0dp"
   android:orientation="horizontal"
   android:layout_weight="0.51">
    <Button
        android:id="@+id/btn_cancel_new_color"
        style="@style/Widget.AppCompat.Button.Borderless"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="@string/btn_cancel" />

    <Button
        android:id="@+id/btn_save_new_color"
        style="@style/Widget.AppCompat.Button.Borderless.Colored"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="@string/btn_save" />
</LinearLayout>
</LinearLayout>

1

From what I understand, you want to put the buttons fixed at the bottom of the screen and the scrollview fill the remaining space.

You can do this as follows

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <ScrollView
        android:id="@+id/content_new_color"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:clipToPadding="false"
        android:fillViewport="true"
        android:scrollbarStyle="insideOverlay"
        app:layout_behavior="@string/appbar_scrolling_view_behavior"
        tools:context="com.ions.colorcodes.activities.NewColorActivity"
        tools:showIn="@layout/activity_new_color">

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

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="vertical"
                android:paddingBottom="@dimen/activity_vertical_margin"
                android:paddingLeft="@dimen/activity_horizontal_margin"
                android:paddingRight="@dimen/activity_horizontal_margin"
                android:paddingTop="@dimen/activity_vertical_margin">

                <android.support.design.widget.TextInputLayout
                    android:id="@+id/input_layout_name"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content">

                    <EditText
                        android:id="@+id/input_color_name"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:hint="@string/hint_color_name" />

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

                <android.support.design.widget.TextInputLayout
                    android:id="@+id/input_layout_code"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content">

                    <EditText
                        android:id="@+id/input_color_code"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:hint="@string/hint_color_code" />

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

                <android.support.v4.widget.Space
                    android:layout_width="match_parent"
                    android:layout_height="16dp" />

                <TextView
                    android:id="@+id/info_preview"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:text="@string/info_preview" />

                <android.support.v4.widget.Space
                    android:layout_width="match_parent"
                    android:layout_height="8dp" />

                <LinearLayout
                    android:id="@+id/card_preview_wrapper"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:orientation="vertical"
                    android:weightSum="1">

                    <android.support.v7.widget.CardView
                        android:id="@+id/card_preview"
                        android:layout_width="160dp"
                        android:layout_height="wrap_content"
                        android:layout_gravity="center"
                        android:layout_margin="@dimen/card_margin"
                        android:elevation="4dp"
                        app:cardCornerRadius="@dimen/card_album_radius">

                        <RelativeLayout
                            android:id="@+id/preview_card"
                            android:layout_width="match_parent"
                            android:layout_height="match_parent">

                            <TextView
                                android:id="@+id/new_color_name"
                                android:layout_width="match_parent"
                                android:layout_height="wrap_content"
                                android:layout_alignParentStart="true"
                                android:layout_below="@+id/new_color_preview"
                                android:paddingLeft="@dimen/album_title_padding"
                                android:paddingRight="@dimen/album_title_padding"
                                android:paddingTop="@dimen/album_title_padding"
                                android:textStyle="bold" />

                            <TextView
                                android:id="@+id/new_color_hex_code"
                                android:layout_width="match_parent"
                                android:layout_height="wrap_content"
                                android:layout_alignParentStart="true"
                                android:layout_below="@+id/new_color_name"
                                android:paddingBottom="@dimen/songs_count_padding_bottom"
                                android:paddingLeft="@dimen/album_title_padding"
                                android:paddingRight="@dimen/album_title_padding" />

                            <ImageView
                                android:id="@+id/new_color_preview"
                                android:layout_width="match_parent"
                                android:layout_height="128dp"
                                android:layout_alignParentStart="true"
                                android:layout_alignParentTop="true"
                                android:background="#ffffff"
                                android:clickable="true"
                                android:scaleType="fitXY" />

                        </RelativeLayout>
                    </android.support.v7.widget.CardView>
                </LinearLayout>
            </LinearLayout>


        </LinearLayout>
    </ScrollView>

    <RelativeLayout
        android:id="@+id/preview_button_bar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

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

            <Button
                android:id="@+id/btn_cancel_new_color"
                style="@style/Widget.AppCompat.Button.Borderless"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="@string/btn_cancel" />

            <Button
                android:id="@+id/btn_save_new_color"
                style="@style/Widget.AppCompat.Button.Borderless.Colored"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="@string/btn_save" />
        </LinearLayout>
    </RelativeLayout>
</LinearLayout>

Browser other questions tagged

You are not signed in. Login or sign up in order to post.