Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make sure specific view is visible when keyboard appears?

Tags:

android

I have a layout with a graphic at the top, an EditText in the middle and a button some distance below it, like so:

Example layout

When the user is typing in the EditText, I want to pan the layout so that the "Go" button is still visible, even if that means clipping the image off the top, like so:

Wanted layout

I know about windowSoftInputMode="adjustPan" in the manifest, but that doesn't work because it only pans far enough for the EditText to be visible. Is there a way to make sure it pans until the button is visible too?


For comments with K_Anas, this is my layout. It has all the extra margins and spacing removed compared to the first screenshot, to remove any other source of problems.

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">

    <View
        android:layout_width="300dp"
        android:layout_height="150dp"
        android:layout_alignParentTop="true"
        android:background="#999"/>

    <EditText
        android:layout_width="280dp"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:hint="Sample..."/>

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:text="Go"/>

</RelativeLayout>
like image 205
Steve Haley Avatar asked Jun 05 '12 16:06

Steve Haley


People also ask

How do I make my keyboard always visible?

You must have an EditText in your layout and that need to extent EditText base class. then Override onKeyPreIme() method, and return True. Now your keyboard will be always visible and can't be dismissed by Back key. Caution: Because of your onKeyPreIme() method returns true you can't exit your app using back key.

How do I know if my keyboard is visible?

Android provides no direct way to determine if the keyboard is open, so we have to get a little creative. The View class has a handy method called getWindowVisibleDisplayFrame from which we can retrieve a rectangle which contains the portion of the view visible to the user.

How do you move the view up when keyboard appears on Android flutter?

Scrolling up scrollview when keyboard is shown We can add bottom padding into the scrollview using its contentInset property, so the content inside will be moved up when a keyboard is shown.


2 Answers

Spoke with the Android developers hangout, and it doesn't seem like there's an easy way to solve this problem. The consensus seems to be reworking the layout rather than having an API to fix the issue.

like image 148
Steve Haley Avatar answered Oct 12 '22 13:10

Steve Haley


did you tried:

android:windowSoftInputMode="adjustResize"

in your activity Tag in Manifest

see these docs on android developer blog

I tried your layout with: android:windowSoftInputMode="stateVisible|adjustResize"

and give me these results:

enter image description hereenter image description here

like image 1
K_Anas Avatar answered Oct 12 '22 13:10

K_Anas