Editext hide behind the keyboard when it appear - android

I want to move only edittext when keyboard will appear.
Here is my XML codes.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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=".chat.MultipleMediaActivity">
<androidx.viewpager.widget.ViewPager
android:id="#+id/multiple_media_pager"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginStart="0dp"
android:layout_marginTop="0dp" />
<include
android:layout_width="match_parent"
android:layout_height="60dp"
android:layout_marginTop="40dp"
layout="#layout/multiple_media_custom_toolbar" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentStart="true"
android:background="#drawable/multimedia_edittext_bg_ca"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="50dp"
android:orientation="horizontal"
android:layout_marginBottom="5dp">
<ImageView
android:id="#+id/emoj_button"
android:layout_width="24dp"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_marginStart="13dp"
android:src="#drawable/main_emoj_ca"
android:tag="emoji" />
<com.vanniktech.emoji.EmojiEditText
android:id="#+id/multiple_media_text_message"
android:layout_width="match_parent"
android:layout_height="50dp"
android:background="#color/AppTrasColor"
android:hint="#string/write_message_ca"
android:inputType="textCapSentences|textMultiLine"
android:maxLines="4"
android:minHeight="50dp"
android:textColor="#color/AppFirstTextColorDark"
android:textColorHint="#color/AppDullTextColor"
android:textSize="#dimen/Text17"
tools:ignore="LabelFor"
android:layout_marginStart="10dp"/>
</LinearLayout>
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/multiple_media_preview"
android:layout_width="match_parent"
android:layout_height="60dp"
android:layout_marginStart="10dp"
android:layout_marginEnd="10dp">
</androidx.recyclerview.widget.RecyclerView>
</LinearLayout>
</RelativeLayout>
What I have tried
in manifest
android:windowSoftInputMode="adjustPan"
When edittext getting focus, edittext stay below the keyboard,
I want to setup like
Bottom line of edittext and Top line of keyboard will be the same. It means edittext will always stay above the keyboard when it appear.

You can't. Android has 3 modes for when the keyboard appears:
adjustNone- do nothing. The keyboard will appear over the app as it is. This is almost never used
adjustResize- the keyboard is asked for its height, and the app is fit into the remaining screen. This will cause elements to be relaid out.
adjustPan- the app will scroll such that the cursor is on screen. If this is a multiline edit box, it promises that the line with the cursor will be shown. Other lines below it may or may not be, depending on where on the screen that cursor is.
There is no mode for moving the entire edit text above the keyboard.

Related

Android: SoftKeyBoard cover up MultiLine edittext

I have a multi-line editText inside a scrollView follow by a button.
If using adjustPan, the softkeyboard cover up the whole editText, leaving only the first line.
When using adjustResize, even the button shows up which is worst
Here is my xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ScrollView
android:layout_width="match_parent"
android:layout_height="0dp"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toTopOf="#id/btn_save">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="600dp"
android:background="#color/LightBlue"/>
<androidx.appcompat.widget.AppCompatEditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter text"
android:inputType="textMultiLine"
android:singleLine="false"
android:gravity="top"
android:minLines="8" />
</LinearLayout>
</ScrollView>
<com.google.android.material.button.MaterialButton
android:id="#+id/btn_save"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Save"
android:clickable="false"
app:backgroundTint="#color/gray_jll"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintBottom_toBottomOf="parent"/>
</androidx.constraintlayout.widget.ConstraintLayout>
What I want (temporarily achived using gravity bottom, but of course we want the text to display on the top using gravity top)
I looked at Android soft keyboard covers EditText field.
But I am not using full screen mode.
android:isScrollContainer="true" does nothing.
Please tell me what is causing this. Thanks in advance.

Squeeze spaces one after another

