-3

I am trying to put the two button in framelayout. I could do it with RelativeLayout using alight left attribute, but with Framelayout I did not find any such attributes. How do I align tow button in same row at upper side?

2 Answers2

1

Try to use a LinearLayout to encapsulate the buttons, like that:

<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: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"
    android:id="@+id/main">

    <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:text="New Button"
            android:id="@+id/button1" />

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="New Button"
            android:id="@+id/button2" />
    </LinearLayout>

</FrameLayout>
Alberto Malagoli
  • 1,173
  • 10
  • 14
0

You can use layout_gravity attribute:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

        <Button
            android:layout_gravity="left"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="button 1"
            android:id="@+id/button_1" />

        <Button
            android:layout_gravity="right"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="button 2"
            android:id="@+id/button_2" />

</FrameLayout>

Values for Gravity can be combined like: left|top, left|center_vertical, left|bottom, and so on.

You could also use values for left margin for the second button, for example:

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="200dp"
            android:text="button 2"
            android:id="@+id/button_2" />

... or, why not, use gravity and margin right, to place the button a number of dp's from the right margin

        <Button
            android:layout_gravity="right"
            android:layout_marginRight="200dp"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="button 2"
            android:id="@+id/button_2" />
rupps
  • 9,712
  • 4
  • 55
  • 95