Search code examples
androidandroid-layoutandroid-tablelayoutandroid-relativelayout

RelativeLayout or TableLayout Android 3 buttons


I want to do something simple, 3 buttons in 1 column, responsive, like on screen.

Should I create it on RelativeLayout like in my code, or TableLayout would be better idea?

How to link RelativeLayout to the top and to the bottom, to be sure that last button never go out of the bottom of screen?

<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
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"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:showIn="@layout/activity_main"
tools:context=".MainActivity"
android:background="@color/green"
>
<ImageButton
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="NAGRAJ"
    android:id="@+id/nagraj"
    android:src="@drawable/button_sos"
    android:scaleType="fitStart"
    android:layout_alignParentTop="true"
    android:adjustViewBounds="true"
    android:background="@null"
    android:layout_marginBottom="25px"
    android:tag="nagraj"
    />

<ImageButton
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="112"
    android:id="@+id/zadzwon"
    android:layout_below="@+id/nagraj"
    android:layout_alignParentRight="true"
    android:layout_alignParentEnd="true"
    android:src="@drawable/button_112"
    android:scaleType="fitStart"
    android:background="@null"
    android:adjustViewBounds="true"
    android:layout_marginBottom="25px"/>

<ImageButton
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="ZDJECIE"
    android:id="@+id/foto"
    android:layout_below="@+id/zadzwon"
    android:layout_alignParentRight="true"
    android:layout_alignParentEnd="true"
    android:src="@drawable/button_foto"
    android:scaleType="fitStart"
    android:background="@null"
    android:adjustViewBounds="true"
    /> 
</RelativeLayout>

enter image description here


Solution

  • Use both RelativeLayout and LinearLayout like this

    RelativeLayout
    
       LinearLayout (orientation:vertical && weightsum = 3)
    
          ImageView (weightsum = 1)
    
          ImageView (weightsum = 1)
    
          ImageView (weightsum = 1)
    
       LinearLayout
    
       Button (align to the bot of the above LinearLayout and android:layout_alignParentBottom="true")
    
    RelativeLayout