I'm having some trouble with Android's LayoutParams.
Basically I am trying to create an application that is completely dynamic, with all layouts generated programmatically and practically nothing beyond my splash screen committed to XML. I am having some success using linear layouts, and the app is rendering everything that I want it to. I am just having some trouble with fine tuning the layouts. e.g. forcing my footer to the bottom of the screen, stretching button bars to cover the entire width, etc.
As far as I can tell, the way to achieve this programmatically is using the setLayoutParams method. My problem is that I am not sure how to get the information into the Layout Params.
At the moment I am using the LinearLayout.LayoutParams (int width, int height) constructor, but I have not found a way of setting the parameters once it is created.
Instead I would like to use the LinearLayout.LayoutParams (Context c, AttributeSet attrs) constructor, but I can't find the right way of declaring an AttributeSet.
Can anyone help me?
Look at all of the "set" methods for the LinearLayout here:
http://developer.android.com/reference/android/widget/LinearLayout.html
Try like below -
android.view.ViewGroup.MarginLayoutParams params = (MarginLayoutParams) surfaceView.getLayoutParams();
params.height = DesireHeight;
params.width = DesireWidth;
params.leftMargin = DesireMarginLeft;
params.topMargin = DesiremarginTop;
params.rightMargin = DesireMarginRight;
params.bottomMargin = DesireMarginBottom;
YOURView.setLayoutParams(params);
Related
I don't have any code. However, for example, if I had a button and wanted it to go from 50dp to 100dp when it is clicked, how could I do that? I want to develop in Kotlin, and I am new to this and only 16 :). I am open to any suggestions to help me learn, but please help with my question.
tôi does not use Android (Kotlin), it is only with Andorid (java) that think kotlin code also equivalent in the java construct other languages. in java you can use:
RelativeLayout.LayoutParams lp = (RelativeLayout.LayoutParams) myButton.getLayoutParams();
//RelativeLayout.LayoutParams or LinearLayout.LayoutParams or v.v...
lp.width = 100;
lp.height = 100;
myButton.setLayoutParams(lp)
Every view has a LayoutParam. This object holds info about how it is placed in its parent- the layout_XXX attributes in the xml. So you just need to change its height, then tell it to lay itself out again:
view.getLayoutParams().height = new height;
view.requestLayout();
Im having problems specifiying the height of a relative Layout. From what i understand, these two blocks of code should be equivalent (myLayout is a RelativeLayout that i defined in XML, with an initial height of "0dp", and it's parent is also a RelativeLayout):
BLOCK 1:
RelativeLayout.LayoutParams p = (RelativeLayout.LayoutParams)myLayout.getLayoutParams();
p.height = (int)(35*scale);
myLayout.setLayoutParams(p);
myLayout.invalidate();
BLOCK 2:
RelativeLayout.LayoutParams p = (RelativeLayout.LayoutParams)myLayout.getLayoutParams();
RelativeLayout.LayoutParams newP = new RelativeLayout.LayoutParams(p.width, (int)(35*scale));
myLayout.setLayoutParams(newP);
myLayout.invalidate();
Scale, in my case, is 2.
So i expect myLayout to have a height of 70 after the execution of either of these blocks, and i expect it to return that height when i call myLayout.getHeight(). Also, i expect myLayout to occupy a rect with the height of 70 and its former width (happens to be match_parent).
But when i run the code, the first block does not change the height of myLayout on screen, nor does it change the return value of myLayout.getHeight(). It does, however, change myLayout.getLayoutParams().height.
Now the second block does work, although i have to run it twice(!?) for the change to take effect. Im seriously at a loss here and i cant find anything even closely related to this in the docs.
I thought this would be an easy task when i set out yesterday, but by now im questioning my sanity, among other things. Any Ideas?
If size is determined dynamically, you may need to use ViewTreeObserver to get the size seen on the screen.
Set your width and height in defining your params. If you want the width to be match_parent, change your code like below and it is going to work (if you don't want match_parent, just set the width that you desire in the code below):
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
LayoutParams.MATCH_PARENT, (int)(35*scale));
myLayout.setLayoutParams(params);
I have a very short problem. I have a custom control that is based upon LinearLayout. I inflate it's view from xml', in which the root is set as element.
Now when I try to set the padding of this custom control by "this.setPadding(x,x,x,x)" it does not affect the TextView and ImageView I add to this control in a few lines of code later.
Currently I am bypysing it, by setting the margin of each control separately, to achieve the global padding of the whole custom control, but it would be nice to know if there is a catch in using setPadding dynamicaly, or maybe a mistake is mine.
To simplify, my case looks like that:
setGravity(Gravity.CENTER_HORIZONTAL | Gravity.CENTER_VERTICAL);
LinearLayout.LayoutParams lp = new
LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.MATCH_PARENT); setLayoutParams(lp);
setPadding(0, 30, 0, 30); //I know it's in px
Afterwards I'm adding a large image, which shrinks due to it's size, but the padding of LinearLayout(which I try to set dynamicaly) does not affect it, only if I set margin on it directly.
Any tip would be greatly appriciated.
After a little of digging, found an answer through other StackOverflow question:
https://stackoverflow.com/a/13363318/905938
Basicaly if one person sets a background reasource with a selector xml given (as in my case) it overrides completely the previous padding setting. So the padding I was setting within the custom control initialization was lost as soon as it was set.
Now that I know the problem, I basicaly just intercept the call to this method in my custom control like this:
#Override
public void setBackgroundResource(int resid)
{
int paddingLeft, paddingTop, paddingRight, paddingBottom;
paddingLeft = getPaddingLeft();
paddingTop = getPaddingTop();
paddingRight = getPaddingRight();
paddingBottom = getPaddingBottom();
super.setBackgroundResource(resid);
setPadding(paddingLeft, paddingTop, paddingRight, paddingBottom);
}
That solves the problem, and I hope will solve a problem for anybody who will find this question with a similar problem.
I want to fill the screen with a 100 different letters in random positions. On the iPhone I just created a bunch of UILabels set their x and y positions and then used animations to move them about.
On Android it doesn't look like I can add a TextView to my view and specify its X and Y. Is there a way to do this?
View gameView = findViewById(R.id.gameboard);
tv = new TextView(gameView.getContext());
tv.setText("A");
tv.setWidth(w); tv.setHeight(h);
// How to set the X and Y?
EDIT: The solution was to use AbsoluteLayout:
AbsoluteLayout al = (AbsoluteLayout)findViewById(R.id.gb_layout);
tv = new TextView(this);
AbsoluteLayout.LayoutParams params = new AbsoluteLayout.LayoutParams(
AbsoluteLayout.LayoutParams.WRAP_CONTENT,
AbsoluteLayout.LayoutParams.WRAP_CONTENT,10,10);
params.x = 50;
params.y = 50;
al.addView(tv, params);
and to move it base on MotionEvent me:
AbsoluteLayout.LayoutParams p = new AbsoluteLayout.LayoutParams(
AbsoluteLayout.LayoutParams.WRAP_CONTENT,
AbsoluteLayout.LayoutParams.WRAP_CONTENT,(int)me.getX(), (int)me.getY());
mTV.setLayoutParams (p);
I think you are making a game and for this you should look into SurfaceView here is an good example then
1...if you look into code there is onDraw method where you will get the width of screen and height
2...Now taking this width and height as seed for random generator get x and y position.
3...Iterate through point 2 100 times and you will get the desired result
You can use a FrameLayout for this. Set each TextView's background to transparent and add it to the FrameLayout. Make each TextView, and the FrameLayout, fill their parent. The FrameLayout places all its child views at (0,0). To move letters around, just change the top and left padding of the corresponding TextView.
In android positioning child views is the responsibility of the layout class.
You need to create a custom.layout and overide the onLayout method. In this method you can iterate through all the child views calling View.layout(left,top,right,bottom) on each child.
i found this example code whilst trawling the net :
https://gist.github.com/882650
You should also check out view.setTranslationX (and Y) which might be exactly what you need
I'm not entirely sure how you'd go about doing this in code, but you need to use an absolute layout as your root element. (edit: Do not use absolute layout, as it was pointed out that it is deprecated. The closest alternative seems to be RelativeLayout.)
The properties in the XML format for the absolute layout coordinates are layout_x and layout_y.
edit: A little research is saying you need to be using setLayoutParams, but my Eclipse IDE is not working properly, so unfortunately I can't test exactly what you're looking for.
I have a custom view, derived from Button, which I want to position at runtime, relative to another view. Because I don't know that other view's position yet when my view is being inflated (because layouting hasn't started), I leverage the onSizeChanged handler to set my view's position relative to the other view.
In onSizeChanged:
LayoutParams lp = new LayoutParams(this.getMeasuredWidth(), this.getMeasuredHeight());
lp.leftMargin = x;
lp.topMargin = y;
this.setLayoutParams(lp);
forceLayout();
That, however, has no effect. How come?
Some things to consider:
Are you sure this code is being executed?
Have you examined the UI in hierarchyviewer to see if the margins are being set but they are simply not having the visual effect you expect?
Are you sure this isn't better handled just via a RelativeLayout?
Have you tried modifying the existing LayoutParams rather than replacing it?