Android relativelayout align bottom programmatically

Align a view to bottom of relativelayout android dynamically on button click.

On relative layout android app developer can move views easily inside activity layout screen but moving view using coding file is more required. So here is the complete step by step tutorial for Android relativelayout align bottom programmatically.

Android relativelayout align bottom programmatically.

Code for MainActivity.java file.

package com.androidrelativelayoutalignbottomprogrammatically_android_examples.com;
import android.app.Activity;
import android.os.Bundle;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.RelativeLayout.LayoutParams;
import android.widget.TextView;

public class MainActivity extends Activity {

 TextView txt;
 Button btn;
 @Override
 protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_main);
 txt = (TextView)findViewById(R.id.textView1);
 
 LayoutParams lp = (LayoutParams) txt.getLayoutParams();
 
 lp.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
 txt.setLayoutParams(lp);
 }
}

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.androidrelativelayoutalignbottomprogrammatically_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:

Android relativelayout align bottom programmatically

Leave a Reply

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