How to add element below another in relative layout in android
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);
View Comments
Comment or Ask a Question
Quick Links