setMargins to relative layout not working in Grid layout - android

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);

Related

Can't set layout gravity for RelativeLayout programmatically

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);

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

Adding more than one relative layout to the linear layout dynamically

How to add more than one relative layout to the linear layout programmatically .
I tried but it gives me this exception "Specified child already has a parent. You must call removeview()"
Here is my code,
RelativeLayout addlangmid = new RelativeLayout(mActivity);
addlangmid.setBackgroundResource(R.drawable.language_bg_top);
TextView langname = new TextView(mActivity);
LayoutParams params = new LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
params.setMargins(20, 0, 30, 30);
langname.setText(str[0]);
addlangmid.addView(langname, params);
fulllay.addView(addlangmid, 0);
RelativeLayout addlangmid1 = new RelativeLayout(mActivity);
addlangmid.setBackgroundResource(R.drawable.language_bg_middle);
TextView langname1 = new TextView(mActivity);
LayoutParams params1 = new LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
params1.setMargins(20, 0, 30, 30);
langname1.setText(str[0]);
addlangmid1.addView(langname, params1);
fulllay.addView(addlangmid1, 1);
Change the below line
addlangmid1.addView(langname, params1);
to
addlangmid1.addView(langname1, params1);
You are adding langname to addlangmid and addlangmid1. Hence the error is coming.

How to overlay text on image programmatically

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.

android TextView setting margin and background using code

In an Android activity....how do I do the following using just code instead of XML?
<TextView android:background="#0000ff" android:layout_margin="2dip"/>
I also need text align right...
stuck on it for a while =( if someone could help
thanks
UPDATE
I tried the code below
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.FILL_PARENT,
RelativeLayout.LayoutParams.FILL_PARENT);
//250);
lp.setMargins(5,5,5,5);
and i did
row.addView(t1, lp);
row.addView(t2);
row.addView(t3);
row.addView(t4);
I also tried LinearLayout
but t1 for some reason doesn't show at all anymore....
here is how to code a textView:
mTextView = new TextView(this);
mTextView.setGravity(Gravity.CENTER_VERTICAL);
mTextView.setText(R.string.instructions);
mTextView.setTextColor(0xFF000000);
mTextView.setPadding(20, 8, 8, 20);
mTextView.setTextSize(TypedValue.COMPLEX_UNIT_PT, 8);
mScroll = new ScrollView(this);
mScroll.setScrollbarFadingEnabled(false);
mTextPane = new RelativeLayout(this);
mTextPane.setVisibility(View.GONE);
//mScroll.setVisibility(View.GONE);
mScroll.addView(mTextView);
mTextPane.addView(mScroll);
Resources res = getResources();
//Drawable drawable = res.getDrawable(R.drawable.text_pane_feather2);
Drawable drawable = res.getDrawable(R.drawable.text);
mTextPane.setBackgroundDrawable(drawable);
//RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams( 420, 420 );
RelativeLayout.LayoutParams lp =
new RelativeLayout.LayoutParams( RelativeLayout.LayoutParams.WRAP_CONTENT,
//RelativeLayout.LayoutParams.WRAP_CONTENT);
250);
lp.setMargins(0,0,0,30);
lp.addRule(RelativeLayout.CENTER_HORIZONTAL );
lp.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
layout.addView(mTextPane, lp);
Try the TextViews setBackgroundColour() method.
For setting the layout params, use the setMargins(int,int,int,int) from the MarginLayoutParams class.
See the documentation for more details at:
View.setBackgroundColour and
Layout Params

Categories

Resources