Move layouts up when soft keyboard is shown? - android

I have a few elements in a RelativeView with the align bottom attribute set, when the soft keyboard comes up the elements are hidden by the soft keyboard.
I would like them to move up so that if there is enough screen space they are shown above the keyboard, or to make the section above the keyboard scrollable so the user can still see the elements.
Any ideas on how to approach this?

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.

You can also use this code in onCreate() method:
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);

Make changes in the activity of your Manifest file like
android:windowSoftInputMode="adjustResize"
OR
Make changes in your onCreate() method in the activity class like
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);

Add in AndroidManifest.xml for your activity:
android:windowSoftInputMode="adjustPan|adjustResize"

similar issue i was facing with my layout which consist scroll view inside.
Whenever i try to select text in EditText,Copy/Cut/Paste action bar gets moved up with below code as such it does not resize layout
android:windowSoftInputMode="adjustPan"
By modifying it to as below
AndroidManifest.xml
android:windowSoftInputMode="stateVisible|adjustResize"
style.xml file ,in activity style
true
It worked for me.

In AndroidManifest.xml, don't forget to set:
android:windowSoftInputMode="adjustResize"
and for RelativeLayout inside ScrollView ,set :
android:layout_gravity="center" or android:layout_gravity="bottom"
it will be okay

for Activity in its oncreate methode insert below line
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);
and for fragments in its onCreate method
getActivity().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);

There are 3 steps to follow to scroll screen up.
Write in manifest file for your activity:
android:windowSoftInputMode="adjustResize"
Add following line in oncreate() method of your activity
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE|WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
Then place your whole view in "Scrollview" in XML file like :
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:background="#drawable/bg"
android:layout_height="match_parent"
>
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:layout_gravity="center" >
<TextView />
.........
<TextView />
<EditText >
<requestFocus />
</EditText>
<Button />
</RelativeLayout>
</ScrollView>
P.S. Don't forget to add android:layout_gravity="center" in parent Relative layout.

<activity name="ActivityName"
android:windowSoftInputMode="adjustResize">
...
</activity>
Add NestedScrollView around layout which you want to scroll, this will perfectly work

I tried a method of Diego Ramírez, it works.
In AndroidManifest:
<activity
android:name=".MainActivity"
android:windowSoftInputMode="stateHidden|adjustResize">
...
</activity>
In activity_main.xml:
<LinearLayout ...
android:orientation="vertical">
<Space
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_weight="1" />
<EditText
android:id="#+id/edName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:hint="#string/first_name"
android:inputType="textPersonName" />
<EditText ...>
...
</LinearLayout>

I just added the following line to the top level layout and everything worked:
android:fitsSystemWindows="true"

This is what worked for me after looking at a lot of solutions:
In AndroidManifest.xml add the following line to an activity declaration in the manifest file of your project.
android:windowSoftInputMode="stateHidden|adjustPan"
The code should look something like this:
<activity
android:name=".MainActivity"
android:screenOrientation="landscape"
android:windowSoftInputMode="stateHidden|adjustPan">
</activity>

I have found a solution to my problems that it's pretty same as yours.
First of all, I only have a LinearLayout with 2 elements, an ImageView and a LinearLayout wich contains 2 EditText and one Button, so I needed to keep them separate in full screen without keyboard, and when the keyboard appears they should look closer.
So I added the view between them with the height attribute in 0dp and weight 1, that let me keep my views separate one form each other. and when the keyboard appers, as they don't have a fix height, they just resize and keep the aspect.
Voila! The layout resize and the distance between my views is the same when the keyboard is or not present.

If user want this task using manifest then add this line in manifest
In manifest add this line: - android:windowSoftInputMode="adjustPan"
e.g.
In Java File or Kotlin File (Programmatically):- If the user wants this using Activity then add the below line in onCreate Method
activity?.window()?.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE)

Here is full manifest code
<activity
android:name=".activities.MainActivity"
android:windowSoftInputMode="adjustResize"
android:label="#string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>

In your manifest file add the below line. It will work
<activity name="MainActivity"
android:windowSoftInputMode="stateVisible|adjustResize">
...
</activity>

