Aligning buttons inside of horizontal linear layout - android

I'm trying to align a button inside of a horizontal linear layout to the right side of the screen in my Android application.
How can I do that? I couldn't find an align option in the properties pane. I've tried some Googling, but I couldn't find much since I don't know the correct terms with which to search.
Here's a render so you can get an idea:
Here's my current activity_main.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:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin" tools:context=".MainActivity">
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Button"
android:id="#+id/button"
android:layout_gravity="center_vertical" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Button"
android:id="#+id/button3" />
</LinearLayout>
</RelativeLayout>
Thanks for your time!

It is easier to use FrameLayout in that case so that you can assign gravity.
<FrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Button"
android:id="#+id/button"
android:layout_gravity="left|center_vertical" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Button"
android:id="#+id/button3"
android:layout_gravity="right|center_vertical" />
</FrameLayout>
If you have to use LinearLayout, you can put an empty view with weight to occupy that space in the middle
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Button"
android:id="#+id/button"
android:layout_gravity="center_vertical" />
<View
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Button"
android:id="#+id/button3" />
</LinearLayout>

Related

Android Studio: Button does not show up under the listview in a linear layout

I want to have the delete button under the listview, and I tried writing it in both linear layout and relative layout, but the delete button just doesn't show up in both layout. What could be the problem?
In RelativeLayout version
<FrameLayout 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"
tools:context="myproject.testing.listviewwithbutton">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<AutoCompleteTextView
android:id="#+id/autoCompleteTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="New Ingredient"
android:layout_alignParentTop="true" />
<Button
android:id="#+id/addButton"
android:layout_width="wrap_content"
android:layout_height="41dp"
android:text="Add"
android:layout_alignBaseline="#id/autoCompleteTextView"
android:layout_alignParentRight="true"/>
</RelativeLayout>
<ListView
android:layout_width="wrap_content"
android:layout_height="457dp"
android:id="#+id/mainListView"
android:layout_weight="1"/>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="bottom">
<Button
android:id="#+id/deleteButton"
android:layout_width="wrap_content"
android:layout_height="41dp"
android:text="Delete"
android:layout_gravity="center_horizontal"
android:layout_weight="0"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" />
</RelativeLayout>
</LinearLayout>
In LinearLayout version
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<AutoCompleteTextView
android:id="#+id/autoCompleteTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="New Ingredient"
android:layout_alignParentTop="true" />
<Button
android:id="#+id/addButton"
android:layout_width="wrap_content"
android:layout_height="41dp"
android:text="Add"
android:layout_alignBaseline="#id/autoCompleteTextView"
android:layout_alignParentRight="true"/>
</RelativeLayout>
<ListView
android:layout_width="wrap_content"
android:layout_height="457dp"
android:id="#+id/mainListView"
android:layout_weight="1"/>
<Button
android:layout_width="wrap_content"
android:layout_height="41dp"
android:id="#+id/deleteButton"
android:text="Delete"
android:layout_weight="0"
android:layout_gravity="center_horizontal"/>
</LinearLayout>
Instead of making different layout for each views, using single relative layout with below attributes:
AutoCompleteTextView & Add button - no changes
Delete button - align parent bottom as true
List view - align below AutoCompleteTextView and align above delete button

ScrollView is not scrolling?

