i use this code to change the position of my view
RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams)myview.getLayoutParams();
params.topMargin = topmargin;
params.leftMargin = leftmargin;
myview.setLayoutParams(params);
and nothing change.but when i use this code
myview.setTop(topmargin);
myview.setLeft(leftmargin);
it will work.now i want to know what is wrong with my first code??
because in the internet so many people suggest first code to change the position of views.
EDIT
also this code doesn't work
RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams)myview.getLayoutParams();
params.setMargins(100,100,0,0);
myview.setLayoutParams(params);
myview.requestLayout();
thanks in advance
Try calling myview.requestLayout(); after myview.setLayoutParams(params);
UPDATE
RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams)myview.getLayoutParams();
params.setMargins(left,top, right,bottom);
myview.setLayoutParams(params);
myview.requestLayout();
You need to call requestLayout() after updating the layout params
RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams)myview.getLayoutParams();
params.topMargin = topmargin;
params.leftMargin = leftmargin;
myview.setLayoutParams(params);
myview.requestLayout();
And this is a brief explanation from official documentation:
.setTop() ( same with .setLeft() )
Sets the top position of this view relative to its parent. This method
is meant to be called by the layout system and should not generally be
called otherwise, because the property may be changed at any time by
the layout.
topMargin ( same with leftMargin )
The top margin in pixels of the child. Margin values should be
positive. Call setLayoutParams(LayoutParams) after reassigning a new
value to this field.
Related
I am writing a table programmatically from an SQLite database.
In a loop I am generating needed TextViews and am attempting to wrap the data in a TextView called descCol when the data is longer than the existing screen allows.
Suggestions to do this were offered in the following link:
Setting width to wrap_content for TextView through code
However when using either of the suggested methods I'm getting a java.lang.NullPointerException.
Here's my code example:
TextView descCol = new TextView(this);
descCol.getLayoutParams().width = ViewGroup.LayoutParams.WRAP_CONTENT;
In this case debug shows it throws a java.lang.NullPointerException on the getLayoutParams() line.
Also I've tried:
TextView descCol = new TextView(this);
ViewGroup.LayoutParams params = descCol.getLayoutParams();
params.height = ViewGroup.LayoutParams.WRAP_CONTENT;
params.width = ViewGroup.LayoutParams.WRAP_CONTENT;
descCol.setLayoutParams(params);
In this case debug shows it throws a java.lang.NullPointerException on the params.height line.
Debug shows that in either case after performing the getLayoutParams() method, params is equal to null, apparently throwing the exception.
I've tried to assign other parameters first to descCol (textcolor, text, gravity etc.) prior to the getLayoutParams() but get the same result.
Suggestions as to how to avoid the java.lang.NullPointerException would be appreciated.
Also I was able to accomplish my task programmatically by simply using the setWidth and setHeight methods.
textview.setWidth(ViewGroup.LayoutParams.WRAP_CONTENT);
textview.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
Try this way:
LinearLayout.LayoutParams textParam = new LinearLayout.LayoutParams
(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT, 1.0f);
textView.setLayoutParams(textParam);
Why are you using getLayoutParams() when you didnt even set the layout Params ?
You need to setLayout Params first. like this.
TextView view = new TextView(this);
view.setText("example textview ");
//adding layout properties
view.setLayoutParams(new LinearLayout.LayoutParams
(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT, 0));
// add the textview to the parentLayout
parentLayout.addView(view);
I want to stretch all columns in my grid layout equally , How can I do that ?
I am using this params for every cell
LayoutParams param2 =new LayoutParams();
param2.height = LayoutParams.WRAP_CONTENT;
param2.width = ViewGroup.LayoutParams.WRAP_CONTENT;
param2.setGravity(Gravity.CENTER);
param2.columnSpec = GridLayout.spec(i,FILL);
param2.rowSpec = GridLayout.spec(rowCount,FILL);
param2.setGravity(Gravity.FILL);
Build Your Own GridLayout using nested LinearLayouts so you can customize it like you want, Or simply set the weight for each cell you add but you will need set you minimum sdk to 21
Setting width of child dynamically
GridLayout.LayoutParams params = (GridLayout.LayoutParams) child.getLayoutParams();
params.width = (parent.getWidth()/parent.getColumnCount()) -params.rightMargin - params.leftMargin;
child.setLayoutParams(params);
i want to use a unknown (at begin) number of Views in my CustomView, so I have to set the position of each of them programatically. The problem is that I cannot use setX()/... or seTop()/... , because Im developing for froyo. But how can I set the position else?
With RelativeLayout you can do the following:
RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams)child.getLayoutParams();
layoutParams.leftMargin = x;
layoutParams.topMargin = y;
child.setLayoutParams(layoutParams);
Is there an easy and simple way to set LayoutParams ? Or to be exactly, the MarginLayoutParams? I want to set the MarginRight to a few dp, unfortunately im not able to set these in the LayoutFile cause the Target is a ListFragment and in Code-Behind looks it very ugly.
The reason i do this not in the Layout of the items is so the code is optimized and perfomant.
To sum up: Is there any very simple and clean way to set Params ?
Yes, you can do something like this:
MyImageView i2 = new MyImageView(context);
LayoutParams lp = new LayoutParams(300, 300);
lp.LeftMargin = 100;
lp.TopMargin = 100;
lp.Gravity = 0;
this.AddView(i2, lp);
LayoutParams lp = new LayoutParams( LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT );
lp.setMargins( left, top, right, bottom );
You might need to write it as LinearLayout.LayoutParams, depending on what type of layout is the container.
And then you call the method setLayoutParams( lp ); on the given view/layout/widget.
I have to add more than one buttons on a LinearLayout. But I want to put those buttons say 5px apart. I could not find a way to set a margin of a Button. Any idea?
Use the layout_margin property of the button element. Does that not work?
<Button android:layout_margin="5px" (...)/>
Edit
When creating a button in java, LayoutParams is used to specify margins and so.
Button button = new Button();
(....)
params = new LinearLayout.LayoutParams();
params.leftMargin = 5;
(set params as you need)
parent.addView(button, params);
The LayoutParams you use must match the Layout you add your button in (see http://developer.android.com/reference/android/view/ViewGroup.LayoutParams.html). For a LinearLayout, use a LinearLayout.LayoutParams and set the *Margin fields accordingly.
Try this:
//Assuming your button is in a LinearLayout as stated
LinearLayout.LayoutParams params = myButton.getLayoutParams();
params.setMargins(0, 0, 0, 0) //left, top, right, bottom
myButton.setLayoutParams(params);
MarginLayoutParams params = (MarginLayoutParams) vector8.getLayoutParams();
params.width = 200; params.leftMargin = 100; params.topMargin = 200;
vector8.setLayoutParams(params);
Need use type of: MarginLayoutParams
Adding padding to the buttons would also solve your problem. Check this out
http://developer.android.com/resources/tutorials/views/hello-formstuff.html#CustomButton
use this code to set margin at runtime
LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) btnContainer.getLayoutParams();
params.setMargins(5, 50, 10, 0); //left, top, right, bottom
view_whwre_set_margin.setLayoutParams(params);