Have a Xamarin Android app but I am sure this question applies to non-Xamarin as well. Most of my fragments have EditText controls. Each time they receive focus, the soft keyboard shows as expected. However, the soft keyboard will cover my EditText boxes on the lower half of the View.
I saw that I can set the following in either the Activity class attribute or the manifest. I am setting this in the Activities class attribute:
[Activity(Label = "MainActivity", ScreenOrientation = ScreenOrientation.Portrait, Theme = "#style/AppTheme", LaunchMode = LaunchMode.SingleTask, WindowSoftInputMode = SoftInput.AdjustResize)]
As you can see, I am setting WindowSoftInputMode to AdjustResize. The good news is that I can confirm that this does in fact work for the EditText controls that are on the actual MainActivity.
However, from my MainActivity which works, I also put fragments in to a FrameLayout. So, my MainActivity axml looks like this:
<?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">
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:animateLayoutChanges="true"
android:background="#drawable/sortingpackages">
<LinearLayout
android:id="#+id/LayoutControls"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:background="#FFFFFF"
android:animateLayoutChanges="true"
android:layout_marginTop="10dp">
<ImageView
android:src="#drawable/odenlogotransscaled"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/imageView1"
android:layout_marginTop="0.0dp"
android:layout_marginBottom="0.0dp"
android:layout_centerInParent="true"
android:layout_gravity="center_horizontal" />
<Spinner
android:layout_width="220dp"
android:layout_height="40dp"
android:id="#+id/spSelectLocation"
android:layout_margin="10dp"
android:spinnerMode="dialog"
android:dropDownWidth="220dp"
style="#style/SpinnerOdenTheme" />
<EditText
android:layout_width="220dp"
android:layout_height="35dp"
android:id="#+id/tbUserLogin"
android:layout_margin="10dp"
android:hint="#string/UserLogin"
android:ellipsize="start"
android:gravity="left"
android:inputType="text"
android:background="#drawable/EditTextStyle"
android:cursorVisible="true"
android:textColorHint="#000000"
android:padding="3dp" />
<Button
android:text="#string/Login"
android:layout_width="220dp"
android:layout_height="wrap_content"
android:id="#+id/btnLogin"
android:layout_margin="10dp"
style="#style/TextButtonOdenTheme" />
<Button
android:text="#string/Exit"
android:layout_width="220dp"
android:layout_height="wrap_content"
android:id="#+id/btnLogoutExit"
android:layout_margin="10dp"
style="#style/TextButtonOdenTheme" />
</LinearLayout>
<Button
android:text="#string/Settings"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/btnSettings"
android:layout_margin="10dp"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
style="#style/TextButtonOdenTheme" />
</RelativeLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<FrameLayout
android:id="#+id/LoginFragmentContainer"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
</RelativeLayout>
You can see I put the FrameLayout for the Fragment Container towards the bottom. Any fragment I swap into this FrameLayout using the Support Fragment Manager, does not respect the Soft keyboard setting and resize or pan (I have tried both settings).
I have also tried wrapping either the FrameLayout or the whole Layout inside of a ScrollContainer as suggested by a post I was reading and nothing fixes the problem.
Anybody else have an issue getting the fragments in a FrameLayout to adjust to the soft keyboard and how do I fix it?
Thanks!
Related
I'm trying to make a chat layout. The problem is when I focus the EditText, softkeyboard appears and pushes everything up. I want it to push only bottom part with send instructions so that top (brown) header part and a recyclerview with messages are still visible. Something that facebook messenger app has. I tried putting "adjust pan, resize" and everything in manifest file, but it didn't work. Here is my default layout when nothing is in focus:
And this is when I press the EditText and keyboard appears and shifts everything up:
I can't see header anymore nor the message in recyclerView.
Basically I want all elements to stay visible at all times, so when Keyboard pushes up, header, messages and send are still visible to the user.
Here is my layout code:
<LinearLayout 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:orientation="vertical">
<LinearLayout
android:id="#+id/ll_profile"
android:layout_width="match_parent"
android:layout_height="60dp"
android:layout_alignParentTop="true"
android:layout_marginBottom="10dp"
android:layout_marginStart="10dp"
android:layout_marginTop="10dp">
<de.hdodenhof.circleimageview.CircleImageView
android:id="#+id/iv_profile"
android:layout_width="40dp"
android:layout_height="40dp"
android:src="#drawable/img_back" />
<TextView
android:id="#+id/tv_profile"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_marginStart="10dp" />
</LinearLayout>
<android.support.v4.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#id/ll_profile"
android:fillViewport="true"
android:isScrollContainer="false">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<View
android:id="#+id/view_line"
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_marginEnd="10dp"
android:layout_marginStart="10dp"
android:background="#color/gray_light_trans" />
<LinearLayout
android:id="#+id/ll_send"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:weightSum="10">
<EditText
android:id="#+id/et_message"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="9"
android:hint="Message" />
<Button
android:id="#+id/btn_send"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Send" />
</LinearLayout>
<android.support.v7.widget.RecyclerView
android:id="#+id/rv_chat"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_above="#id/ll_send"
android:layout_below="#id/view_line" />
</RelativeLayout>
</android.support.v4.widget.NestedScrollView>
use android:windowSoftInputMode="stateHidden" in your activity at manifest
The issue was that I was using a theme in my App activity that was fullscreen. When I removed fullscreen from main base theme, it's working.
My interface looks like this:
XML:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:id="#+id/profile_fields_container"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="20dp"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/account_person_name_label" />
<EditText
android:id="#+id/person_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:minWidth="150dp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:text="#string/account_person_emailId_label" />
<EditText
android:id="#+id/person_emailId"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="textEmailAddress"
android:minWidth="150dp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:text="#string/account_person_phoneNo_label" />
<EditText
android:id="#+id/person_phoneNo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="phone"
android:maxLength="10"
android:minWidth="150dp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:text="#string/account_person_password_label" />
<EditText
android:id="#+id/password"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="textPassword"
android:minWidth="150dp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:text="#string/account_person_retypePassword_label" />
<EditText
android:id="#+id/retype_password"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="textPassword"
android:minWidth="150dp"/>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_marginTop="20dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/learner_tutor_status_label" />
<android.support.v7.widget.AppCompatSpinner
android:id="#+id/learner_tutor_status_spinner"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
</LinearLayout>
</ScrollView>
<!--Floating Button-->
<Button
android:id="#+id/create_account"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:text="#string/create_account_string"
style="#style/Widget.AppCompat.Button.Colored"/>
</FrameLayout>
Now, you can see at the bottom there is I am here to.... This is a dropdown and when I choose one of the options, my code dynamically adds a few views to the hierarchy but the problem is they are not visible(not all of them; just I am a... is visible.)
I checked the layout hierarchy through the Layout inspector and turns out that the hierarchy is alive but the floating button(Create Account) has overshadowed it.
Now, I know if I were to replace the root FrameLayout with a LinearLayout, it would be visible but the problem is that if then I would click on a EditText button, the keypad would cover the screen with the Create Account button as covered too. I actually want this button to be on the screen at all times.
Does anyone how can I get around this problem?
Your layout is not totally correct because your floating button hides the bottom of your ScrollView all the time. You should replace the FrameLayout with a vertical LinearLayout (just like you said) and then specify the window behavior of your activity when the soft keyboard pops up with the windowSoftInputMode attribute in your activity element on the AndroidManifest.xml file. If you use android:windowSoftInputMode="adjustResize", the window will resize accordingly and the button should be always visible.
I have relative layout. Within which I have many views like edit text, text views and so on. I do lots of animations in that. Everything was perfectly fine. But at one point, I have a requirement.
It is the password page. Create and confirm password. Two edittexts one after another. If the password doesnt meet the requirement, a plain orange view should slide up with error message "Invalid password".
So I put create and confirm password in linear layout. The linear layout will slide up, letting user type in passwords. After validation, if there is an error, the linear layout should slide down and the plain view will slide up. I kept the view invisible and when required I made it visible and wrote the code to slide up. The same code works when I just use single edit text instead of linear layout. But it is not working when using linear layout. Am I doing something wrong?
Adding code to it. The code specific to this issue starts with till the end of coding
I declared linear layout as view and NOT VIEWGROUP.
EDIT: After doing analysis, I found that the view I am trying to slide up after sliding down the layout is hidden somewhere in the view hierarchy.
view.bringToFront() helps to bring the error view to the front and slides up.
But after that the problem is, after my linear layout slide down, the error view is not sliding up straight away. Because I am using bringToFront(), it displays the view first on the whole screen (with background color as orange) and then the sliding starts.
I am not sure whether it is correct but I fixed this issue like this.
no background color has been given to the view
I used errorView.bringToFront () and errorView.setVisibility(true) in the animationEnd() of a Layout slidingdown animation listener.
I defined sliding up animation for the errorView in the same animationEnd() of layout sliding down listener
In the animationStart() of errorView sliding up animation listener, I defined the background color of the errorView. Now its working as expected.
But let me know better way of doing this.
//Declaration
View greyView, orangeView, yellowView, blueView, orangeErrorView, letsGetStartedView, welcomeBckView, passwordMasterLayout, orangePwdErrorView;
TextView welcomeText,errorTextView, letsGetStartedTextView, welcomeBckTxtView, errorPwdMsg;
//Password Error Handling
orangePwdErrorView = v.findViewById(R.id.orangePasswordErrorView);
errorPwdMsg = (TextView) v.findViewById(R.id.errorPasswordMessage);
//Layout code
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/sample_main_layout">
<FrameLayout
android:id="#+id/sample_content_fragment"
android:layout_weight="76.4"
android:layout_width="match_parent"
android:layout_height="0px" />
<FrameLayout
android:id="#+id/bottomPanel"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="23.6">
</FrameLayout>
</LinearLayout>
sample_content_fragment framelayout takes the below code
<RelativeLayout
android:layout_height="match_parent"
android:layout_width="match_parent"
xmlns:android="http://schemas.android.com/apk/res/android">
<View
android:id="#+id/grey"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#58595B" />
<View
android:id="#+id/orange"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#F26724"
android:visibility="invisible" />
<View
android:id="#+id/yellow"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#FDB518"
android:visibility="invisible" />
<View
android:id="#+id/blue"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#1893D2"
android:visibility="invisible" />
<TextView
android:layout_width="260dp"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="#string/welcome"
android:id="#+id/welcomeTextView"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="51dp"
android:textColor="#ffffff"
android:textSize="20dp"
android:textStyle="bold"
android:visibility="invisible"
/>
<ImageView
android:layout_height="60dp"
android:layout_width="60dp"
android:id="#+id/clinicloud_logo"
android:clickable="true"
android:src="#drawable/clinicloud_icon"
android:layout_marginRight="50dp"
android:layout_alignParentStart="true"
android:layout_marginStart="35dp"
android:layout_alignParentBottom="true"
android:layout_marginBottom="40dp" />
<View
android:id="#+id/orangeErrorView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#F26724"
android:visibility="invisible" />
<EditText
android:layout_width="match_parent"
android:layout_height="90dp"
android:inputType="textEmailAddress"
android:ems="10"
android:id="#+id/emailEditText"
android:text="#string/enter_email"
android:layout_gravity="center"
android:background="#f4f4f4"
android:layout_alignParentBottom="true"
android:layout_alignParentStart="true"
android:visibility="invisible"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:id="#+id/errorMessage"
android:layout_alignTop="#+id/welcomeTextView"
android:layout_centerHorizontal="true"
android:textColor="#ffffff"
android:visibility="invisible"/>
<!--UI components for welcome back page-->
<View
android:id="#+id/welcomeBackView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#134F9F"
android:visibility="invisible" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/welcome_back"
android:id="#+id/welcomeBackTxtView"
android:textColor="#ffffff"
android:layout_alignTop="#+id/welcomeTextView"
android:layout_alignStart="#+id/clinicloud_logo"
android:textSize="20dp"
android:textStyle="bold"
android:visibility="invisible"/>
<EditText
android:layout_width="wrap_content"
android:layout_height="90dp"
android:ems="10"
android:id="#+id/password"
android:layout_alignTop="#+id/emailEditText"
android:layout_alignParentStart="true"
android:layout_alignParentBottom="true"
android:layout_alignParentEnd="true"
android:text="Password"
android:visibility="invisible"
android:background="#d2d1d1"
/>
<!--UI components for Let's get started page-->
<View
android:id="#+id/letsGetStartedView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#5C3A91"
android:visibility="invisible" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Let's get started"
android:id="#+id/letsGetStartedTxtView"
android:textColor="#ffffff"
android:layout_alignTop="#+id/welcomeTextView"
android:layout_alignStart="#+id/clinicloud_logo"
android:textSize="20dp"
android:textStyle="bold"
android:visibility="invisible"/>
<!--Linear layout to show the create and confirm password-->
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="180dp"
android:gravity="bottom"
android:layout_alignBottom="#+id/emailEditText"
android:layout_alignParentStart="true"
android:weightSum="1"
android:id="#+id/passwordMasterView"
android:visibility="invisible">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="text"
android:id="#+id/createPwd"
android:layout_weight="0.50"
android:background="#F4F4F4"
android:text="#string/create_password"
/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="text"
android:id="#+id/confirmPwd"
android:layout_weight="0.50"
android:background="#E3E3E3"
android:text="#string/confirm_password" />
</LinearLayout>
<!-- To show password errors-->
<LinearLayout
android:id="#+id/orangePasswordErrorView"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#F26724"
android:visibility="invisible" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:id="#+id/errorPasswordMessage"
android:layout_alignTop="#+id/welcomeTextView"
android:layout_centerHorizontal="true"
android:textColor="#ffffff"
android:visibility="invisible"
/>
<!-- To show password errors-->
</RelativeLayout>
The java logic is in https://stackoverflow.com/questions/30523599/sliding-up-and-down-android
Please direct for any incorrect codings
I kept the view invisible and when required I made it visible and wrote the code to slide up.
I think you must do it in DialogFragment or custom AlertDialog that will be shown above your layout, if dont want to do this try envelop your RelativeLayout in FrameLayout and adding in it LinearLayout for example but visout your code difficult to say in what problem it is
You should consider using a library for the sliding panel, one that I've had excellent experiences with (and supports the functionality that your after) is umano's AndroidSlidingUpPanel. https://github.com/umano/AndroidSlidingUpPanel
I have a simple layout with:
top bar (like action bar) with save and back button
and below a scroll view with some EditText inside... (check the image below),
When I call keyboard, my view is push above... I would like my Top Bar to stay always visible and fixed on the top... (check below the image of what is happening and what I would like to happen)
I tried several solutions with android:windowSoftInputMode, putting the top bar as relative layout and set isScrollContainer in scrollview but none have worked for me...
Code Below:
<RelativeLayout
android:id="#+id/llWriteCommentWall"
style="#style/TextSubSeparator"
android:layout_height="wrap_content"
android:layout_width="wrap_content">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
style="#style/TextSubSeparator"
android:layout_marginBottom="#dimen/margin_separator_bottom"
android:layout_alignParentTop="true">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:gravity="left|center_horizontal|center_vertical"
android:orientation="horizontal"
android:layout_marginTop="#dimen/margin_title_top"
android:layout_marginBottom="#dimen/margin_title_bottom"
android:layout_marginLeft="#dimen/margin_title_left"
android:layout_marginRight="#dimen/margin_title_right"
>
<ImageView
android:id="#+id/ivMainMenu"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:adjustViewBounds="true"
android:scaleType="fitCenter"
android:src="#drawable/tk_btn_home"
android:onClick="clickHome"/>
</LinearLayout>
<TextView
android:id="#+id/tvSeconHeader"
android:text="#string/title"
android:layout_width="match_parent"
android:layout_weight="2.5"
android:layout_height="match_parent"
android:gravity="center"
style="#style/TextTileHealth"/>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:gravity="right|center_horizontal"
android:orientation="horizontal"
android:layout_marginTop="#dimen/margin_title_top"
android:layout_marginBottom="#dimen/margin_title_bottom"
android:layout_marginLeft="#dimen/margin_title_left"
android:layout_marginRight="#dimen/margin_title_right">
<ImageView
android:id="#+id/ivSave"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:adjustViewBounds="true"
android:scaleType="fitCenter"
android:src="#drawable/tk_btn_tab_save"
android:onClick="clickSave"/>
</LinearLayout>
</LinearLayout>
</RelativeLayout>
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:isScrollContainer="false">
<LinearLayout
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="#dimen/margin_separator_bottom"
android:gravity="center">
<!-- EditBoxes -->
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<EditText
android:id="#+id/et1"
style="#style/TextEditBox"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:digits="0123456789."
android:inputType="numberDecimal"
android:maxLength="3"
android:layout_weight="0.33"
android:gravity="center"
android:ems="3"
android:layout_marginBottom="10dp"/>
<EditText
android:id="#+id/et2"
style="#style/TextEditBox"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:digits="0123456789."
android:inputType="numberDecimal"
android:maxLength="3"
android:layout_weight="0.33"
android:gravity="center"
android:ems="3"
android:layout_marginBottom="10dp"/>
<EditText
android:id="#+id/et3"
style="#style/TextEditBox"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:digits="0123456789."
android:inputType="numberDecimal"
android:maxLength="3"
android:layout_weight="0.33"
android:gravity="center"
android:ems="3"/>
</LinearLayout>
</LinearLayout>
</ScrollView>
And Manifest:
<activity
android:name="com.example.slyzer.gui.new.NewValue"
android:label="#string/app_name"
android:windowSoftInputMode="adjustResize"
android:screenOrientation="portrait">
</activity>
EDIT:
I recently find out, that when you put the activity as Full Screen:
this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
, the windows starts to act different from usual:
https://code.google.com/p/android/issues/detail?id=5497&q=fullscreen&colspec=ID%20Type%20Status%20Owner%20Summary%20Stars
and as described by Dianne Hackborn in here, this is how it's working, and it seems that there is no interest in changing it.
So, I tried the Joseph Johnson's AndroidBug5497Workaround, but I always end up with a "extra" blank bar above my keyboard, and the View steel doesn't show as I want it to...
Does anyone have a nice/simple solution to this problem?
android:windowSoftInputMode="adjustResize" in the manifest for your activity will keep the top bar in view and resize the scrollview to use what room is left.
The only issue I can see with this would be lining up the edit text with the viewable area after it has been resized (if this is needed)
Add this to the activity tag in the manifest:
android:windowSoftInputMode="adjustPan"
Or programmatically:
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);
OK, so I have this very basic layout:
As you can see, the edit texts and the login button are centered in the area below the icon. They are not aligned with the bottom. And I want to keep it this way.
Now, when the soft keyboard is shown, all I want is that the whole view is pushed upwards, so that the login button is above the soft keyboard but takes into account its lower margin.
My activity has windowSoftInputMode set to adjustPan, but it just assures that the currently focused control is visible, not all three of them:
Question: What do I need to change so that all three controls are visible as soon as the keyboard is visible?
This is my view:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:local="http://schemas.android.com/apk/res/Recson.Apps.Android"
style="#style/Theme.Recson.NoActionBar"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/LoginViewRoot">
<ImageView
android:src="#drawable/Logo"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/Icon"
android:layout_marginBottom="30dp"
android:layout_marginTop="30dp"
android:layout_gravity="top" />
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/Credentials"
android:layout_gravity="center_vertical"
android:gravity="center_vertical"
android:layout_below="#id/Icon">
<EditText
android:inputType="textEmailAddress"
android:layout_width="match_parent"
android:layout_height="wrap_content"
local:MvxBind="Text Username"
android:hint="#string/Username"
android:layout_marginLeft="30dp"
android:layout_marginRight="30dp"
android:textColorHint="#color/text_hint" />
<EditText
android:inputType="textPassword"
android:layout_width="match_parent"
android:layout_height="wrap_content"
local:MvxBind="Text Password"
android:hint="#string/Password"
android:layout_marginLeft="30dp"
android:layout_marginRight="30dp"
android:textColorHint="#color/text_hint" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/Login"
local:MvxBind="Click Login;Enabled CanLogin"
android:layout_marginLeft="30dp"
android:layout_marginRight="30dp"
android:layout_marginTop="20dp"
android:layout_marginBottom="30dp" />
</LinearLayout>
</RelativeLayout>
Try to use ScrollView and put your main layout inside it. I think it will do the job.
Try this in Manifest file.
<activity name="ActivityName"
android:windowSoftInputMode="stateVisible|adjustResize">
...
</activity>
As you're experiencing, adjustPan, adjustResize etc will only try to ensure the currently focused input field is visible.
What you could try to do is remove the LinearLayout then position your username password and login views in such a way that the system thinks they occupy the same space by using layout_marginTop and layout_marginBottom.
Just put this in your activity tag of your menifest file:
android:configChanges="screenSize"
And Use ScrollView in your Xml file something like below:
(I have changed your layout a bit):
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/LoginViewRoot"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ImageView
android:id="#+id/Icon"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="top"
android:layout_marginBottom="30dp"
android:layout_marginTop="30dp"
android:src="#drawable/ic_launcher" />
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_above="#+id/btnLogin"
android:layout_below="#id/Icon" >
<LinearLayout
android:id="#+id/Credentials"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:gravity="center_vertical"
android:orientation="vertical" >
<LinearLayout
android:id="#+id/Credentials1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:gravity="center_vertical"
android:orientation="vertical" >
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="30dp"
android:layout_marginRight="30dp"
android:inputType="textEmailAddress" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="30dp"
android:layout_marginRight="30dp"
android:inputType="textPassword" />
</LinearLayout>
</LinearLayout>
</ScrollView>
<Button
android:id="#+id/btnLogin"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_marginBottom="30dp"
android:layout_marginLeft="30dp"
android:layout_marginRight="30dp"
android:layout_marginTop="20dp"
android:text="Login" />
</RelativeLayout>
At least, the answers here show that I wasn't missing anything obvious.
I now solved it like that:
windowSoftInputMode set to adjustResize
Complete layout inside a ScrollView
On layout change, I simply scroll down the ScrollView to the bottom:
public class LoginView : MvxActivity, ViewTreeObserver.IOnGlobalLayoutListener
{
private ScrollView _activityRootView;
public void OnGlobalLayout()
{
var focusedControl = Window.CurrentFocus;
_activityRootView.FullScroll(FocusSearchDirection.Down);
if (focusedControl != null)
focusedControl.RequestFocus();
else if (Window.CurrentFocus != null)
Window.CurrentFocus.ClearFocus();
}
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
SetContentView(Resource.Layout.LoginView);
_activityRootView = FindViewById<ScrollView>(Resource.Id.LoginViewRoot);
_activityRootView.ViewTreeObserver.AddOnGlobalLayoutListener(this);
}
}
(this is C# code using Xamarin.Android and MvvmCross. It is based on this answer)
This solution has one drawback: The controls are no longer centered in the area below the icon, but right now, I am OK with that:
See it in action
Used This Code :
public MainPage()
{
InitializeComponent();
App.Current.On<Android>().UseWindowSoftInputModeAdjust(WindowSoftInputModeAdjust.Resize);
}