layout_gravity="bottom" not working - android

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>

Related

Android layout_gravity Bottom/Top in LinearLayout

There are millions of questions and answers in related to "layout_gravity Bottom/Top in LinearLayout" on stack overflow but I still haven't solved my problem.
I want to place my EditText which says "Please place me at the very top" at the very top, and TextView which says "Please place me at very bottom" at the very bottom. My dream is very basic but I cannot achieve it!!
Can anyone help me?
This is my XML:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_horizontal"
android:orientation="vertical"
android:weightSum="1">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="210dp"
android:background="#android:color/holo_green_light"
android:gravity="center_vertical"
android:orientation="vertical"
android:weightSum="1">
<EditText
android:id="#+id/editText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="textPersonName"
android:text="Please place me at very top" />
<TextView
android:id="#+id/textView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:background="#android:color/background_light"
android:text="TextView"
android:layout_weight="0.19" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="248dp"
android:layout_weight="0.70"
android:background="#color/colorAccent"
android:gravity="center_vertical"
android:orientation="vertical"
android:weightSum="1">
<Button
android:id="#+id/button"
android:layout_width="match_parent"
android:layout_height="62dp"
android:layout_gravity="top"
android:text="Button" />
<TextView
android:id="#+id/textView2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0.16"
android:background="#android:color/background_light"
android:text="Please place me at very bottom" />
</LinearLayout>
</LinearLayout>
And this is my output:
If LinearLayout isn't working, then don't force yourself to use it.
Nested LinearLayouts and weights are bad for performance.
RelativeLayout (or ConstraintLayout) are naturally meant to place things at the anchor points of ViewGroups
I assume you want the text and button centered based on your other attributes
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:weightSum="2">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:background="#android:color/holo_green_light"
android:layout_weight="1">
<EditText
android:id="#+id/editText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:layout_alignParentTop="true"
android:inputType="textPersonName"
android:text="Please place me at very top" />
<TextView
android:id="#+id/textView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:background="#android:color/background_light"
android:text="TextView" />
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:background="#android:color/holo_red_light"
android:orientation="vertical">
<Button
android:id="#+id/button"
android:layout_width="match_parent"
android:layout_height="62dp"
android:layout_centerInParent="true"
android:text="Button"/>
<TextView
android:id="#+id/textView2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:background="#android:color/background_light"
android:text="Please place me at very bottom"/>
</RelativeLayout>
</LinearLayout>
Alternatively, you only need android:gravity="bottom" on the second LinearLayout, then all views within are at the bottom.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:weightSum="1">
<!-- Top Gravity -->
<LinearLayout
android:gravity="top"
android:layout_width="match_parent"
android:layout_height="0dp"
android:background="#android:color/holo_green_light"
android:orientation="vertical"
android:layout_weight="0.5">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Please place me at very top"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#android:color/background_light"
android:text="TextView"/>
</LinearLayout>
<!-- Bottom Gravity -->
<LinearLayout
android:gravity="bottom"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.50"
android:background="#android:color/holo_red_light"
android:orientation="vertical">
<Button
android:layout_width="match_parent"
android:layout_height="62dp"
android:text="Button"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#android:color/background_light"
android:text="Please place me at very bottom"/>
</LinearLayout>
</LinearLayout>
You have set gravity bottom for your second linear layout to place the views at the bottom.
change this line at first LinearLayout:
android:gravity="center_vertical"
To:
android:gravity="top"
change this line at Second LinearLayout:
android:gravity="center_vertical"
To:
android:gravity="bottom"
Note: Avoid using nested weights it will slow down the performance.

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).

edittext must move up on the top of the keyboard without moving other views

