LinearLayout setMargins programmatically has not effect - android

Please have a look at the following code:
LinearLayout ll1 = new LinearLayout(context);
ll1.setBackgroundColor(Color.BLUE);
ll1.setOrientation(LinearLayout.VERTICAL);
LinearLayout.LayoutParams ll1LayoutParams = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
ll1LayoutParams.setMargins(100, 0, 100, 0);
ll1.setLayoutParams(ll1LayoutParams);
...
// parentLayout is FrameLayout
parentLayout.addView(ll1, ll1LayoutParams);
Why doesn't it work?

Change
LinearLayout.LayoutParams ll1LayoutParams = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
to
FrameLayout.LayoutParams ll1LayoutParams = new FrameLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
When assigning layout params to a child, you must assign the LayoutParams class of its parent and not the view. Since here your parent view is a FrameLayout, you have to use FrameLayout.LayoutParams.

Related

LinearLayout inside a ScrollView doesn't show

Here's my Code
mainView = new MainView(this, imageId, text, 3);
mainView.setOnTouchListener(this);
LinearLayout.LayoutParams mainParams = new
LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
LinearLayout mainLinear = new LinearLayout(this);
ScrollView.LayoutParams scrollParams = new
ScrollView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
ScrollView scrollView = new ScrollView(this);
LinearLayout.LayoutParams myViewParams = new
LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
mainLinear.addView(mainView, myViewParams);
scrollView.addView(mainLinear, mainParams);
addContentView(scrollView, scrollParams);
Of course it works within LinearLayout only. Please tell me how to put it inside a ScrollView.
ScrollView's child should only have height as WRAP_CONTENT. If you try the same layout using xml AS will give you a lint warning saying exactly this.
If you need your child view to occupy full height of scroll view when content is less you could use setFillViewport().
This should work for you:
mainView = new MainView(this, imageId, text, 3);
mainView.setOnTouchListener(this);
LinearLayout.LayoutParams mainParams = new
LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
LinearLayout mainLinear = new LinearLayout(this);
ScrollView.LayoutParams scrollParams = new
ScrollView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
ScrollView scrollView = new ScrollView(this);
LinearLayout.LayoutParams myViewParams = new
LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
mainLinear.addView(mainView, myViewParams);
scrollView.addView(mainLinear, mainParams);
scrollView.setFillViewport(true);
addContentView(scrollView, scrollParams);

How to set TextView Dynamically left of paraent?

here is my code i got error params is null
viewDynamic = new TextView(getApplicationContext());
RelativeLayout relativeLayout = (RelativeLayout) findViewById(R.id.relative);
RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams)viewDynamic.getLayoutParams();
params.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
params.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
params.addRule(RelativeLayout.ABOVE,userEditTxt.getId());
viewDynamic.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
viewDynamic.setText(userEditTxt.getText());
viewDynamic.setTextColor(Color.RED);
viewDynamic.setTextScaleX(2);
viewDynamic.setTextSize(20);
viewDynamic.setPadding(10, 10, 10, 10);
//viewDynamic.setVisibility(View.GONE);
relativeLayout.addView(viewDynamic,params);
If you haven't added the view to any parent container, or havent called setLayoutParams manually, then getLayoutParams() will return null.
Make soure you call viewDynamic.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT)); before you call viewDynamic.getLayoutParams();, or add the view to a container, so LayoutParams will be generated for the view.
I suggest you add your View like this:
viewDynamic = new TextView(getApplicationContext());
RelativeLayout relativeLayout = (RelativeLayout) findViewById(R.id.relative);
RelativeLayout.LayoutParams params = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT)
params.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
params.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
params.addRule(RelativeLayout.ABOVE,userEditTxt.getId());
viewDynamic.setText(userEditTxt.getText());
viewDynamic.setTextColor(Color.RED);
viewDynamic.setTextScaleX(2);
viewDynamic.setTextSize(20);
viewDynamic.setPadding(10, 10, 10, 10);
relativeLayout.addView(viewDynamic,params);//you attach params to your View here

Android Programmatically set linerlayout at the bottom of relativelayout

I want to add a linerlayout at the bottom of relativelayout. How could I achieve that?
This is the code snippet that I am using:
rl=new RelativeLayout(this);
ll = new LinearLayout(this);
buttons=new LinearLayout(this);
buttons.setOrientation(LinearLayout.HORIZONTAL);
ll.setOrientation(LinearLayout.VERTICAL);
//buttons.addRule();
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT);
params.topMargin=450;
//params.gravity = Gravity.BOTTOM;
rl.addView(ll);
rl.addView(buttons,params);
I think something like this, should work :
rl=new RelativeLayout(this);
buttons=new LinearLayout(this);
buttons.setOrientation(LinearLayout.HORIZONTAL);
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
lp.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
rl.addView(buttons, lp);

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

How to set child weight in the code?

I want that button(child) will have 25% of full width of horizontal LinearLayout (its parent). The code:
LinearLayout ll = new LinearLayout(this);
ll.setLayoutParams(new ViewGroup.LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT));
ll.setWeightSum (1.0f);
Button b = new Button (this);
ll.addView(b);
How can I do it? Thanks.
Try this:
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENTS,LayoutParams.FILL_PARENT));
lp.weight = 0.25;
b.setLayoutParams(lp);
You can pass it in as part of the LinearLayout.LayoutParams constructor:
LayoutParams param = new LinearLayout.LayoutParams(
LayoutParams.MATCH_PARENT,
LayoutParams.MATCH_PARENT, 1.0f);

Categories

Resources