How can I set the focus in a ListView when the keyboard is shown?



I'm implementing a chat app. The views appear to resize without any problems, my EditText box and send Button appear on top of the keyboard, with the ListView on top of that. But the ListView doesn't scroll to maintain focus on the most recent message.


How can I set the ListView to maintain focus on the last list item when the keyboard is shown?


When I load the data initially and update the ListView when new messages arrive, I use ListView.setSelection(int) to set the focus to the most recent message. Is there a way I can do this in XML maintain that focus when the keyboard is shown? android:windowSoftInputMode="adjustResize" is set in the Manifest file.


Here's my XML layout file:



<RelativeLayout xmlns:android="http://ift.tt/nIICcg"
xmlns:tools="http://ift.tt/LrGmb4"
android:id="@+id/fragment_messages"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.chatapp.fragments.ConversationFragment" >

<ListView
android:id="@+id/messages_listview"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:background="@android:color/white"
android:divider="@android:color/transparent"
android:dividerHeight="0dp"
android:paddingBottom="5dp" />

<RelativeLayout
android:id="@+id/send_message_layout"
android:layout_width="match_parent"
android:layout_height="60dp"
android:layout_alignParentBottom="true" >

<Button
android:id="@+id/button_smilies"
style="?android:attr/buttonStyleSmall"
android:layout_width="60dp"
android:layout_height="match_parent"
android:layout_alignParentLeft="true"
android:background="@android:color/white"
android:radius="10dp"
android:text="+"
android:textColor="@color/Grey"
android:color="@color/Grey" />

<EditText
android:id="@+id/message_edittext"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:layout_toRightOf="@id/button_smilies"
android:background="@android:color/white"
android:ems="10"
android:inputType="textMultiLine"
android:radius="10dp"
android:shadowColor="@android:color/white"
android:textColor="@color/Grey"
android:color="@color/Grey" >
</EditText>

<Button
android:id="@+id/button_send"
style="?android:attr/buttonStyleSmall"
android:layout_width="60dp"
android:layout_height="match_parent"
android:layout_alignParentRight="true"
android:background="@android:color/white"
android:radius="10dp"
android:text="Send"
android:textColor="@color/Grey"
android:color="@color/Grey" />
</RelativeLayout>

</RelativeLayout>

No comments:

Post a Comment