[This is the image i need to move up only edittext not the logo ][1]edittext appearing on the top of the keyboard after setting windowSoftInputMode but other views also moving up. is their any way to move only edittext .Thanks in advance
[1]: https://i.stack.imgur.com/NEpf3.png`
<ScrollView
android:id="#+id/scroll"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:orientation="vertical">
<LinearLayout
android:id="#+id/linear"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="50dp"
android:src="#drawable/logo_light"
android:text="LOGO"
android:textColor="#color/black"
android:textSize="100dp" />
</LinearLayout>
</ScrollView>
<LinearLayout
android:id="#+id/ll_send_message"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_marginBottom="50dp"
android:orientation="horizontal">
<EditText
android:id="#+id/editMessage"
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_gravity="center_vertical|center_horizontal"
android:layout_weight="5"
android:background="#color/white"
android:gravity="center"
android:inputType="textMultiLine|textCapSentences"
android:maxLines="5"
android:hint="EDIT TEXT"
android:minLines="3"
android:textColorHint="#color/black"
android:scrollHorizontally="false"
android:textColor="#color/white" />
<Button
android:id="#+id/buttonSend"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical|center_horizontal"
android:background="#color/black"
android:gravity="center"
android:text="send"
android:textAppearance="?android:attr/textAppearanceMedium" />
</LinearLayout>
`
Try to use framlayout and xml like below
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout 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" >
<EditText
android:id="#+id/edit_text_message"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom|center_vertical"
android:layout_marginLeft="12dp"
android:background="#null"
android:layout_alignParentBottom="true"
android:hint="Enter you something"
android:inputType="textCapSentences|textMultiLine"
android:maxLength="2000"
android:maxLines="8" />
</FrameLayout>
If your using relative layout , in the edit text tag add this.
android:layout_alignParentBottom="true"

Organizing layouts with edit text at bottom?

I wanted to make a layout with 3 LinearLayouts. In the 2nd LinearLayout I would have a ListView and the 3rd LinearLayout I would have an EditText and Button at the bottom. This was my attempt:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:fresco="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:id="#+id/view_post"
android:layout_width="fill_parent"
android:layout_height="200dp"
android:background="#color/com_facebook_blue">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="#+id/view_status"/>
</LinearLayout>
<LinearLayout
android:id="#+id/view_comments"
android:layout_below="#+id/view_post"
android:layout_width="match_parent"
android:layout_height="250dp">
<ListView
android:id="#+id/lv_comments_feed"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
</ListView>
</LinearLayout>
<LinearLayout
android:layout_below="#+id/view_comments"
android:id="#+id/send_message"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_alignParentBottom="true"
android:orientation="horizontal" >
<EditText
android:id="#+id/write_comment"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical|center_horizontal"
android:layout_weight="5"
android:gravity="top|left"
android:inputType="textMultiLine"
android:scrollHorizontally="false" />
<Button
android:id="#+id/send_comment"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical|center_horizontal"
android:gravity="center"
android:text="send"
android:textAppearance="?android:attr/textAppearanceMedium" />
</LinearLayout>
</RelativeLayout>
Now the design looks great but when I load the app on my phone, I have 2 problems:
I don't want to hardcode the LinearLayout heights. I have looked into layout_weight but I can't seem to have these layout_weights work without running into the 2nd problem below:
When I click the EditText, the EditText is hidden even though I have declared android:windowSoftInputMode="adjustResize|stateHidden" in my activity and I'm not sure why that's happening. Also, the EditText and Button are not directly at the bottom of the screen which is what I would want. Any help is appreciated, thanks!
you don't need to add listview as child of linear layout. and avoid fill_parent & use match_parent. if first linear layout is only for textview then don't use it inside of linearlayout. or only use wrap_content instead of a specific layout_height
Here i made some changes hope it will work
<?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">
<LinearLayout
android:id="#+id/view_post"
android:layout_width="match_parent"
android:layout_height="200dp"
android:background="#222dc9">
<TextView
android:id="#+id/view_status"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
<ListView
android:id="#+id/listView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="#+id/send_message"
android:layout_below="#id/view_post"
></ListView>
<LinearLayout
android:id="#+id/send_message"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="5dp"
android:layout_alignParentBottom="true"
android:orientation="horizontal">
<EditText
android:id="#+id/write_comment"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical|center_horizontal"
android:layout_weight="5"
android:gravity="top|left"
android:inputType="textMultiLine"
android:scrollHorizontally="false" />
<Button
android:id="#+id/send_comment"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical|center_horizontal"
android:gravity="center"
android:text="send"
android:textAppearance="?android:attr/textAppearanceMedium" />
</LinearLayout>
</RelativeLayout>
and use in Manifest with activity name is
android:windowSoftInputMode="adjustPan"
<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=".MainActivity">
<RelativeLayout
android:id="#+id/view_post"
android:layout_width="match_parent"
android:background="#color/material_grey_300"
android:padding="5dp"
android:layout_height="wrap_content">
<TextView
android:id="#+id/view_status"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="hello" />
</RelativeLayout>
<RelativeLayout
android:id="#+id/view_comments"
android:layout_above="#+id/send_message"
android:layout_below="#+id/view_post"
android:layout_width="match_parent"
android:background="#color/material_deep_teal_200"
android:padding="10dp"
android:layout_height="match_parent">
<ListView
android:id="#+id/lv_comments_feed"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</ListView>
</RelativeLayout>
<LinearLayout
android:id="#+id/send_message"
android:background="#color/material_blue_grey_800"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_alignParentBottom="true">
<EditText
android:id="#+id/write_comment"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:inputType="textMultiLine"
android:scrollHorizontally="false" />
<Button
android:id="#+id/send_comment"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="3dp"
android:layout_weight="1"
android:text="send" />
</LinearLayout>
</RelativeLayout>

How do I ensure the buttons align horizontally in this Android login dialog?

I'm creating a login dialog on Android where I want two buttons to align horizontally both on the top and bottom edge, irregardless of the screen size. Sometimes "Forgot password" will break over two lines, sometimes not. I do not want to impose a minHeight on the buttons, and would prefer a solution based on XML design only (not code).
Landscape is fine:
Portrait, default implementation leaves the login button shorter than "forgot password"
One solution I tried was setting match_parent for the layout_height param of the first button. Didn't quite work:
XML:
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:id="#+id/terms_layout"
android:layout_marginTop="10dp"
>
<Button
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="#string/login"
android:textSize="18sp"
android:id="#+id/dialog_login_button_login"
/>
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="#string/forgotPassword"
android:textSize="18sp"
android:id="#+id/dialog_login_button_forgot_password"
/>
</LinearLayout>
Use RelativeLayout and make "Login" button to align to the top and bottom of "Forgot Password" Button
As #manjusg answered, make the Login button align the bottom and top to the other button.
Here is how I did it:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Email" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Password" />
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:id="#+id/terms_layout"
android:layout_marginTop="10dp">
<View
android:id="#+id/center_strut"
android:layout_height="0dp"
android:layout_width="0dp"
android:layout_centerInParent="true" />
<Button
android:layout_width="0dp"
android:layout_height="0dp"
android:text="Login"
android:textSize="18sp"
android:layout_alignParentLeft="true"
android:layout_alignRight="#+id/center_strut"
android:layout_alignBottom="#+id/dialog_login_button_forgot_password"
android:layout_alignTop="#+id/dialog_login_button_forgot_password"
android:id="#+id/dialog_login_button_login" />
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignLeft="#+id/center_strut"
android:text="Forgot Password"
android:textSize="18sp"
android:id="#+id/dialog_login_button_forgot_password" />
</RelativeLayout>
</LinearLayout>
replace your xml with this xml
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:id="#+id/terms_layout"
android:layout_marginTop="10dp"
android:weightSum="2"
>
<RelativeLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
>
<Button
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="#string/login"
android:textSize="18sp"
android:id="#+id/dialog_login_button_login"
android:layout_alignParentTop="true"
android:layout_alignParentBottom="true"
/>
</RelativeLayout>
<RelativeLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
>
<Button
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="#string/forgotPassword"
android:textSize="18sp"
android:id="#+id/dialog_login_button_forgot_password"
android:layout_alignParentTop="true"
android:layout_alignParentBottom="true"
/>
</RelativeLayout>
</LinearLayout>
check 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:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin">
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/linearLayout">
<Button
android:layout_weight="1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="Login"
android:id="#+id/button"/>
<Button
android:layout_weight="1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="FORGOT PASSWORD"
android:id="#+id/button2"/>
</LinearLayout>
<LinearLayout
android:layout_alignBottom="#+id/linearLayout"
android:layout_width="wrap_content"
android:layout_centerInParent="true"
android:paddingTop="6dp"
android:paddingBottom="6dp"
android:layout_height="wrap_content">
<View
android:layout_width="0.5dp"
android:layout_height="wrap_content"
android:background="#000000"/>
</LinearLayout>
</RelativeLayout>

Categories

Resources