Does the keyboard screen close the screen instead of pushing the layout?

I have a problem: when I click on my keyboard, the contents of my screen do not push - rather, the keyboard just overlaps everything (namely, covers my ListView ) on my screen. This is the structure of my layout:

 <ScrollView> <RelativeLayout> </RelativeLayout> </ScrollView> <ListView> </ListView> <LinearLayout> <EditText></EditText> </LinearLayout> 

I ScrollView RelativeLayout in RelativeLayout in ScrollView because I thought that it would make my whole layout scrollable, that would β€œpush” all layouts so that the user could view these parts while entering his answer. I also have in my AndroidManifest.xml: android:windowSoftInputMode="adjustResize|stateVisible|stateAlwaysHidden" Here is an image of what is happening:

Image of what is happening

And here is my code. Any help would be appreciated!

 <?xml version="1.0" encoding="utf-8"?> <android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:fitsSystemWindows="true" xmlns:fresco="http://schemas.android.com/tools" android:id="@+id/comments_coordinator_layout"> <android.support.design.widget.AppBarLayout android:layout_height="wrap_content" android:layout_width="match_parent" android:theme="@style/AppTheme.AppBarOverlay"> <android.support.v7.widget.Toolbar android:id="@+id/comments_appbar" android:layout_width="match_parent" android:layout_height="?attr/actionBarSize" android:background="?attr/colorPrimary" app:popupTheme="@style/AppTheme.PopupOverlay" /> </android.support.design.widget.AppBarLayout> <ScrollView android:layout_width="match_parent" android:layout_height="match_parent" android:layout_below="@id/comments_coordinator_layout" > <RelativeLayout android:layout_marginTop="?attr/actionBarSize" android:id="@+id/view_post" android:layout_width="match_parent" android:paddingRight="5dp" android:paddingLeft="5dp" android:orientation="horizontal" android:layout_height="175dp" android:background="#e6e6e6"> <com.facebook.drawee.view.SimpleDraweeView android:layout_marginTop="15dp" android:id="@+id/poster_picture" android:layout_width="75dp" android:layout_height="75dp" android:layout_marginLeft="10dp" fresco:placeholderImage="@mipmap/blank_prof_pic" fresco:roundedCornerRadius="5dp" /> <TextView android:layout_marginLeft="5dp" android:layout_marginTop="15dp" android:layout_toRightOf="@id/poster_picture" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="15sp" android:textStyle="bold" android:id="@+id/poster_name"/> <TextView android:layout_alignParentRight="true" android:layout_marginTop="15dp" android:layout_toRightOf="@id/poster_name" android:layout_marginLeft="5dp" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="15sp" android:id="@+id/post_date"/> <TextView android:layout_marginLeft="5dp" android:layout_toRightOf="@id/poster_picture" android:layout_below="@id/poster_name" android:textSize="20sp" android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/view_status" /> </RelativeLayout> </ScrollView> <RelativeLayout android:layout_width="match_parent" android:layout_height="match_parent"> <ListView android:layout_marginTop="225dp" android:id="@+id/lv_comments_feed" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_above="@+id/send_message"> </ListView> <LinearLayout android:id="@+id/send_message" android:layout_width="match_parent" android:layout_height="wrap_content" android:padding="5dp" android:layout_alignParentBottom="true" android:orientation="horizontal" > <EditText android:id="@+id/write_comment" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_gravity="center_vertical|center_horizontal" android:layout_weight="5" android:gravity="top|left" android:hint="Comment back!" android:inputType="textMultiLine" android:scrollHorizontally="false" /> <Button android:id="@+id/send_comment" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_vertical|center_horizontal" android:gravity="center" android:text="send" android:textAppearance="?android:attr/textAppearanceMedium" /> </LinearLayout> </RelativeLayout> </android.support.design.widget.CoordinatorLayout> 

