Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Show last item in ListiView when you click on edit text

Tags:

android

currently I'm doing this to get the focus of the last item in a listview after clicking on an edit text:

    bodyText.setOnFocusChangeListener(new OnFocusChangeListener() {

        public void onFocusChange(View v, boolean hasFocus) {               
            getListView().setSelection(getListView().getCount());
        }
    });

    bodyText.setOnClickListener(new View.OnClickListener() {

        public void onClick(View view) {
            getListView().setSelection(getListView().getCount());
        }
    });

It works great and grabs the last item, problem is the soft keypad appears and covers the last 3 items in the listview. Is there a way to show the last item AFTER the soft keypad appears and resizes the screen? Thanks!

like image 904
Maurice Avatar asked Mar 16 '11 06:03

Maurice


1 Answers

I made my own Custom ListView like this:

public class CustomListView extends ListView {

public CustomListView (Context context) {
    super(context);
}

public CustomListView (Context context, AttributeSet attrs) {
    super(context, attrs);
}

public CustomListView (Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
}

@Override
protected void onSizeChanged(int xNew, int yNew, int xOld, int yOld) {
    super.onSizeChanged(xNew, yNew, xOld, yOld);

    setSelection(getCount());

}
}

You must include all 3 constructors or an exception will be thrown.

Then in the XML where you usually put

ListView

android:id="@+id/android:list"
android:layout_width="fill_parent" 
android:layout_height="fill_parent"

Change ListView to com.blah.blah.CustomListView.

Sorry for the messy layout, for some reason I can't get the formatter to properly work.

Run your application. Now when you click on the EditText, it shows the last item AFTER the soft keyboard shows! Note that there are some limitations like when the auto-complete function appears when you type text in the EditText, it will show the last item as well due to a change in size of the ListView.

Enjoy!

like image 188
Maurice Avatar answered Sep 28 '22 03:09

Maurice