setmargin does not work in relative layout in android - android

I have created 2 buttons in a relative layout programmatically. I want no margin space between the buttons and tried to achieve this by using 'setmargin' but failed. Below is the code.
//creating the relative layout
RelativeLayout relativeLayout = new RelativeLayout(this);
RelativeLayout.LayoutParams rlp = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.MATCH_PARENT,
RelativeLayout.LayoutParams.MATCH_PARENT);
//creating buttons
Button button1 = new Button(this);
button1.setId(R.id.button1);
RelativeLayout.LayoutParams params1 = new RelativeLayout.LayoutParams(
SCR_W/2,
RelativeLayout.LayoutParams.WRAP_CONTENT);//SCR_W is the device screenwidth
params1.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
params1.addRule(RelativeLayout.ALIGN_PARENT_TOP);
params1.setMargins(0, 0, 0, 0);
button1.setLayoutParams(params1);
relativeLayout.addView(button1);
Button button2 = new Button(this);
button2.setId(R.id.button2);
RelativeLayout.LayoutParams params2 = new RelativeLayout.LayoutParams(
SCR_W/2,
RelativeLayout.LayoutParams.WRAP_CONTENT);
params2.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
params2.addRule(RelativeLayout.BELOW, button1.getId());
params2.setMargins(0, 0, 0, 0);
button2.setLayoutParams(params2);
relativeLayout.addView(button2);
//setting the relative layout as the contentview
setContentView(relativeLayout, rlp);
Thanks in advance..

I tried your code, and works ok to me,
The two buttons are at distance 0. To check it, simply set different color to each button, and you will see that they are together.
button1.setBackgroundColor(Color.BLUE);
button2.setBackgroundColor(Color.RED);

Try setting the height of the relative layout to wrap_content..
RelativeLayout.LayoutParams rlp = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.MATCH_PARENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);

Just try something like this, it will work -:
params1.setMargins(0, 0, 0, -5);
params2.setMargins(0, -5, 0, 0);

Related

RelativeLayout.BELOW doesn't work in programmatically created RelativeLayout

I'm creating a Layout programmatically consisting of a RelativeLayout and some TextViews. I want them to be placed like two columns like following example:
textview0 textview1
textview2 textview3
textview4 textview5
and the following LayoutParameters is applied to them:
width: WRAP_CONTENT
height: WRAP_CONTENT
and rules for example textview4:
RelativeLayout.BELOW, textview3.getId()
RelativeLayout.ALIGN_PARENT_LEFT, RelativeLayout.TRUE
and rules for textview5:
RelativeLayout.RIGHT_OF, textview4.getId()
RelativeLayout.ALIGN_BASELINE, textview4.getId()
This ensures that the contents in column to the right can be wrapped but textviews below it is still showing fine, as it's placed below the textview that could be wrapped.
Well, when I try this out I get it to work for the first two rows of textviews, they are placed as they should, but then I add a third one and that one get placed on the same row as it should be placed below. And I cant figure out what's wrong.
RelativeLayout rl = new RelativeLayout(context);
rl.setPadding(5, 5, 5, 5);
rl.setLayoutParams(new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT));
RelativeLayout.LayoutParams lp0 = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
lp0.addRule(RelativeLayout.ALIGN_PARENT_LEFT, RelativeLayout.TRUE);
textView0.setLayoutParams(lp0);
rl.addView(textView0);
RelativeLayout.LayoutParams lp1 = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
lp1.addRule(RelativeLayout.RIGHT_OF, textView0.getId());
lp1.addRule(RelativeLayout.ALIGN_BASELINE, textView0.getId());
lp1.setMargins(5, 0, 0, 0);
textView1.setLayoutParams(lp1);
rl.addView(textView1);
RelativeLayout.LayoutParams lp2 = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
lp2.addRule(RelativeLayout.BELOW, textView1.getId());
lp2.addRule(RelativeLayout.ALIGN_PARENT_LEFT, RelativeLayout.TRUE);
textView2.setLayoutParams(lp2);
rl.addView(textView2);
RelativeLayout.LayoutParams lp3 = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
lp3.addRule(RelativeLayout.RIGHT_OF, textView2.getId());
lp3.addRule(RelativeLayout.ALIGN_BASELINE, textView2.getId());
lp3.setMargins(5, 0, 0, 0);
textView3.setLayoutParams(lp3);
rl.addView(textView3);
RelativeLayout.LayoutParams lp4 = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
lp4.addRule(RelativeLayout.BELOW, textView3.getId());
lp4.addRule(RelativeLayout.ALIGN_PARENT_LEFT, RelativeLayout.TRUE);
textView4.setLayoutParams(lp4);
rl.addView(textView4);
RelativeLayout.LayoutParams lp5 = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
lp5.addRule(RelativeLayout.RIGHT_OF, textView4.getId());
lp5.addRule(RelativeLayout.ALIGN_BASELINE, textView4.getId());
lp5.setMargins(5, 0, 0, 0);
textView5.setLayoutParams(lp5);
rl.addView(textView5);
So the example code above results in textView4 being placed above textView2 even though it should be placed below it, ruled by LayoutParameters for textView3. I can't see anything wrong with this code, and I have debugged it to ensure that the ID's was unique among the textviews and they are.
Any suggestions, I'm kind of stuck!

