Try #1: I am trying to set layout gravity for RelativeLayout programmatically.
FrameLayout frameLayout = new FrameLayout(this);
FrameLayout.LayoutParams frameLayoutParams = new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
frameLayout.setLayoutParams(frameLayoutParams);
setViewPaddingInDp(frameLayout, 16, 16, 16, 16);
RelativeLayout relativeLayout = new RelativeLayout(this);
LinearLayout.LayoutParams relativeLayoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
relativeLayoutParams.gravity = Gravity.CENTER;
relativeLayout.setLayoutParams(relativeLayoutParams);
frameLayout.addView(relativeLayout);
ImageView imageView = new ImageView(this);
setViewWidthAndHeight(imageView, 128, 192);
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setImageResource(R.drawable.maxwell);
relativeLayout.addView(imageView);
setContentView(frameLayout);
This is what I am getting as result:
Try #2: I have tried set CENTER_IN_PARENT programmatically, but no result
RelativeLayout relativeLayout = new RelativeLayout(this);
RelativeLayout.LayoutParams relativeLayoutParams = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
relativeLayoutParams.addRule(RelativeLayout.CENTER_IN_PARENT);
relativeLayout.setLayoutParams(relativeLayoutParams);
frameLayout.addView(relativeLayout);
Try #3: Also tried to set CENTER_HORIZONTAL, but no effect:
RelativeLayout relativeLayout = new RelativeLayout(this);
RelativeLayout.LayoutParams relativeLayoutParams = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
relativeLayoutParams.addRule(RelativeLayout.CENTER_HORIZONTAL, RelativeLayout.TRUE);
// relativeLayoutParams.addRule(RelativeLayout.CENTER_VERTICAL, RelativeLayout.TRUE);
// relativeLayoutParams.addRule(RelativeLayout.CENTER_IN_PARENT, RelativeLayout.TRUE);
relativeLayout.setLayoutParams(relativeLayoutParams);
frameLayout.addView(relativeLayout);
How to set layout gravity CENTER for RelativeLayout? I need to set gravity for RelativeLayout without changing FrameLayout or ImageView layout params.
Here is the fix, just verified myself. Working as required. The important part we were missing is imageViewParam.addRule(RelativeLayout.CENTER_HORIZONTAL);
FrameLayout frameLayout = new FrameLayout(this);
FrameLayout.LayoutParams frameLayoutParams = new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
frameLayout.setLayoutParams(frameLayoutParams);
//setViewPaddingInDp(frameLayout, 16, 16, 16, 16);
RelativeLayout relativeLayout = new RelativeLayout(this);
RelativeLayout.LayoutParams relativeLayoutParams = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
relativeLayoutParams.addRule(CENTER_IN_PARENT, TRUE);
relativeLayout.setLayoutParams(relativeLayoutParams);
frameLayout.addView(relativeLayout);
ImageView imageView = new ImageView(this);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.CENTER_HORIZONTAL);
imageView.setLayoutParams(params);
// params.addRule(RelativeLayout.CENTER_IN_PARENT);
// params.addRule(RelativeLayout.CENTER_VERTICAL);
// setViewWidthAndHeight(imageView, 128, 192); // if you are using this method, then set the layout param accordingly
// imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setImageResource(R.drawable.maxwell);
relativeLayout.addView(imageView);
setContentView(frameLayout);
Set MATCH_PARENT instead of WRAP_CONTENT to RelativeLayout like
RelativeLayout.LayoutParams relativeLayoutParams = new
RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.MATCH_PARENT);
And set a RelativeLayout.LayoutParams to your ImageView
RelativeLayout.LayoutParams layoutParams =
(RelativeLayout.LayoutParams) imageView.getLayoutParams();
layoutParams.addRule(RelativeLayout.CENTER_IN_PARENT,
RelativeLayout.TRUE);
imageView.setLayoutParams(layoutParams);
Related
Guys I have a cardview and I want to add 2 button on it with rule but addRule() method is not working. In the picture, figure A is occurring but I want to make figure B, I mean, I want the buttons set align_parent_right and align_parent_bottom and the second button adjacent to first one. When I run it, figure A occuring. Any suggestions?
RelativeLayout.LayoutParams rl= (RelativeLayout.LayoutParams) iView.getLayoutParams();
RelativeLayout.LayoutParams lparams = new RelativeLayout.LayoutParams(width/4,height/5);
RelativeLayout.LayoutParams lparams2 = new RelativeLayout.LayoutParams(width/4,height/5);
removeButton=new Button(mContext);
modifyButton=new Button(mContext);
lparams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
lparams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
cardView.setLayoutParams(rl);
cardView.addView(removeButton,lparams);
cardView.addView(modifyButton,lparams2);
Try this:-
RelativeLayout mainLayout = (RelativeLayout) findViewById(R.id.mainLayout);
CardView cardView = new CardView(this);
ViewGroup.LayoutParams cardParams = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
cardView.setLayoutParams(cardParams);
mainLayout.addView(cardView);
RelativeLayout relativeLayout = new RelativeLayout(this);
ViewGroup.LayoutParams layoutParams = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
relativeLayout.setLayoutParams(layoutParams);
cardView.addView(relativeLayout);
LinearLayout linearLayout = new LinearLayout(this);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
linearLayout.setLayoutParams(params);
linearLayout.setOrientation(LinearLayout.HORIZONTAL);
linearLayout.setGravity(Gravity.RIGHT);
relativeLayout.addView(linearLayout);
Button removeButton = new Button(this);
Button modifyButton = new Button(this);
removeButton.setText("Remove");
modifyButton.setText("Modify");
linearLayout.addView(removeButton);
linearLayout.addView(modifyButton);
GridLayout glSameLoactions;
#OnClick(R.id.tvAddLocation)
void addLocationOnClickListener(){
if(i==0) {
glSameLoactions = new GridLayout(this);
glSameLoactions.setColumnCount(3);
glSameLoactions.setBackgroundResource(R.drawable.shape_rounded_corner_blurfilled);
glSameLoactions.setPadding(16,16,16,16);
}
RelativeLayout rlLocation = new RelativeLayout(this);
rlLocation.setPadding(16, 16, 16, 16);
LinearLayout.LayoutParams relativeParams = new LinearLayout.LayoutParams(300,200);
relativeParams.setMargins(16, 16, 16, 16);//<<<<----- NOT WORKING
rlLocation.setLayoutParams(relativeParams);
rlLocation.requestLayout();
rlLocation.setBackgroundResource(R.drawable.shape_rounded_corner_blurfilled);
TextView tvLocationName = new TextView(this);
tvLocationName.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT));
tvLocationName.setId(R.id.tvLocationName);
tvLocationName.setText("amritsar,PB");
tvLocationName.setTextColor(getResources().getColor(R.color.white));
tvLocationName.setTextSize(14f);
ImageView ivRadiobtn = new ImageView(this);
tvLocationName.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT));
ivRadiobtn.setImageResource(R.drawable.ic_circle_empty);
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
lp.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
lp.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
rlLocation.addView(tvLocationName);
rlLocation.addView(ivRadiobtn,lp);
glSameLoactions.addView(rlLocation);
if (i==0) {
llLocations.addView(glSameLoactions);
llLocations.setVisibility(View.VISIBLE);
}
i++;
}
I am making a layout programmatically, in which on a click of button, a new relative layout is added in a grid layout. All my code is working fine, but the problem is that i am unable to set margins to the relative layout inside the grid layout. Please help!!
Thanks
You should use RelativeLayout.LayoutParams instead of LinearLayout.LayoutParams.
Try this:
RelativeLayout.LayoutParams relativeParams =new RelativeLayout.LayoutParams(300, 300);
relativeParams.setMargins(16, 16, 16, 16);
rlLocation.setLayoutParams(relativeParams);
I've created many custom views and I am trying to add them to my fragment. They get added but I can't seem to get them to go where I want. There should be 2 columns and 3 rows but it ends up as 1 column with all of the custom views stacked on top of one another. Here is my code to add the views and set the layout params to the fragment layout:
RelativeLayout fm = (RelativeLayout) view.findViewById(R.id.fragmentLayout);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.ALIGN_PARENT_LEFT, RelativeLayout.TRUE);
CustomImages cs = new CustomImages(getActivity());
cs.setId(R.id.one);
cs.setLayoutParams(params);
params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.ALIGN_PARENT_RIGHT, RelativeLayout.TRUE);
params.addRule(RelativeLayout.RIGHT_OF, cs.getId());
CustomImages2 cs2 = new CustomImages2(getActivity());
cs2.setId(R.id.two);
cs2.setLayoutParams(params);
RelativeLayout.LayoutParams params2 = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
params2.addRule(RelativeLayout.ALIGN_PARENT_LEFT, RelativeLayout.TRUE);
params2.addRule(RelativeLayout.BELOW, cs2.getId());
CustomImages3 cs3 = new CustomImages3(getActivity());
cs3.setId(R.id.three);
cs3.setLayoutParams(params);
RelativeLayout.LayoutParams params3 = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
params3.addRule(RelativeLayout.ALIGN_PARENT_RIGHT, RelativeLayout.TRUE);
params3.addRule(RelativeLayout.RIGHT_OF, cs3.getId());
CustomImages4 cs4 = new CustomImages4(getActivity());
cs4.setId(R.id.four);
cs4.setLayoutParams(params);
RelativeLayout.LayoutParams params4 = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
params4.addRule(RelativeLayout.ALIGN_PARENT_LEFT, RelativeLayout.TRUE);
params4.addRule(RelativeLayout.BELOW, cs4.getId());
CustomImages5 cs5 = new CustomImages5(getActivity());
cs5.setId(R.id.five);
cs5.setLayoutParams(params);
cs3.setLayoutParams(params);
cs4.setLayoutParams(params);
cs5.setLayoutParams(params);
I believe params there should be replaced with params2, params3 and params4 respectively.
UPDATE:
Also, you should specify LAYOUT_BELOW for all views which are not on top, and do it correctly:
params2.addRule(RelativeLayout.BELOW, cs.getId()); // not cs2
params3.addRule(RelativeLayout.BELOW, cs2.getId()); // add this
params4.addRule(RelativeLayout.BELOW, cs3.getId()); // not cs4
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
How to set android:layout_alignParentBottom="true" for a videoview, by code and not from xml?
I assume you are trying to add your VideoView to a RelativeLayout ?
RelativeLayout relativeLayout = findViewById(R.id.your_relative_layout);
mVideo = new VideoView(this);
RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT); // or wrap_content
layoutParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
relativeLayout.addView(mVideo , layoutParams);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM, RelativeLayout.TRUE);
YourVideoView.setLayoutParams(params);
get layout params from your view and add rule
RelativeLayout.LayoutParams lp = (RelativeLayout.LayoutParams) view.getLayoutParams();
lp.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
Some References are Here1 and Here2
this is what you have to do:
Create a RelativeLayout.LayoutParams object.
Use addRule(int) or addRule(int, int) to set the rules. The first method is used to add rules that don’t require values.
Set the parameters to the view (in this case, to each button).
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.ALIGN_PARENT_LEFT,RelativeLayout.TRUE);
yourView.setLayoutParams(params);