RelativeLayout : different result when change relative between view - android

I have this layout : first, is a spinner, after that is a listview, after that is a linearlayout include EditText and Button, and at the bottom of the screen is a Button.
Here is my first layout file:
<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"
android:orientation="vertical">
<Spinner
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:textColor="#0e0e0e"
android:layout_above="#+id/list_view"
android:id="#+id/spinner"/>
<ListView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="#+id/list_view"
android:layout_alignParentLeft="true"
android:layout_above="#+id/linear_layout" />
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:id="#+id/linear_layout"
android:layout_above="#+id/search_btn"
android:weightSum="3"
android:layout_alignParentLeft="true">
<EditText
android:layout_width="0dp"
android:layout_height="wrap_content"
android:id="#+id/insert_chord_edit_text"
android:layout_weight="2"
android:text=""/>
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:id="#+id/add_chord_button"
android:layout_weight="1"
android:text="Add"/>
</LinearLayout>
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="#+id/search_btn"
android:text="Search"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_marginBottom="32dp" />
</RelativeLayout>
Everything works fine, except that I cannot see Spinner. (and I have tested on real device). after that, I change layout above. Instead of say : spinner is above listview and listview is above of linearlayout. I change to : listview is below of spinner and listview is above of linearlayout. I have change as below layout:
<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"
android:orientation="vertical">
<Spinner
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:textColor="#0e0e0e"
android:id="#+id/spinner"/>
<ListView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="#+id/list_view"
android:layout_alignParentLeft="true"
android:layout_below="#+id/spinner" // ADD THIS LINE
android:layout_above="#+id/linear_layout" />
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:id="#+id/linear_layout"
android:layout_above="#+id/search_btn"
android:weightSum="3"
android:layout_alignParentLeft="true">
<EditText
android:layout_width="0dp"
android:layout_height="wrap_content"
android:id="#+id/insert_chord_edit_text"
android:layout_weight="2"
android:text=""/>
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:id="#+id/add_chord_button"
android:layout_weight="1"
android:text="Add"/>
</LinearLayout>
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="#+id/search_btn"
android:text="Search"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_marginBottom="32dp" />
</RelativeLayout>
And it works as I want, but I cannot explain why. Please tell me.
Thanks :)

In first case, your listview was mentioned as height fill parent and it has layout_above attribute but no layout_below attribute. So the listview occupied all height above the linear_layout element. Thats why you cant see the spinner.as the spinner visibility is overridden by listview.

// try this
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="5dp"
android:orientation="vertical" >
<Spinner
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:textColor="#0e0e0e"
android:id="#+id/spinner"/>
<ListView
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:id="#+id/list_view"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_marginTop="5dp"
android:id="#+id/linear_layout">
<EditText
android:layout_width="0dp"
android:layout_height="wrap_content"
android:id="#+id/insert_chord_edit_text"
android:layout_weight="2"
android:text=""/>
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:id="#+id/add_chord_button"
android:layout_weight="1"
android:text="Add"/>
</LinearLayout>
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="#+id/search_btn"
android:text="Search"
android:layout_marginBottom="5dp"
android:layout_marginTop="5dp"/>
</LinearLayout>

set layout_weight="1" for your Spinner may solve your problem.

Related

(Eclipse) move item to top of listveiw

i create custom listveiw but item of list go bottom of list
my activity xml
<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:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context="ir.sheikhoo.sis.ListuniActivity"
android:background="#drawable/bg2" >
<LinearLayout
android:id="#+id/linearLayoutBottom"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="14dp" >
<Button
android:id="#+id/btnCancel"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight=".5"
android:text="#string/list_activity_btn_cancel" />
<Button
android:id="#+id/btnReload"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight=".5"
android:text="#string/list_activity_btn_ok" />
</LinearLayout>
<ListView
android:id="#android:id/list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_above="#+id/linearLayoutBottom"
android:dividerHeight="0dp" />
</RelativeLayout>
and this is list_row.xml
<?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="wrap_content"
android:padding="8dp">
<ImageView
android:id="#+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentTop="true"
android:src="#drawable/ic_university_white" />
<TextView
android:id="#+id/uniName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_toStartOf="#+id/imageView1"
android:textColor="#color/white_color"
android:text="#string/list_title" />
<ImageView
android:id="#+id/next_ic"
android:layout_width="15dp"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:paddingLeft="5dp"
android:paddingRight="5dp"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:src="#drawable/next_ic"
android:layout_centerVertical="true" />
</RelativeLayout>
The issue is that you have android:layout_above="#+id/linearLayoutBottom" on your ListView. Since it is wrapping content, it only wraps the vertical content that is inside of it, and since you have it constrained above the button linear layout, it's staying there. You probably want to remove the above constraint and try adding android:alignParentTop="true" on your ListView

How to add button between listview?

