How to add multiple button widgets one after another into horizontal form left to right in android.
Android button widgets are easily addable through Button tag, But sometimes there is a special requirement comes with unique type of android app project that app user need multiple buttons format in one by one format in their application. So here is the complete step by step tutorial for Place two or Multiple buttons side by side in relativelayout android.
How to Place two or Multiple buttons side by side in relativelayout android.
What we are doing in this project : We are placing three buttons inside relative layout from left to right at the top of screen using android:layout_alignParentLeft=”true” android:layout_alignParentTop=”true” attributes.
Code for MainActivity.java file.
package com.placemultiplebuttons_android_examples.com; import android.app.Activity; import android.os.Bundle; public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } }
Code for activity_main.xml layout file.
<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" 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="com.placemultiplebuttons_android_examples.com.MainActivity" > <Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_alignParentTop="true" android:text="Button-1" /> <Button android:id="@+id/button2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignBaseline="@+id/button1" android:layout_alignBottom="@+id/button1" android:layout_toRightOf="@+id/button1" android:text="Button-2" /> <Button android:id="@+id/button3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignBaseline="@+id/button2" android:layout_alignBottom="@+id/button2" android:layout_toRightOf="@+id/button2" android:text="Button-3" /> </RelativeLayout>
Screenshot: