Aligning runtime views in RelativeLayout

How to add a view to a relative layout at runtime so that it aligns at the top of the parent. Let's say I add an EditText when the button is clicked. Now this EditText should appear so that the EditText goes above the Button.

here is an example of what i am doing.

    button = new Button(this);  //here I am just checking if the button can come above the EditText
    button.setText("I am a button");  
    params.addRule(RelativeLayout.BELOW, editText.getId());  
    params.addRule(RelativeLayout.ALIGN_PARENT_ABOVE);  
    relativeLayout.addView(button);  

I can not perform alignment at all. Any key is welcome. Thanks at Advance.

+3
source share
2 answers

You need to use RelativeLayout.addView overload, which takes your LayoutParams as the second parameter

    button = new Button(this);  //here I am just checking if the button can come above the EditText
    button.setText("I am a button");  
    params.addRule(RelativeLayout.BELOW, editText.getId());  
    params.addRule(RelativeLayout.ALIGN_PARENT_ABOVE);  
    relativeLayout.addView(button, params);

addView method

+6
source

Don't go so complicated

Instead

1: , -

2: EditText GONE

3:

4: VISIBLE

, :)

+1

Source: https://habr.com/ru/post/1793858/


All Articles