Align Multiple Views at the bottom of screen - android

I'm new to Android and I want to create an activity with 3 set (line) of Buttons at the bottom of screen .For my example I wrote this code for 2 set (line) of buttons:
<?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">
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:layout_alignParentBottom="true">
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="1" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="2" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="3" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="4" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="5" />
</LinearLayout>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:layout_alignParentBottom="true">
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="6" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="7" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="8" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="9" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="10" />
</LinearLayout>
</RelativeLayout>
I want to buttons 6-10 put on (above) the buttons 1-5 (like Stack).
How I can do this?
Thanks

You are using a RelativeLayout with two LinearLayouts.
You should give both your LinearLayout ids and after that you will position the other LinearLayout which you want to be on top by giving it the atrribute
android:layout_above="#id/the_id_of_the_one_you_want_to_be_below"

Try to wrap your two LinearLayout in a parent linearLayout.
That parent layout need to be aligned to the bottom.
Something like 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">
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:layout_alignParentBottom="true"
android:orientation="vertical">
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="1" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="2" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="3" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="4" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="5" />
</LinearLayout>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="6" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="7" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="8" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="9" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="10" />
</LinearLayout>
</LinearLayout>
</RelativeLayout>

Related

How to make TextView fill space vertically in ScrollView?

I have following layout. I have FrameLayout (fuchsia) filling entire screen and on the left I have ScrollView (silver). In ScrollView is LinearLayout (lime) with buttons. Above bottom button is TextView spacer. I want it to fill vertical space so that bottom button is at the bottom of screen. With scroll view scrolling works but spacer is not stretched:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ff00ff">
<ScrollView
android:layout_width="134dp"
android:layout_height="match_parent"
android:background="#aaaaaa">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#00ff00"
android:orientation="vertical">
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Top" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="1" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="2" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="3" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="4" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="5" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="6" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="7" />
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="Spacer" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Bottom" />
</LinearLayout>
</ScrollView>
</FrameLayout>
If I remove ScollView then spacer is stretched but scrolling doesn't work in landscape mode:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ff00ff">
<LinearLayout
android:layout_width="134dp"
android:layout_height="match_parent"
android:background="#00ff00"
android:orientation="vertical">
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Top" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="1" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="2" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="3" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="4" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="5" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="6" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="7" />
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="Spacer" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Bottom" />
</LinearLayout>
</FrameLayout>
Solution:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ff00ff">
<ScrollView
android:layout_width="134dp"
android:layout_height="match_parent"
android:fillViewport="true"
android:background="#aaaaaa">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#00ff00"
android:orientation="vertical">
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Top" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="1" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="2" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="3" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="4" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="5" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="6" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="7" />
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="Spacer" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Bottom" />
</LinearLayout>
</ScrollView>
</FrameLayout>
Setting android:fillViewport="true" on the ScrollView fixed your issue and setting the height of ScrollView to match_parent.
I think constraint layout will help you.
<ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ScrollView
android:layout_width="134dp"
android:layout_height="0dp"
app:layout_constraintBottom_toTopOf="#+id/btnBottom"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<LinearLayout
android:layout_width="134dp"
android:layout_height="wrap_content"
android:background="#00ff00"
android:orientation="vertical">
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Top" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="1" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="2" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="3" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="4" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="5" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="6" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="7" />
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="Spacer" />
</LinearLayout>
</ScrollView>
<Button
android:id="#+id/btnBottom"
android:layout_width="134dp"
android:layout_height="wrap_content"
android:text="Bottom"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent" />
</ConstraintLayout>

Linear layout inside Frame Layout

