I am dynamically creating an Editext in my code. i want to position it in my x and y position in my code itself in an absolute layout.
can anyone help me to do this.
Don't use AbsoluteLayout. It's deprecated, and just a bad idea, as it will look differently on screens with different DPIs. Consider using a RelativeLayout instead. If you can give a more specific problem, we can recommend an alternative solution.
Keeping in mind that you shouldn't actually use this, and that using AbsoluteLayout has been indirectly linked to causing cancer, and may also lead to psychosis, here is how you would do this in code assuming you have an AbsoluteLayout with a child EditText in an XML layout:
EditText editText = (EditText)findViewById(R.id.my_edit_text);
AbsoluteLayout.LayoutParams abs_params =
new AbsoluteLayout.LayoutParams(
//width in pixels, or AbsoluteLayout.FILL_PARENT/WRAP_CONTENT
60,
//height in pixels, or AbsoluteLayout.FILL_PARENT/WRAP_CONTENT
30,
//x position with regards to the origin
10,
//y position with regards to the origin
10
);
editText.setLayoutParams(abs_params);
Here are some documentation pages to review:
http://developer.android.com/reference/android/widget/AbsoluteLayout.LayoutParams.html
http://developer.android.com/reference/android/widget/AbsoluteLayout.html
http://developer.android.com/reference/android/widget/EditText.html
I am not responsible for any trauma that may occur as a result of using an AbsoluteLayout. May god have mercy on your soul. :)
Related
I am trying to dynamically create and then move an image in an Android activity. However, the setX() and setY() methods seem to not work correctly. It correctly sets the position of an image when it is first created and placed, but any attempt to update it results in the image being placed in the wrong spot. For instance, the image moves on the following code:
ImageView image;
RelativeLayout layout = (RelativeLayout)findViewById(R.id.activity_this);
if(action == MotionEvent.ACTION_DOWN){
image = new ImageView(MyClass.this);
layout.addView(image, width, height);
image.setX(206);
image.setY(206);
}
else if(action == MotionEvent.ACTION_MOVE){
if(image != null){
image.setX(206);
image.setY(206);
}
}
On ACTION_MOVE the image is moved even though the x and y position values remain the same. The parent of the image remains the same. The size remains the same. If I get the x and y values it will still say 206, but it is not placed at (206, 206) on the activity anymore. I am lost as to why this is happening. I can't find any indication that the image has been altered except for it physically changing location.
Really, this shouldn't be happening. Alternatively, try setting another variable and setting x and y to it, or get x and get y and add a 0 to each one of them for same location.
As stated in Android - Use of view.setX() and setY in api 8, if you have searched, there is another solution that also works even before api 8.
LayoutParams works like this -
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT); //WRAP_CONTENT param can be FILL_PARENT
params.leftMargin = 206; //XCOORD
params.topMargin = 206; //YCOORD
childView.setLayoutParams(params);
There is more information there. I hope this helps
Run into the same issue. View.setLeft(int)/View.setTop(int) worked for me.
Note that since the original post of this answer things changed and on the more recent android versions it may produce unexpected results while it did the trick for me on older versions. So if you are targeting older devices (android 3.0 and below) this may help but for a more generic solution please consider other answers here as well.
From the docs, setTranslationX is:
Sets the horizontal location of this view relative to its left position. This effectively positions the object post-layout, in addition to wherever the object's layout placed it.
And setX is:
Sets the visual x position of this view, in pixels. This is equivalent to setting the translationX property to be the difference between the x value passed in and the current left property.
Thus you can think of setTranlsationX as a relative offset: move 3 pixels left of where you normally would be. And setX is a fixed position: move whatever you have to so that you end up drawing at coordinate X.
Is your activity in full screen mode? If no try to make it to full screen and it should solve your problem.
Pretty late to answer, but if someone else is facing the same problem. This fixed it for me it was the paddings in the layout file:
android:paddingBottom="16dp"
android:paddingLeft="16dp"
android:paddingRight="16dp"
android:paddingTop="16dp"
As someone who actually facing this problem, I have solve this issue by removing any padding in the parentView. The padding seem cause a change in the layout size
I have a TextView. How can I position it by using x and y coordinates?
TextView leftArrow = new TextView(this);
leftArrow.setTypeface(tf);
leftArrow.setText("<");
leftArrow.setTextSize(30);
ll.addView(leftArrow);
you can use view.setX() and view.setY() ,see the following link
http://developer.android.com/reference/android/view/View.html#setX(float)
If you really, really want x,y positioning, use the deprecated AbsoluteLayout -- but read Alternative to AbsoluteLayout in Android? before (it's no good for different screen sizes).
You'll likely fare much better with RelativeLayout depending on your 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 see posts saying that FrameLayout is the alternative, and that I should use margins to position things (this strikes me as wildly counter intuitive, but ok... if it works, I'll take it). However, I can't get it to work, so, I'm looking for assistance.
here's my code
FrameLayout layout = new FrameLayout(this);
layout.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
Button btn = new Button(this);
btn.setBackgroundResource(R.drawable.btn);
FrameLayout.LayoutParams lp = new FrameLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
lp.setMargins(100,100,100,100);
btn.setLayoutParams(lp); //i've tried with and without this line, no change
layout.addView(btn , lp);
The button is drawn at 0,0 no matter what I do. When I change the lp's LayoutParam to FILL_PARENT the button is then stretched to take up the entire screen (which makes sense).
HOW do you get it to draw somewhere else on the screen, irrespective of what else is there?
As always, super grateful in advance.
[EDIT]
It seems my question isn't entirely clear (given the answers) so...
In the code above, the intent is to create a button, pass it to a layout and have it draw at 100,100 on the screen.
I'm aware of the fact that this may mean different things on different devices. I'm ok with that. I simply need a way to, programatically, and at run time, place an item at a SPECIFIC location. I don't want to rely on gravity (or the laws of thermodynamics). I just want to specify a location and have the element appear there :)
As many have pointed out, setting a button at an absolute pixel position on the screen is a really bad idea, and will never work across all of the available android phones. I'm sure there is a better way to achieve the layout you want.
However, to answer the question as asked: to position it at runtime you can use the AbsoluteLayout.LayoutParms.
AbsoluteLayout absoluteLayout = //get absolute layout
Button button = new Button();
AbsoluteLayout.LayoutParms params = absoluteLayout.generateDefaultLayoutParams();
params.x = 100;
params.y = 100;
absoluteLayout.addView(button, params);
A good explanation of the different available layouts is available at
http://mobiforge.com/designing/story/understanding-user-interface-android-part-1-layouts
As I do not completely understand from your description what you're trying to accomplish (do you want to overlay stuff?) maybe the visual examples there can help you in understanding how the layouts work and how to position stuff on them.
What exactly is your final objective with the layout? If all you need is a button -- say, in the center of the screen, you can just make a layout with:
<RelativeLayout
android:xmlns="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center"
>
<Button
android:id="#+id/the_button"
android:layout_width="wrap_content"
android:layout_height = "wrap_content"
android:text="#string/the_button_text"
/>
</RelativeLayout>
AbsoluteLayout is a bad idea because of the large number of screen resolutions you need to support. 100 pixels over on one screen may be halfway, while it may be less than a quarter across the screen on a higher dpi device.
If AbsoluteLayout is deprecated what can I use instead of it?
I've done an app that uses AbsoluteLayout but it doesn't work well with the different screen resolutions. I use because I can set the X and Y position of a button. Can I set the position of a button using another layout?
you can use RelativeLayouts as described here: Set the absolute position of a view
Use RelativeLayout and set left and right margins like
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
lp.leftMargin = x;
lp.topMargin = y;
Consider using FrameLayout. If you set your child gravity to TOP|LEFT then you can use leftMargin and topMargin as the positions. As a bonus you get a few other useful positioning mechanisms by changing the gravity.
I will suggest you guys if you want to have full control over the positions of your views on the screen just to extend ViewGroup and make your own implementation of AbsoluteLayout. The easiest solution will be to use one of the existing Layouts and play with margins, paddings, gravity and so on, but the control is not gonna be so powerful and can cost some problems on the diff device screens.
I would just use a relative layout and set it based off of the other items/edges of the screen. Otherwise your layout appears different on different devices.