Amine116
Amine116

Reputation: 29

How can I add Views dynamically in a Relative layout or in any Layout

Well, my problem is little bit complicated(for me). Suppose i have a RelativeLayout(Or any other Layout as needed).

Now i make some View like TextView.(multiple View). Those View will include following characteristics i will give from java code.

  1. I want to add the View to the Layout in a specific position.
  2. Two or more View can be overlapped (like following picture)
  3. It is kind of AddView(child, positionX, width, height); i know there is no method like this. but i need one.

where positionX is will be an integer defining where the View will be set in the Layout from top.

Now i want to add those View to the Layout like the following photo.

enter image description here

(All View will be of rectangular size; Sorry for the bad Editing.)

How can i do this? I badly need the solution.

I hope i have explained my problem with my poor English :(

Thank you in advance <3 <3

Upvotes: 0

Views: 1398

Answers (4)

Aminul Haque Aome
Aminul Haque Aome

Reputation: 2599

You can also use FlexBox layout provided by google to add view dynamically. You can check this Medium post for clear concept about Flexbox Layout

Upvotes: 2

Dro3fer
Dro3fer

Reputation: 13

If you want to add multiple views dynamically maybe try to refactor them into one compund view (it will be easier to position them)

https://www.vogella.com/tutorials/AndroidCustomViews/article.html

What type of ViewGroup do you use? If its constraint layout you can set constraints programatically using ConstraintLayout.LayoutParams in same way as SaadAAkash mentioned

Upvotes: 0

Juan Sancho
Juan Sancho

Reputation: 321

Try this:

//-----Main Layout-----\\
LinearLayout l1 = findViewById(R.id.myLinear);
ll.setOrientation(LinearLayout.VERTICAL);

//-----First layout-----\\
LinearLayout l11 = new LinearLayout(this);
l11.setBackgroundColor(android.R.color.holo_orange_dark);
l1.addView(l11, new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT));

//-----Second layout-----\\
LinearLayout l12 = new LinearLayout(this);
l12.setBackgroundColor(android.R.color.holo_green_dark);
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
layoutParams.setMargins(-30, 20, 20, 0); //Negative margin top to superposition views
l1.addView(l12, layoutParams);

Upvotes: 1

SaadAAkash
SaadAAkash

Reputation: 3193

As you've mentioned,

All View will be of rectangular size

So let's take a button yourButton for example. You need to pass two values, one from top & one from left where you want to put the view. Then use the following method, as you wanted it addView() to achieve the desired output in a RelativeLayout:

private void addView(child, positionFromTop, positionFromLeft, width, height) {
    RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
    layoutParams.topMargin = (int)(positionFromTop);
    layoutParams.leftMargin = (int)(positionFromLeft);
    yourButton.setLayoutParams(layoutParams);
}

You can customize the layout using the width and height in the similar way programmatically, if required. Please refer to this answer to get the code of how to resize a custom view programmatically. Hope it helps!

Upvotes: 2

Related Questions