This should be pretty easy, but since I am new in android development I am asking. I basicaly want to create a static menu bar at the bottom of my avtivity with simple buttons (previous / home / next). However I have problem in aligning those since android:layout_alignParentRight="true" does not seem to do the work. I have also tried out gravity, but neither that works.My current code:
<LinearLayout
android:id="#+id/bottomBar"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:layout_alignParentBottom="true"
android:orientation="horizontal"
android:gravity="center_vertical">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Button"
android:id="#+id/previousButton"
android:layout_alignBottom="#+id/bottomBar"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Button"
android:id="#+id/nextButton"
android:layout_alignBottom="#+id/bottomBar"
/>
</LinearLayout>
EDIT: here is all my layout xml 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:id="#+id/container"
android:paddingBottom="#dimen/activity_vertical_margin"
tools:context="menelaos.example.com.geolearning.QuizActivity">
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/ScrollView01"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/quizTableLayout"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
</TableLayout>
<TableLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="#+id/answerTableLayout"
android:layout_below="#+id/quizTableLayout">
</TableLayout>
</LinearLayout> </ScrollView>
<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:id="#+id/bottomBar">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerVertical="true"
android:orientation="horizontal" >
<Button
android:id="#+id/previousButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Button" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Button"
android:id="#+id/nextButton"
android:layout_toRightOf="#id/previousButton"/>
</LinearLayout>
</RelativeLayout>
You have to use a RelativeLayout.
Try something 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" >
<Button
android:id="#+id/previousButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:text="New Button" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Button"
android:layout_alignParentBottom="true"
android:id="#+id/nextButton"
android:layout_toRightOf="#id/previousButton"
/>
</RelativeLayout>
EDIT:
try this then:
<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:id="#+id/bottomBar">
/>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerVertical="true"
android:orientation="horizontal" >
<Button
android:id="#+id/previousButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Button" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Button"
android:id="#+id/nextButton"
android:layout_toRightOf="#id/previousButton"/>
</LinearLayout>
</RelativeLayout>
EDIT 2
Ok, so if I understood correctly:
You want a Layout composed by a Scroll View and a View in the bottom of the screen with two buttons, one aligned to the left of the screen and the other aligned to the right.
I think the following layout suggestion can work out for you. Nevertheless, without seeing your current layout I cannot clearly understand how you want your layout to be.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/bottomBar"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
/>
<ScrollView
android:id="#+id/scrollView1"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="420dp"
android:orientation="vertical" >
</LinearLayout>
</ScrollView>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<Button
android:id="#+id/previousButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="left"
android:text="New Button"
android:layout_alignParentLeft="true"/>
<Button
android:id="#+id/nextButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Button"
android:layout_alignParentRight="true"/>
</RelativeLayout>
</LinearLayout>
EDIT 3
In order to have a 3rd button just add the XML below after the last Button view:
<Button
android:id="#+id/center"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Center"
android:layout_toRightOf="#id/previousButton"
android:layout_toLeftOf="#id/nextButton"
android:layout_marginLeft="15dp"
android:layout_marginRight="15dp"/>
I've added the marginLeft and marginRightso that the buttons have a distance between them, as I think this is what you want.
you can't use the xml attribute alignParentBottom in a LinearLayout. It works for a RelativeLayout.
Secondly, the attribute gravity only position the content of the tag ( it will put the buttons in the center of the page verticalty)
Try this
<RelativeLayout
android:id="#+id/bottomBar"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:layout_alignParentBottom="true">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Button"
android:id="#+id/previousButton"
android:layout_alignBottom="#+id/bottomBar"
android:layout_alignParentLeft="true"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Button"
android:id="#+id/nextButton"
android:layout_alignBottom="#+id/bottomBar"
android:layout_alignParentRight="true"
/>
</RelativeLayout>
If you only need to make each button take the same width and occupy the total width of the parent layout:
In buttons, change to android:layout_width="0dp"
and add android:layout_weight="1"
This will distribute all the buttons with homogeneous width.
Related
My layout is like this
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<EditText
android:id="#+id/searchKey"
android:gravity="center_horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="xx" />
<ScrollView
android:layout_weight="1"
android:fillViewport="true"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="#+id/search_results"
android:textSize="#dimen/text_font_size"
android:layout_gravity="center_horizontal"
android:gravity="center_horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</ScrollView>
<LinearLayout
android:orientation="horizontal"
android:layout_gravity="center|bottom"
android:layout_weight="1"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button
android:id="#+id/lastCharacterButton"
android:layout_weight="1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/lastChar_btn" />
<Button
android:id="#+id/nextCharacterButton"
android:layout_weight="1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/nextChar_btn" />
</LinearLayout>
</LinearLayout>
However, when there are little or no text, the buttons are pulled up, when there are too many text in search_results TextView, the buttons won't show up because scrollview takes the space all the way to the bottom. Any suggestion to always keep the buttons at the bottom and always visible?
First of all you have to set the ScrollView's height to 0dp.
<ScrollView
android:layout_weight="1"
android:fillViewport="true"
android:layout_width="match_parent"
android:layout_height="0dp">
<TextView
android:id="#+id/search_results"
android:textSize="#dimen/text_font_size"
android:layout_gravity="center_horizontal"
android:gravity="center_horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</ScrollView>
You have to do this because the weight param is going to control the size of the view depending on how much space is available. If you set it to wrap content, it will take how much space it needs to wrap everything within.
Then you need to remove your LinearLayout's weight param:
<LinearLayout
android:orientation="horizontal"
android:layout_gravity="center|bottom"
android:id="#+id/LL1"
android:layout_alignParentBottom="true"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button
android:id="#+id/lastCharacterButton"
android:layout_weight="1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Last_Char" />
<Button
android:id="#+id/nextCharacterButton"
android:layout_weight="1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="next_char" />
</LinearLayout>
And the reason for doing that is just the opposite of the previous explanation: in this case the wrap content is going to take the space necessary to always display the buttons within.
Here is some sample code I put together for you. You basically need to take out the linear layout you have wrapping you xml layout and use a relative layout instead. Once you do that you can add ids to the linear layout with the buttons and then have the edit text align to the parent top. Then you can put the scrollview below the edittext and then put the scrollview above to the linear layout containing the buttons. This should work!
<?xml version="1.0" encoding="utf-8"?>
<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="com.example.thegamefan93.loginregister.LoginActivity">
<EditText
android:id="#+id/searchKey"
android:gravity="center_horizontal"
android:layout_width="match_parent"
android:layout_alignParentTop="true"
android:layout_height="wrap_content"
android:hint="xx" />
<ScrollView
android:layout_weight="1"
android:fillViewport="true"
android:layout_below="#id/searchKey"
android:layout_width="match_parent"
android:layout_above="#+id/LL1"
android:layout_height="wrap_content">
<TextView
android:id="#+id/search_results"
android:textSize="20sp"
android:layout_gravity="center_horizontal"
android:gravity="center_horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</ScrollView>
<LinearLayout
android:orientation="horizontal"
android:layout_gravity="center|bottom"
android:layout_weight="1"
android:id="#+id/LL1"
android:layout_alignParentBottom="true"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button
android:id="#+id/lastCharacterButton"
android:layout_weight="1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Last_Char" />
<Button
android:id="#+id/nextCharacterButton"
android:layout_weight="1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="next_char" />
</LinearLayout>
</RelativeLayout>
Try this,
<?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">
<EditText
android:id="#+id/searchKey"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:hint="xx" />
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_above="#+id/linear"
android:layout_below="#+id/searchKey"
android:layout_weight="1"
android:fillViewport="true">
<TextView
android:id="#+id/search_results"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:gravity="center_horizontal"
android:textSize="#dimen/text_font_size" />
</ScrollView>
<LinearLayout
android:id="#+id/linear"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:orientation="horizontal">
<Button
android:id="#+id/lastCharacterButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="#string/lastChar_btn" />
<Button
android:id="#+id/nextCharacterButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="#string/nextChar_btn" />
</LinearLayout>
</RelativeLayout>
I am creating a revision app and I want to add notes to the app.
How do I add lots of text to one screen (I have already tried adding a TextView but there is not enough room to fit it all in is there a way to make the screen longer (with a scroll down))?
I am new to android and your help will be appreciated.
use scrollview inside linearlayout
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ScrollView
android:id="#+id/scrollView1"
android:fillViewport="true"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="10dp"
android:text="mytexts"
android:textAppearance="?android:attr/textAppearanceSmall"
/>
</ScrollView>
</LinearLayout>
Yes you can make your screen scrollabel by adding just a few lines in
AndroidManifest.xml. Here is an example.
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:weightSum="1">
<WebView
android:layout_width="match_parent"
android:layout_height="540dp"
android:id="#+id/webView"
android:layout_gravity="center_horizontal" />
<Button
android:layout_height="fill_parent"
android:layout_width="125dp"
android:text="Forward"
android:layout_gravity="center_horizontal" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Stop"
android:id="#+id/button"
android:layout_gravity="center_horizontal" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Back"
android:id="#+id/button2"
android:layout_gravity="center_horizontal" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Right"
android:id="#+id/button3"
android:layout_gravity="center_horizontal" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="eft"
android:id="#+id/button4"
android:layout_gravity="center_horizontal" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Cam0"
android:id="#+id/button5"
android:layout_gravity="center_horizontal" />
</LinearLayout>
</RelativeLayout>
</ScrollView>
<ScrollView>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:singleLine="false"/>
</ScrollView>
set width and height to wrap_content and singleLine to false;
I am really getting frustrated with the layout as shown below:
I would like to have listview and below that I need to have two buttons side by side as shown above(LIstview should be scrollable and buttons should be as bottom bar ie..ontop tof the listview ). I have tried using android:layout_above and android:layout_below but nothing worked.Can anyone gimme an idea how to achieve this layout ?
This is what my layout code as of now actually I have this code where I am able to achieve this but I'm unable to click the buttons here.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<LinearLayout
android:id="#+id/secondLayout"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="15dp" >
<ListView
android:id="#+id/list"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
<LinearLayout
android:id="#+id/firstLayout"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_above="#id/secondLayout"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:background="#00ffffff"
android:gravity="bottom"
android:orientation="horizontal"
android:padding="15dp" >
<Button
android:id="#+id/btnPrevious"
android:layout_width="wrap_content"
android:layout_height="35dp"
android:layout_marginLeft="10dp"
android:layout_weight="1"
android:background="#drawable/redbutton"
android:onClick=""
android:text="Previous"
android:textColor="#ffffff" />
<Button
android:id="#+id/btnNext"
android:layout_width="wrap_content"
android:layout_height="35dp"
android:layout_marginLeft="10dp"
android:layout_weight="1"
android:background="#drawable/redbutton"
android:text="Next"
android:textColor="#ffffff" />
</LinearLayout>
Step #1: Get rid of the useless LinearLayout wrapping your ListView, moving the android:padding attribute into the ListView.
Step #2: Add android:layout_alignParentTop="true" and android:layout_above="#+id/firstLayout to the ListView.
Step #3: Get rid of android:layout_above="#id/secondLayout" from the remaining LinearLayout, as you definitely do not want that above the ListView.
Step #4: Switch from dp to sp for your Button dimensions, to take into account font scaling.
Step #5: Either change the width of the LinearLayout to wrap_content or remove the android:layout_centerHorizontal="true", as you cannot center something that fills all available space.
Off the cuff, the rest should be OK, or at least be a lot closer to what you want.
how about adding the buttons to your actionbar?
Otherwise wrap the ListView inside an ScrollView and make your RelativeLayout to LinearLayout
this is working. What problem are you facing exactly?
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:id="#+id/secondLayout"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="15dp" >
<ListView
android:id="#+id/list"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
<LinearLayout
android:id="#+id/firstLayout"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_above="#id/secondLayout"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:background="#00ffff"
android:gravity="bottom"
android:orientation="horizontal"
android:padding="15dp" >
<Button
android:id="#+id/btnPrevious"
android:layout_width="wrap_content"
android:layout_height="35dp"
android:layout_marginLeft="10dp"
android:layout_weight="1"
android:background="#FF0000"
android:onClick="test"
android:text="Previous"
android:textColor="#ffffff" />
<Button
android:id="#+id/btnNext"
android:layout_width="wrap_content"
android:layout_height="35dp"
android:layout_marginLeft="10dp"
android:layout_weight="1"
android:onClick="test"
android:background="#FF0000"
android:text="Next"
android:textColor="#ffffff" />
</LinearLayout>
This may work for you:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ListView
android:id="#+id/list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_above="#+id/firstLayout" />
<LinearLayout
android:id="#+id/firstLayout"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:background="#00ffffff"
android:orientation="horizontal"
android:padding="15dp" >
<Button
android:id="#+id/btnPrevious"
android:layout_width="wrap_content"
android:layout_height="35dp"
android:layout_marginLeft="10dp"
android:layout_weight="1"
android:onClick=""
android:text="Previous"
android:textColor="#ffffff" />
<Button
android:id="#+id/btnNext"
android:layout_width="wrap_content"
android:layout_height="35dp"
android:layout_marginLeft="10dp"
android:layout_weight="1"
android:text="Next"
android:textColor="#ffffff" />
</LinearLayout>
</RelativeLayout>
I want to align two buttons with a linear layout, one on the left, and one on the right, like the next and previous buttons on an image gallery. I tried to align them but it doesn't work.
XML layout code:
<?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"
android:orientation="vertical"
android:background="#android:color/white"
android:gravity="bottom" >
<LinearLayout
android:id="#+id/linearLayout1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#android:color/black" >
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="prev"
android:layout_alignParentRight="true" />
<Button
android:id="#+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="next" />
</LinearLayout>
</LinearLayout>
Actual output:
Expected output:
How can I fix it?
Use a RelativeLayout. There you can set android:layout_alignParentLeft and android:layout_alignParentRight.
This should work for you:
<?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"
android:orientation="vertical"
android:background="#android:color/white"
android:gravity="bottom" >
<RelativeLayout
android:id="#+id/relativeLayout1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#android:color/black" >
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="prev"
android:layout_alignParentLeft="true" />
<Button
android:id="#+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="next"
android:layout_alignParentRight="true"/>
</RelativeLayout>
</LinearLayout>
With Linear Layout
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Prev"/>
</LinearLayout>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Next"/>
</LinearLayout>
With Relative Layout
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Prev"
android:layout_alignParentLeft="true"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Next"
android:layout_alignParentRight="true"/>
</RelativeLayout>
Use Relative layout in your LinearLayout;
Also add android:layout_alignParentLeft="true" to "prev" Button
and android:layout_alignParentRight="true" to "next" Button
<?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"
android:background="#android:color/white"
android:gravity="bottom"
android:orientation="vertical" >
<RelativeLayout
android:id="#+id/relativeLayout1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#android:color/black" >
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true" <---- ADD this prop
android:text="prev" />
<Button
android:id="#+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true" <----- ADD this prop
android:text="next" />
</RelativeLayout>
</LinearLayout>
I guess, the simplest solution is to use View in your inner LinearLayout.
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="prev"/>
<View
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_weight="1"/>
<Button
android:id="#+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="next"/>
</LinearLayout>
Something like 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="match_parent"
android:orientation="vertical" >
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:gravity="right" >
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:text="Button" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_toLeftOf="#+id/button1"
android:orientation="vertical" >
<Button
android:id="#+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" />
</LinearLayout>
</RelativeLayout>
</LinearLayout>
The idea is, to create a RelativeLayout as container and put first Button and then stick it to the right of parent view. After that, add Another button inside LinearLayout and set this LinearLayout into the left of first Button.
Here's the result.
ur layout xml should be as shown below
<?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"
android:gravity="bottom"
android:background="#fff">
<RelativeLayout
android:id="#+id/relativeLayout1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#000"
android:layout_gravity="bottom" >
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:text="Pre" />
<Button
android:id="#+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:text="Next" />
</RelativeLayout>
</RelativeLayout>
Use a RelativeLayout instead of a LinearLayout you can add the tags android:layout_alignParentRight="true" and android:layout_alignParentLeft="true" on each button.
You can use RelativeLayout like this:
<?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"
android:background="#android:color/white"
android:orientation="vertical" >
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#android:color/black"
android:gravity="bottom"
android:orientation="horizontal" >
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:text="prev" />
<Button
android:id="#+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:text="next" />
</RelativeLayout>
</LinearLayout>
(I used grey instead of white for the screenshot)
Use a FrameLayout and add the layout_gravity attribute in each of the buttons
Do something like this it will surely help you out
<?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"
>
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/Linear"
android:textSize="35dp"
android:layout_margin="90dp"
/>
<LinearLayout
android:id="#+id/linearLayout1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#android:color/black">
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Relative"
/>
<Button
android:id="#+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="200dp"
android:text="Constraint" />
</LinearLayout>
</LinearLayout>
Use margin_Start and End to seprate the buttons in the layout
<?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:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="-:Linear Layout:-"
android:textSize="35sp"
android:layout_gravity="center"
android:layout_marginTop="250dp"
tools:ignore="HardcodedText"/>
<LinearLayout
android:id="#+id/linear2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_marginTop="90dp">
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Relative"
tools:ignore="ButtonStyle,HardcodedText"/>
<Button
android:id="#+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="200dp"
android:text="Constraint"
tools:ignore="ButtonStyle"
/>
</LinearLayout>
</LinearLayout>
[enter image description here][1]
[1]: https://i.stack.imgur.com/NMYj2.png
For Better Performance:
Best to put two views left and right and one view between in linear layout rather than relative layout. Use the below code it will also remove the nesting of views .... It will be good in performance perpective :
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/linearLayout1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical">
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:text="prev" />
<TextView android:text="Hi its working"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1" />
<Button
android:id="#+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="next" />
</LinearLayout>
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.