Place a view to left side of another view in relativelayout programmatically

Dynamically move view or widget after activity start time on button click function.

What we are doing in this project : Firstly we are creating two buttons inside activity_main.xml layout file. Now we are setting up button first alignment at the right side of second button on second button click using three major different function called as layoutparams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT, 0);
layoutparams.addRule(RelativeLayout.LEFT_OF, two.getId());
layoutparams.addRule(RelativeLayout.ALIGN_BASELINE, two.getId()); . So here is the complete step by step tutorial for Place a view to left side of another view in relativelayout programmatically.

android-project-download-code-button

How to Place a view to left side of another view in relativelayout programmatically.

Code for MainActivity.java file.

 package com.view2leftsideofanotherview_android_examples.com;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.RelativeLayout.LayoutParams;

public class MainActivity extends Activity {

 Button one,two;
 LayoutParams layoutparams;
 @Override
 protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_main);
 
 one = (Button)findViewById(R.id.button1);
 two = (Button)findViewById(R.id.button2);
 
 layoutparams = (LayoutParams)one.getLayoutParams();
 
 two.setOnClickListener(new View.OnClickListener() {
 
 @Override
 public void onClick(View v) {
 // TODO Auto-generated method stub
 
 layoutparams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT, 0);
 layoutparams.addRule(RelativeLayout.LEFT_OF, two.getId());
 layoutparams.addRule(RelativeLayout.ALIGN_BASELINE, two.getId());
 
 one.setLayoutParams(layoutparams);
 }
 });
 
 }
}

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.view2leftsideofanotherview_android_examples.com.MainActivity" >

 <Button
 android:id="@+id/button1"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:text="Button-1" />

 <Button
 android:id="@+id/button2"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_alignParentRight="true"
 android:layout_centerVertical="true"
 android:text="Move Button 1" />

</RelativeLayout>

Screenshot without button click :

without right

Screenshot with button click :

Place a view to left side of another view in relativelayout programmatically

Click here to download Place a view to left side of another view in relativelayout programmatically project.

Leave a Reply

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