Set Margins Between Linear Layout Programatically - android

I am dynamically adding linearlayouts to a main linearlayout which is contained in a scrollview.
Scrollview --> MainLinearLayout --> Childrens.
Now i wanted some space between childrens so i i did the following :
RelativeLayout.LayoutParams lp=new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT,height_layout);
lp.setMargins(50,50,50,50);
linearLayout.setLayoutParams(lp);
But i am unable to see any space between the childrens they all are stuck with each other.
Height width works fine but not the margins.

You should use LinearLayout.LayoutParams if you want to set things to LinearLayout
e.g.:
LinearLayout.LayoutParams lp=new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,height_layout);
lp.setMargins(50,50,50,50);
linearLayout.setLayoutParams(lp);

Use below code
((RelativeLayout.LayoutParams)linearLayout.getLayoutParams()).setMargins(left, top, right, bottom);

Related

Margin in container which apply it all children in android

Suppose I have a container like LinearLayout and it has 10 Element(TextView,editText,..etc), so I want to give top margin on each element inside linear layout. But I don't want to give margin individual each element, I want give top Margin to container ( LinearLayout) which apply to its all element . is it possible in android?
LinearLayout layout = yourLayout;
for (int i = 0; i < layout.getChildCount(); i++) {
View view = layout.getChildAt(i);
LayoutParams layoutParams = (LayoutParams) view.getLayoutParams();
layoutParams.setMargins(0, 10, 0, 0);
view.setLayoutParams(layoutParams);
}
May be you can use paddingTop in LinearLayout but I m not sure.
Try This in your XML file
android:paddingTop="10dp"
Late answer but...
For vertical LinearLayout and layout_marginTop you only need to set it to the first children.
For horizontal LinearLayout and layout_marginTop (or any property that is relative to the parent and not a sibling element), you need an additional layout as child with the layout_marginTop value, another LinearLayout or ConstraintLayout for example.

Programmatically using MATCH_PARENT for a RelativeLayout inside a FrameLayout

I'm subclassing HorizontalScrollView (which is a FrameLayout) and adding a RelativeLayout to it programmatically.
The FrameLayout correctly fills the parent view, but RelativeLayout inside doesn't show up.
MainActivity::OnCreate()
setContentView(R.layout.activity_main);
CustomHorizontalScrollView custom = new CustomHorizontalScrollView(this);
ViewGroup contentView = (ViewGroup) findViewById(R.id.content);
contentView.addView(custom,new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, 180));
CustomHorizontalScrollView::Constructor()
this.setBackgroundColor(Color.MAGENTA);
relativeLayout = new RelativeLayout(context);
relativeLayout.setBackgroundColor(Color.BLACK);
this.addView(relativeLayout, new FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT, FrameLayout.LayoutParams.MATCH_PARENT));
Result:
The RelativeLayout should be black and should fill parent but is not.
The weird thing is that if I specify width and height in pixels instead using MATCH_PARENT, the RelativeLayout appears.
this.addView(relativeLayout, new FrameLayout.LayoutParams(90, 90));
Does that mean that I can't use MATCH_PARENT when programmatically adding a RelativeLayout to a FrameLayout? Or am I missing something? Or maybe HorizontalScrollView only supports having a child with fixed width and height?
I do not find strict info in Android SDK API Reference, but actually the HorizontalScrollView is a "Layout container for a view hierarchy that can be scrolled by the user, allowing it to be larger than the physical display."
So, why do you need a HorizontalScrollView if its unique child must have the same width?
Likely, you can try the following:
this.addView(relativeLayout, new FrameLayout.LayoutParams(90, FrameLayout.LayoutParams.MATCH_PARENT));
... and the RelativeLayout will appear.
But maybe the following makes more sense:
this.addView(relativeLayout, new FrameLayout.LayoutParams(FrameLayout.LayoutParams.WRAP_CONTENT, FrameLayout.LayoutParams.MATCH_PARENT));
Then the RelativeLayout will be large enough to contain its children and you'll can scroll it using the HorizontalScrollView.
The HorizontalScrollView has the property fillViewport that you can set to true if you want
that the HorizontalScrollView "stretch its content to fill the viewport": I think it can be useful only when the content is less large than the HorizontalScrollView, but (I repeat), if the content is ALWAYS less large than the HorizontalScrollView, then the HorizontalScrollView is useless.

