Monday, July 15, 2019

Android – How to center button on screen

A little tip for Android to show you how to center button on the screen, buttons in RelativeLayout and set the following properties in the right type Real.
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
 Following is a full example button center screen to demonstrate the use of above tip to center a button on screen.
1. Android Layout

File : res/layout/main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/relativeLayout1"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >
    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:layout_centerHorizontal="true"
        android:text="Button" />
</RelativeLayout>
2. Activity
Simple activity class, do nothing but display the above layout file.
public class HelloWorldActivity extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    }
}
3. Demo on code android example

How to center button on screen

No comments:

Post a Comment