edittext inside scrollview, how to scroll to edittext when it gets the focus

e13420xx picture e13420xx · Apr 18, 2012 · Viewed 10.5k times · Source

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.

Answer

Vinayak Bevinakatti picture Vinayak Bevinakatti · Apr 18, 2012

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