Positioning a TextView in Android - android

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.

Related

Android Studio - getLocationOnScreen

why there ist no counterpart to getLocationOnScreen? The intuitive way would be setLocationOnScreen... I have an Image View and want to get these coordinates to give the same coordinates to another Image View.
The mentioned intuitive way would be:
int [] location = new int [2];
imageView1.getLocationOnScreen(location);
imageView2.setLocationOnScreen(location);
But this set command is not available. This would be the easiest thing :( Is there any counterpart?
PS: my Image has no margins...
Thanks for help
Because position on screen is controlled by the layout that the ImageView is in, not the ImageView itself. If you need a layout that allows you to position subviews at specific coordinates, try a FrameLayout.

Android - How to draw a letter at a specific point?

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.

How to poistion edittext in program in android?

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. :)

What is the alternative to AbsoluteLayout in Android?

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.

Alternative to AbsoluteLayout in Android?

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.

Categories

Resources