Android: How do I prevent the soft keyboard from pushing my view up?
You can simply switch your Activity's windowSoftInputMode
flag to adjustPan
in your AndroidMainfest.xml
file inside your activity tag.
Check the official documentation for more info.
<activity
...
android:windowSoftInputMode="adjustPan">
</activity>
If your container is not changing size, then you likely have the height set to "match parent". If possible, set the parent to "Wrap Content", or a constraint layout with constraingts to top and bottom of parent.
The parent container will shrink to fit the available space, so it is likely that your content should be inside of a scolling view to prevent (depending on the phone manufacturer and the layout choosen...)
- Content being smashed together
- Content hanging off the screen
- Content being inacccessable due to it being underneath the keyboard
even if the layout it is in is a relative or constraint layout, the content could exhibit problems 1-3.
How to adjust layout when soft keyboard appears
Just add
android:windowSoftInputMode="adjustResize"
in your AndroidManifest.xml where you declare this particular activity and this will adjust the layout resize option.
some source code below for layout design
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="20dp"
android:text="FaceBook"
android:textAppearance="?android:attr/textAppearanceLarge" />
<EditText
android:id="@+id/editText1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/textView1"
android:layout_marginTop="30dp"
android:ems="10"
android:hint="username" >
<requestFocus />
</EditText>
<EditText
android:id="@+id/editText2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/editText1"
android:layout_marginTop="20dp"
android:ems="10"
android:hint="password" />
<Button
android:id="@+id/button1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/editText2"
android:layout_centerHorizontal="true"
android:layout_marginLeft="18dp"
android:layout_marginTop="20dp"
android:text="Log In" />
<TextView
android:id="@+id/textView2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_marginTop="17dp"
android:gravity="center"
android:text="Sign up for facebook"
android:textAppearance="?android:attr/textAppearanceLarge" />
</RelativeLayout>
Move layouts up when soft keyboard is shown?
Yes, check out this article on the Android developers' site which describes how the framework handles the soft keyboard appearing.
The android:windowSoftInputMode
attribute can be used to specify what happens on a per-activity basis: whether the layout is resized or whether it scrolls etc.
How to prevent soft keyboard from pushing up only the toolbar?
Use a ScrollView.
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.Toolbar
android:id="@+id/my_toolbar"
android:layout_height="wrap_content"
android:layout_width="match_parent">
</android.support.v7.widget.Toolbar>
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/my_toolbar">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/edit_text1"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/edit_text2"/>
...
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/edit_text7"/>
</LinearLayout>
</ScrollView>
Android Toolbar moves up when keyboard appears
The problem was that I was using <item name="android:windowTranslucentStatus">true</item>
in styles.xml
To enable adjustResize
add android:fitsSystemWindows="true"
in your activity's parent layout
Keyboard hides content in fragment, I need the frame layout to resize so bottom is just above the keyboard and now scrollable
Okay, here's how I solved it.
The basic idea is that you have to:
- Detect whether or not a
soft-keyboard
is showing, - React. Based on the detected information (is-soft-keyboard-showing), resize your layout accordingly.
There are two ways of achieving this:
to give your activity's root view a known ID, say '@+id/activityRoot', hook a
GlobalLayoutListener
into theViewTreeObserver
, and from there calculate the size diff between your activity's view root and the window size:Customize your top-level layout class into one which overrides
onMeasure()
And I would like to credit the above answer to this SO Post: how-to-check-visibility-of-software-keyboard-in-android, which I have found earlier on this particular problem.
Related Topics
Http Failure Response for (Unknown Url): 0 Unknown Error on Android
How to Control Navigationview Margin Between Menu Items
Get City Name and Postal Code from Google Place API on Android
How to Set Android_Ndk_Home So That Android Studio Does Not Ask for Ndk Location
Convert File: Uri to File in Android
Android Textview Text Not Getting Wrapped
How to Change Background Color of Layout of List-View Item Using Array-Adapter
How to Remove Bottom Navigation View and Toolbar in Some Fragments If Using Navigation Controller
Setting Onclicklistener for the Drawable Right of an Edittext
Android Emulator Is Start But Window Show Blank Screen
Cart Item Count Increment/Decrement & Add to Cart
Navigating to Previous Fragment Without Reloading It
How to Hide Action Bar for Fragment
How to Delete Shared Preferences Data from App in Android