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
Related
I am trying to set margin from top.
RelativeLayout root = (RelativeLayout) view.findViewById(R.id.root);
listview = new PullListView(getActivity());
root.addView(listview);
How can I set margin of listview?
RelativeLayout.LayoutParams relativeParams =(RelativeLayout.LayoutParams)relativeLayout.getLayoutParams();
relativeParams.setMargins(0, 80, 0, 0); // left, top, right, bottom
relativeLayout.setLayoutParams(relativeParams);
Use LayoutParams
LayoutParams params=(LayoutParams) root.getLayoutParams();
params.leftMargin=0;
params.rightMargin=0;
params.rightMargin=0;
params.leftMargin=0;
root.setLayoutParams(params);
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
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);
I have a image placed in the relative layout. How can i place a small image over the background image and two text view side by side next to the image. Any help will be really appreciated. The only thing is I want to do this programmatically. I have tried doing it the following way but for some reason the textview appears on the top left of the screen. I want it appear on the imagebutton.
LinearLayout LinearLayout = new LinearLayout(this);
RelativeLayout relativeLayout = new RelativeLayout(this);
RelativeLayout.LayoutParams relativeLayoutParams;
Button[] btn = new Button[1];
btn[0] = new Button(this);
btn[0].setId(1);
btn[0].setGravity(Gravity.CENTER);
btn[0].setText("text");
btn[0].setBackgroundResource(R.drawable.button_grey);
btn[0].setTextColor(Color.parseColor("#FFFFFF"));
relativeLayoutParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
relativeLayoutParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT | RelativeLayout.ALIGN_PARENT_TOP);
relativeLayoutParams.setMargins(20, 20, 15, 15);
relativeLayout.addView(btn[0], relativeLayoutParams);
ImageButton[] Imgbtn = new ImageButton[10];
Imgbtn[0] = new ImageButton(this);
Imgbtn[0].setId(2);
Imgbtn[0].setBackgroundResource(R.drawable.box_round_corners);
Imgbtn[0].setMaxHeight(200);
Imgbtn[0].setClickable(true);
relativeLayoutParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
relativeLayoutParams.addRule(RelativeLayout.BELOW,btn[0].getId());
relativeLayoutParams.height = 100;
relativeLayoutParams.setMargins(15, 0, 15, 10);
Imgbtn[0].setLayoutParams(relativeLayoutParams);
relativeLayout.addView(Imgbtn[0], relativeLayoutParams);
TextView[] txtview = new TextView[10];
txtview[0] = new TextView(this);
txtview[0].setId(3);
txtview[0].setTextColor(Color.parseColor("#000000"));
txtview[0].setText("text");
relativeLayoutParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
// relativeLayoutParams.addRule(RelativeLayout.ALIGN_LEFT | RelativeLayout.ALIGN_TOP);
relativeLayoutParams.addRule(RelativeLayout.BELOW,Imgbtn[0].getId());
relativeLayoutParams.height = 20;
relativeLayoutParams.setMargins(5, 5, 5, 5);
txtview[0].setLayoutParams(relativeLayoutParams);
relativeLayout.addView(txtview[0], relativeLayoutParams);
android.widget.ScrollView ScrollV = new ScrollView(this);
ScrollV.addView( relativeLayout, new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
addContentView(ScrollV, new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT));
try to set your views LayoutParams with setLayoutParams() method;
RelativeLayout.LayoutParams lp=new RelativeLayout.LayoutParams(width,height);
lp.topMargin=//what is your desired y coordinate
lp.leftMargin=//what is your desired x coordinate
lp.gravity=Gravity.NO_GRAVITY;
view.setLayoutParams(lp);
FrameLayout is better choise for overlaying purposes in my opinion.
Plz validate the below relative layout
RelativeLayout objRLActionBar=new RelativeLayout(this);
objRLActionBar.setId(2534);
RelativeLayout.LayoutParams objRLActionBarParams=new RelativeLayout.LayoutParams(LayoutParams.FILL_PARENT,(int) (screenHeight*layoutHeights[1]));
objRLActionBarParams.addRule(RelativeLayout.BELOW,objRLTitleBar.getId());
objRLActionBar.setBackgroundColor(Color.parseColor("#2e4862"));
ImageView objIVActivityIcon = new ImageView(this);
objRLActionBar.setId(25324);
RelativeLayout.LayoutParams objIVActivityIconParams=new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
objIVActivityIconParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT,objRLActionBar.getId());
objIVActivityIcon.setLayoutParams(objIVActivityIconParams);
objIVActivityIcon.setImageResource(R.drawable.home_def);
objIVActivityIcon.setPadding(2, 0, 2, 0);
objRLActionBar.addView(objIVActivityIcon);
ImageView objIVSeperator = new ImageView(this);
objIVSeperator.setId(25342);
RelativeLayout.LayoutParams objIVSeperatorParams=new RelativeLayout.LayoutParams(1,LayoutParams.FILL_PARENT);
objIVSeperatorParams.addRule(RelativeLayout.RIGHT_OF,objIVActivityIcon.getId());
objIVSeperator.setLayoutParams(objIVSeperatorParams);
objIVSeperator.setImageResource(R.drawable.separator);
objIVSeperator.setBackgroundColor(Color.parseColor("#1f3449"));
objRLActionBar.addView(objIVSeperator);
TextView objTVPageName = new TextView(this);
RelativeLayout.LayoutParams objTVPageNameParams=new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
objTVPageNameParams.addRule(RelativeLayout.RIGHT_OF,objIVSeperator.getId());
objTVPageName.setLayoutParams(objTVPageNameParams);
objTVPageName.setTextColor(Color.WHITE);
objTVPageName.setTextSize(TypedValue.COMPLEX_UNIT_PX,18+sizeAdjust);
objTVPageName.setText("House Details");
objTVPageName.setTypeface(null, Typeface.BOLD);
objTVPageName.setPadding(2, 0, 2, 0);
objRLActionBar.addView(objTVPageName);
objRLBody.addView(objRLActionBar,objRLActionBarParams);
And the output is is as shown below
the image overlapped with text and the 'separator image' comes first! I need the components in this order objIVActivityIcon, objIVSeperator,objTVPageName. What is wrong with the above code plz help...
objRLActionBar.setId(25324);
should be
objIVActivityIcon.setId(25324);