if you are in fragments then you have to add the below code to your onCreate on your activity it solves issue for me
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN | WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);

Use Scroll View as a Parent and use this Tag in Scroll View
android:fillViewport="true"

Alright this is very late however I've discovered that if you add a List View under your edit text then the keyboard will move all layouts under that edittext without moving the ListView
<EditText
android:id="#+id/et"
android:layout_height="match_parent"
android:layout_width="match_parent"
/>
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"//can make as 1dp
android:layout_below="#+id/et" // set to below editext
android:id="#+id/view"
>
**<any layout**
This is the only solution that worked for me.

Not a single advice solve my issue. Adjust Pan is almost as like work Adjust Resize. If i set Adjust Nothing than layout not move up but it hide my edittext of bottom that i am corrently writing. So after 24 hour of continuous searching I found this source and it solve my problem.

It might be late but I want to add few things.
In Android Studio Version 4.0.1, and Gradle Version 6.1.1, the windowSoftInputMode will not work as expected if you are adding the following flags in your Activity :
getWindow().setFlags(
WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS,
WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS
);
After wasting lot of time, I have found that hack. In order for adjustResize or AdjustPanor any attribute of windowSoftInputMode to work properly, you need to remove that from your activity.

Add this line to the view:
view.requestFocus();

Related

Android - How would I push my edittexts above my softkeyboard? [duplicate]

I have a few elements in a RelativeView with the align bottom attribute set, when the soft keyboard comes up the elements are hidden by the soft keyboard.
I would like them to move up so that if there is enough screen space they are shown above the keyboard, or to make the section above the keyboard scrollable so the user can still see the elements.
Any ideas on how to approach this?
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.
You can also use this code in onCreate() method:
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);
Make changes in the activity of your Manifest file like
android:windowSoftInputMode="adjustResize"
OR
Make changes in your onCreate() method in the activity class like
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
Add in AndroidManifest.xml for your activity:
android:windowSoftInputMode="adjustPan|adjustResize"
similar issue i was facing with my layout which consist scroll view inside.
Whenever i try to select text in EditText,Copy/Cut/Paste action bar gets moved up with below code as such it does not resize layout
android:windowSoftInputMode="adjustPan"
By modifying it to as below
AndroidManifest.xml
android:windowSoftInputMode="stateVisible|adjustResize"
style.xml file ,in activity style
true
It worked for me.
In AndroidManifest.xml, don't forget to set:
android:windowSoftInputMode="adjustResize"
and for RelativeLayout inside ScrollView ,set :
android:layout_gravity="center" or android:layout_gravity="bottom"
it will be okay
for Activity in its oncreate methode insert below line
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);
and for fragments in its onCreate method
getActivity().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);
There are 3 steps to follow to scroll screen up.
Write in manifest file for your activity:
android:windowSoftInputMode="adjustResize"
Add following line in oncreate() method of your activity
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE|WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
Then place your whole view in "Scrollview" in XML file like :
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:background="#drawable/bg"
android:layout_height="match_parent"
>
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:layout_gravity="center" >
<TextView />
.........
<TextView />
<EditText >
<requestFocus />
</EditText>
<Button />
</RelativeLayout>
</ScrollView>
P.S. Don't forget to add android:layout_gravity="center" in parent Relative layout.
<activity name="ActivityName"
android:windowSoftInputMode="adjustResize">
...
</activity>
Add NestedScrollView around layout which you want to scroll, this will perfectly work
I tried a method of Diego Ramírez, it works.
In AndroidManifest:
<activity
android:name=".MainActivity"
android:windowSoftInputMode="stateHidden|adjustResize">
...
</activity>
In activity_main.xml:
<LinearLayout ...
android:orientation="vertical">
<Space
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_weight="1" />
<EditText
android:id="#+id/edName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:hint="#string/first_name"
android:inputType="textPersonName" />
<EditText ...>
...
</LinearLayout>
I just added the following line to the top level layout and everything worked:
android:fitsSystemWindows="true"
This is what worked for me after looking at a lot of solutions:
In AndroidManifest.xml add the following line to an activity declaration in the manifest file of your project.
android:windowSoftInputMode="stateHidden|adjustPan"
The code should look something like this:
<activity
android:name=".MainActivity"
android:screenOrientation="landscape"
android:windowSoftInputMode="stateHidden|adjustPan">
</activity>
I have found a solution to my problems that it's pretty same as yours.
First of all, I only have a LinearLayout with 2 elements, an ImageView and a LinearLayout wich contains 2 EditText and one Button, so I needed to keep them separate in full screen without keyboard, and when the keyboard appears they should look closer.
So I added the view between them with the height attribute in 0dp and weight 1, that let me keep my views separate one form each other. and when the keyboard appers, as they don't have a fix height, they just resize and keep the aspect.
Voila! The layout resize and the distance between my views is the same when the keyboard is or not present.
If user want this task using manifest then add this line in manifest
In manifest add this line: - android:windowSoftInputMode="adjustPan"
e.g.
In Java File or Kotlin File (Programmatically):- If the user wants this using Activity then add the below line in onCreate Method
activity?.window()?.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE)
Here is full manifest code
<activity
android:name=".activities.MainActivity"
android:windowSoftInputMode="adjustResize"
android:label="#string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
In your manifest file add the below line. It will work
<activity name="MainActivity"
android:windowSoftInputMode="stateVisible|adjustResize">
...
</activity>
if you are in fragments then you have to add the below code to your onCreate on your activity it solves issue for me
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN | WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);
Use Scroll View as a Parent and use this Tag in Scroll View
android:fillViewport="true"
Alright this is very late however I've discovered that if you add a List View under your edit text then the keyboard will move all layouts under that edittext without moving the ListView
<EditText
android:id="#+id/et"
android:layout_height="match_parent"
android:layout_width="match_parent"
/>
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"//can make as 1dp
android:layout_below="#+id/et" // set to below editext
android:id="#+id/view"
>
**<any layout**
This is the only solution that worked for me.
Not a single advice solve my issue. Adjust Pan is almost as like work Adjust Resize. If i set Adjust Nothing than layout not move up but it hide my edittext of bottom that i am corrently writing. So after 24 hour of continuous searching I found this source and it solve my problem.
It might be late but I want to add few things.
In Android Studio Version 4.0.1, and Gradle Version 6.1.1, the windowSoftInputMode will not work as expected if you are adding the following flags in your Activity :
getWindow().setFlags(
WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS,
WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS
);
After wasting lot of time, I have found that hack. In order for adjustResize or AdjustPanor any attribute of windowSoftInputMode to work properly, you need to remove that from your activity.
Add this line to the view:
view.requestFocus();

