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.
Related
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);
So I have tried horizontal listview (adapter), but I cannot get the padding in the left so it will overlap each other. it's just separated like in wrap content. any suggestion on how to do this dynamically?
http://www.bild.me/bild.php?file=8656221tabs.jpg
I even tried dynamic addview
<LinearLayout
android:id="#+id/frag_productline_list"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" >
</LinearLayout>
for (int i = 0 ; i < productList.size(); i++){
TextView tv = new TextView(context);
tv.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT) );
tv.setPadding(-40, 0, 0, 0);
tv.setText(productList.get(i));
tv.setBackgroundResource(R.drawable.tab);
layout.addView(tv);
}
But it looks like just an adapter I've tried in horizontal list view . Even worse with no handling of onItemClickListener
I suggest having an absolute layout/relative layout and scroll view instead of horizontal list view. Programmatically append the image buttons in an overlapped manner.
You can't overlap views by setting a negative padding. However, you can give them a negative margin.
LayoutParams lp = tv.getLayoutParams();
lp.setMargin(negative left margin, top, right, bottom);
tv.setLayoutParams(lp);
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.
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.
I am having an issue with my app, basically i have few layouts like this:
|ABC|
each letter describe one layout(A is the relative layout).
When i am adding an image view to the relative layout, this image view
can move over all the layout. But the relative layout became stretched to whole the width of the screen.
|A|
so basically you can`t see the B,C layouts any more.
This is my function:
public void startGame(){
int i;
for(i=0;i<currentLevelObs.size();i++){
if(currentLevelObs.get(i)==Globals.OBS_ENTRY_INDEX)
break;
}
lp = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.FILL_PARENT,
RelativeLayout.LayoutParams.FILL_PARENT);
lp.addRule(RelativeLayout.BELOW, (i+1));
lp.addRule(RelativeLayout.RIGHT_OF, i);
lp.leftMargin = 25;
ImageView img = new ImageView(this);
img.setLayoutParams(lp);
img.setBackgroundResource(R.drawable.hero);
gameLayout.addView(img);
}
This function called from onCreate method.
You have given layout width to fill parent. So it will take up the whole screen. Thats the problem. Change it to wrap-content and then check.
Add your ABC layouts to a ScrollView, that way you can scroll down to see B and C.
it should be like this:
<ScrollView
..
...
....>
<D
..>
A
B
C
</D>
</ScrollView>
a ScrollView can contain only 1 child, so keep your ABC in a D layout which is in ScrollView