Align view to vertically center in relativelayout android programmatically

How to set button in vertical center on activity screen dynamically add via coding.

Vertically center means to adjust your widget(TextView or button or anything) just middle of your android mobile phone activity screen vertically middle section. So here is the complete step by step tutorial for Align view to vertically center in relativelayout android programmatically via activity_main.xml layout file.

How to Align view to vertically center in relativelayout android programmatically.

Code for MainActivity.java file.

 package com.viewverticallycenter_android_examples.com;
import android.app.Activity;
import android.widget.TextView;
import android.os.Bundle;
import android.widget.Button;

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.viewverticallycenter_android_examples.com.MainActivity"
 android:id="@+id/relativelayout1" >

 <TextView
 android:id="@+id/textView1"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_alignParentLeft="true"
 android:layout_centerVertical="true"
 android:text="TextView"
 android:textAppearance="?android:attr/textAppearanceLarge" />

</RelativeLayout>

Screenshots:

Align view to vertically center in relativelayout android programmatically

Leave a Reply

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