Push only the bottom part up when Soft keyboard appears - android

The only thing I am trying do that there will be an editText at the top and after that there will be an ImageView.
Now at the bottom line there will be some buttons (ex. emojis) and this bottom part will only be pushed up by the soft keyboard whenever it will appear.
I am making it more clear by attaching this layout of Facebook.
required output
I have gone through all these SO questions and also tried their answers but no luck. May be I am doing something wrong.
Android: Resize only parts of view with soft keyboard on screen
Android: How do I prevent the soft keyboard from pushing my view up?
android : How to prevent resizing the window when displaying the virtual keyboard
My XML code
<ScrollView
android:id="#+id/scrollOnPost"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:isScrollContainer="false"
android:fillViewport="true"
android:layout_below="#+id/header_layout">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:weightSum="4"
android:layout_gravity="bottom"
android:gravity="bottom">
<EditText
android:id="#+id/ed_post"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
<ImageView
android:id="#+id/img_posted_list_row"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="2.7"
android:adjustViewBounds="true"
android:src="#mipmap/ic_launcher"
android:scaleType="fitCenter"
android:background="#color/red"/>
</LinearLayout>
</ScrollView>
<ImageButton
android:id="#+id/imgBtn_Emoji"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_alignParentBottom="true"
android:src="#drawable/smile_face"
android:background="#drawable/button_pressed_white"
/>
All I getting that my soft keyboard always pushing the whole layout up not just the emoji at the bottom. Thanks.

Dude it worked
manifest :
<activity android:name=".TestActivity"
android:windowSoftInputMode="adjustResize"/>
xml :
<?xml version="1.0" encoding="utf-8"?>
<ScrollView android:id="#+id/scrollOnPost"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:isScrollContainer="false"
android:fillViewport="true"
android:layout_below="#+id/header_layout"
xmlns:android="http://schemas.android.com/apk/res/android">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:weightSum="4"
android:layout_gravity="bottom"
android:gravity="bottom">
<EditText
android:id="#+id/ed_post"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
<ImageView
android:id="#+id/img_posted_list_row"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="2.7"
android:adjustViewBounds="true"
android:scaleType="fitCenter"
android:background="#color/colorPrimary"/>
<ImageButton
android:id="#+id/imgBtn_Emoji"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_alignParentBottom="true"
android:src="#mipmap/call"
/>
</LinearLayout>
I guess you want only the editText to push up when keyboard is visible(Correct me if i am wrong) for that i have got an idea i don't know if it will work or not you can try it out if you find it logical
Idea is to first check if keyboard is visible or not and secondly if visible get its height and programatically give your layout a marginBottom which will be equal to the height of keyboard(using layoutParams).
check this links for implementing above idea :
How to capture the "virtual keyboard show/hide" event in Android?
Is there any way in android to get the height of virtual keyboard of device
How to set RelativeLayout layout params in code not in xml

Related

Android: ListView pushes out everything else below it

In my app, there is a tabbed activity with three fragments. The first fragment has a form to create a new task, the second fragment has the list of all the saved tasks, and the third fragment will show the comments on a task when selected from the list in the second fragment. The third fragment is also supposed to act like a chat activity which posts comments when you type them in and tap the send button. The XML layout of this third fragment is as follows:
<?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:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#android:color/darker_gray"
android:orientation="vertical"
tools:context="com.example.ishita.assigntasks.CommentsFragment">
<TextView
android:id="#+id/frag_task_details"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#dd55ff"
android:padding="10dp" />
<ListView
android:id="#+id/frag_comment_list"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:transcriptMode="normal" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:background="#android:color/white">
<EditText
android:id="#+id/frag_msg_edit"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:inputType="textMultiLine"
android:layout_weight="1" />
<ImageButton
android:id="#+id/frag_send_btn"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0"
android:background="#android:color/background_light"
android:contentDescription="#string/send_btn"
android:src="#android:drawable/ic_menu_send" />
</LinearLayout>
</LinearLayout>
As you can see, there is a TextView and a ListView and below that is another LinearLayout. Here is how it should look (the purple bar is the TextView):
And here is how it actually looks:
The TextView shows up above the ListView, but the LinearLayout does not. It is there, though. If I do android:layout_marginBottom="20dp" on the outermost LinearLayout, it does show up, but the ActionBar scrolls up and overlaps with the notification bar so that the title on the ActionBar and the notifications on the notification bar are both visible simultaneously and neither is legible.
I searched a lot and this seemed a common issue, but none of the solutions helped me. Believe me, I tried everything I could find. I tried wrapping the whole thing in a FrameLayout, using a RelativeLayout in place of the outermost LinearLayout, using two LinearLayouts--one to wrap the TextView and the ListView and the other to wrap the EditText and the ImageButton, etc., but nothing was able to show the bottom LinearLayout below the ListView. I even tried to set focus on the EditText when the fragment launches so that the keyboard would show and I can type, but even that doesn't help.
Note: On the other hand, if I use the exact same layout on an activity, the bottom LinearLayout displays exactly as it should.
I am unable to find the bug. Please help!
It looks like your toolbar pushes fragment layout down without decreasing its height. I have no idea why this happens (there are no root layout code here).
As a workaround you can set fragments layout bottom margin to ?attr/actionBarSize
As you have given layout_weight 1 in listview layout, so it occupies the whole space available. In order to get rid of you have to give some static height or use layout_weight in right manner
<ListView
android:id="#+id/frag_comment_list"
android:layout_width="match_parent"
android:layout_height="500dp"
android:transcriptMode="normal" />
or try like this
<?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:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#android:color/darker_gray"
android:orientation="vertical"
tools:context="com.example.ishita.assigntasks.CommentsFragment">
<TextView
android:id="#+id/frag_task_details"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#dd55ff"
android:padding="10dp" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:orientation="vertical"
android:background="#android:color/white">
<ListView
android:id="#+id/frag_comment_list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:transcriptMode="normal" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:orientation="horizontal"
android:background="#android:color/white">
<EditText
android:id="#+id/frag_msg_edit"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:inputType="textMultiLine"
android:layout_weight="1" />
<ImageButton
android:id="#+id/frag_send_btn"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0"
android:background="#android:color/background_light"
android:contentDescription="#string/send_btn"
android:src="#android:drawable/ic_menu_send" />
</LinearLayout>
</LinearLayout>
All,
Thank you for all your responses. I found a workaround by trial and error now and thought I should post the answer for others who face the same issue. I set android:layout_marginBottom="50dp" on the inner LinearLayout (the one wrapping the comments bar--EditText and ImageButton). Somehow, this sets the layout correctly and the fragment functions properly in both Lollipop and Jellybean OS's. I haven't tested on other OS versions.
First of all you should read this, what is layout_weight and how it works.
You assigned layout_weight to ListView as 1, it means it covers whole height. just change it to 0.7 or any proportion you want (less than 1) will solve the problem.
<ListView
android:id="#+id/frag_comment_list"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.7"
android:transcriptMode="normal" />

Hidden EditText when soft keyboard appears

I have this typical issue when soft keyboard appears for user to enter input and covers the EditText where text should appear.
I tried many different "fixes" and solution without success. I guess there is certain combination in my layout that causes this issue.
Right now in AndroidManifest.xml I have this line
<activity
android:screenOrientation="landscape"
android:windowSoftInputMode="adjustPan">
The layout with the EditText is as follows witht he EditText at bottom
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:layout_height="fill_parent"
android:fillViewport="true"
android:scrollbars = "vertical"
android:windowSoftInputMode="adjustPan|adjustResize">
<LinearLayout
android:layout_width="fill_parent" android:layout_height="fill_parent"
android:orientation="vertical" >
<RelativeLayout
android:id="#+id/cargoRoot"
android:layout_width="fill_parent" android:layout_height="wrap_content"
android:numColumns="auto_fit"
android:stretchMode="columnWidth" >
<LinearLayout
android:layout_height="wrap_content" android:layout_width="600dp"
android:orientation="vertical" >
<TextView style="#style/smalltext" />
<TextView
android:id="#+id/arrivalEta"
android:layout_height="wrap_content" android:layout_width="wrap_content" />
<TextView
android:text="#string/cargoComments"
style="#style/smalltext" />
<ScrollView android:id="#+id/scrollEditCargoComments"
android:layout_width="fill_parent" android:layout_height="142dp"
android:scrollbars="vertical">
<EditText
android:id="#+id/editCargoComments"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:textSize="#dimen/text_normal"
android:minLines="5"
android:imeOptions="actionDone"
android:imeActionLabel="#string/send"
android:inputType="textNoSuggestions" />
</ScrollView>
</LinearLayout>
</RelativeLayout>
</LinearLayout>
Other things worth commenting is that I have this EditText with visibility gone until certain points in the app where the user is prompted to enter comments. Then I change visibility to visible and when user taps on the text the soft keyboard appears.
It appears hiding everything behind it. No Resize or Pan operation is done.
The view that holds the EditText is infalted code-behind and inserted inside another view inside a PopupWindow.
So is like MainActivity > PopupWindow > ViewFlipper > EditText
I wonder if either PopupWindow or ViewFlipper add some poison to soft keyboard management.
Thanks in advance
Try this:
android:windowSoftInputMode="adjustResize|adjustPan|stateHidden"

Softkeyboard hides elements of my layout that I want to be visibles

