Activity with fragments does not resize when the keyboard opens
Solution 1
When the soft keyboard appears on the screen, the amount of space available for the application's UI reduces. The system makes decision on how to organize the available space between the application's UI and soft keyboard.
- If the window content contains
ListView
,ScrollView
, the application's window is resized provided all the content is visible. - If re-sizing is not feasible,
pan and scan
approach is used, which simply involves scrolling the application's window so that the currently focused view is visible.
Since your layout does not contain any ListView
, ScrollView
, re-sizing is ruled out.
The window's root view is a FrameLayout
to which you were originally adding LinearLayout
. Since LinearLayout
does not support scrolling, pan and scan
approach is also ruled out. Hence wrapping the layout inside ScrollView solves the scrolling issue.
You can refer Android Developers blog for more details.
Update 1:
OP was able to solve the issue, as indicated in this answer. The issue was happening due to one fragment overlaying another fragment after animation and the parent of these Fragment
's was a LinearLayout
. For overlay purpose, you need to use RelativeLayout
or FrameLayout
only.
Solution 2
You can use this code in your Fragment also
This code provide help for resize view
public static class MyDialog extends DialogFragment {
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
// Dialog layout inflater code
// getDialog() need to be called only after onCreateDialog(), which is invoked between onCreate() and onCreateView().
getDialog().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN | WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);
// return view code
}
}
Solution 3
To change the mode to “resize” at the following statement to your activity in “AndroidManifest.xml”.
<activity android:name=".ActivityName" android:windowSoftInputMode="adjustResize">
and please remove the complexity of LinearLayout ( means you are using multiple level of LinearLayout ) instead of that you can use RelativeLayout also .. using this you can easily set Layout as you want .. try it that also hope your problem may rectifier
I hope this helps.
Solution 4
Use this in onCreateDialog in BottomSheetFragment / Dailogfragment
KeyboardUtil(getActivity(), view);
or
For fragment use
new KeyboardUtil(this, findViewById(R.id.fragment_container));
by using this Util class
Credit:Mikepenz
Solution 5
Turns out my problem was caused by a completely unexpected source.
What happened is that I used a LinearLayout
as the parent of my two fragment containers.
Then, in my expanding animation I raised the bottom container to overlap the other one.
I don't really know why but since LinearLayout
is not supposed to hold overlapping children, switching to a RelativeLayout
or a FrameLayout
immediately solved the issue.
Since this cause might not be apparent from the code I posted, I do want to apologize for the lack of information, and will credit the most detailed answer as the best one.
Asaf
I'm a language-fanatic, an anime/manga fan, and an avid gamer. Also, I'm super interested in programming languages and hope to someday make it my job. I currently know/study: Japanese (advanced level) Korean (just started) Hebrew (mother tongue) English (judge for yourself) PHP, HTML5 + CSS3, Java, Javascript, C# I usually +1 everyone who comments/answers (unless I forget!) as I feel anyone who takes their time to answer a question deserves to be credited for it in some way. Especially when they don't necessarily get paid for it.
Updated on July 13, 2022Comments
-
Asaf almost 2 years
In my main activity there is a
RelativeLayout
that has 2 childs:- An
ImageView
which serves as the background - A
LinearLayout
that has 2 fragment containers
activity_main.xml
<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:layout_gravity="bottom" tools:context="${packageName}.${activityClass}"> <ImageView android:layout_width="match_parent" android:layout_height="match_parent" android:adjustViewBounds="true" android:baselineAlignBottom="false" android:scaleType="fitStart" android:src="@drawable/bg_image" /> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:paddingTop="?android:attr/actionBarSize" android:layout_alignParentBottom="true"> <LinearLayout android:id="@+id/ContainerA" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_gravity="center" android:orientation="vertical" /> <LinearLayout android:id="@+id/ContainerB" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@color/background_gray" android:orientation="vertical" /> </LinearLayout> </RelativeLayout>
Container A simply shows a fragment that doesn't change.
However, Container B holds a fragment that does change depending on user clicks.Container B changes in 2 ways:
- It can expand (from a height of about 70% to full height).
- It changes fragments.
One of the fragments Container B can hold is a form, which contains
EditText
s and other views.My problem is that I can't get the activity to resize when the keyboard is open.
I setadjustResize
in the manifest file and my theme is not a fullscreen theme (it is apparently a cause for this problem). I also tried to edit the views, addScrollViews
but absolutely nothing worked.The theme I'm using for this activity is the light theme with the dark actionbar from the
AppCompat
library. I only edited the theme to allow an overlay actionbar. This is not the problem as I've already tried removing those edits to no avail.Here's a link to the form layout
TL;DR
adjustResize
/adjustPan
do not work for me. Searched, tried various solutions, nothing worked. My question is; what is the cause for this in my case?Update: AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" ...> <uses-sdk android:minSdkVersion="11" android:targetSdkVersion="19" /> <application ... android:theme="@style/AppTheme"> <activity android:name=".SplashScreenActivity" android:label="@string/app_name" android:screenOrientation="portrait" android:theme="@android:style/Theme.Black.NoTitleBar"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <activity android:name=".MainActivity" android:configChanges="keyboardHidden|screenSize" android:label="@string/app_name" android:screenOrientation="portrait" android:theme="@style/OverlayActionBarTheme" android:windowSoftInputMode="adjustResize" /> <activity android:name=".SettingsActivity" android:label="@string/title_activity_settings" android:parentActivityName=".MainActivity" android:windowSoftInputMode="adjustResize"> <meta-data android:name="android.support.PARENT_ACTIVITY" android:value=".MainActivity" /> </activity> </application> </manifest>
Update 2:
I created a completely new app, added my form layout as the main layout, and tested whether the problem was in the layout itself. Turns out it is, as even on this newly created app it doesn't seem to resize.As stated above, the link to the layout can be found Here
Update 3: I managed to make it work on the new application by surrounding the whole layout with a
ScrollView
. I couldn't replicate the same effect in the original app for some reason... still trying to figure out why. - An