Make soft keyboard behave like IOS in Android - android

I have a layout which I placed my widgets proportionally with weight attribute. I don't want to use scrollView, I want my widgets to fit onto screen according to their weights.
However, I want my layout to be scrollable when soft keyboard pops up. I don't want it to be resized so I can't use android:windowSoftInputMode="adjustResize"
I tried android:windowSoftInputMode="adjustPan" and it moves the edittext field above the soft keyboard. However, this time it does not scroll and I have to close soft keyboard to choose next EditText field.I know that by adding input type, I can have forward buttons on my keyboard. However, I don't want to depend on that only, I also want it to be scrollable without resizing like in IOS.
Can I achieve that? If yes, could you explain this to me , please?
Here is my layout:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/bg"
android:orientation="vertical"
android:weightSum="20">
<LinearLayout
android:layout_width="0px"
android:layout_height="0px"
android:focusable="true"
android:focusableInTouchMode="true" />
<FrameLayout
android:id="#+id/imagelogin"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_gravity="center"
android:layout_weight="12"
android:gravity="center">
<ImageView
android:id="#+id/image"
android:layout_width="300dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
android:src="#drawable/logo_home" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="40dp"
android:layout_gravity="bottom"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:orientation="horizontal"
android:weightSum="2">
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_marginRight="5dp"
android:layout_weight="1"
android:background="#color/material_blue_grey_800"
android:gravity="center"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:text="Tab 1"
android:textColor="#android:color/white"
android:textSize="20sp" />
</LinearLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_marginLeft="5dp"
android:layout_weight="1"
android:gravity="center"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:text="Tab 2"
android:textColor="#android:color/white"
android:textSize="20sp" />
</LinearLayout>
</LinearLayout>
<include
android:id="#+id/toolbar"
layout="#layout/app_bar"></include>
</FrameLayout>
<FrameLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="8">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Input 1" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Input 1" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Input 1" />
</LinearLayout>
</FrameLayout>
</LinearLayout>
And this is how it looks:

That functionality isn't built into Android. If you want that, you need to put your entire layout inside of a scroll view.

Related

Android How to only resize ScrollView on showing softkeyboard

So i have a layout that has a ToolBar above, a ScrollView with TextViews on it and 2 Buttons under the ScrollView. I need the Buttons to be under the SoftKeyboard when shown but making the ScrollView resize on the edge of the top of softkeyboard. I already added on the manifest
android:windowSoftInputMode="adjustNothing"
and on the ScrollView
android:isScrollContainer="false"
The problem is i cant scroll on the edge of scrollview when keyboard is shown. So I need to resize it on showing the keyboard
Heres the Layout
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/map_new_bg"
android:orientation="vertical"
android:padding="10dp">
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:isScrollContainer="false" >
<LinearLayout
android:id="#+id/llRegInfo"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="vertical"
android:padding="10dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<EditText
android:id="#+id/etFirstName"
style="#style/EditText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_weight="1"
android:ems="10"
android:inputType="textPersonName"
android:text="" />
<EditText
android:id="#+id/etFirstName"
style="#style/EditText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_weight="1"
android:ems="10"
android:inputType="textPersonName"
android:text="" />
....
</LinearLayout>
<!--MoreEditTexts Inside LinearLayout Like Above-->
</LinearLayout>
</ScrollView>
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:id="#+id/linearLayout7"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintLeft_toLeftOf="parent">
<Button
android:id="#+id/btnRegBack"
style="#style/RedButton"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:layout_weight="1"
android:text="#string/return_text" />
<Button
android:id="#+id/btnRegNext"
style="#style/RedButton"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:layout_weight="1"
android:text="#string/next_text"
android:textAllCaps="false" />
</LinearLayout>
</LinearLayout>
Have you tried to tell Android what to do when the SoftKeyboard appears?
getWindow()
.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
(Take a look at all the values, to see which one fits your bill).

How to put buttons below scrollview and always show button without being overlapped by scrollview in Android?