What is difference between .setGravity(...) method and getLayoutParams().gravity=x?

If you want to set the gravity of a View like LinearLayout programmatically, you have 2 ways:
1)
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.FILL_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
params.gravity = Gravity.RIGHT;
MyLinearLayout.setLayoutParams(params);
2)
MyLinearLayout.setGravity(Gravity.RIGHT);
What is difference between these 2 ways?
It is important you know the difference.
In the first way you are setting that layout gravity of your LinearLayout. It means you are setting the position of your layout in its parent View. It is equivalent to android:layout_gravity="right" in xml layouts.
But in the second way you are setting the position of child views in your Linearlayout and it is equivalent to android:gravity="right" in xml layouts. For example if you put a TextView in your LinearLayout and its width was wrap_content, the TextView will be place in the right side of your LinearLayout.

Setting RelativeLayout.ALIGN_PARENT_LEFT to LinearLayout

How can I set the RelativeLayout.ALIGN_PARENT_LEFT to LinearLayout in code? I have a RelativeLayout which child is LinearLayout and I need to set the RelativeLayout.ALIGN_PARENT_LEFT in the child LinearLayout instance. The problem is that when I set it, I will lose the child LinearLayout parameters (I will lose the weight). My understanding is that the only way to set the RelativeLayout.ALIGN_PARENT_LEFT to child element is to use RelativeLayout.Layoutparams when setting the child view layout parameters.
Thanks in advance!!
You have to set the width height parms and weight along with your aligh left rule in programtically
layoutparms = new LayoutParams(WindowManager.LayoutParams.FILL_PARENT,
WindowManager.LayoutParams.FILL_PARENT, weightthere);
layoutparms.addRule(RelativeLayout.LEFT_OF, leftisdeview);
yourview.setLayoutParams(layoutparms);
You just have to give android:gravity="left" to the parent linear layout.Remember that it is gravity not layout_gravity.

How do I make two relativelayouts show as top and bottom on a linearlayout versus side by side, programmatically?

I have a linearlayout as a container for two relativelayouts. Both relativelayouts appear on the screen but they are side by side. I want them to be top and bottom. It looks as if the linearlayout initialization defaults to Horizontal. I have tried using setorientation to Vertical but the screen blanks out.
The following code is an example of what I am trying to do:
LinearLayout layoutContainer = new LinearLayout(this);
layoutContainer.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT,
LayoutParams.FILL_PARENT));
//layoutContainer.setOrientation(LinearLayout.VERTICAL);
// Arguments here: width, height, weight
LinearLayout.LayoutParams childLp = new LinearLayout.LayoutParams(0,
LayoutParams.FILL_PARENT, 1);
layoutTop = new RelativeLayout(this);
layoutContainer.addView(layoutTop, childLp);
layoutBot = new RelativeLayout(this);
layoutContainer.addView(layoutBot, childLp);
layoutTop.setBackgroundColor(GREEN);
layoutBot.setBackgroundColor(Color.BLUE);
setContentView(layoutContainer);
It looks as if the linearlayout initialization defaults to Horizontal.
That is exactly right.
I have tried using setorientation to Vertical but the screen blanks out.
You do need to set orientation to vertical to get this effect. As for it "blanks out", I see several things wrong such as setting the width to "0". There is no width so it won't show anything. I think you would want something like LinearLayout.WRAP_CONTENT. Also, you are using LinearLayout params for your RelativeLayout which may or may not make a difference in this case.
If there isn't a necessary reason to create the layout in Java it is much easier to do this in the xml.
I think you are complicating your layouts by trying to programmatically manipulate them. Set the orientation to Vertical and do the following:
<LinearLayout
------
------
android:orientation="vertical">
Your first Relative layout
<RelativeLayout
android:id="#+id/rel1"
android:layout_alignParentTop="true"
----------------------
---------------------
/>
Your next Relative layout
<RelativeLayout
android:id="#+id/rel2"
android:layout_below="#id/rel1"
----------------------
---------------------
/>

Categories

Resources