Back to Top

How to add element below another in relative layout in android

Updated 25 June 2015

To set an element like TextView below another TextView in RelativeLayout in android app is easy to do with layout xml. You can do it like this.

<TextView
	android:id="@+id/heading"
	android:layout_width="wrap_content"
        android:layout_height="wrap_content"
	android:text="@string/activity_action_title"
	android:textAppearance="?android:attr/textAppearanceLarge"/>
<TextView
        android:id="@+id/welcome"
	android:layout_width="wrap_content"
	android:layout_height="wrap_content"
	android:layout_below="@+id/heading"
	android:layout_marginTop="10dp"
	android:textAppearance="?android:attr/textAppearanceMedium"/>

in the above code welcome TextView will come after heading.

Now we will do it programatically.

TextView ResultText = new TextView(Activity.this);
ResultText.setText("Your Custom text.");
RelativeLayout.LayoutParams p = new RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT);
p.addRule(RelativeLayout.BELOW, R.id.heading);
ResultText.setLayoutParams(p);                    
View relativeLayout = findViewById(R.id.Header);
((RelativeLayout) relativeLayout).addView(ResultText);

Start your headless eCommerce
now.
Find out More
. . .

Leave a Comment

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


Be the first to comment.

Back to Top

Message Sent!

If you have more details or questions, you can reply to the received confirmation email.

Back to Home