How to align the buttons at the center of the screen in android

sangeetha

I have two buttons. I want to align them at the center of the screen,. I tried doing.. but i'm not able to do it.

fragment_main.xml :

<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.example.assignment_1.MainActivity$PlaceholderFragment" >

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/button1" 
        android:id="@+id/button1"
        android:onClick="Display"/>
    <Button
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/button1"
        android:layout_toRightOf="@+id/button1"
        android:text="@string/button2" 
        android:id="@+id/button2"
        android:onClick="opaquemessage"/>
    <TextView 
        android:id="@+id/display_mes"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"/>

</RelativeLayout>
Naveen Kumar

You just add this line into your button that's all.... This one is for your Relative layout

    android:layout_centerHorizontal="true"
    android:layout_centerVertical="true" 

suppose you will use the linear layout means use this below

    android:layout_gravity="center"

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How to auto align the buttons to fit in center of the screen

ConstraintLayout how to align to center of screen and not to overlap uneven side buttons

Align android button to the screen center

How to align the widget to the center of the screen

How to center align multiple radio buttons in FlowLayoutPanel?

how to align ul list as a buttons to the center

How to align the canvas object to center of the screen?

How to align a button to the center of the screen html

How to align row of images at the center top of the screen

How to align background color and text in center screen

align AlertDialog buttons to center

How to align two buttons and views left to center, center to right with

center a group of buttons on the screen

How to have a space between the two buttons? And how to align them to center

How do I center align 3 radio buttons?

How to align the two buttons to the center of a div of 'position:absolute' using flexbox?

How to Align four buttons at dead center in Xcode 7?

bootstrap 4 align buttons in center

How to Align 3 buttons in a line, android?

How do I align these buttons in Android?

Android how to center align chips in chipgroup?

How to center align the ActionBar title in Android?

How to align button center of screen overlapping two layouts

How to center align a glyphicon to the middle of the page at all screen sizes

How to align TextView to center of the screen, and ImageView to stick next to it?

How to adjust the size of shapes in order to fit the screen and align them to center?

Flexbox in react native : How to align 2 items at the center of the screen?

How to align action bar title in center of screen in fragment?

How to align Navigation image in center when screen size reduces