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();
Related
I need to change the size of the buttons programmatically.
I read some answers and I do the next to change the button size.
button.setLayoutParams(new RelativeLayout.LayoutParams(150, 50));
This code change the size correctly but the problem is that the button not stay in the position I want, it moves the button to the top left corner.
I need to change the button size but I don't want to change de position.
How can I fix this?
You have to use the existing RelativeLayout Rules as well,i.e whether the item is center aligned,align parent left etc. Or you can make use of the existing Params and modify the width and height as follows the RelativeLayout rules will not be modified.
RelativeLayout.LayoutParams layoutParams= (RelativeLayout.LayoutParams) button.getLayoutParams();
layoutParams.width=150;
layoutParams.height=50;
RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) button.getLayoutParams();
params.width = 500;
params.height = 500;
button.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'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);
If you use an AbsoluteLayout (I know that it is deprecated, but it was the only way to solve my problem) you can give the childViews the tag android:layout_x and android:layout_y to set their absolute position within the AbsoluteLayout.
However I don't want to set these information in the xml, because I only know them at runtime. So how can I set these parameters at runtime programmatically? I don't see any method on the View like view.setLayoutX(int x) or something.
Here is my XML, which works fine, when I set the layout_x and layout_y values:
<?xml version="1.0" encoding="utf-8"?>
<AbsoluteLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/myLayout"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageView
android:src="#drawable/myImageView"
android:layout_width="1298px"
android:layout_height="945px"
android:layout_x="0px"
android:layout_y="0px" />
<Button
android:id="#+id/myButton1"
android:text="23"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_x="50px"
android:layout_y="300px"
android:tag="23"/>
<Button
android:id="#+id/myButton2"
android:text="48"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_x="50px"
android:layout_y="300px"
android:tag="48"/>
</AbsoluteLayout>
In fact, I don't want to set any button within the xml anymore, but rather retrieve some information via remote and add buttons depending on that information.
Here is the part the code I'm using so in my onCreateMethod to add these buttons:
for (MyRemoteObject remoteObject: list) {
Button button = new Button(this);
button.setOnClickListener (listener);
button.setTag(remoteObject.id);
button.setText(remoteObject.id);
// button.setLayoutX(remoteObject.x) ????
// button.setLayoutY(remoteObject.y) ????
myLayout.addView(button);
}
Use the version of addView that takes LayoutParams:
LayoutParams params = mLayout.generateLayoutParams();
params.x = remoteObject.x;
params.y = remoteObject.y;
mLayout.addView(button, params);
In response to your comment on Mayra's answer, I had a similar issue with RelativeLayout instead of AbsoluteLayout. The solution was to use a similar method and cast it as your layout type. Something like this:
LayoutParams params = (android.widget.RelativeLayout.LayoutParams) this.generateDefaultLayoutParams();
params.height = 360;
params.width = 360;
params.addRule(CENTER_IN_PARENT);
this.addView(board, params);
It took me forever to figure this out so I wanted to post it here for someone else if they needed it.
I had just the same problem but found a somewhat different solution.
int width = 100, height = 50, x = 10, y = 20;
Button button = new Button(this);
AbsoluteLayout myLayout = (AbsoluteLayout)findViewById(R.id.myLayout);
myLayout.add(button, new AbsoluteLayout.LayoutParams(width, height, x, y));
And if you find out what to use for "fill_parent" (hint try -1) then you may use those constants for width and height.
I don't know why, but when moving top and left edges the Android keeps the right and bottom edges in same place. As I could not change the width and height properly (the image was disappearing), I've used this:
LayoutParams layoutParams=new LayoutParams(width, height);
layoutParams.leftMargin = newLeft;
layoutParams.topMargin = newTop;
imageView.setLayoutParams(layoutParams);
I don't know if it's the best way, but worked for me. I've tested it in 2.2+ and works fine!
You must import android.widget.LinearLayout.LayoutParams; because the default is ViewGroup's LayoutParams (by Davit T)
the above answers are right.but this one would be more perfect
AbsoluteLayout.LayoutParams layoutParams = new AbsoluteLayout.LayoutParams(AbsoluteLayout.LayoutParams.WRAP_CONTENT,AbsoluteLayout.LayoutParams.WRAP_CONTENT,DragLayer.int left_location,int top_location);
layout.addView(view,layoutParams)
I need to create a map with items on it (the map consists of a drawable object, which represents a room) and I thought about using buttons with background images for the items so that they are clickable.
I guess the AbsoluteLayout fits here the best, but unfortunately it's deprecated.
What layout would you recommend me for this kind of application ? Is there another layout which supports X/Y coordinates ?
Relative Layout supports X,Y coords -- and that would probably be the best, since you can set the layout relative to the map instead of the screen.
Cool. THanks so much !
Here is the code, if somebody needs it too.
RelativeLayout RL = new RelativeLayout(this);
RL.addView(V, RelativeLayout.LayoutParams.FILL_PARENT, RelativeLayout.LayoutParams.FILL_PARENT);
Button t = new Button(this);
t.setText("text");
t.setBackgroundResource(R.drawable.test);
int top = 200; //y
int left = 100; //x
RelativeLayout.LayoutParams LP= new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
LP.setMargins(left, top, 0, 0);
t.setLayoutParams(LP);
RL.addView(t);
setContentView(RL);