I am trying to put button between two list view which are vertically aligned.Can someone please help me out with the problem.I am very new to android ,I don't have any idea how to do that.
I want to put two small button in the space available between two columns.Please find the below xml code :
<?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"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
tools:context="com.edkul.vimal.edkul.Library"
android:id="#+id/linearLayout1"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="#string/library"
android:textSize="25sp"
android:id="#+id/textView27"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" />
<LinearLayout
android:id="#+id/linearLayout2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_marginTop="35dp"
android:layout_below="#+id/textView27">
<ListView
android:id="#+id/list_item1"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_marginRight="55dp"
android:layout_height="wrap_content"
android:layout_gravity="left"
android:background="#88FF0000">
</ListView>
<ListView
android:id="#+id/list_item3"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:layout_gravity="left"
android:background="#88FF0000">
</ListView>
</LinearLayout>
<Button
android:layout_width="match_parent"
android:layout_below="#+id/linearLayout2"
android:layout_height="match_parent"
android:text="Button1"/>
</RelativeLayout>
Try with this.
Replace this full code in your XML.
<?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:id="#+id/linearLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
tools:context="com.edkul.vimal.edkul.Library">
<TextView
android:id="#+id/textView27"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:text="Library"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textSize="25sp" />
<LinearLayout
android:id="#+id/linearLayout2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/textView27"
android:layout_marginTop="35dp"
android:orientation="horizontal"
android:weightSum="2">
<ListView
android:id="#+id/list_item1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="left"
android:layout_weight="1"
android:background="#88FF0000"></ListView>
<LinearLayout
android:id="#+id/linearLayout3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:orientation="vertical"
android:weightSum="2">
<Button
android:id="#+id/btn1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Button1" />
<Button
android:id="#+id/btn2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Button1" />
</LinearLayout>
<ListView
android:id="#+id/list_item3"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="left"
android:layout_weight="1"
android:background="#88FF0000"></ListView>
</LinearLayout>
</RelativeLayout>
It will show you button exactly between your listviews.
Surely it will help you.
Make the layout orientation as horizontal .use your first list views and button as 3views in your layout .
Make height as match parent for listviews but wrap content for button .
Check if it works

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>

Android Footer Fixed On Bottom And Will Sit On Top Of Other Views

I want to have two buttons that is fixed on the bottom screen like a footer and other views won't make pass on it or overlap on the two buttons. It will just stay still on the bottom always. Sorry for my English. Please help me. Btw I cannot post images so it requires 10 reputation that's why I used online image viewing. Sorry for that.
Here is the screenshot of my work which is wrong
http://postimg.org/image/pzry53ey9/
As you can see the multiline edit text overlaps on the two buttons as well as the scrollview. How to fix that?
Here is my code:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin" >
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<ImageView
android:id="#+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:src="#drawable/ic_launcher" />
<EditText
android:id="#+id/editText1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:singleLine="true" >
<requestFocus />
</EditText>
<EditText
android:id="#+id/editText2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:singleLine="true" />
<EditText
android:id="#+id/editText3"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
</LinearLayout>
</ScrollView>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:gravity="bottom" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<Button
android:id="#+id/button1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Save" />
<Button
android:id="#+id/button2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Cancel" />
</LinearLayout>
</RelativeLayout>
</RelativeLayout>
Try this way,hope this will help you to solve your problem.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:orientation="vertical">
<ScrollView
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<ImageView
android:id="#+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:src="#drawable/ic_launcher" />
<EditText
android:id="#+id/editText1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:singleLine="true" >
<requestFocus />
</EditText>
<EditText
android:id="#+id/editText2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:singleLine="true" />
<EditText
android:id="#+id/editText3"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
</ScrollView>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button
android:id="#+id/button1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Save" />
<Button
android:id="#+id/button2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Cancel" />
</LinearLayout>
</LinearLayout>
your layout is quite complex for a simple view but i have modified it to achieve what you want
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin" >
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="#+id/relativeLayout1"
android:layout_marginBottom="5dp" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<ImageView
android:id="#+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:src="#drawable/ic_launcher" />
<EditText
android:id="#+id/editText1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:singleLine="true" >
<requestFocus />
</EditText>
<EditText
android:id="#+id/editText2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:singleLine="true" />
<EditText
android:id="#+id/editText3"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
</LinearLayout>
</ScrollView>
<RelativeLayout
android:id="#+id/relativeLayout1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:gravity="bottom" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<Button
android:id="#+id/button1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Save" />
<Button
android:id="#+id/button2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Cancel" />
</LinearLayout>
</RelativeLayout>
You don't need the RelativeLayout containing the LinearLayout that holds your buttons...get rid of the RelativeLayout and specify the LinearLayout like so:
<LinearLayout
android:id="#+id/buttons_container"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_alignParentBottom="true" >
Next, since your parent Layout is RelativeLayout, you'll need to specify the positioning of your ScrollView containing the EditText:
<ScrollView
android:layout_above="#+id/buttons_container"
android:layout_width="match_parent"
android:layout_height="match_parent">
Also, if you want the buttons to stay at the very bottom of your view then remove this line from the parent Layout:
android:paddingBottom="#dimen/activity_vertical_margin"
Hope this helps!
Your root layout file is relative layout. It allows overlapping of UI elements(z-index placement). So you need to provide the boundary for a view to extend. Assign a android:id= "#+id\relativeLayout" to your relative layout(bottom not root). Then in your scrollview put
android:layout_above="#+id\relativeLayout". Follow similar approach for other views.

