i'm developing a soft keyboard for an android and I faced a problem which I could not solve. The problem is that when I open a keyboard it moves other views incorrectly (see the picture, my keyboard on the left).
I have tried multiple solutions that I found on stackoverflow or somewhere else, but they didn't work for me.
I've tried:
android:windowSoftInputMode="adjustResize"
android:windowSoftInputMode="stateHidden|adjustPan"
android:windowSoftInputMode="adjustResize|stateVisible|stateAlwaysHidden"
but it didn't change the behavior you see on the screenshot.
So, please help me to solve this problem.
Ps. I know that there are many similar questions where "adjustResize" or "stateHidden|adjustPan" is offered, but it doesn't work for me.
P.p.s screenshot is taken in android 6.0.1.
Thanks.
I finally found an answer to my question. You need to use this code in your class that extends InputMethodService:
#Override
public void onComputeInsets(InputMethodService.Insets outInsets) {
super.onComputeInsets(outInsets);
if (!isFullscreenMode()) {
outInsets.contentTopInsets = outInsets.visibleTopInsets;
}
}
You can simply switch your activity's windowSoftInputMode flag to "adjustPan". Remove adjustResize.
<activity
...
android:windowSoftInputMode="adjustPan">
</activity>
Try to wrap your layout with scrollView
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
.
.
.
.
</ScrollView>
I think the android:windowSoftInputMode can work, but like this:
android:windowSoftInputMode="adjustNothing"
Try it.
Related
I had some issues using adjustPan in my app, as it is not working as it is supposed to do. Some other users also experienced this problem, so I think it's important to share this issue (Layout doesn't move up enough when clicking EditText).
Note: I am using Android Studio and I'm using an "Instant App", so the location of the files are going to be a bit different.
When I use adjustPan in my layout, my activity does not move up enough to the point where the EditText appears above the Soft Keyboard (See Image For Details). The same result still appears no matter if you use it in a Manifest file or a Java file.
Not only that, but the weirdest part is that Status Bar becomes transparent and the dark blue bar which is supposed to go behind the notification icons goes down a little bit (See Image For Details).
I have tried making several attempts of solving this issue, like using adjustResize and setOnTouchListener but both of these answers that I received didn't solve this issue.
I still can't figure out the cause of this issue, so any answers or comments regarding how to discover why this issue is happening is also fine.
Here is some of my code:
Manifest File:
<activity
android:name=".Input_Activity"
android:screenOrientation="portrait"
android:windowSoftInputMode="adjustPan" />
Java File:
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.WindowManager;
public class Input_Activity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_input__activity);
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);
}
}
XML File:
<android.support.constraint.ConstraintLayout 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"
tools:context=".Input_Activity">
<EditText
android:layout_width="214dp"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:layout_marginBottom="8dp"
android:inputType="number|numberDecimal"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.585"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.725" />
All feedback would be appreciated.
create an object like InputMethodManager methodManager at class level.
add these lines to your onCreate() method,
methodManager = (InputMethodManager) this.getSystemService(this.INPUT_METHOD_SERVICE);
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
and in your onClick() method, show keyboard like,
methodManager.toggleSoftInputFromWindow(
your_view_name.getApplicationWindowToken(),
InputMethodManager.SHOW_FORCED, 0);
If this word getApplicationWindowToken throws an error then replace it with getWindowToken
hope it helps.
android:windowSoftInputMode
Above attribute can be used to specify what happens with activity contents.
Below image can clear your doubts more
adjustPan: The activity’s main window is not resized to make room for
the soft keyboard. Rather, the contents of the window are
automatically panned so that the current focus is never obscured by
the keyboard and users can always see what they are typing. This is
generally less desirable than resizing, because the user may need to
close the soft keyboard to get at and interact with obscured parts of
the window.
adjustResize: The activity’s main window is always resized to make
room for the soft keyboard on screen.
I think there is no need add below code which you have added.
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);
Also need to add following code in your onTouch event so that when soft keyboard appears it will go up. Example:
editText.setTransformationMethod(WordBreakUtil.getInstance());
editText.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE | WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);
return false;
}
});
I'm not sure about the answer. but try replacing
<activity
android:name=".Input_Activity"
android:screenOrientation="portrait"
android:windowSoftInputMode="adjustPan" />
with
<activity
android:name=".Input_Activity"
android:screenOrientation="portrait"
android:windowSoftInputMode="adjustResize" />
in your android manifest.
Read here
Hope this helps!
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"
I have a layout which contain RelativeLayout as Root layout,Relative layoout contain ListView at Top and one Edittext align at the bottom of relative layout.
In Pre lollipop version devices whenever soft keyboard open Edittext pushed up and I am able to see the Editext.
But in lollipop soft keyboard hide the Editext.
I have already set android:windowSoftInputMode="adjustPan|adjustResize" in manifest file.
Please help me
try to give fitsSystemWindows in your main layout( for lollipop versions)
android:fitsSystemWindows="true"
also in the manifest give
android:windowSoftInputMode="adjustResize"
hope this will help
Thanks for your valuable input. Finally I have fixed this issue.
What I found the reason behind this issue is application theme. My application theme was
android:Theme.Light in which I found this issue. I have only changed the theme of my EditText layout activity by setting
android:theme="#android:style/Theme.Black.NoTitleBar"
e.g.
<activity
android:name=".activity.LiveStreamingActivity"
android:label="#string/title_activity_event_performer"
android:theme="#android:style/Theme.Black.NoTitleBar"
android:screenOrientation="portrait" />
This solved my problem !!!
May be your activity is full screen. Try clearing the FULLSCREEN flag.
I too had the same issue and it was because of the usage of a menudrawer library which was not supported by lollipop and when I replace that with default navigation drawer of android the things started working fine.Please check with the libraries if you use any.
The important value is the adjustResize. This will shift the whole UI up to give room for the soft keyboard.
<activity
android:name="com.my.MainActivity"
android:screenOrientation="portrait"
android:label="#string/title_activity_main"
android:windowSoftInputMode="adjustResize|stateHidden" >
</activity>
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
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"