Add margin programmatically to RelativeLayout

I'm trying to add programmatically a margin top to my RelativeLayout in my activity. Using the xml I can do it in this mode: android:layout_marginTop="10dp", but when I'm trying to do it programmatically nothing changes... As you can see I'm using some RelativeLayout (there is a for loop) in one LinearLayout container.
This is the code that I'm using:
//LINEAR LAYOUT
LinearLayout linearLayout = new LinearLayout(this);
linearLayout.setOrientation(LinearLayout.VERTICAL);
linearLayout.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT));
for (int i=1; i<=3; i++){
//RELATIVE LAYOUT
RelativeLayout relativeLayout = new RelativeLayout(this);
relativeLayout.setLayoutParams(new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.FILL_PARENT,
RelativeLayout.LayoutParams.FILL_PARENT));
relativeLayout.setBackgroundColor(getResources().getColor(R.color.grayColor));
//CODE FOR ADD MARGINS
RelativeLayout.LayoutParams relativeParams = (RelativeLayout.LayoutParams)relativeLayout.getLayoutParams();
relativeParams.topMargin=80;
relativeLayout.setLayoutParams(relativeParams);
//IMAGE VIEW
ImageView selectedPhoto = new ImageView(this);
selectedPhoto.setLayoutParams(new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT));
selectedPhoto.setImageResource(R.drawable.ic_launcher);
//TEXT VIEWS
TextView numberCopies = new TextView(this);
numberCopies.setLayoutParams(new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT));
numberCopies.setGravity(Gravity.CENTER);
numberCopies.setPadding(25, 25, 25, 25);
numberCopies.setTextColor(getResources().getColor(R.color.blackColor));
numberCopies.setText("2 copies ");
RelativeLayout.LayoutParams layoutParamsNumberCopies = (RelativeLayout.LayoutParams) numberCopies.getLayoutParams();
layoutParamsNumberCopies.addRule(RelativeLayout.CENTER_HORIZONTAL);
numberCopies.setLayoutParams(layoutParamsNumberCopies);
TextView priceCopies = new TextView(this);
priceCopies.setLayoutParams(new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT));
priceCopies.setGravity(Gravity.CENTER);
numberCopies.setPadding(25, 25, 25, 25);
priceCopies.setTextColor(getResources().getColor(R.color.redColor));
priceCopies.setText("$ 25 ");
RelativeLayout.LayoutParams layoutParamsPriceCopies = (RelativeLayout.LayoutParams) priceCopies.getLayoutParams();
layoutParamsPriceCopies.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
priceCopies.setLayoutParams(layoutParamsPriceCopies);
relativeLayout.addView(selectedPhoto);
relativeLayout.addView(numberCopies);
relativeLayout.addView(priceCopies);
linearLayout.addView(relativeLayout);
}
scrollView.addView(linearLayout);
setContentView(scrollView);
I think that the failing block code is this:
//CODE FOR ADD MARGINS
RelativeLayout.LayoutParams relativeParams = (RelativeLayout.LayoutParams)relativeLayout.getLayoutParams();
relativeParams.topMargin=80;
relativeLayout.setLayoutParams(relativeParams);
Got solution.
When you are using RelativeLayout inside LinearLayout, you need to use LinearLayout.LayoutParams instead of RelativeLayout.LayoutParams.
So replace your following code...
RelativeLayout.LayoutParams relativeParams = (RelativeLayout.LayoutParams)relativeLayout.getLayoutParams();
relativeParams.setMargins(0, 80, 0, 0); // left, top, right, bottom
relativeLayout.setLayoutParams(relativeParams);
with...
// CODE FOR ADD MARGINS
LinearLayout.LayoutParams linearParams = new LinearLayout.LayoutParams(
new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT));
linearParams.setMargins(0, 80, 0, 0);
relativeLayout.setLayoutParams(linearParams);
relativeLayout.requestLayout();
You can use setMargins (int left, int top, int right, int bottom).
Try like this..
//CODE FOR ADD MARGINS
RelativeLayout.LayoutParams relativeParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
relativeParams.setMargins(0, 80, 0, 0);
relativeLayout.setLayoutParams(relativeParams);
Layout params should be set with resp to parentLayout .. in this case its LinearLayout as rightly pointed by #ChintanRathod, so
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
will do the trick..
Be aware that you are using a relative layout inside a linear layout so you should use LinearLayout.LayoutParams
I would replace this :
//RELATIVE LAYOUT
RelativeLayout relativeLayout = new RelativeLayout(this);
relativeLayout.setLayoutParams(new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.FILL_PARENT,
RelativeLayout.LayoutParams.FILL_PARENT));
relativeLayout.setBackgroundColor(getResources().getColor(R.color.grayColor));
//CODE FOR ADD MARGINS
RelativeLayout.LayoutParams relativeParams = (RelativeLayout.LayoutParams)relativeLayout.getLayoutParams();
relativeParams.topMargin=80;
relativeLayout.setLayoutParams(relativeParams);
by this :
//RELATIVE LAYOUT WITH PROPER LAYOUT PARAMS TO ADD MARGINS
RelativeLayout relativeLayout = new RelativeLayout(this);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
params.setMargins(0, 80, 0, 0);
relativeLayout.setLayoutParams(params);
relativeLayout.setBackgroundColor(getResources().getColor(R.color.grayColor));
Note also that you should use WRAP_CONTENT as height configuration for children in a vertical linearLayout. ( in FILL_PARENT or MATCH_PARENT mode it will fill the parent without leaving space for the other children)
// kotlin
val relativeParams = relativeLayoutGeneral.layoutParams as FrameLayout.LayoutParams
relativeParams.setMargins(0, 7, 0, 111)
relativeLayoutGeneral.layoutParams = relativeParams

