Setting RelativeLayout.ALIGN_PARENT_LEFT to LinearLayout - android

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.

Related

Set Margins Between Linear Layout Programatically

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

Set gravity of dynamic textview

I have created a text view dynamically.I want to set its gravity to right.I have used textview.setGravity(Gravity.RIGHT) but its not working.When I am using it the text view is aligned to left direction only.How can I set the gravity of textview?I dont want to use setPadding().
textMore[i]=new TextView(this);
textMore[i].setGravity(Gravity.Right);
textMore[i].setText("more....");
textMore[i] .setTextColor(Color.BLUE);
you can't change alignment of TextView by setGravity , it changes the TextView contents alignment.
You can do
LinearLayout.LayoutParams textParams = (LinearLayout.LayoutParams) textMore[i].getLayoutParams();
textParams.gravity = Gravity.RIGHT;
textMore[i].setLayoutParams(textParams);
and then add the view in your dynamic LinearLayout
If the parent of your text view is RelativeLayout(Prefer this)
set : alignParentRight
If the parent of your text view is LinearLayout
set : setLayoutGravity(Gravity.Right)
or
LinearLayout.LayoutParams textParams = (LinearLayout.LayoutParams)textMore[i].getLayoutParams();
textParams.gravity = Gravity.RIGHT;
textMore[i].setLayoutParams(textParams);
setGravity(Gravity.Right) ==> This property will align the text inside your textView, rather than aligning the view in its parent.
Firstly set your textView Width property to fill parent then you can set it gravity to right
if you are using linear layout

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.

Change LayoutParams of LinearLayout that is inside of RelativeLayout

I have LinearLayout inside of RelativeLayout. I would like to change height of LinearLayout programmatically. I have tried do this with LinearLayout.LayoutParams but I've got an exception. How to solve this problem? Thanks.
Try with RelativeLayout.LayoutParams as the parent is a RelativeLayout.
The type of the LayoutParams depends on the parent type.

Android: Retrieve layout_marginBottom programmatically?

In my a LinearLayout I have set android:layout_marginBottom in my XML. I just want to retrieve that value.
Just LayoutParams doesn't give me access to bottomMargin . I tried to typecast :
row1 = (LinearLayout) findViewById(R.id.mainrow1);
LinearLayout.LayoutParams row1Params = (android.widget.LinearLayout.LayoutParams) row1.getLayoutParams();
but this gives me Runtime exception ClassCastException.
The main aim is I got buttons in nested LinearLayouts & I want to set height/width of LinearLayout programmatically that will inturn auto set height of buttons. I have added bottomMargin after 1st layout - so there is space between 2 rows.
Change Your layout of Button according to following logic,
set Layout_Width to 0dip
set Layout_Weight to 1
set Layout_Height to Wrap_Content
try this. When you get layout, R.layout. is best option. It is working for me.
row1 = (LinearLayout) findViewById(R.layout.mainrow1);

Categories

Resources