In my application I have the following layout:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<RelativeLayout
android:id="#+id/fragment_container"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1" />
<EditText
android:id="#+id/edit_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:background="#color/text"
android:gravity="center_horizontal"
android:hint="#string/input_hint"
android:inputType="textVisiblePassword"
android:padding="#dimen/margin"
android:singleLine="true"
android:textColor="#color/input_text"
android:textColorHint="#color/input_hint">
</EditText>
</LinearLayout>
When EditText is clicked, the keyboard pushes the screen up including the ActionBar so that EditText is just above the keyboard. What I want is for only EditText to come up sitting on the keyboard, but the FragmentContainer that is above remains intact "as background". I got some of this by using android: windowSoftInputMode = "adjustResize" in the AndroidManifest.xml file, but my application is fullscreen and this tag apparently does not work with fullscreen.
As far as I am aware, there's no way to achieve what you're describing.
These are the choices available to you for windowSoftInputMode:
adjustNothing: When the keyboard appears, the window is not adjusted at all. This will stop your fragment_container from being moved or resizing, but will also cause the keyboard to cover your text input field.
adjustResize: When the keyboard appears, the window is "shrunk" vertically. This will cause your fragment_container to occupy less space than when the keyboard is closed, potentially affecting your fragment layout.
adjustPan: When the keyboard appears, the screen is "pushed" upwards. This will cause your fragment_container's top portion to be clipped by the edge of the screen.
adjustUnspecified: Allow the system to choose between the above three options.
Below are some pictures that (hopefully) help illustrate these attributes.
The layout with keyboard closed
From left to right: adjustNothing, adjustResize, and adjustPan
Related
This question already has answers here:
Bottomsheet with edit text moved up by keyboard
(2 answers)
Closed 2 years ago.
I am using a BottomSheetDialogFragment which contains:
- A RecyclerView with a list of comments made by users
- A EditText at the bottom, where users can write a new comment and post it
When the user taps on the EditText, the keyboard shows up from the bottom.
What i want is the keyboard to push ONLY the EditText, so that the user can see what he's typing, but not push the whole BottomSheetDialogFragment.
You can see the desirable behaviour in the Facebook app for example.
I have tried setting different values for setSoftInputMode but all i can achieve is either to move the whole BottomSheetDialogFragment, or to move nothing (leaving the EditText covered).
The easiest way to do this is to not use a BottomSheetDialogFragment but instead to use a regular fragment that covers the entire window or an activity. Fragment or Activity; whichever you prefer doesn't matter as long as you're taking up the whole screen.
When the on-screen keyboard appears, the activity window (by default) resizes to make way for the on-screen keyboard.
Since a BottomSheetDialogFragment has it's gravity set to bottom, when the activity window resizes, it pushes the bottom sheet and all of it's contents upwards.
Here's an example of the simplest layout that can achieve what you're going for
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<View
android:layout_width="match_parent"
android:layout_height="80dp"
android:background="#80000000" />
<androidx.recyclerview.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:orientation="vertical" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
First is a view with a constant height which imitates the dark area outside of a dialog. No matter how the window resizes this will always be 80dp.
Second is the RecyclerView which resizes based on the remaining available space the LinearLayout has. Pay attention to the attribute android:layout_weight.
Third is EditText which has a constant height of wrap_content. Again No matter how the window resizes this will always be the same height. Since the RecyclerView will take up as much space as it can, this EditText will remain at the bottom of the screen.
Once the window resizes due to the on-screen keyboard appearing, the LinearLayout will resize and recalculate it's children's sizes. Since the View and the EditText has a constant height it will have to make the RecyclerView smaller making it appear like the EditText moved upwards.
You don't have to use a LinearLayout to do this, you can achieve the same effect with any other layout. The key point here is to use a layout that takes up the whole screen.
I have the following layout in an activity.
<CoordinatorLayout ...>
<ScrollView ...>
<LinearLayout
...
android:orientation="vertical">
<!-- Some stuff -->
<EditText ...>
<!-- Some more stuff -->
<View
android:id="#+id/keyboard_extender"
...>
</LinearLayout>
</ScrollView>
<LinearLayout
android:id="#+id/bottom_sheet"
android:orientation="vertical"
...
app:layout_behavior="android...BottomSheetBehavior">
<!-- Even more stuff -->
</LinearLayout>
</CoordinatorLayout>
and I want that the keyboard shouldn't push up the bottom_sheet but it should push up the keyboard_extender. Please note that the bottom_sheet is not a BottomSheetDialog but just a view using the android.support.design.widget.BottomSheetBehavior. This can't be changed to a BottomSheetDialog because I want the rest of the screen to remain active at the same time as the bottom_sheet
I never set the visibility of the bottom_sheet to View.GONE. I only change its state from BottomSheetBehavior.EXPANDED to BottomSheetBehavior.COLLAPSED and back. Also, I want the bottom_sheet to be visible only when the keyboard isn't.
Since I need that the keyboard_extender should always be above the keyboard, I have set in the AndroidManifest.xml that android:windowSoftInputMode="adjustResize" (which I assume is unavoidable)
I have a method which when called should hide the keyboard and show the bottom_sheet, but the problem I'm facing is that the bottom_sheet starts animating to BottomSheetBehavior.EXPANDED at the same time that the keyboard starts hiding, and when the keyboard starts hiding, the screen is actually resized because of the windowSoftInputMode="adjustResize" and the bottom_sheet starts expanding from the top of the keyboard and it doesn't move down along with the keyboard. So, when the keyboard completely hides, the bottom_sheet is actually sitting in the middle of the screen (just above the height of the keyboard).
Ideally, I'd like that the keyboard starts hiding and the bottom_sheet starts expanding at the same time, but the bottom_sheet starts expanding from the bottom of the screen and not the top of the keyboard.
The very hacky fix I'm currently using to overcome this issue is to delay the expanding of the bottom_sheet by SoftInput.KEYBOARD_POPUP_DELAY, which seems to be working for the time being. Is there any cleaner alternative?
I have a situation where I need a scrollview to have a background image that shouldn't scroll along with it's parent when moving. Before any of you suggest me the links for setting background image and this that, I have already tried and it's not working.
The whole story goes like: I have an activity with fragments which have their own backgrounds with some input fields. When focusing over input fields, keyboard appears and background image squeezes. For that I put an image on background of scrollview that fixed my issue of squeezing background but raised another concern that background Image should stay static while scrolling the parent scrollview.
The second solution any of you may suggest is setting background of my activity rather playing with scrollview. That's right, but for that I had to make a style element with background of theme which appears odd while transitioning different fragments plus it adds overhead when I have a lot of code and fragments to move forward and back.
That's the point where I am stuck. I have gone through links below, if you just need to know that I tried it or not.
link1
link2
link3
... and so on
below is the layout I am using for my fragments (it's all being done programmatically)
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/top_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<ScrollView
android:id="#+id/scrollView1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true"
android:scrollbars="none" >
<FrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<ImageView
android:id="#+id/backgroundView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:contentDescription="#string/app_name"
android:scaleType="fitXY" />
<LinearLayout
android:id="#+id/parent_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
</LinearLayout>
</FrameLayout>
</ScrollView>
</LinearLayout>
So if you guys have any better solution keeping in mind the situation I have, will be warmly welcomed. Thank you
None of the above works for me with some reason but this things works.
getWindow().setBackgroundDrawableResource(R.mipmap.img_reg_bg);
put it in oncreate method hope it will help.
If I guess right you try to fix your hotfix to get it working propperly. If I'm right you sould go back to scratch and do it without your scrollview.
If you have a look at following Link:
http://developer.android.com/guide/topics/manifest/activity-element.html#wsoft
you'll see softkeyboard mode can be adjusted. "adjustPan" should solve your problem.
"adjustPan"
The activity's main window is not resized to make room for the soft keyboard. Rather, the contents of the window are automatically panned so that the current focus is never obscured by the keyboard and users can always see what they are typing. This is generally less desirable than resizing, because the user may need to close the soft keyboard to get at and interact with obscured parts of the window.
Add someimgage to your drawable folderes, like drawable-mdpi, drawable-hdpi.
In your LinearLayout "#+id/top_layout" add attribute:
android:background="#drawable/someimage"
Good day (or evening, or night)
I'm developing an app for android and I'm very curious about one thing. I have an activity, where user chats with another, like "im" chat. There are an EditText on the bottom and some kind of actionbar on the top. What I need is when user enters a message and the software keyboard is on screen, my activity should move up, but the actionbar should still be "glued" to the top of the screen, because it has some valuable controls on it.
Again, that's not an ActionBar, but just a 48dp height layout in a parent vertical linear layout. So I need to know is there an easy way to prevent it from moving to the top, when the layout moves off the screen.
I tried to put everything in a FrameLayout and put this bar on top of it, but on keyboard opens it goes off the screen too...
On you Activity at AndroidManifest you should put this: android:windowSoftInputMode="adjustResize"
Use something like this:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<com.myapp.MyActionBar
android:layout_width="match_parent"
android:layout_height="48dp"/>
<LinearLayout
android:id="#+id/mylayout"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1dp"/>
<!-- Add your edittext and button -->
</LinearLayout>
This will make sure the actionbar and edittext + button are allways on screen, and the mylayout takes up the rest of the screen. When your keyboard is shown, the mylayout will shrink.
Try adding android:windowSoftInputMode="adjustResize" to your activity in the manifest. This tells Android to completely resize your layout when the keyboard comes up, rather than pan it. Note that if there isn't enough room for the entire layout this still won't work. But you ought to be able to make it work if your top level layout is a RelativeLayout, with the edit text set to align bottom, the top bar to align top, and the middle section to fill_parent and be above the edit text and below the bar.
use a RelativeLayout as your base Layout and add android:layout_alignParentTop="true" to your action bar to keep it up
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<LinearLayout
android:id="#+id/action_bar_layout"
android:layout_width="match_parent"
android:layout_height="48dp"
android:layout_alignParentTop="true" >
</LinearLayout>
<TextView
android:id="#+id/text_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerInParent="true" />
</RelativeLayout>
I'm working on an Android app with a slightly complex layout and I'm having nightmares to make it work the way I want.
My main layout is defined like this:
<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:background="#000000"
tools:context=".MainLayout" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<RelativeLayout
android:id="#+id/content"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:scrollbars="none" />
<LinearLayout
android:id="#+id/menudown"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
... buttons ...
</LinearLayout>
</LinearLayout>
<LinearLayout
android:id="#+id/header"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerInParent="true" >
... header content ...
</LinearLayout>
</RelativeLayout>
The idea of the layout is having a content frame(content), a footer menu (menudown) and a header (header) hovering over the content, aligned to the top of the screen. - Sorry guys, no image, confidential project.
The content is inflated with other layouts, depending on the section the user is, and some of them are forms to be filled. So, i just used the adjustPan and everything worked fine.
The problem here is: the client asked me to make the content scrollable while there's a soft keyboard showing, so that the user can view whatever he wrote on the other fields.
And it's painful because: the form fits the screen, so using content as a scrollview won't help, unless it gets resized.
And simply changing to adjustResize does not work because: the menudown goes up with the soft keyboard, which should not heappen. Also, the visual effect is terrible.
The app was already made for iPad (by someone else), and what heappens there is: the menudown remains aligned to the bottom of the screen, under the soft keyboard, which pushes only the content view up. Then, the content becomes scrollable, so that the user is able to see it entirely.
I have this same issue too, making keyboard behave the same way as it does on iOS. The best thing I can get from all the other posts is manually detecting the soft keyboard and moving the view up the difference.
This seems to be the best: How to check visibility of software keyboard in Android?