My layout is like this
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<EditText
android:id="#+id/searchKey"
android:gravity="center_horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="xx" />
<ScrollView
android:layout_weight="1"
android:fillViewport="true"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="#+id/search_results"
android:textSize="#dimen/text_font_size"
android:layout_gravity="center_horizontal"
android:gravity="center_horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</ScrollView>
<LinearLayout
android:orientation="horizontal"
android:layout_gravity="center|bottom"
android:layout_weight="1"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button
android:id="#+id/lastCharacterButton"
android:layout_weight="1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/lastChar_btn" />
<Button
android:id="#+id/nextCharacterButton"
android:layout_weight="1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/nextChar_btn" />
</LinearLayout>
</LinearLayout>
However, when there are little or no text, the buttons are pulled up, when there are too many text in search_results TextView, the buttons won't show up because scrollview takes the space all the way to the bottom. Any suggestion to always keep the buttons at the bottom and always visible?
First of all you have to set the ScrollView's height to 0dp.
<ScrollView
android:layout_weight="1"
android:fillViewport="true"
android:layout_width="match_parent"
android:layout_height="0dp">
<TextView
android:id="#+id/search_results"
android:textSize="#dimen/text_font_size"
android:layout_gravity="center_horizontal"
android:gravity="center_horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</ScrollView>
You have to do this because the weight param is going to control the size of the view depending on how much space is available. If you set it to wrap content, it will take how much space it needs to wrap everything within.
Then you need to remove your LinearLayout's weight param:
<LinearLayout
android:orientation="horizontal"
android:layout_gravity="center|bottom"
android:id="#+id/LL1"
android:layout_alignParentBottom="true"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button
android:id="#+id/lastCharacterButton"
android:layout_weight="1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Last_Char" />
<Button
android:id="#+id/nextCharacterButton"
android:layout_weight="1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="next_char" />
</LinearLayout>
And the reason for doing that is just the opposite of the previous explanation: in this case the wrap content is going to take the space necessary to always display the buttons within.
Here is some sample code I put together for you. You basically need to take out the linear layout you have wrapping you xml layout and use a relative layout instead. Once you do that you can add ids to the linear layout with the buttons and then have the edit text align to the parent top. Then you can put the scrollview below the edittext and then put the scrollview above to the linear layout containing the buttons. This should work!
<?xml version="1.0" encoding="utf-8"?>
<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"
tools:context="com.example.thegamefan93.loginregister.LoginActivity">
<EditText
android:id="#+id/searchKey"
android:gravity="center_horizontal"
android:layout_width="match_parent"
android:layout_alignParentTop="true"
android:layout_height="wrap_content"
android:hint="xx" />
<ScrollView
android:layout_weight="1"
android:fillViewport="true"
android:layout_below="#id/searchKey"
android:layout_width="match_parent"
android:layout_above="#+id/LL1"
android:layout_height="wrap_content">
<TextView
android:id="#+id/search_results"
android:textSize="20sp"
android:layout_gravity="center_horizontal"
android:gravity="center_horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</ScrollView>
<LinearLayout
android:orientation="horizontal"
android:layout_gravity="center|bottom"
android:layout_weight="1"
android:id="#+id/LL1"
android:layout_alignParentBottom="true"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button
android:id="#+id/lastCharacterButton"
android:layout_weight="1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Last_Char" />
<Button
android:id="#+id/nextCharacterButton"
android:layout_weight="1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="next_char" />
</LinearLayout>
</RelativeLayout>
Try 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"
android:orientation="vertical">
<EditText
android:id="#+id/searchKey"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:hint="xx" />
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_above="#+id/linear"
android:layout_below="#+id/searchKey"
android:layout_weight="1"
android:fillViewport="true">
<TextView
android:id="#+id/search_results"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:gravity="center_horizontal"
android:textSize="#dimen/text_font_size" />
</ScrollView>
<LinearLayout
android:id="#+id/linear"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:orientation="horizontal">
<Button
android:id="#+id/lastCharacterButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="#string/lastChar_btn" />
<Button
android:id="#+id/nextCharacterButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="#string/nextChar_btn" />
</LinearLayout>
</RelativeLayout>

The layout changes greatly on rotation. How to fix it?

I made a layout for my app using linear layout. It looks fine and works fine but on rotation the layout changes very differently like the edit text becomes very small and disappears when keyboard appears. So how to provide a consistent look for my application. Or what am I missing?
Here is the code of the layout
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.example.nobodyme.errorrepository.MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="2"
android:background="#F5F5F5"
android:elevation="3dp"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="8dp"
android:layout_marginTop="16dp"
android:text="Folders"
android:textAppearance="?android:textAppearanceLarge"
android:textColor="#android:color/background_dark" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="2"
android:gravity="center"
android:orientation="horizontal"
android:layout_marginBottom="16dp">
<EditText
android:id="#+id/foldername"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_margin="4dp"
android:layout_weight="4"
android:background="#android:color/background_light" />
<Button
android:id="#+id/enterFolderButton"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginBottom="4dp"
android:layout_weight="1"
android:text="OK"
android:textColor="#android:color/background_light"
android:background="#000000" />
</LinearLayout>
</LinearLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="5">
<ListView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/folderlistview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:cacheColorHint="#color/colorPrimary" />
<TextView
android:id="#+id/empty_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="Create folders to add error"
android:textAppearance="?android:textAppearanceMedium" />
</RelativeLayout>
</LinearLayout>
Here how it looks on my device, four images below show how it looks on vertical and horizontal position with and without clicking on edit text, i.e with keyboard on and off.

keyboard hides the layout components