I'm not sure what title should I put in here but here is the question.
I want my fields to be attached to the bottom of the screen when keyboard is out.
When there is no keyboard, I want my fields to be at the center of the screen.
Basically 1 and 2 are spaces and I want Space I to be first candidate on squeezing when keyboard is out.
<?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">
<LinearLayout
android:fitsSystemWindows="true"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<android.support.v4.widget.Space
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"/>
<android.support.design.widget.TextInputLayout
android:id="#+id/walletPasswordEditLayout"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginEnd="16dp"
android:layout_marginStart="16dp">
<android.support.design.widget.TextInputEditText
style="#style/AppEditText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textPassword"/>
</android.support.design.widget.TextInputLayout>
<Button
style="#style/AppButton.Black"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginEnd="16dp"
android:layout_marginStart="16dp"
android:text="#string/unlock_button"/>
<android.support.v4.widget.Space
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"/>
</LinearLayout>
</FrameLayout>
add this in your activity
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);
Please check this Link:
You can add in your menifest for the activity you want to prevent such issue. Also make sure if you are having a bit more content use scrollView as a parent. And soft input mode as "adjustResize"
android:windowSoftInputMode="adjustResize"
or
android:windowSoftInputMode="adjustPan"

Resize views on Soft Keyboard launch

I am trying to create a chat application. The chat layout currently looks like this
The corresponding layout file is
<?xml version="1.0" encoding="utf-8"?>
<ScrollView
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:fillViewport="true"
android:layout_height="match_parent">
<RelativeLayout
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="com.example.nirmal.chatadapter.MainActivity">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="#color/colorPrimary">
<de.hdodenhof.circleimageview.CircleImageView
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/profile_image"
android:layout_width="40dp"
android:layout_height="wrap_content"
android:src="#drawable/iconuser"
app:civ_border_width="2dp"
app:civ_border_color="#color/White"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginStart="#dimen/marginLeftForToolbar"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#color/White"
android:text="UserName Here"
android:layout_marginTop="#dimen/marginTopBottomForBubble"
android:id="#+id/user_name"
android:textSize="#dimen/textMedium"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#color/White"
android:text="User details Here"
android:layout_marginTop="#dimen/marginTopBottomForBubble"
android:id="#+id/user_details"
android:textSize="#dimen/textSmall"/>
</LinearLayout>
</android.support.v7.widget.Toolbar>
<android.support.v7.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#id/toolbar"
android:layout_marginTop="#dimen/recyclerViewMargin"
android:isScrollContainer="true"
android:id="#+id/chatRecycler"
android:layout_above="#+id/chatBoxAndSend">
</android.support.v7.widget.RecyclerView>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentBottom="true"
android:orientation="horizontal"
android:layout_marginBottom="5dp"
android:id="#+id/chatBoxAndSend">
<EditText
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:minWidth="50dp"
android:maxWidth="50dp"
android:id="#+id/chatBox"
android:paddingStart="#dimen/cardViewPadding"
android:background="#drawable/rounded_chat_box"
android:layout_margin="#dimen/marginForChatBox"
/>
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.2"
android:background="#drawable/oval_button"
android:minWidth="50dp"
android:maxWidth="50dp"
android:text="Send"
android:imeOptions="flagNoExtractUi"
android:id="#+id/chatSendButton"
android:layout_margin="#dimen/marginForChatBox"
android:textColor="#color/Black"
/>
<!--<ImageView
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="0.1"
android:id="#+id/send_button"
android:src="#drawable/send"
android:background="#drawable/oval_button"
android:layout_marginBottom="#dimen/marginForChatBox"
android:layout_marginTop="#dimen/marginForChatBox"
android:layout_marginEnd="#dimen/marginForChatBox"
/>
<de.hdodenhof.circleimageview.CircleImageView
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="0.1"
android:id="#+id/send_button"
android:src="#drawable/send"
app:civ_border_width="2dip"
android:layout_marginEnd="#dimen/marginForChatBox"
app:civ_border_color="#color/greenChatColor"/>-->
</LinearLayout>
</RelativeLayout>
</ScrollView>
When I open my soft keyboard the layout looks likes this
I have added this in my manifest file
android:windowSoftInputMode="adjustPan|stateHidden"
The output is not what I expected it to be. I want the recyclerview to dynamically resize in the center of the screen and the Toolbar and the edittext to stay fully visible.
As you can see the toolbar has been pushed up and is not visible even when scrolled. The edittext is hidden partially by the suggestions given by the keyboard. Some of the posts I saw asked me to add a scrollview which I did and still the same result. How can I solve this problem?
If you want to resize RecyclerView on SoftInput open or close then you can use below single line code
((LinearLayoutManager)myRecyclerView.getLayoutManager()).setStackFromEnd(true);
For more read this post: https://trinitytuts.com/tips/resize-recyclerview-on-android-softinput-keyboard-appear/
Hope this code helps you
The flag adjustPan is wrong. Use the adjustResize instead:
android:windowSoftInputMode="adjustResize|stateHidden"
What adjustPan does is equivalent to enlarging a picture more than the screen and then panning - i.e. moving the picture so you see different parts of it. (like a map in google map for instance).