I have the following code in Android Studio in which I have two Linear Layouts inside a Frame layout. However, the buttons in both layout map on top of each other. Is it possible to have the two linear layouts (including the buttons) placed next to each other vertically rather than being mapped on top of peach other?
<FrameLayout 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:orientation="vertical"
android:background="#FFFFFF"
tools:context="com.dji.GSDemo.GoogleMap.MainActivity">
<fragment
android:id="#+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent"
class="com.google.android.gms.maps.SupportMapFragment" />
<FrameLayout
android:id="#+id/fram_map"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:id="#+id/locate"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Locate"
android:layout_weight="1"/>
<Button
android:id="#+id/add"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Add"
android:layout_weight="1"/>
<Button
android:id="#+id/clear"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Clear"
android:layout_weight="1"/>
<Button
android:id="#+id/btn_draw_State"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Free Draw" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:id="#+id/config"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Config"
android:layout_weight="0.9"/>
<Button
android:id="#+id/upload"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Upload"
android:layout_weight="0.9"/>
<Button
android:id="#+id/start"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Start"
android:layout_weight="1"/>
<Button
android:id="#+id/stop"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Stop"
android:layout_weight="1"/>
</LinearLayout>
</FrameLayout>
The following is a screenshot of how the buttons appears.
Try to change FrameLayout to LinearLayout in the root of xml code .
And set android:orientation="vertical"
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/fram_map"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:id="#+id/locate"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Locate" />
<Button
android:id="#+id/add"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Add" />
<Button
android:id="#+id/clear"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Clear" />
<Button
android:id="#+id/btn_draw_State"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Free Draw" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:id="#+id/config"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0.9"
android:text="Config" />
<Button
android:id="#+id/upload"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0.9"
android:text="Upload" />
<Button
android:id="#+id/start"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Start" />
<Button
android:id="#+id/stop"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Stop" />
</LinearLayout>
</LinearLayout>
FrameLayout will layout all children in the same frame on screen.
If you want multiple children to stack vertically or horizontally, you can wrap them in a LinearLayout instead. In this case, you can simply replace that root FrameLayout with a LinearLayout with android:orientation="vertical" to stack the two LinearLayouts vertically.

How Can I edit my buttons

I tried to use a LinearLayout with 4 buttons but the last one was cut. How can I edit my code to get button's width corresponding to all smartphones'resolutions
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0.2"
android:gravity="center_horizontal"
android:orientation="horizontal">
<Button
android:id="#+id/red"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="RED" />
<Button
android:id="#+id/blue"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="BLUE" />
<Button
android:id="#+id/yellow"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="YELLOW" />
<Button
android:id="#+id/green"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="GREEN" />
</LinearLayout>
Thanks
make width="0dp" in all buttons and add
android:layout_weight="1"
to all buttons
First,set android:weightSum="4" in the LinearLayout.
And set android:layout_width="Odp" and android:layout_weight="1" in your Button .
<?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">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:weightSum="4"
android:gravity="center_horizontal"
android:orientation="horizontal">
<Button
android:id="#+id/red"
android:layout_width="Odp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="RED" />
<Button
android:id="#+id/blue"
android:layout_width="Odp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="BLUE" />
<Button
android:id="#+id/yellow"
android:layout_width="Odp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="YELLOW" />
<Button
android:id="#+id/green"
android:layout_width="Odp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="GREEN" />
</LinearLayout>
</LinearLayout>
Use weightSum to get this equally divided layout by adjusting layout_weight
Try this once:
<?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">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:weightSum="4"
android:gravity="center_horizontal"
android:orientation="horizontal">
<Button
android:id="#+id/red"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="RED" />
<Button
android:id="#+id/blue"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="BLUE" />
<Button
android:id="#+id/yellow"
android:layout_width=match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="YELLOW" />
<Button
android:id="#+id/green"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="GREEN" />
</LinearLayout>
</LinearLayout>

Position of buttons in LinearLayout

I've added 5 buttons (see below), I need that reset button should be equally align as that of the above two buttons in order to maintain uniformity. Also I am not able to align submit button exact below of GoodButton.
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="2dp"
android:layout_weight="0"
android:text="1" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="4dp"
android:layout_weight="0"
android:text="1" />
<View
android:layout_width="1dp"
android:layout_height="match_parent"
android:layout_weight="0"
android:background="#android:color/darker_gray"></View>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="4dp"
android:layout_weight="2"
android:text="Good" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="4dp"
android:layout_weight="1"
android:text="Reset" />
<View
android:layout_width="1dp"
android:layout_height="match_parent"
android:layout_weight="0"
android:background="#android:color/darker_gray"></View>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="4dp"
android:layout_weight="1"
android:text="Submit" />
</LinearLayout>
</LinearLayout>
I feel it is due to extra layout margin space i have added on top buttons.
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:orientation="horizontal"
android:layout_weight="1"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1">
<Button android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"></Button>
<Button android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"></Button>
</LinearLayout>
<Button android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"></Button>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:orientation="horizontal"
android:layout_weight="1"
android:layout_height="match_parent">
<Button android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"></Button>
<Button android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"></Button>
</LinearLayout>
</LinearLayout>
Use it, exactly what you need
Using nested weight can bring performance issues.
This design can easily be achieved by using Relative layout.
Check this code
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:id="#+id/layout1"
android:layout_width="match_parent"
android:layout_height="wrap_content" android:orientation="horizontal" android:weightSum="1"
android:layout_margin="10dp">
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.25"
android:text="1" />
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.25"
android:text="2" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="5"
android:text="3" />
</LinearLayout>
<LinearLayout
android:layout_below="#+id/layout1"
android:layout_width="match_parent"
android:layout_height="wrap_content" android:orientation="horizontal" android:weightSum="1"
android:layout_margin="5dp">
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.5"
android:text="4" />
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.5"
android:text="5" />
</LinearLayout>
your problem is that you are not distributing well the weight of the views.
You must apply weight to both horizontal LinearLayout and then try to distribute the weight of their child views equally.
See my example above:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="5dp"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:weightSum="2"
android:orientation="horizontal">
<LinearLayout
android:layout_width="0dip"
android:layout_height="match_parent"
android:layout_weight="1">
<Button
android:layout_width="0dip"
android:layout_height="match_parent"
android:layout_weight=".5"/>
<Button
android:layout_width="0dip"
android:layout_height="match_parent"
android:layout_weight=".5" />
</LinearLayout>
<Button
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Good" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:layout_width="0dip"
android:layout_height="match_parent"
android:layout_marginRight="4dp"
android:layout_weight="1"
android:text="Reset" />
<Button
android:layout_width="0dip"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="Submit" />
</LinearLayout>
You can also use a TableLayout like this:
<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TableRow>
<Button
android:layout_marginRight="2dp"
android:text="1" />
<Button
android:layout_marginRight="4dp"
android:text="1" />
<View
android:layout_width="1dp"
android:layout_height="match_parent"
android:background="#android:color/darker_gray" />
<Button
android:layout_marginLeft="4dp"
android:layout_weight="1"
android:text="Good" />
</TableRow>
<TableRow>
<Button
android:layout_marginRight="4dp"
android:layout_span="2"
android:text="Reset" />
<View
android:layout_width="1dp"
android:layout_height="match_parent"
android:background="#android:color/darker_gray" />
<Button
android:layout_marginLeft="4dp"
android:layout_weight="1"
android:text="Submit" />
</TableRow>
</TableLayout>

place and align a button above 4 views

I want to place a button above 4 other views:
The views b1-b4 are not really buttons, but they look and behave like buttons (clickable and focusable). Equal spacing is desirable.
I have found Evenly spaced out row of buttons required but how do I make button0 occupy exactly the width of b1-b4?
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="match_parent"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="20dp"
android:orientation="vertical" >
<Button
android:id="#+id/button1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Button0" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<Button
android:id="#+id/button2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="b1" />
<Button
android:id="#+id/button3"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="b2" />
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="b3" />
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="b4" />
</LinearLayout>
</LinearLayout>
Now let me know, is it helpful or not..??
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" >
<LinearLayout
android:id="#+id/linear"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true" >
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="B1" />
<Button
android:id="#+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="B2" />
<Button
android:id="#+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="B3" />
<Button
android:id="#+id/button4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="B4" />
</LinearLayout>
<Button
android:id="#+id/button0"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/linear"
android:layout_alignLeft="#+id/linear"
android:layout_alignRight="#+id/linear"
android:text="Button0" />
</RelativeLayout>

Categories

Resources