Android / Layout - Add margin top pragmatically to Relative Layout

I'm trying to add pragmatically a margin top to my Relative Layout in my activity.
Using the xml i can do it in this mode: android:layout_marginTop="10dp", but when i'm trying to do it pragmatically nothing change...
This is the code that i'm using:
//SCROLL VIEW
ScrollView scrollView = new ScrollView(this);
scrollView.setBackground(getResources().getDrawable(R.drawable.background));
scrollView.setLayoutParams(new ScrollView.LayoutParams(ScrollView.LayoutParams.MATCH_PARENT,
ScrollView.LayoutParams.MATCH_PARENT));
scrollView.setPadding(20, 20, 20, 20);
//LINEAR LAYOUT
LinearLayout linearLayout = new LinearLayout(this);
linearLayout.setOrientation(LinearLayout.VERTICAL);
linearLayout.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT));
for (int i=1; i<=3; i++){
//RELATIVE LAYOUT
RelativeLayout relativeLayout = new RelativeLayout(this);
relativeLayout.setLayoutParams(new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.FILL_PARENT,
RelativeLayout.LayoutParams.FILL_PARENT));
relativeLayout.setBackgroundColor(getResources().getColor(R.color.grayColor));
RelativeLayout.LayoutParams head_params = (RelativeLayout.LayoutParams)relativeLayout.getLayoutParams();
head_params.setMargins(0, 80, 0, 0);
relativeLayout.setLayoutParams(head_params);
//Need to understand how put a margin top to the relativeLayout
//IMAGE VIEW
ImageView selectedPhoto = new ImageView(this);
selectedPhoto.setLayoutParams(new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT));
selectedPhoto.setImageResource(R.drawable.ic_launcher);
//TEXT VIEWS
TextView numberCopies = new TextView(this);
numberCopies.setLayoutParams(new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT));
numberCopies.setGravity(Gravity.CENTER);
numberCopies.setPadding(25, 25, 25, 25);
numberCopies.setTextColor(getResources().getColor(R.color.blackColor));
numberCopies.setText("2 copies ");
RelativeLayout.LayoutParams layoutParamsNumberCopies = (RelativeLayout.LayoutParams) numberCopies.getLayoutParams();
layoutParamsNumberCopies.addRule(RelativeLayout.CENTER_HORIZONTAL);
numberCopies.setLayoutParams(layoutParamsNumberCopies);
TextView priceCopies = new TextView(this);
priceCopies.setLayoutParams(new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT));
priceCopies.setGravity(Gravity.CENTER);
numberCopies.setPadding(25, 25, 25, 25);
priceCopies.setTextColor(getResources().getColor(R.color.redColor));
priceCopies.setText("$ 25 ");
RelativeLayout.LayoutParams layoutParamsPriceCopies = (RelativeLayout.LayoutParams) priceCopies.getLayoutParams();
layoutParamsPriceCopies.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
priceCopies.setLayoutParams(layoutParamsPriceCopies);
relativeLayout.addView(selectedPhoto);
relativeLayout.addView(numberCopies);
relativeLayout.addView(priceCopies);
linearLayout.addView(relativeLayout);
}
scrollView.addView(linearLayout);
setContentView(scrollView);
}
I have tried also relativeLayout.setMargins(20, 0, 0, 0); but seems that this method isn't available for relativeLayout. I'm not sure about it.
Thanks
Change your setmargins line to
head_params.setMargins(0, 80, 0, 0);
Margin parameters go clockwise starting with the left margin, i.e. (left, top, right, bottom). However, if you have other margins other than the top one, this will delete them. A much easier and more intuitive way is to simply write
header_params.topMargin=80;
Also not to forget, you never set your header_params back to your view. Add this line below:
relativeLayout.setLayourParams(header_params);
Try this
RelativeLayout.LayoutParams head_params = (RelativeLayout.LayoutParams)headView.getLayoutParams();
head_params.setMargins(0, 80, 0, 0);//substitute parameters for left, top, right, bottom

