Question

have an edittext inside scrollview. (and there are other views inside scrollview above edittext.)
When user presses the edittext, keyboard becames visible and the visible area of the scrollview becames small. Because of that edittext does not shown in the screen. (it does not scroll the scrollview so that edit text will be shown.)
After user presses any key in the keyboard, scrollview scrolls so that edittext becames visible again.

how can i force the scrollview to scroll so that edittext will be visible before user presses on the keyboard?

<ScrollView
    android:layout_width="fill_parent"
    android:layout_height="0dip"
    android:layout_weight="1" >

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_weight="1"
        android:orientation="vertical"
        android:paddingBottom="0dip"
        android:paddingLeft="0dip"
        android:paddingRight="0dip"
        android:paddingTop="0dip" >


        <include
            layout="@layout/trade_row_item_3cell" />

        <include
            android:id="@+id/rowQuantity"
            layout="@layout/trade_quantity_selection_row_layout" />

        <include
            layout="@layout/trade_row_item_3cell" />

        <include
            layout="@layout/trade_price_selection_et_row_layout" />

        <include
            layout="@layout/trade_row_item_3cell" />

        <include
            layout="@layout/trade_row_item_3cell" />

       <Button
            style="@style/CustomButtonText"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal"
            android:layout_weight="0" />
    </LinearLayout>
</ScrollView>

<include
    layout="@layout/trade_bottom_view" />

Here is the layout(trade_quantity_selection_row_layout) which contains the edittext

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="50dip"
android:layout_gravity="left"
android:background="@android:drawable/list_selector_background">
<TextView 
    android:id="@+id/tvLeft"
    android:layout_width="0dip"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:singleLine="true"
    android:textColor="#000000"
    android:paddingLeft="10dip"
    android:gravity="center_vertical"
    android:layout_gravity="center_vertical"/>
<EditText 
    android:id="@+id/etRight"
    android:paddingLeft="10dip"
    android:background="@null"
    android:layout_width="0dip"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:inputType="number"
    android:gravity="center_vertical"
    android:layout_gravity="center_vertical"/>
<ImageView
    android:id="@+id/ivRight"
    android:src="@drawable/proceed"
    android:paddingRight="10dip"
     android:layout_weight="0"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center_vertical"/>    

trade_row_item_3cell layout contains linearlayout with edittexts.

Was it helpful?

Solution

First get the x/y coordinates of your Edittext using View.getLocationOnScreen (int[] location) ( After the method returns, the array contains the x and y location in that order)

OR

This links helps you find the views coordinates - https://stackoverflow.com/a/2761184/28557

Then

scrollView.scrollTo(x, y);

hope this helps

OTHER TIPS

simply do this:

scrollView.smoothScrollTo(yourEditTextView.getLeft(), yourEditTextView.getTop())

or if you want this to happen automatically on focus:

yourEditTextView.onFocusChangeListener = View.OnFocusChangeListener { view, hasFocus ->
            if (hasFocus) scrollView.smoothScrollTo(view.left, view.top)
        }

java code

EditText dwEdit = (EditText) findViewById(R.id.DwEdit);       
 dwEdit.setOnTouchListener(new OnTouchListener() {
       public boolean onTouch(View view, MotionEvent event) {
            // TODO Auto-generated method stub
            if (view.getId() ==R.id.DwEdit) {
                view.getParent().requestDisallowInterceptTouchEvent(true);
                switch (event.getAction()&MotionEvent.ACTION_MASK){
                case MotionEvent.ACTION_UP:
                    view.getParent().requestDisallowInterceptTouchEvent(false);
                    break;
                }
            }
            return false;
        }
    });

xml code

 <EditText
android:id="@+id/DwEdit"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:minLines="10"
android:scrollbarStyle="insideInset"
android:scrollbars="vertical" 
android:overScrollMode="always"
android:inputType="textCapSentences">
</EditText> 
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top