Ok I am trying to create a screen with a lot of buttons on it, obviously I ran out of space so I set my layout to a ScrollView with a relative layout underneath it and then placed all of my buttons inside that.
heres the code:
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="1000dp" 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:fillViewport="true"
tools:context="com.cecode.www.gjcode.MainTypes"
android:animateLayoutChanges="true">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Options Menu(placeholder)"
android:id="#+id/optionsBtn"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<Button
android:layout_width="400dp"
android:layout_height="80dp"
android:text="Top Text"
android:id="#+id/button1"
android:layout_marginTop="110dp"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
/>
<Button
android:layout_width="400dp"
android:layout_height="80dp"
android:text="placeholder text"
android:id="#+id/button2"
android:layout_marginTop="20dp"
android:layout_below="#+id/button1"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<Button
android:layout_width="400dp"
android:layout_height="80dp"
android:text="placeholder text"
android:id="#+id/button3"
android:layout_marginTop="20dp"
android:layout_below="#+id/button2"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<Button
android:layout_width="400dp"
android:layout_height="80dp"
android:text="placeholder text"
android:id="#+id/button4"
android:layout_marginTop="20dp"
android:layout_below="#+id/button3"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="mid text"
android:id="#+id/textView3"
android:layout_below="#+id/button4"
android:layout_alignRight="#+id/optionsBtn"
android:layout_alignEnd="#+id/optionsBtn"
android:layout_marginTop="37dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="bottom text"
android:id="#+id/textView4"
android:layout_alignParentBottom="true"
android:layout_alignLeft="#+id/textView3"
android:layout_alignStart="#+id/textView3" />
</RelativeLayout>
</ScrollView>
heres a screenshot of the screen-
My problem is that when I test this on a device it will not scroll past the mid text point, so I cannot get to the bottom text bit. Thanks!
replace
android:layout_height="1000dp"
with
android:layout_height="match_parent"
and let me know if it works
ok as you specified as per your requirement you can use LinearLayout with verticla orientation and between mid and bottom text put another linear layout with wrap_content height to add your buttons if you want some space before adding buttons to it you can specify minheight to layout take look at below code
<ScrollView 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:fillViewport="true"
tools:context="com.cecode.www.gjcode.MainTypes"
android:animateLayoutChanges="true">
<LinearLayout
android:layout_width="match_parent"
android:orientation="vertical"
android:gravity="center"
android:layout_height="match_parent">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Options Menu(placeholder)"
android:id="#+id/optionsBtn"
/>
<Button
android:layout_width="400dp"
android:layout_height="80dp"
android:text="Top Text"
android:id="#+id/button1"
android:layout_marginTop="110dp"
/>
<Button
android:layout_width="400dp"
android:layout_height="80dp"
android:text="placeholder text"
android:id="#+id/button2"
android:layout_marginTop="20dp"
/>
<Button
android:layout_width="400dp"
android:layout_height="80dp"
android:text="placeholder text"
android:id="#+id/button3"
android:layout_marginTop="20dp"
/>
<Button
android:layout_width="400dp"
android:layout_height="80dp"
android:text="placeholder text"
android:id="#+id/button4"
android:layout_marginTop="20dp"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="mid text"
android:id="#+id/textView3"
/>
<LinearLayout android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minHeight="50dp"
android:orientation="vertical">
<!-- add your button here -->
</LinearLayout>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="bottom text"
android:id="#+id/textView4"
/>
</LinearLayout>
</ScrollView>
it is really hard to catch your requirements. Let me explain how it works at the moment. ScrollView doesn't give exact height so when you set match_parent for relative layout I guess it sets max height for this view - Integer.MAX_VALUE. That is why Android Studio warns you to change match_parent to wrap_content.
If you need this view in the bottom - set the margin of your bottom view in runtime.

RelativeLayout doesn't fit in all screen sizes android

I am developing my first application in android and i am stuck with my xml layout. I have 5 buttons placed vertically in my layout under relativelayout. I am using sony xperia M and according to my screen size the layout looks perfectly fine but for big screens the layout is messed up. When i run my app in a device which has a big screen size the buttons don't have proper gap in between. How can i make my layout to fit all screen sizes ?
<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:background="#drawable/blue3"
tools:context=".MainActivity">
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="My Tasks"
android:id="#+id/mytasks_btn"
android:onClick="mytask"
style="#style/btnStyleBeige"
android:layout_alignParentTop="true"
android:layout_alignParentStart="true" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Add Task"
android:id="#+id/addtask_btn"
android:layout_marginTop="65dp"
style="#style/btnStyleBeige"
android:onClick="addtask"
android:layout_below="#+id/mytasks_btn"
android:layout_centerHorizontal="true" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Show Current Location"
android:id="#+id/shw_loc_btn"
style="#style/btnStyleBeige"
android:onClick="shwlocn"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:layout_alignParentEnd="true" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Search Nearby"
android:id="#+id/button4"
android:onClick="searchnear"
style="#style/btnStyleBeige"
android:layout_below="#+id/shw_loc_btn"
android:layout_alignParentStart="true"
android:layout_marginTop="67dp" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
style="#style/btnStyleBeige"
android:text="Location Distance"
android:id="#+id/button7"
android:onClick="locdis"
android:layout_alignParentBottom="true"
android:layout_alignParentStart="true" />
You should consider wrapping your buttons inside another layout, for example LinearLayout. If you want your buttons to be evenly distributed on the screen you should use code like this.
<?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">
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true">
<Button
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:text="New Button"
android:id="#+id/button" android:layout_weight="1"/>
<Button
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:text="New Button"
android:id="#+id/button2" android:layout_weight="1"/>
<Button
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:text="New Button"
android:id="#+id/button3" android:layout_weight="1"/>
<Button
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:text="New Button"
android:id="#+id/button4" android:layout_weight="1"/>
<Button
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:text="New Button"
android:id="#+id/button5" android:layout_weight="1"/>
</LinearLayout>
</RelativeLayout>
Very important is to set children's heights to match_parent and set proper layout_weight values.

