Center 2 Buttons for Any Screen Size in a Layout (Android)

Screen Shot 2013-12-03 at 2.33.39 PM

A rendering of the two center buttons

Here is how to center 2 buttons inside a layout. These do not have to be buttons but can be any type of view. The idea behind it is to create a layout and add two horizontal Linear Layout inside and assign the same weight to both.  When the buttons are inside then center the buttons inside the layout. Since each layout is set to ‘match_parent’ then it will scale to any screen size.

Here  is the idea of centering two views inside a layout:

center_two_buttons

General idea of the concept (excuse the horrible drawing).

Here is the code:

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

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            android:layout_weight="1" >

            <Button
                android:id="@+id/cancel"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center_horizontal"
                android:layout_marginBottom="18dp" />

        </LinearLayout>

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:orientation="vertical"
            android:layout_weight="1" >

            <Button
                android:id="@+id/continue"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center_horizontal"/>

        </LinearLayout>

    </LinearLayout>

Posted in Coding

Tags:

Permalink 1 Comment

  • Victor Manuel Ortiz Guardeño

    thank you, I needed that and it’s very well explained.


Graveyard Database Logo

Has everything you want to know about grave yards. Plus has many celebrites and world leaders graves. Pssst it is my other site

Google + Profile
Check out Android Photo Mapping, its a app/site I been working on

  • wholesale nfl jerseys
  • wholesale nfl jerseys china
  • wholesale sports jerseys