android layout: how to write this one?

I want an activity with below layout:
top button
middle(scroll view)
bottom button
I want the bottom button to attach to the bottom of the activity view, and the middle part occupies the rest of the space.
How I can write the layout xml file?
Below doesn't work:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:layout_height="fill_parent">
<LinearLayout android:focusable="true"
android:focusableInTouchMode="true" android:layout_width="0px"
android:layout_height="0px" />
<LinearLayout android:orientation="vertical"
android:layout_width="fill_parent" android:weightSum="1" android:layout_height="wrap_content">
<LinearLayout android:orientation="horizontal"
android:layout_width="fill_parent" android:layout_height="wrap_content"
android:weightSum="1">
<EditText android:id="#+id/edit_search"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:lines="1" android:hint="发微博内容" android:layout_weight="0.9" />
<Button android:layout_height="wrap_content" android:text="Search"
android:layout_width="wrap_content" android:id="#+id/search_btn"
android:layout_weight="0.1"></Button>
</LinearLayout>
<TextView android:layout_width="fill_parent"
android:layout_height="wrap_content" android:id="#+id/TextView01"
android:paddingTop="5dip" />
<ListView android:layout_width="fill_parent"
android:id="#+id/lv01" android:layout_height="390dp">
</ListView>
<Button android:id="#+id/create_new_btn"
android:layout_height="wrap_content" android:text="我要评论" android:layout_gravity="bottom"
android:layout_width="fill_parent"></Button>
</LinearLayout>
</LinearLayout>
For that case, just take a RelativeLayout, its better for this case and you don't need to take any sub-layout.
Just try this once and post your XML layout here for the further improvement.
From your xml layout code, i can assume, you just want to have:
EditText+Button=>Top,
TextView+ListView=>Middle,
Create New button=>Bottom,
is this the case?
Keep it simple, go with LinearLayout, and play with the weights:
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0">
</Button>
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1">
<!-- Stuff in scroll view -->
</ScrollView>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0">
</Button>
</LinearLayout>
#ParashMayani is right..
Here is the implementation :
<LinearLayout android:focusable="true"
android:focusableInTouchMode="true" android:layout_width="fill_parent"
android:layout_height="wrap_content" android:id="#+id/relLayout" android:orientation="horizontal" >
<EditText android:id="#+id/edit_search"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:lines="1" android:hint="Hello" android:layout_weight="0.7"/>
<Button android:layout_height="wrap_content" android:text="Search"
android:layout_weight="0.3" android:layout_width="wrap_content" android:id="#+id/search_btn" />
</LinearLayout>
<TextView android:layout_width="fill_parent"
android:layout_height="wrap_content" android:id="#+id/TextView01"
android:paddingTop="5dip" android:layout_below="#id/relLayout"/>
<Button android:id="#+id/create_new_btn"
android:layout_height="wrap_content" android:text="我要评论" android:layout_gravity="bottom"
android:layout_width="fill_parent" android:layout_alignParentBottom="true"/>
<ListView android:layout_width="fill_parent"
android:id="#+id/lv01" android:layout_height="0dp" android:layout_above="#id/create_new_btn" android:layout_below="#id/TextView01" />
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button android:id="#+id/button1" android:layout_height="wrap_content" android:layout_width="wrap_content" android:text="Button" android:layout_alignParentTop="true" android:layout_alignParentLeft="true"></Button>
<Button android:id="#+id/button2" android:layout_height="wrap_content" android:layout_width="wrap_content" android:text="Button" android:layout_alignParentBottom="true" android:layout_alignParentLeft="true"></Button>
<ScrollView android:id="#+id/scrollView1" android:layout_height="wrap_content" android:layout_width="match_parent" android:layout_below="#+id/button1" android:layout_above="#+id/button2">
<LinearLayout android:id="#+id/linearLayout1" android:layout_width="match_parent" android:layout_height="match_parent"></LinearLayout>
</ScrollView>
</RelativeLayout>
I think something like that would work. The important part in that layout is the android:layout_below and android:layout_above. You can replace the ScrollView with other layouts such as ListView and it will remain below the first button and above the second regardless of how big it gets. Likewise, the buttons have the attributes android:layout_alignParentTop="true" and android:layout_alignParentBottom="true" which will keep them to the top and the bottom of the RelativeView respectively.

Categories

Resources