Android: Entering text with soft keyboard in ScrollView pushing header views up

I spent hours debugging the same issue described in these SO questions:
Android: How do I prevent the soft keyboard from pushing my view up?
How to avoid soft keyboard pushing up my layout?
Android: How do I prevent the soft keyboard from pushing my view up?
The generally accepted answer is to add android:windowSoftInputMode="adjustPan" to the manifest activity declaration. This works to make the screen stay put when the keyboard is opened, however it does not stop the screen from scrolling when text is entered that has more lines than the screen can display. Here is what I see when I implement that solution:
The header remains when the keyboard opens, however if you type a bunch of lines the header eventually scrolls off the top of the screen.
I tried Googling this problem to try and solve it and none of the solutions worked. Then I managed to solve it by doing the opposite of what the accepted answer above said.
For reference, my layout xml file:
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="#+id/header"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintTop_toTopOf="parent"/>
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_constraintTop_toBottomOf="#id/header"
android:fillViewport="true">
<EditText
android:id="#+id/noteText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textMultiLine"
android:windowSoftInputMode="adjustPan"/>
</ScrollView>
</androidx.constraintlayout.widget.ConstraintLayout>
My manifest:
<activity android:name=".activity.MainActivity"
android:screenOrientation="portrait"
android:windowSoftInputMode="adjustPan"
android:resizeableActivity="true"
tools:targetApi="n"/>
I solved this by changing the manifest activity declaration to remove the android:windowSoftInputMode="adjustPan" line from the manifest:
<activity android:name=".activity.MainActivity"
android:screenOrientation="portrait"
android:resizeableActivity="true"
tools:targetApi="n"/>
...and now everything works as expected, the header stays on the screen when the keyboard was open and text was entered.
Hoping that I can save someone the time it took me to figure this out. Here is what it looks like after fixing the manifest:

Adjust scrollview when keyboard is up

I have an activity with a form in it. Because the form is quite long, I've used a scrollview.
The problem is that the scrollview doesn't change when the keyboard is up. The keyboard overlaps the last part of the scrollview.
How can I make sure that the keyboard is below the scrollview and the scrollview is adjusted to fit the space above it?
In the meanwhile, is there a way to make sure the buttons ‘previous’ and ‘next’ are in the keyboard as well?
In the application's manifest file, add the following to the desired <activity /> --
android:windowSoftInputMode="stateAlwaysHidden|adjustResize|adjustPan"
You need to add android:windowSoftInputMode="adjustResize" in the AndroidManifest.xml file.
if you use fragment, you just need to use the code to control it, like below
context.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
For ScrollView, I like to use the following soft input mode for the Activity in the AndroidManifest.xml file:
android:windowSoftInputMode="adjustPan"
This works the best for me out of the other options.
Note, if you are using CoordinatorLayout, make sure you set android:fitsSystemWindows="false" to be able to make it work for "adjustResize"
The above answers are not enough!
See this article
https://code.luasoftware.com/tutorials/android/move-layout-when-keyboard-shown/
<ScrollView ... android:layout_height="fill_parent" android:fillViewport="true" android:fitsSystemWindows="true" />
Manifest
<activity ... android:windowSoftInputMode="stateHidden|adjustResize" />
it's work for me

Android : Showing keyboard moves my components up, i want to hide them instead

I have added a LinearLayOut having some buttons My screen is RelativeLayOut it self
Here is the code for that linear layout manager
<LinearLayout
android:orientation="horizontal"
android:gravity="bottom"
android:layout_alignParentBottom="true"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:id="#+id/Footer"
android:layout_marginBottom="5dp">
Here is the problem:
There is an EditText component on the top and it pops a soft keyboard on the screen , and brings my Footer manager on top of the keyboard and eventually SHATTERS my whole UI.
What is the exact solution?
P.S. I have removed android:gravity="bottom" and android:layout_alignParentBottom="true" one by one but with hard luck i did not get desired result.
Thanks
Add android:windowSoftInputMode="adjustPan" to manifest - to the corresponding activity:
<activity android:name="MyActivity"
...
android:windowSoftInputMode="adjustPan"
...
</activity>
You probably want
<activity
...
android:windowSoftInputMode="adjustNothing">
</activity>
That will prevent any layout changes when the soft keyboard is shown.
There is probably some confusion over this since it's currently missing from the documentation at http://developer.android.com/guide/topics/manifest/activity-element.html#wsoft

Set initial focus in an Android application

In my Android application it automatically focuses the first Button I have in my layout, giving it an orange outline. How can I set the initial focus preferably in XML, and can this be set to nothing?
You could use the requestFocus tag:
<Button ...>
<requestFocus />
</Button>
I find it odd though that it auto-focuses one of your buttons, I haven't observed that behavior in any of my views.
#Someone Somewhere, I tried all of the above to no avail. The fix I found is from http://www.helloandroid.com/tutorials/remove-autofocus-edittext-android . Basically, you need to create an invisible layout just above the problematic Button:
<LinearLayout android:focusable="true"
android:focusableInTouchMode="true"
android:layout_width="0px"
android:layout_height="0px" >
<requestFocus />
</LinearLayout>
Set both :focusable and :focusableInTouchMode to true and call requestFocus. It does the trick.
I found this worked best for me.
In AndroidManifest.xml <activity> element add android:windowSoftInputMode="stateHidden"
This always hides the keyboard when entering the activity.
I just add this line of code into onCreate():
this.getWindow().setSoftInputMode(
WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
Problem solved.
Use the code below,
TableRow _tableRow =(TableRow)findViewById(R.id.tableRowMainBody);
tableRow.requestFocus();
that should work.
#Someone Somewhere I used this to clear focus:
editText.clearFocus();
and it helps
android:focusedByDefault="true"

Categories

Resources