EDIT: My attempt to add a LinearLayout parent to ScrollView . Problem now: The vertically scrolling ScrollView should not contain another vertically scrolling widget when hovering over a ListView

 <?xml version="1.0" encoding="utf-8"?> <android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent" android:layout_height="match_parent" android:fitsSystemWindows="true" xmlns:fresco="http://schemas.android.com/tools" android:id="@+id/comments_coordinator_layout"> <android.support.design.widget.AppBarLayout android:layout_height="wrap_content" android:layout_width="match_parent" android:theme="@style/AppTheme.AppBarOverlay"> <android.support.v7.widget.Toolbar android:id="@+id/comments_appbar" android:layout_width="match_parent" android:layout_height="?attr/actionBarSize" android:background="?attr/colorPrimary" app:popupTheme="@style/AppTheme.PopupOverlay" /> </android.support.design.widget.AppBarLayout> <ScrollView android:layout_width="match_parent" android:layout_height="match_parent" android:layout_below="@id/comments_coordinator_layout" > <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical"> <RelativeLayout android:layout_marginTop="?attr/actionBarSize" android:id="@+id/view_post" android:layout_width="match_parent" android:paddingRight="5dp" android:paddingLeft="5dp" android:orientation="horizontal" android:layout_height="175dp" android:background="#e6e6e6"> <com.facebook.drawee.view.SimpleDraweeView android:layout_marginTop="15dp" android:id="@+id/poster_picture" android:layout_width="75dp" android:layout_height="75dp" android:layout_marginLeft="10dp" fresco:placeholderImage="@mipmap/blank_prof_pic" fresco:roundedCornerRadius="5dp" /> <TextView android:layout_marginLeft="5dp" android:layout_marginTop="15dp" android:layout_toRightOf="@id/poster_picture" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="15sp" android:textStyle="bold" android:id="@+id/poster_name"/> <TextView android:layout_alignParentRight="true" android:layout_marginTop="15dp" android:layout_toRightOf="@id/poster_name" android:layout_marginLeft="5dp" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="15sp" android:id="@+id/post_date"/> <TextView android:layout_marginLeft="5dp" android:layout_toRightOf="@id/poster_picture" android:layout_below="@id/poster_name" android:textSize="20sp" android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/view_status" /> </RelativeLayout> <RelativeLayout android:layout_width="match_parent" android:layout_height="match_parent"> <ListView android:layout_marginTop="225dp" android:id="@+id/lv_comments_feed" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_above="@+id/send_message"> </ListView> <LinearLayout android:id="@+id/send_message" android:layout_width="match_parent" android:layout_height="wrap_content" android:padding="5dp" android:layout_alignParentBottom="true" android:orientation="horizontal" > <EditText android:id="@+id/write_comment" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_gravity="center_vertical|center_horizontal" android:layout_weight="5" android:gravity="top|left" android:hint="Comment back!" android:inputType="textMultiLine" android:scrollHorizontally="false" /> <Button android:id="@+id/send_comment" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_vertical|center_horizontal" android:gravity="center" android:text="send" android:textAppearance="?android:attr/textAppearanceMedium" /> </LinearLayout> </RelativeLayout> </LinearLayout> </ScrollView> </android.support.design.widget.CoordinatorLayout> 

EDIT: I think the best solution would be to create a custom ListView ... that contains the same layout that is in every ListView element. Any suggestions or ideas on how to implement this?

EDIT: Tried to answer @VanillaBoy. I know that the created image is due to the fact that I have marginTop = 225 dp , but even if I delete it when I click on my EditText , it only shows my ListView and my RelativeLayout above is hidden Picture

