d-man
d-man

Reputation: 58083

android scroll down

I have text area and and down to that "ok" and "cancel" button. when i click on text area keyboard appear and focus on the text area and buttons get hide behind the keyboard.

i want when text area get focus scroll a little bit and also display the bottom buttons while text area id selected.

Upvotes: 4

Views: 21540

Answers (3)

Dan B
Dan B

Reputation: 281

You could try wrapping your existing layout in a ScrollView:
https://developer.android.com/reference/android/widget/ScrollView.html
Your layout xml file might look like:

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/ScrollView01" android:layout_width="wrap_content" 
        android:layout_height="wrap_content">
    <LinearLayout 
        android:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent">
        <EditText android:text="@string/hello" android:id="@+id/EditText01" 
            android:layout_width="wrap_content" android:layout_height="wrap_content">
        </EditText>
        <LinearLayout 
            android:orientation="horizontal"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content">
            <Button android:text="@+id/Button01" android:id="@+id/Button01" 
                android:layout_width="wrap_content" android:layout_height="wrap_content">
            </Button>
            <Button android:text="@+id/Button02" android:id="@+id/Button02" 
                android:layout_width="wrap_content" android:layout_height="wrap_content">
            </Button>
        </LinearLayout>
    </LinearLayout>
</ScrollView>

Comment Response:
You might want to check out this page:
https://developer.android.com/training/keyboard-input/index.html

I think results you want might be accomplished by moving the buttons outside of the Scrollview and setting android:windowSoftInputMode="adjustResize" on the activity.

Upvotes: 4

Mario Velasco
Mario Velasco

Reputation: 3456

I would adjust few things from the solution:

1.- Put smoothScroll instead of hard scroll

2.- Decrease the delay from 1000 to 300 (enough)

    OnFocusChangeListener onFocus = new OnFocusChangeListener() {
        @Override
        public void onFocusChange(View v, boolean hasFocus) {
            new Handler().postDelayed(new Runnable() {
                @Override
                public void run() {
                    scrollView.smoothScrollTo(0, scrollView.getBottom());
                }
            },300);
        }
    };

Upvotes: 0

d-man
d-man

Reputation: 58083

i fixed it by myself :D following is the code

i called the following method when textfield gain the focus

private final void focusOnButtons(){
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                ScrollView sv = (ScrollView)findViewById(R.id.scrl);
                sv.scrollTo(0, sv.getBottom());
            }
        },1000);
    }

Upvotes: 9

Related Questions