android creating a RelativeLayout programmatically

I'm having some troubles creating a relativeLayout programmatically.For better understanding I attached a picture below. The things with plus and minus are buttons, which should also be dynamically created and added to the layout. The values of tv4 and tv5 should increase/decrease accordingly to button presses.
What I have done so far:
1) creating the layout:
RelativeLayout rl = new RelativeLayout(this);
rl.setId(i);
rl.setBackgroundResource(R.drawable.bg);
RelativeLayout.LayoutParams Lparams = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.MATCH_PARENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
Lparams.addRule(RelativeLayout.BELOW, R.id.RL_default);
Lparams.setMargins(3, 5, 3, 0);
rl.setLayoutParams(Lparams);
2) adding the tv1:
Lparams = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.MATCH_PARENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
Lparams.addRule(RelativeLayout.ALIGN_PARENT_LEFT, RelativeLayout.TRUE);
Lparams.setMargins(10, 0, 0, 0);
TextView txt = new TextView(this);
txt.setTextColor(Color.parseColor("#FFFFFF"));
txt.setId(x);
txt.setTextSize(25);
txt.setLayoutParams(Lparams);
txt.setText(name);
rl.addView(txt);
3) adding the tv2:
Lparams = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.MATCH_PARENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
Lparams.addRule(RelativeLayout.ALIGN_PARENT_LEFT, RelativeLayout.TRUE);
Lparams.addRule(RelativeLayout.BELOW, txt.getId());
Lparams.setMargins(10, 0, 0, 0);
TextView txtS = new TextView(this);
txtS.setId(y);
txtS.setText("Test: ");
txtS.setTextSize(22);
txtS.setLayoutParams(Lparams);
txtS.setGravity(Gravity.BOTTOM);
txtS.setPadding(0, 0, 0, 20);
rl.addView(txtS);
4) now I want to create the first button:
Button btnSminus = new Button(this);
btnSminus.setId(btn1);
btnSminus.setText("<");
btnSminus.setTextSize(20);
Lparams= new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
Lparams.addRule(RelativeLayout.RIGHT_OF, txtS.getId());
btnSminus.setLayoutParams(Lparams);
rl.addView(btnSminus);
The problem is, the the button View seems to just disappear from the screen when the line Lparams.addRule(RelativeLayout.RIGHT_OF, txtS.getId()); is executed. What can be the reason?
My guess would be that it's because you set the width of txtS to match_parent, so the button gets pushed off the screen. You can fix that by changing that to wrap_content.

Margin in Relative Layout

How to set margin in relative layout with code (not XML)?
I use this code but nothing happened:
RelativeLayout rlDetail = new RelativeLayout(context);
rlDetail.setBackgroundResource(R.drawable.bg_round);
RelativeLayout.LayoutParams rlDetailParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
rlDetailParams.setMargins(35, 10, 35, 0);
rlDetail.setLayoutParams(rlDetailParams);
try this answer set the absolute position of a view in Android
as mentioned in the link above you should use
TextView tv1 = new TextView(context);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(30, 40);
params.leftMargin = needed_margin;
params.topMargin = needed_margin;
// add your textview like this
rl.addView(tv1, params);

Categories

Resources