0

I am following the Android Programming: The Big Nerd Ranch Guide book and have a very newbish question:

In my first app named GeoQuiz - how to move the right button to the very right of the screen (and still keep the sizes of the buttons at wrap_content):

screenshot

My layout file can be seen an GitHub as activity_quiz.xml and here is an excerpt:

<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:gravity="center"
    android:orientation="vertical"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="de.afarber.geoquiz.QuizActivity" >
....
    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

        <Button
            android:id="@+id/true_button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/true_button" />

        <Button
            android:id="@+id/false_button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/false_button" />

    </LinearLayout>
....       
</LinearLayout>
Alexander Farber
  • 21,519
  • 75
  • 241
  • 416

4 Answers4

2

Try this please

<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:gravity="center"
android:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="de.afarber.geoquiz.QuizActivity" >
....
<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content" >

    <Button
        android:id="@+id/true_button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:text="@string/true_button" />

    <Button
        android:id="@+id/false_button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:text="@string/false_button" />

</RelativeLayout>
....       
</LinearLayout>
Farouk Touzi
  • 3,451
  • 2
  • 19
  • 25
1

Try this way,hope this will help you to solve your problem.

<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:gravity="center"
    android:orientation="vertical"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="de.afarber.geoquiz.QuizActivity" >

    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

        <Button
            android:id="@+id/true_button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/true_button" />
        <View
            android:layout_width="0dp"
            android:layout_height="1dp"
            android:layout_weight="1"/>
        <Button
            android:id="@+id/false_button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/false_button" />

    </LinearLayout>
</LinearLayout>
Haresh Chhelana
  • 24,720
  • 5
  • 57
  • 67
0

I hope this will be helpful for you:

<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:gravity="center"
android:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="de.afarber.geoquiz.QuizActivity" >

<LinearLayout
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" 
    android:weightSum="3">

    <Button
        android:id="@+id/true_button"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/true_button"
        android:layout_weight="1" />
    <View
        android:layout_width="match_parent"
        android:layout_height="1dp"
        android:layout_weight="1"
        android:visibility="invisible"/>
    <Button
        android:id="@+id/false_button"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/false_button"
        android:layout_weight="1" />

</LinearLayout>

-1

I would recommend creating two more horizontal layouts within the earlier horizontal layout and giving each a weight of 0.5. You can then place the true and false button at the center of the horizontal layout. The code is as follows:

<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:gravity="center"

android:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="de.afarber.geoquiz.QuizActivity" >

<LinearLayout
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" >


    <LinearLayout
    android:orientation="horizontal"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_weight="0.5"
    android:gravity="center" >

   <Button
       android:id="@+id/true_button"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:text="@string/true_button" />

       </LinearLayout>

    <LinearLayout
    android:orientation="horizontal"
    android:layout_width="0dp"
    android:layout_height="wrap_content" 
    android:layout_weight="0.5"
    android:gravity="center">

        <Button
            android:id="@+id/false_button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/false_button"/>

        </LinearLayout>
</LinearLayout>

I hope this helps as the size of your button will remain as wrap_content and you can spread your buttons out. Although you could easily solve your problem using relative layout I would strongly advise you against using relative layouts as they may create problems when the resolution of the screen is changed

Anav Sanghvi
  • 66
  • 1
  • 7
  • 1
    Right. So while Google suggests you use a `RelativeLayout` for simpler layouts (_and this one is particularly simple_), you recommend against it? Ref: [Android Layout Tricks #1](http://android-developers.blogspot.in/2009/02/android-layout-tricks-1.html). And also, a simple implementation like this, would not create _problems_ when the resolution changes. – Siddharth Lele Sep 10 '14 at 12:34