Automatically scroll down an android layout after click event

Asked

Viewed 141 times

1

Is it possible to automatically scroll down the layout after a click event? I have this layout that displays a result when the user fills the data and clicks, but I’m afraid that on devices with smaller screen the result is hidden

Activity

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

<androidx.appcompat.widget.Toolbar
    android:id="@+id/barra"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="#607d8b"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:title=""
    app:titleMarginStart="10dp"
    app:titleTextColor="#fff" />

<ScrollView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_above="@+id/adView"
    android:layout_below="@+id/barra"
    android:layout_marginBottom="0dp"
    android:fillViewport="true">

    <androidx.constraintlayout.widget.ConstraintLayout
        android:id="@+id/relativeLayout2"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#f6f6f6">

        <TextView
            android:id="@+id/textView"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginStart="16dp"
            android:layout_marginLeft="16dp"
            android:layout_marginTop="12dp"
            android:layout_marginEnd="16dp"
            android:layout_marginRight="16dp"
            android:text="Preço Álcool:"
            android:textAppearance="?android:attr/textAppearanceMedium"
            ads:layout_constraintTop_toTopOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            tools:layout_conversion_wrapHeight="63"
            tools:layout_conversion_wrapWidth="325" />

        <EditText
            android:id="@+id/editText"
            android:layout_width="match_parent"
            android:layout_height="45dp"
            android:layout_marginStart="16dp"
            android:layout_marginLeft="16dp"
            android:layout_marginTop="10dp"
            android:layout_marginEnd="16dp"
            android:layout_marginRight="16dp"
            android:background="@drawable/circulo"
            android:ems="10"
            android:inputType="numberDecimal"
            android:paddingLeft="10dp"
            android:paddingRight="10dp"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/textView"
            tools:layout_conversion_wrapHeight="118"
            tools:layout_conversion_wrapWidth="0" />

        <TextView
            android:id="@+id/textView2"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginStart="16dp"
            android:layout_marginLeft="16dp"
            android:layout_marginTop="10dp"
            android:layout_marginEnd="16dp"
            android:layout_marginRight="16dp"
            android:text="Preço Gasolina:"
            android:textAppearance="?android:attr/textAppearanceMedium"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/editText"
            tools:layout_conversion_wrapHeight="63"
            tools:layout_conversion_wrapWidth="277" />

        <EditText
            android:id="@+id/editText2"
            android:layout_width="match_parent"
            android:layout_height="45dp"
            android:layout_marginStart="16dp"
            android:layout_marginLeft="16dp"
            android:layout_marginTop="10dp"
            android:layout_marginEnd="16dp"
            android:layout_marginRight="16dp"
            android:background="@drawable/circulo"
            android:ems="10"
            android:inputType="numberDecimal"
            android:paddingLeft="10dp"
            android:paddingRight="10dp"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/textView2"
            tools:layout_conversion_wrapHeight="118"
            tools:layout_conversion_wrapWidth="0" />

        <TextView
            android:id="@+id/textView6"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginStart="16dp"
            android:layout_marginLeft="16dp"
            android:layout_marginTop="7dp"
            android:layout_marginEnd="16dp"
            android:layout_marginRight="16dp"
            android:text="Cálculo baseado na agência nacional de petróleo que só compensa abastecer com álcool quando o valor do mesmo represente no máximo 70% do valor da gasolina. "
            android:textAppearance="?android:attr/textAppearanceSmall"
            android:textSize="12sp"
            ads:layout_constraintHorizontal_bias="0.0"
            ads:layout_constraintTop_toBottomOf="@+id/linearLayoutResultado"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            tools:layout_conversion_wrapHeight="1878"
            tools:layout_conversion_wrapWidth="0" />

        <TextView
            android:id="@+id/textSepara2"
            android:layout_width="match_parent"
            android:layout_height="2dp"
            android:layout_marginStart="16dp"
            android:layout_marginLeft="16dp"
            android:layout_marginTop="8dp"
            android:layout_marginEnd="16dp"
            android:layout_marginRight="16dp"
            android:text=""
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/textView6" />

        <LinearLayout
            android:id="@+id/linearLayout"
            android:layout_width="match_parent"
            android:layout_height="52dp"
            android:layout_marginStart="16dp"
            android:layout_marginLeft="16dp"
            android:layout_marginTop="13dp"
            android:layout_marginEnd="16dp"
            android:layout_marginRight="16dp"
            android:orientation="horizontal"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/editText2">

            <Button
                android:id="@+id/Calcular"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:onClick="calculo"
                android:text="Calcular" />

            <Button
                android:id="@+id/Limpar"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:onClick="limpa"
                android:text="Limpar" />
        </LinearLayout>

        <LinearLayout
            android:id="@+id/linearLayoutResultado"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            android:visibility="gone"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/linearLayout">

            <androidx.constraintlayout.widget.ConstraintLayout
                android:id="@+id/relativeLayoutResultado"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:background="#f6f6f6">

                <View
                    android:id="@+id/viewResultado"
                    android:layout_width="0dp"
                    android:layout_height="0dp"
                    android:layout_marginStart="16dp"
                    android:layout_marginLeft="16dp"
                    android:layout_marginTop="13dp"
                    android:layout_marginEnd="16dp"
                    android:layout_marginRight="16dp"
                    android:layout_marginBottom="5dp"
                    android:background="@drawable/circulo"
                    ads:layout_constraintBottom_toBottomOf="parent"
                    ads:layout_constraintEnd_toEndOf="parent"
                    ads:layout_constraintHorizontal_bias="0.0"
                    ads:layout_constraintStart_toStartOf="parent"
                    ads:layout_constraintTop_toTopOf="parent" />

                <TextView
                    android:id="@+id/txtParte1"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginStart="10dp"
                    android:layout_marginLeft="10dp"
                    android:layout_marginTop="8dp"
                    android:text="Valores do Cálculo"
                    android:textColor="#000"
                    android:textSize="16sp"
                    ads:layout_constraintStart_toStartOf="@+id/viewResultado"
                    ads:layout_constraintTop_toBottomOf="@+id/txtEstimativa" />

                <TextView
                    android:id="@+id/txtGasolina"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginStart="10dp"
                    android:layout_marginLeft="10dp"
                    android:layout_marginTop="5dp"
                    android:text="Preço Álcool"
                    android:textSize="12sp"
                    ads:layout_constraintStart_toStartOf="@+id/viewResultado"
                    ads:layout_constraintTop_toBottomOf="@+id/txtParte1" />

                <TextView
                    android:id="@+id/txtAlcool"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginStart="10dp"
                    android:layout_marginLeft="10dp"
                    android:layout_marginTop="2dp"
                    android:text="Preço Gasolina"
                    android:textSize="12sp"
                    ads:layout_constraintStart_toStartOf="@+id/viewResultado"
                    ads:layout_constraintTop_toBottomOf="@+id/txtGasolina" />

                <TextView
                    android:id="@+id/txtRelacao"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginStart="10dp"
                    android:layout_marginLeft="10dp"
                    android:layout_marginTop="2dp"
                    android:layout_marginBottom="10dp"
                    android:text="Relação ÁLCOOL/GASOLINA:"
                    android:textSize="12sp"
                    ads:layout_constraintBottom_toBottomOf="@+id/viewResultado"
                    ads:layout_constraintStart_toStartOf="@+id/viewResultado"
                    ads:layout_constraintTop_toBottomOf="@+id/txtAlcool" />

                <TextView
                    android:id="@+id/txtResu1"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_marginStart="8dp"
                    android:layout_marginLeft="8dp"
                    android:layout_marginEnd="10dp"
                    android:layout_marginRight="10dp"
                    android:text="R$: 0,00"
                    android:textSize="12sp"
                    ads:layout_constraintEnd_toEndOf="@+id/viewResultado"
                    ads:layout_constraintStart_toEndOf="@+id/txtRelacao"
                    ads:layout_constraintTop_toTopOf="@+id/txtGasolina" />

                <TextView
                    android:id="@+id/txtResu2"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_marginEnd="10dp"
                    android:layout_marginRight="10dp"
                    android:text="R$: 0,00"
                    android:textSize="12sp"
                    ads:layout_constraintEnd_toEndOf="@+id/viewResultado"
                    ads:layout_constraintStart_toStartOf="@+id/txtResu1"
                    ads:layout_constraintTop_toTopOf="@+id/txtAlcool" />

                <TextView
                    android:id="@+id/txtResu3"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_marginEnd="10dp"
                    android:layout_marginRight="10dp"
                    android:text="0%"
                    android:textSize="12sp"
                    ads:layout_constraintEnd_toEndOf="@+id/viewResultado"
                    ads:layout_constraintStart_toStartOf="@+id/txtResu2"
                    ads:layout_constraintTop_toTopOf="@+id/txtRelacao" />

                <TextView
                    android:id="@+id/textView14"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginStart="10dp"
                    android:layout_marginLeft="10dp"
                    android:layout_marginTop="15dp"
                    android:text="Abastecendo..."
                    android:textColor="#000"
                    android:textSize="16sp"
                    ads:layout_constraintStart_toStartOf="@+id/viewResultado"
                    ads:layout_constraintTop_toBottomOf="@+id/view2" />

                <SeekBar
                    android:id="@+id/seekBar"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_marginStart="10dp"
                    android:layout_marginLeft="10dp"
                    android:layout_marginTop="5dp"
                    android:layout_marginEnd="10dp"
                    android:layout_marginRight="10dp"
                    android:max="210"
                    android:padding="0dp"
                    android:progress="100"
                    android:progressTint="#16a085"
                    android:thumbTint="#16a085"
                    ads:layout_constraintEnd_toStartOf="@+id/textView15"
                    ads:layout_constraintStart_toStartOf="@+id/viewResultado"
                    ads:layout_constraintTop_toBottomOf="@+id/textView14" />

                <TextView
                    android:id="@+id/txtEstimativa"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_marginStart="10dp"
                    android:layout_marginLeft="10dp"
                    android:layout_marginTop="5dp"
                    android:layout_marginEnd="10dp"
                    android:layout_marginRight="10dp"
                    android:text="Seu carro fará aproximadamente 0km no álcool ou 0km na gasolina."
                    android:textSize="15sp"
                    ads:layout_constraintEnd_toEndOf="@+id/viewResultado"
                    ads:layout_constraintStart_toStartOf="@+id/viewResultado"
                    ads:layout_constraintTop_toBottomOf="@+id/seekBar" />

                <TextView
                    android:id="@+id/txtValorEstima"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="4dp"
                    android:layout_marginEnd="10dp"
                    android:layout_marginRight="10dp"
                    android:text="100"
                    android:textSize="15sp"
                    ads:layout_constraintEnd_toEndOf="@+id/viewResultado"
                    ads:layout_constraintTop_toBottomOf="@+id/textView14" />

                <View
                    android:id="@+id/view2"
                    android:layout_width="0dp"
                    android:layout_height="125dp"
                    android:background="@drawable/circulotop"
                    ads:layout_constraintEnd_toEndOf="@+id/viewResultado"
                    ads:layout_constraintStart_toStartOf="@+id/viewResultado"
                    ads:layout_constraintTop_toTopOf="@+id/viewResultado" />

                <ImageView
                    android:id="@+id/imageView4"
                    android:layout_width="70dp"
                    android:layout_height="70dp"
                    android:layout_marginTop="3dp"
                    ads:layout_constraintEnd_toEndOf="@+id/view2"
                    ads:layout_constraintStart_toStartOf="@+id/view2"
                    ads:layout_constraintTop_toTopOf="@+id/viewResultado"
                    ads:srcCompat="@mipmap/suv" />

                <TextView
                    android:id="@+id/txtResultado"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="Sua melhor opção é:"
                    android:textColor="#ffffff"
                    android:textSize="15sp"
                    ads:layout_constraintEnd_toEndOf="@+id/view2"
                    ads:layout_constraintStart_toStartOf="@+id/view2"
                    ads:layout_constraintTop_toBottomOf="@+id/imageView4" />

                <TextView
                    android:id="@+id/Resultado"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginBottom="5dp"
                    android:text="GASOLINA"
                    android:textColor="#ffffff"
                    android:textSize="17sp"
                    ads:layout_constraintBottom_toBottomOf="@+id/view2"
                    ads:layout_constraintEnd_toEndOf="@+id/view2"
                    ads:layout_constraintStart_toStartOf="@+id/view2"
                    ads:layout_constraintTop_toBottomOf="@+id/txtResultado" />

                <TextView
                    android:id="@+id/textView15"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="4dp"
                    android:text="R$: "
                    android:textSize="15sp"
                    ads:layout_constraintEnd_toStartOf="@+id/txtValorEstima"
                    ads:layout_constraintTop_toBottomOf="@+id/textView14" />

            </androidx.constraintlayout.widget.ConstraintLayout>
        </LinearLayout>


    </androidx.constraintlayout.widget.ConstraintLayout>
</ScrollView>

<com.google.android.gms.ads.AdView
    android:id="@+id/adView"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:background="#333333"
    android:gravity="bottom"
    ads:adSize="SMART_BANNER"
    ads:adUnitId="0000000" />

I would like that if the person is on a device with a small screen and the bottom of the layout that displays the result is hidden, automatically after clicking is focused on the linear layout with the id "@+id/linearLayoutResulted" making the layout scroll down to be visible the result, this is possible?

1 answer

0

Enter an id on ScrollView, and right after create an "event" post. When you do the action click on LinearLayout perform the event, as in the example below:

mScrollView.post(new Runnable() { 
        public void run() { 
             mScrollView.fullScroll(mScrollView.FOCUS_DOWN);
        } 
});

mScrollView is the id of Scrollview.

Browser other questions tagged

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