Not able to move view up with keyboard Android - android

I have gone through various answers but was unable to move my view up when keyboard is visible. I have a edittext in recyclerview on whose focus keyboard is visible.I want the tv_character_limit text to be moved up when keyboard is visible. I need the character limit text to be shown elevated with a z-index. I have set the windowSoftInputMode to adjustResize for the activity and for the fragment in view pager i have added the java code:
getActivity().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);
Thank you for help.Below is my layout :
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/rel_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:background="#color/pink">
<include
android:id="#+id/toolbar"
layout="#layout/toolbar_post" />
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#+id/toolbar">
<android.support.v7.widget.RecyclerView
android:id="#+id/rv_status"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp" />
<TextView
android:id="#+id/tv_character_limit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|right"
android:layout_marginRight="10dp"
android:layout_marginBottom="30dp"
android:text="#string/character_limit"
android:textColor="#color/tap_to_type"
android:textSize="15dp" />
</FrameLayout>

use,
android:windowSoftInputMode="adjustPan"
in the manifest file for your activity.
ex :
<activity
android:name=".View.Activity.yourActivity"
android:windowSoftInputMode="adjustPan"/>

Instead of FrameLayout, use RelativeLayout and when your EditText gets focus, just change the position of TextView by calling the RelativeLayout class method similar to layout_above.

Related

Android: TextInputLayout doesn’t fit into page and prevents the page from scrolling inside NestedScrollView

The structure of my layout file: Coordinator layout - NestedScrollView - Coordinator Layout - objects that i want to be able to scroll
My TextInputLayout with TextInputEditText is at the bottom of the screen and when filled, extends out of bounds of the screen. When I collapse the keyboard it’s still not scrollable.
Note: I don’t want to set maxLines to Edittext and make it scrollable itself. I want it to scroll with other elements.
However, if I put, for example, TextView at the bottom of the screen and it doesn’t fit, the page is scrollable. Any ideas?
<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<androidx.core.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fillViewport="true"
>
<androidx.coordinatorlayout.widget.CoordinatorLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<!--other items i want to scroll-->
<com.google.android.material.textfield.TextInputLayout
android:id="#+id/descriptionLayout"
android:layout_width="348dp"
android:layout_height="200dp"
android:layout_gravity="center_horizontal"
android:layout_marginTop="545dp"
android:ems="16"
android:fontFamily="#font/montserrat"
android:hint="Add image description (optional)"
android:inputType="textMultiLine|textAutoCorrect"
app:counterEnabled="true"
app:counterMaxLength="256"
app:boxBackgroundColor="#color/white"
app:srcCompat="#drawable/ic_round_cloud_upload_24"
android:textColor="#68B2A0"
android:textColorHint="#68B2A0"
app:hintTextColor="#68B2A0"
app:layout_anchor="#id/titleLayout"
app:boxStrokeErrorColor="#F75010"
app:boxStrokeColor="#68B2A0"
app:boxStrokeWidth="2dp"
app:errorIconTint="#color/error"
app:errorTextColor="#color/error"
app:layout_anchorGravity="center|bottom"
app:counterOverflowTextColor="#color/error"
style="#style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
app:counterTextColor="#color/main_green"
>
<com.google.android.material.textfield.TextInputEditText
android:id="#+id/descriptionTxt"
android:ems="16"
android:textSize="18sp"
android:textColor="#68B2A0"
android:fontFamily="#font/montserrat"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:maxLines="7"
/>
</com.google.android.material.textfield.TextInputLayout>
</androidx.coordinatorlayout.widget.CoordinatorLayout>
</androidx.core.widget.NestedScrollView>
<!--bottom app bar-->
</androidx.coordinatorlayout.widget.CoordinatorLayout>
This is because you hard set android:layout_marginTop="545dp". Try never to do that. Also i think use costaint layour in youre case is better. Just change <androidx.coordinatorlayout.widget.CoordinatorLayout> inside nested scroll view to <androidx.constraintlayout.widget.ConstraintLayout> and add this atributes:
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent
If you need to show nested view in fullscteen, just chaange android:layout_height="wrap_content" to android:layout_height="match_parent"

How to remove focus from RecyclerView inside ScrollView?

I have a Scrollview which contains an ImageView and RecyclerView.
if navigation drawer opened then closed the RecyclerView auto scrolling to top, How to stop this?
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto">
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/scrollViewMain"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/imageView_main_icons_line"
android:src="#drawable/main_line" />
...
<android.support.v7.widget.RecyclerView
android:id="#+id/recyclerView_activity_main_passenger_log"
android:paddingBottom="2dp"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
</ScrollView>
</RelativeLayout>
This problem because of recyclerView has default focus.
Solution
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:descendantFocusability="blocksDescendants"
android:orientation="vertical">
Add android:descendantFocusability="blocksDescendants" to your immediate layout of scrollView
That is because RecyclerView ALWAYS set:
setFocusableInTouchMode(true);
this is hard coded in its constructor.
so if you apply those attribute in your XML for a RecyclerView
android:focusable="false"
android:focudeableInTouchMode="false"
that would be useless, you end up with recycleView.isFocusable() == true...
so the most elegant solution is to disable foucusable for RecyclerView's Parent.
<LinearLayout
android:focusable="false"
android:focusableInTouchMode="false"
android:descendantFocusability="blocksDescendants"
...
>
<android.support.v7.widget.RecyclerView
...
/>
/>
or you can just simply setFocusable(false)
Check out clearFocus() from here Android Dev Doc.
You can set a DrawerListener to your navigation drawer and use the onDrawerStateChanged() or some of the other options from here to call clearFocus() on your RecyclerView.
Adding android:descendantFocusability="blocksDescendants" can restrict scroll to recylerview but if you have edittext inside your layout, this property can block the focus of that particular edittext too. So if you are using this property please make sure you are removing this property in your kotlin/java class once the layout loaded.
parentLayout?.descendantFocusability = FOCUS_BEFORE_DESCENDANTS
(view as ViewGroup).descendantFocusability = FOCUS_BEFORE_DESCENDANTS
Just add the following code in your linear layout, works 100% ` android:descendantFocusability="blocksDescendants"
<android.support.v4.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<LinearLayout
android:descendantFocusability="blocksDescendants"
android:layout_width="match_parent"
android:layout_height="match_parent">

FrameLayout can not get focus with key press?

I have a view(Let's say it is MyFramLayout) extends FrameLayout, I can not get this FrameLayout get focus when press key(I am working in TV platform).
the layout is something like this:
<LinearLayout android:layout_width="match_parent"
android:layout_height="match_parent">
<ListView android:id="#android:id/list"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<com.roger.MyFrameLayout
android:id="#android:id/my_frame"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:focusable="true"
android:focusableInTouchMode="true" />
</LinearLayout>
Anyway to fix the problem?
Whenever you want your custom layout to get focus you will have to call
boolean b = yourLayout.requestFocus();
This will return a whether focus is gained or not.

After softkeyboard opens, the screen can't adjust itsef correctly

I read a lot of topic related to this item but none of them solved my problem. Here is the layout of my chat screen:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/chatPage"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/speech_bg" >
<include
android:id="#+id/chatHeader"
layout="#layout/header_chat" />
<RelativeLayout
android:id="#+id/chatView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#+id/chatHeader" >
<RelativeLayout
android:id="#+id/chatBottomView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:background="#drawable/speech_text_bg"
android:padding="5dp" >
<RelativeLayout
android:id="#+id/chatSendButton"
android:layout_width="#dimen/chatSendButtonWidth"
android:layout_height="#dimen/chatSendButtonHeight"
android:layout_alignBottom="#+id/chatEditText"
android:layout_alignParentRight="true"
android:layout_marginLeft="5dp"
android:background="#drawable/chat_send_btn_selector"
android:onClick="ChatSendButtonClick" >
<com.xxx.Utils.MyTextView
xmlns:customtext="http://schemas.custom.com/android/customtext"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="#string/chatSendText"
android:textColor="#ffffff"
android:textSize="#dimen/chatSendTextSize" />
</RelativeLayout>
<EditText
android:id="#+id/chatEditText"
android:layout_width="match_parent"
android:layout_height="#dimen/editTextHeight"
android:layout_toLeftOf="#+id/chatSendButton"
android:background="#drawable/edit_text"
android:inputType="textMultiLine"
android:maxLines="5" />
</RelativeLayout>
<com.handmark.pulltorefresh.library.PullToRefreshListView
xmlns:pulltorefresh="http://schemas.custom.com/android/pulltorefresh"
android:id="#+id/chatList"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="#+id/chatBottomView"
android:cacheColorHint="#00000000"
android:divider="#00000000"
android:fadingEdge="none"
android:fadingEdgeLength="0px"
android:listSelector="#00000000"
pulltorefresh:type="chat" />
</RelativeLayout>
I added AdjustPan option to the AndroidManifest.xml:
<activity
android:name=".ui.ChatActivity"
android:screenOrientation="portrait"
android:windowSoftInputMode="stateHidden|adjustPan" >
</activity>
AdjustResize doesn't work in my implementation since there's not a scrollview in it, I guess.
What is the best way to resize/adjust my chat page when soft keyboard opens? As far as I see, there's no implementation for listening the keyboard in Android either. I only saw some samples related with global layoutlistener to understand the keyboard position but it didn't work well for me.
Why does adjustpan option hides my actionbar instead of only chat screen?
Added to pulltorefreshlistview in chat.xml layout:
android:transcriptMode="alwaysScroll"
It solved everything. If the chat list view is scrollable in any kind of conditions, it doesn't need to listen the keyboard event to scroll the list view manually.
Hope it helps to anyone other than me either..

Losing EditText style after adding Views programmatically in Android

I have a layout called wizard that has an empty ViewFlipper inside of it.
I have other views that include this 'wizard' and programmatically add children to the ViewFlipper inside the wizard. The problem I'm having is that one of these children has an EditText, and this View loses it's default style. I tried setting everything up in the xml file and the problem dissapeared, so the problem is in adding the views programmatically. I Also tried calling invalidate() on the main container (in the code below: #+id/recarga_celular_main), but nothing happens.
The Edit text can be seen in the following link: EditText weird style problem
The view is something like this:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/recarga_celular_main"
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:background="#drawable/theme_home_background"
android:gravity="center_horizontal|top" >
...
<CustomWizard
android:id="#+id/recargaCelular_wizard"
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:layout_gravity="top"
android:background="#drawable/theme_layer_blue_bottom_black"
android:gravity="center_horizontal|center_vertical" >
</CustomWizard>
...
</RelativeLayout>
The 'wizard' is something like:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/touch_wizard_main_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#android:color/transparent"
android:gravity="center|top"
android:orientation="vertical" >
...
<ViewFlipper
android:id="#+id/wizard_flipper"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_above="#+id/aceptar_limpiar_btns"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true" />
...
</LinearLayout>
In the java code, first I add the view to the flipper, and then I display the desired child:
flipper.addView(step.getView(), flipper.getChildCount() - 1);
...
flipper.setDisplayedChild(index);
EDIT
Manually set the default EditText style:
<EditText
style="#style/some_style"
android:background="#android:drawable/edit_text"
android:gravity="left"
android:hint="Celular" />

Categories

Resources