pavelkorolevxyz
pavelkorolevxyz

Reputation: 1500

ScrollView padding ignored on keyboard show

Let's say I have this simple layout:

https://i.sstatic.net/QM9i7.jpg

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

    <ScrollView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:paddingBottom="80dp"
            android:clipToPadding="false">

        <LinearLayout
                android:id="@+id/container"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="vertical"
                android:padding="16dp">

            <ImageView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:src="@mipmap/ic_launcher" />

            ...


            <EditText
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:hint="Eighth" />

        </LinearLayout>

    </ScrollView>

    <Button
            android:id="@+id/button"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:layout_centerHorizontal="true"
            android:layout_margin="16dp"
            android:text="Button" />

</RelativeLayout>

Note there is paddingBottom on ScrollView with clipToPadding=false. It's needed so Button looks like floating button over scroll view content. ScrollView padding is used to make space below content to make last child available.

If the last child is EditText I expect ScrollView to scroll making EditText visible over software keyboard. But it ends with this https://i.sstatic.net/3hwNy.jpg

Kind of expected behavior can be achieved using layout_marginBottom instead of paddingBottom, but in this case obviously I can't see my content behind Button. Screenshot https://i.sstatic.net/KPPwy.jpg

Is there a way to make ScrollView to respect its paddings in terms of keyboard avoiding?

UPDATE: full xml code here https://pastebin.com/P8n0aZ2i

Upvotes: 6

Views: 1101

Answers (4)

Dheeraj Upadhyay
Dheeraj Upadhyay

Reputation: 328

I don't know very much about android, but as Java Programmer I would like to suggest you to change your scrollView size in Activity class. Please put Your scrollView into some other CordinatorLayout and put below code in init() or onCreate() method

 protected void onCreate(Bundle savedInstanceState) {
   
    mySwipeRefreshLayout.setOnRefreshListener(
            new SwipeRefreshLayout.OnRefreshListener() {
                @Override
                public void onRefresh() {
                  scrollView.setMinHeight(layoutComposite.computeSize(layoutComposite.getSize().x+100 , SWT.DEFAULT).y+100);
				
                }
            }
    );
}

Call above code in activity refresh/onload/onCreate

Intent intent=new Intent(Current_Activity.this,Current_Activity.class);
    startActivity(intent);
    finish();

refer this link Scroll Implementation

Upvotes: 0

JavaGhost
JavaGhost

Reputation: 406

Use CoordinatorLayout instead of RelativeLayout as below:

<?xml version="1.0" encoding="utf-8"?>
<CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

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

    <LinearLayout
            android:id="@+id/container"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            android:padding="16dp">

        <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:src="@mipmap/ic_launcher" />

        ...


        <EditText
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:hint="Eighth" />

    </LinearLayout>

</ScrollView>

<Button
        android:id="@+id/button"
        android:layout_width="match_parent"
        app:layout_anchorGravity=”bottom|end”
        android:layout_margin="16dp"
        app:layout_anchor=”@id/scroll_container” 
        app:layout_dodgeInsetEdges="bottom"
        android:text="Button" />

</CoordinatorLayout>

I have not tested this, but along with setting android:windowSoftInputMode="adjustResize|adjustPan" for activity tag in manifest, it should work as you want.

Upvotes: 0

swati vishnoi
swati vishnoi

Reputation: 121

you just need to define soft-Input adjustment type in manifest for your activity . You can add following line in your manifest.

 <activity android:name=".Main2Activity" android:theme="@style/BaseTheme_FullScreen2"
              android:windowSoftInputMode="adjustResize|adjustPan">
    </activity>

Upvotes: 5

Quick learner
Quick learner

Reputation: 11457

If you want to achieve scrolling when keyboard is open you have to adjust the widgets in order that they do not overcome each other.

Add android:layout_above="@+id/button" in the scrollview widget , that way it will adjust itself over button when keyboard is showing.

Code

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:paddingBottom="80dp"
        android:layout_above="@+id/button"
        android:clipToPadding="false">

        <LinearLayout
            android:id="@+id/container"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            android:padding="16dp">

            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:src="@mipmap/ic_launcher" />

            ...


            <EditText
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:hint="Eighth" />

        </LinearLayout>

    </ScrollView>

    <Button
        android:id="@+id/button"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:layout_margin="16dp"
        android:text="Button" />

</RelativeLayout>

enter image description here

Upvotes: 0

Related Questions