EDIT: according to @Fllo's answers, I can still reproduce the error I had at the beginning with this layout, and here is what I have in my layout. Also, if this helps my Android manifest to look like this: android:windowSoftInputMode="adjustResize|stateAlwaysHidden"> .

 <?xml version="1.0" encoding="utf-8"?> <android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent" android:layout_height="match_parent" android:fitsSystemWindows="true" xmlns:fresco="http://schemas.android.com/tools" android:id="@+id/comments_coordinator_layout"> <android.support.design.widget.AppBarLayout android:layout_height="wrap_content" android:layout_width="match_parent" android:theme="@style/AppTheme.AppBarOverlay"> <android.support.v7.widget.Toolbar android:id="@+id/comments_appbar" android:layout_width="match_parent" android:layout_height="?attr/actionBarSize" android:background="?attr/colorPrimary" app:popupTheme="@style/AppTheme.PopupOverlay" /> </android.support.design.widget.AppBarLayout> <ScrollView android:layout_width="match_parent" android:layout_height="match_parent" android:layout_below="@id/comments_coordinator_layout" android:fillViewport="true" > <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical"> <RelativeLayout android:layout_marginTop="?attr/actionBarSize" android:id="@+id/view_post" android:layout_width="match_parent" android:paddingRight="5dp" android:paddingLeft="5dp" android:orientation="horizontal" android:layout_height="175dp" android:background="#e6e6e6"> <com.facebook.drawee.view.SimpleDraweeView android:layout_marginTop="15dp" android:id="@+id/poster_picture" android:layout_width="75dp" android:layout_height="75dp" android:layout_marginLeft="10dp" fresco:placeholderImage="@mipmap/blank_prof_pic" fresco:roundedCornerRadius="5dp" /> <TextView android:layout_marginLeft="5dp" android:layout_marginTop="15dp" android:layout_toRightOf="@id/poster_picture" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="15sp" android:textStyle="bold" android:id="@+id/poster_name"/> <TextView android:layout_alignParentRight="true" android:layout_marginTop="15dp" android:layout_toRightOf="@id/poster_name" android:layout_marginLeft="5dp" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="15sp" android:id="@+id/post_date"/> <TextView android:layout_marginLeft="5dp" android:layout_toRightOf="@id/poster_picture" android:layout_below="@id/poster_name" android:textSize="20sp" android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/view_status" /> </RelativeLayout> <RelativeLayout android:layout_width="match_parent" android:layout_height="match_parent"> <LinearLayout android:orientation="vertical" android:id="@+id/container_list" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_above="@+id/send_message"> </LinearLayout> <LinearLayout android:id="@+id/send_message" android:layout_width="match_parent" android:layout_height="wrap_content" android:padding="5dp" android:layout_alignParentBottom="true" android:orientation="horizontal" > <EditText android:id="@+id/write_comment" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_gravity="center_vertical|center_horizontal" android:layout_weight="5" android:gravity="top|left" android:hint="Comment back!" android:inputType="textMultiLine" android:scrollHorizontally="false" /> <Button android:id="@+id/send_comment" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_vertical|center_horizontal" android:gravity="center" android:text="send" android:textAppearance="?android:attr/textAppearanceMedium" /> </LinearLayout> </RelativeLayout> </LinearLayout> </ScrollView> </android.support.design.widget.CoordinatorLayout> 
+5
source share
2 answers

Using an internal ListView with a ScrollView should be avoided in Android. Indeed, these layouts have their own scroll and gesture. Solutions are designed to prevent scrolling gestures from the list or to calculate default heights and extensions. But this will do too much work and will not respect the Android template and recommendations.

Instead of replacing the ListView with LinearLayout and save the parent ScrollView , as my old answer pointed out, I found a possible workaround that has the following steps:

  • Create Your Own ListView
  • Override onSizeChanged() , which redraws the ListView when the keyboard is opened
  • Get lastVisiblePosition and return its height in the previous method
  • Finally, move the list to the previous lastVisiblePosition and calculate the offset
  • Also set a headerView for the header image container
  • And you need to update the height of the child before the user opens the keyboard so that you can update the value in onClickListener EditText

And he needs these layouts:

  • Work with the layout:

     <CoordinatorLayout> <AppBarLayout> ... </AppBarLayout> <RelativeLayout> <CustomListView /> <LinearLayout> ... </LinearLayout> </RelativeLayout> </CoordinatorLayout> 
  • header layout (which will be above the list):

     <RelativeLayout> <SimpleDraweeView /> <TextView /> ... </RelativeLayout> 

Hum that all ... So, good to go:

Create a ListView :

 public class CustomListView extends ListView { // last height item variable (updated from Activity) public int lastItemHeight = 0; public CustomListView(Context context) { super(context, null); } public CustomListView(Context context, AttributeSet attrs) { super(context, attrs); } public CustomListView(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); } } 

Override onSizeChanged() :

 @Override protected void onSizeChanged(int w, final int h, int oldw, int oldh) { // save last position visible before resize final int lastPosition = super.getLastVisiblePosition(); // call super SizeChanged method super.onSizeChanged(w, h, oldw, oldh); // after resizing, show the last visible item at the bottom of new listview height, above the edit text // see : http://developer.android.com/reference/android/widget/AbsListView.html#setSelectionFromTop(int, int) super.setSelectionFromTop(lastPosition, (h - lastItemHeight)); } 

This is all for ListView .
The lastItemHeight variable will be updated when the user clicks on the input (below the list, thanks to setting your container of header images as headerView in your Activity ).

Now let's look at Activity :

Install your own ListView and your Adapter :

 // get custom listview element final CustomListView list = (CustomListView) findViewById(R.id.container_list); // example items child ArrayList<String> items = new ArrayList<>(); for (int n=0; n<25; ++n) items.add("Blablabla n."+n); // example adapter ArrayAdapter<String> adapter = new ArrayAdapter<>(this, R.layout.item_list_text_simple, android.R.id.text1, items); 

Create a headerView and install the Adapter :

 // prepare the header content with picture View header = getLayoutInflater().inflate(R.layout.test_header_image, null); // set datas to the header elements (example) Uri uri = Uri.parse("https://raw.githubusercontent.com/facebook/fresco/gh-pages/static/fresco-logo.png"); SimpleDraweeView draweeView = (SimpleDraweeView) header.findViewById(R.id.poster_picture); draweeView.setImageURI(uri); // add the header to listview list.addHeaderView(header, null, false); // set the adapter list.setAdapter(adapter); 

Open the open program panel with EditText.setOnClickListener :

 // when the user clicks on EditText... editMessage.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // set a new Thread list.post(new Runnable() { @Override public void run() { // get last position of child int lastPosition = list.getLastVisiblePosition() - 1; // if the list can give the view last child if (list.getChildAt(lastPosition) != null) { // update the height of the last child in custom listview list.lastItemHeight = list.getChildAt(lastPosition).getHeight(); } } }); } }); 

Whats all for the code! And the last: (Ow .. where is the end?), Here are the layouts:

The basic layout of Activity :

 <?xml version="1.0" encoding="utf-8"?> <android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/comments_coordinator_layout" android:fitsSystemWindows="true" android:layout_width="match_parent" android:layout_height="match_parent"> <android.support.design.widget.AppBarLayout android:layout_height="wrap_content" android:layout_width="match_parent"> <android.support.v7.widget.Toolbar ... /> </android.support.design.widget.AppBarLayout> <RelativeLayout android:layout_width="match_parent" android:layout_height="match_parent" android:layout_marginTop="?attr/actionBarSize"> <com.package.name.CustomListView android:id="@+id/container_list" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_above="@+id/send_message"/> <LinearLayout android:id="@+id/send_message" android:layout_width="match_parent" android:layout_height="wrap_content" android:padding="5dp" android:layout_alignParentBottom="true" android:orientation="horizontal" > <EditText ... /> <Button ... /> </LinearLayout> </RelativeLayout> </android.support.design.widget.CoordinatorLayout> 

ListView Head Location:

 <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:fresco="http://schemas.android.com/tools" xmlns:app="http://schemas.android.com/apk/res-auto" android:id="@+id/view_post" android:layout_width="match_parent" android:layout_height="225dp" android:paddingRight="5dp" android:paddingLeft="5dp" android:orientation="horizontal" android:background="#e6e6e6"> <com.facebook.drawee.view.SimpleDraweeView ... /> <TextView ... /> <TextView ... /> <TextView ... /> </RelativeLayout> 

OU! I finally finished. I really hope you can have the right behavior. I tested it and it seems to work quite well. Here is another workaround with adjustPan , but you should keep the list starting at the bottom. Whereas this solution holds any child at the bottom of the list when resizing.

I hope you will like it;)

+1
source

add this to your manifest in action

 <activity ....... .................... android:windowSoftInputMode="adjustResize"/> 
+1
source

Source: https://habr.com/ru/post/1237830/


All Articles