Place a view in center of relativelayout android using xml

Set view widget alignment horizontally center, vertically center just middle of application activity screen.

There are lots of attributes available in each and every widget but some of the tags are most important and frequently used to set views alignment( TextView, EditText, MultiAutoCompleteTextview, imageview, imagebutton..etc ) using android:layout_centerHorizontal=”true” and
android:layout_centerVertical=”true” .So here is the complete step by step tutorial for Place a view in center of relativelayout android using xml.

android-project-download-code-button

How to Place a view in center of relativelayout android using xml.

Code for MainActivity.java file.

package com.viewincenterrelativelayout_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.viewincenterrelativelayout_android_examples.com.MainActivity" >

 <Button
 android:id="@+id/button1"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_centerHorizontal="true"
 android:layout_centerVertical="true"
 android:text="Place a view in center of relativelayout android using xml" />

</RelativeLayout>

Screenshot:

Place a view in center of relativelayout android using xml

Click here to download Place a view in center of relativelayout android using xml project.

Leave a Reply

Your email address will not be published. Required fields are marked *