I've got a layout that looks like first image and when I open the softkeyboard looks like the second image, but I want it how it seems on third image:
I read a lot about adjustpan and adjustresize on the manifest but nothing works, the xml structure is:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/blanco"
android:gravity="top"
android:orientation="vertical"
android:weightSum="10" >
<!-- LOGO -->
<LinearLayout
android:id="#+id/logo"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="3.7"
android:background="#color/green_logo"
android:orientation="vertical"
android:gravity="center"
android:paddingTop="65dp" >
<ImageView
android:id="#+id/logo_registro"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:src="#drawable/logo_registro" />
</LinearLayout>
<FrameLayout
android:id="#+id/contentFragment"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="6.3" >
</FrameLayout>
</LinearLayout>
First part is the green part with the app logo and inside the FrameLayout I put Fragments that contains simple LinearLayouts or RelativeLayouts with parts of my Login forms.
Put this on your Manifest, inside the activity
<activity android:name=".YourActivity"
android:configChanges="keyboard|keyboardHidden"></activity>
Set your windowSoftInputMode in your manifest for this activity to be adjustResize. THere are 3 possible settings for this- nothing, resize (shrinks your app to fit the remaining space), and pan (scrolls your app so the focused field remains on screen).
Just Add the empty scrollview and it will work
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
</ScrollView>

Android layout design: image above form above button. When the keyboard appears my layout is screwed

In my application I want to have a layout like this:
I did it with a Relative Layout holding an ImageView, TableLayout and a Button. The problem is that when I open the keyboard to modify a EditText the keyboard is on top of the EditText, so the user can't see what he is writing. I want the layout to be like this when the keyboard is shown:
The user should be able to scroll up the view to see the whole image while the keyboard is shown, even if he doesn't see the EditText then, because he wants to copy some data of the image into the EditTexts. I tried to do it like this:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:baselineAligned="false"
android:orientation="vertical" >
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_above="#+id/buttonEditStop"
android:padding="10dip" >
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<ImageView
android:id="#+id/stopImg"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:clickable="true"
android:contentDescription="#string/entry_img"
android:onClick="ImageClick" />
<TableLayout
android:id="#+id/formLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/stopImg"
android:layout_marginRight="10dp" >
...
</TableLayout>
</RelativeLayout>
</ScrollView>
<ImageButton
android:id="#+id/buttonEditStop"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:contentDescription="#string/edit_stop"
android:onClick="EditButtonClicked"
android:src="#drawable/ic_menu_edit" />
Using this code my whole layout is screwed. I see the form on top of the image, followed by a ImageButton with the image that should be seen in the imageView.
Anyone knows how I can get a layout like the one I want?
Thanks!
To avoid the EditText to be overlapped I had to use
android:windowSoftInputMode="adjustPan"
in my manifest.

ListView in a custom dialog pushes buttons off the screen

I am using my own subclass of Dialog and trying to build it in such a way that if there is not enough content to fill the screen, it will shrink to fit the content (i.e. wrap_content). I can do this with the following layout:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/dialog_bg">
<RelativeLayout
android:id="#+id/title_container"
android:layout_width="match_parent"
android:layout_height="60dp"
android:gravity="center_vertical"
android:background="#drawable/dialog_title_bg">
. . .
</RelativeLayout>
<LinearLayout
android:id="#+id/button_container"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/content"
android:orientation="horizontal">
. . .
</LinearLayout>
<FrameLayout
android:id="#+id/content"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/title_container">
</FrameLayout>
</RelativeLayout>
(I've removed the bits that - I think - are not important). This works great when the content doesn't fill the screen:
But when the content is big, it pushes the buttons off the bottom:
A slight adjustment, I change the button_container so it's got layout_alignParentBottom="true" and the content is above it, like so:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/dialog_bg">
<RelativeLayout
android:id="#+id/title_container"
android:layout_width="match_parent"
android:layout_height="60dp"
android:gravity="center_vertical"
android:background="#drawable/dialog_title_bg">
. . .
</RelativeLayout>
<LinearLayout
android:id="#+id/button_container"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:orientation="horizontal">
. . .
</LinearLayout>
<FrameLayout
android:id="#+id/content"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/title_container"
android:layout_above="#+id/button_container">
</FrameLayout>
</RelativeLayout>
Now it works for when the content is too big for the screen:
But when the content does not fill the screen, the dialog still does:
How can I get the best of both worlds? I could probably do it in code by setting the "maxHeight" to the height of the screen minus the dialog border and the height of the title and buttons, but that seems a bit hacky to me. I tried looking at the implementation of AlertDialog (which seems to handle this situation correctly) but it looked like black magic to me...
Edit: as requested, here is the content of the layout file that I add to the FrameLayout that represents the "content" of the dialog:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<ListView
android:id="#+id/report_items"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true" />
</RelativeLayout>
Instead of frame layout, better to use the linear. It will work.. If u post the xml code then I can check..

Categories

Resources