How do I prevent the keyboard from hiding the layout when it pops up? I'm using this line of code to resize the layout when the keyboard open up.
Window.SetSoftInputMode(SoftInput.AdjustResize);
The result is, while the screen resizes for some devices, it doesn't to the extent I want for other devices. For example here's what the layout looks on one device:
and here's how it looks on another device:
In the second image as you can see, the login button is half visible due to the keyboard overlapping it. Also the "Login" Text at the top is chopped off. How do I make it so the layout looks the same for all devices?
Edit
I also have this line in my Menifest file:
<activity android:name=".AccountActivity" android:label="yourtime" android:windowSoftInputMode="adjustResize|stateHidden"></activity>
here's the code for the layout:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:minWidth="25px"
android:minHeight="25px"
android:weightSum="100"
android:id="#+id/signinwrapper"
android:background="#F73494">
<LinearLayout
android:orientation="vertical"
android:minWidth="25px"
android:minHeight="25px"
android:layout_width="match_parent"
android:layout_height="0dp"
android:id="#+id/linearLayout2"
android:layout_weight="5"
android:gravity="center">
<TextView
android:text="Login"
android:textAppearance="?android:attr/textAppearanceLarge"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/textView1"
android:textAllCaps="true"
android:textColor="#FFFF"
android:textSize="14sp" />
</LinearLayout>
<LinearLayout
android:paddingTop="20dp"
android:orientation="horizontal"
android:minWidth="25px"
android:minHeight="25px"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="40"
android:id="#+id/linearLayout1">
<ImageView
android:src="#drawable/logo"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/imageView1" />
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:minWidth="25px"
android:minHeight="25px"
android:layout_width="match_parent"
android:layout_height="0dp"
android:id="#+id/linearLayout2"
android:layout_weight="15"
android:paddingTop="10dp">
<TextView
android:text="Your home of Entertainment"
android:textAppearance="?android:attr/textAppearanceMedium"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/textView1"
android:gravity="center"
android:paddingLeft="20dp"
android:paddingRight="20dp"
android:textAllCaps="true"
android:textColor="#FFFF" />
</LinearLayout>
<LinearLayout
android:orientation="vertical"
android:minWidth="25px"
android:minHeight="25px"
android:layout_width="match_parent"
android:layout_height="0dp"
android:id="#+id/linearLayout3"
android:layout_weight="45">
<EditText
android:id="#+id/username"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textEmailAddress"
android:hint="Username"
android:background="#drawable/editor"
android:layout_marginLeft="70dp"
android:layout_marginRight="70dp"
android:textSize="20dp"
android:gravity="center"
android:textStyle="bold"
android:textColor="#FFFF" />
<EditText
android:id="#+id/password"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textPassword"
android:hint="Password"
android:background="#drawable/editor"
android:textSize="20dp"
android:layout_marginTop="10dp"
android:layout_marginLeft="70dp"
android:layout_marginRight="70dp"
android:gravity="center"
android:textStyle="bold"
android:textColor="#FFFF" />
<ProgressBar
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/progressBar"
android:layout_centerInParent="true"
android:layout_marginTop="20dp"
android:background="#drawable/ProgressBarStyle"
android:layout_gravity="center_horizontal"
android:visibility="gone" />
<Button
android:text="Login"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/btnlogin"
android:layout_marginLeft="100dp"
android:layout_marginRight="100dp"
android:background="#drawable/buttonstyles"
android:textColor="#F73494"
android:layout_marginTop="20dp"
android:textSize="12sp" />
</LinearLayout>
Don't do that in code. Do it in the AndroidManifest xml file. You'll get odd results doing it in code, especially if you have dialogs.
What doing that mode does is force your app to resize to go above your keyboard. However- some things just take space. If your layout has sufficient whitespace to resize, it will. If it doesn't, then the keyboard will still cover part of the app. You didn't post your layout, so I can't give you a more detailed answer.

layout_gravity="bottom" not working

In my android layout, I have two EditText elements. They appear side by side at the bottom of the screen. But when i type multiple lines into the first EditText(on the left), the second one(at the right) also keeps moving upwards even though it is empty. I have specified android:layout_gravity="bottom" and android:gravity="bottom" for the second EditText. But it has no effect. What am i doing wrong? How can i make the second EditText stay at the vertical center of its parent linear layout, no matter how many lines are entered in the first EditText.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:background="#drawable/bg_textinput"
android:layout_alignParentBottom="true">
<EditText
android:id="#+id/text_input"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:layout_weight="0.7"
android:inputType="textMultiLine" />
<EditText
android:id="#+id/text_input1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:gravity="bottom"
android:layout_weight="0.3"
android:inputType="textMultiLine" />
</LinearLayout>
</RelativeLayout>
This will work:
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_alignParentBottom="true"
android:gravity="center">
<EditText
android:id="#+id/text_input"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.7"
android:inputType="textMultiLine" />
<EditText
android:id="#+id/text_input1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:gravity="bottom"
android:layout_weight="0.3"
android:inputType="textMultiLine" />
</LinearLayout>
try this,
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<EditText
android:id="#+id/text_input"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:layout_weight="0.7"
android:inputType="textMultiLine" />
<EditText
android:id="#+id/text_input1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:layout_weight="0.3"
android:gravity="bottom"
android:inputType="textMultiLine" />
</LinearLayout>

Categories

Resources