Working with relative layout in android XML

I am trying to create a layout in XML something along of the lines of http://i.stack.imgur.com/aPoeU.png but I am little confused as to how to position the buttons in that format. I have created the buttons and tried different things like alignParentBottom, alignParentRight etc but I can't seem to get it the way I want it to be. Can someone please help me out?
<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" >
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:text="Button" />
<Button
android:id="#+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_below="#+id/button1"
android:text="Button" />
<Button
android:id="#+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_below="#+id/button2"
android:text="Button" />
<Button
android:id="#+id/button4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_below="#+id/button3"
android:text="Button" />
We can manage it in all screen and in all resolution by weight_sum
Paste below code in your xml
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#000"
android:weightSum="1" >
<Button
android:id="#+id/button1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#bebebe"
android:layout_weight="0.30"
android:text="Button1" />
<Button
android:id="#+id/button2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="2dp"
android:layout_weight="0.20"
android:background="#bebebe"
android:text="Button2" />
<Button
android:id="#+id/button3"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="2dp"
android:layout_weight="0.20"
android:background="#bebebe"
android:text="Button3" />
<Button
android:id="#+id/button4"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="2dp"
android:layout_weight="0.30"
android:background="#bebebe"
android:text="Button4" />
</LinearLayout>
Paste it in your XML
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#2258A2"
android:gravity="center"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#000"
android:orientation="vertical"
android:weightSum="1" >
<Button
android:id="#+id/button1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="0.40"
android:background="#bebebe"
android:text="Button1" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="1dp"
android:layout_weight="0.20"
android:orientation="horizontal"
android:weightSum="1" >
<Button
android:id="#+id/button2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginRight="1dp"
android:layout_weight="0.50"
android:background="#bebebe"
android:text="Button2" />
<Button
android:id="#+id/button3"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="0.50"
android:background="#bebebe"
android:text="Button3" />
</LinearLayout>
<Button
android:id="#+id/button4"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="1dp"
android:layout_weight="0.40"
android:background="#bebebe"
android:text="Button4" />
</LinearLayout>
Your code seems correct, but if you want to replicate the size of the buttons then you can't set the height as wrap_content. This will make it as tall as the text, change that to some given value:
android:layout_height="50dp"
Well, in RelativeLayout, as the name suggests, the elements are positioned relative to something else, like the parent view, or another widget in the layout. So you can align the widget in many many ways, like apending it to the right of a view, to the left, above, below, and so on.
Please refer to this link and this other link for more information.
So, if you want to make your buttons look exactly like your example image, try 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"
tools:context=".MainActivity" >
<Button
android:id="#+id/button1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:text="Button 1" />
<Button
android:id="#+id/button2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_below="#+id/button1"
android:text="Button 2" />
<Button
android:id="#+id/button3"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_below="#+id/button2"
android:text="Button 3" />
<Button
android:id="#+id/button4"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_below="#+id/button3"
android:text="Button 4" />
You can simply make the button width to fill_parent instead of align left and right. And the height, you can define the measures you'd like to use.
Btw, I used alignParentLeft and alignParentStart because I don't know which API level you're using, so I've used both parameters, old and new. But the effect is the same, your buttons will always start to the left of the parent view.
EDIT: As the user hasternet suggested, you could also use LinearLayout and get similar results.
EDIT 2: And if you like to change the look of your buttons, try to change the style (Take a look here to know what I mean).
Since you need to set the height of the buttons differently and also it depends on different screen size, you cannot achieve this with Relative Layout.
So go for LinearLayout and weightsum attribute. This way screen big or small, you can achieve your expected UI. Mentioned code below modified.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:Orientation="vertical"
android:weightsum="8" >
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:text="Button"
android:layout_weight="1"
/>
<Button
android:id="#+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_below="#+id/button1"
android:text="Button"
android:layout_weight="3"
/>
<Button
android:id="#+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_below="#+id/button2"
android:text="Button"
android:layout_weight="3"/>
<Button
android:id="#+id/button4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_below="#+id/button3"
android:text="Button"
android:layout_weight="1"/>
</LinearLayout>

Aligning Left/Center/Right LinearLayout - Android

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.

Categories

Resources