scrollview not scrolling to bottom of the screen

I have a difficulty with implementing scroll view while using Theme.Black.NoTitleBar.Fullscreen.
When the user opens the application and click on editText the keyboard opens but the user cannot scroll beneath the edittext.
After many attempts I figured it something to do with the theme I am using (fullscreen)
This is my code .xml:
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fadeScrollbars="false"
android:fillViewport="true"
>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<View
android:id="#+id/vieww"
android:layout_width="match_parent"
android:layout_height="1000dp"
android:background="#android:color/holo_green_dark" />
<EditText
android:id="#+id/test_edit"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/vieww" />
<View
android:layout_width="match_parent"
android:layout_height="200dp"
android:layout_below="#id/test_edit"
android:background="#android:color/holo_blue_bright" />
</RelativeLayout>
When the keyboard is opened the user cannot see the lowest view beneath the edittext.
What I tried so far:
I played a lot with windowSoftInputMode of the activity I tried combination of adjustResize/adjustPan but nothing seems to work.
I tried to change the container to LinearLayout but it still not working for me
I found a solution here here
The problem with this solution I need to change the minSdk to 19, when the application starts I can see black stripe where has been the title bar.
the other issue is when the user open the keyboard by pressing on edittext the title bar returns
I think you should put the ScrollView in a Relative Layout, so you can fit the size, if the keyboard appears.
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fadeScrollbars="false"
android:fillViewport="true" >
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<View
android:id="#+id/vieww"
android:layout_width="match_parent"
android:layout_height="1000dp"
android:background="#android:color/holo_green_dark" />
<EditText
android:id="#+id/test_edit"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/vieww" />
<View
android:layout_width="match_parent"
android:layout_height="200dp"
android:layout_below="#id/test_edit"
android:background="#android:color/holo_blue_bright" />
</RelativeLayout>
</ScrollView>
</RelativeLayout>

how to change layout when soft Keyboard is shown

I have the following layout for an activity...
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
tools:context=".MainActivity"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ScrollView
android:id="#+id/scroll_view"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:scrollbars="vertical"
android:fillViewport="true"
android:background="#3ba4a4a4"
android:fadeScrollbars="true">
<TextView
android:id="#+id/text_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textIsSelectable="false"
android:text="#string/test_text"
android:textSize="40sp"/>
</ScrollView>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:padding="2dp">
<EditText
android:id="#+id/edit_text"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:hint="#string/edit_text_hint"
android:layout_marginTop="8dp"
android:theme="#style/InputMethodTheme"/>
<ImageButton
android:id="#+id/button_send"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_action_send"/>
</LinearLayout>
When the soft keyboard is opening I want the end of the TextView to go up with the EditText...
Any Idea on How to do That?
The EditText is going up normally as it should but its blocking the text view
i have tried the following in the manifest file in my activity
android:windowSoftInputMode="adjustPan"
But the action bar disappears which i don't want...
UPDATE
I tried the following in the TextView
android:layout_gravity="bottom"
and it seems to react as i want it to but some of the
lines before it are getting removed and extra spaces are
being added after the last word...
Basically you need to listen to the keyboard opening event.
More How to capture the "virtual keyboard show/hide" event in Android?
Be careful when dealing with devices with soft navigation buttons e.g., Nexus 5, you may need to take the navigation bar height into consideration.

Categories

Resources