1

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);
  • You're going to need to add some LayoutParams. Check out this question: http://stackoverflow.com/questions/2305395/laying-out-views-in-relativelayout-programmatically – Brian D Feb 10 '11 at 04:28
  • http://stackoverflow.com/questions/3268068/android-custom-layout I found this question, which recently was answered with a video link pointing to a custom layout, but no explanation of how to actually specify the childs position ( layout params? ) –  Feb 10 '11 at 04:48

4 Answers4

0

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

Noel Walters
  • 1,843
  • 1
  • 14
  • 20
0

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

ingsaurabh
  • 15,249
  • 7
  • 52
  • 81
  • I'm hoping I can stick to using the TextView since it would really cut down on the amount of work I have to do. Setting the X and Y in an extra line would be a lot easier for me than figuring out a surfaceview and how to draw. –  Feb 10 '11 at 04:34
  • You could define 100 textviews in XML, then update them by getting handles to them in the activity :D -- but I wouldn't dare put this as a real answer, cause that sounds pretty nasty to me. – Brian D Feb 10 '11 at 04:40
  • In the first go you will find it bit difficult using Surfaceview but it is more efficient and clean than using TextViews consider of creating 100 objects textview and the memory they will take and in long run you will find that this approach is not cutting down time but increasing and sluggishness in your game with SurfaceView one can easily achieve >50 FPS while with other views its merely 30-40 FPS – ingsaurabh Feb 10 '11 at 04:40
  • This whole thing was 30 lines of code on the iPhone - so if I can just set TextView X and Y I'll be fine. –  Feb 10 '11 at 04:54
  • you can't compare two platforms iphone & android both are different things it doesnt means the thing that are feasible in one are also in the other and short answer for your problem is you can't AFAIK – ingsaurabh Feb 10 '11 at 05:00
  • I can't stand Objective-C, but what I want to do here is so intuitive I don't know why its not supported. You lose a lot doing your own drawing. –  Feb 10 '11 at 05:05
0

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.

Ted Hopp
  • 232,168
  • 48
  • 399
  • 521
  • Hmm interesting idea, I may try but I think this ruins one point of using the TextView which was to get the ontouch for free. Unless you can determine that the actual text was touched vs the view. –  Feb 10 '11 at 04:59
  • It's not much more complicated. Wrap each TextView in its own container (a LinearLayout or similar view) and set the padding on the container instead of the TextView. Set the size of the TextView to wrap_content and the size of the container to fill_parent. – Ted Hopp Feb 10 '11 at 18:25
-1

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.

John Chadwick
  • 3,193
  • 19
  • 19
  • 2
    absolute layout has been deprecated in Android and everything that is possible from absolute layout is possible from the other layouts that are there. – achie Feb 10 '11 at 04:30
  • Oh, is that so? I wasn't aware. – John Chadwick Feb 10 '11 at 04:31
  • @archie - I'm pretty sure that one can do things with AbsoluteLayout that cannot be done with other stock layouts. (For one thing, it can make the layout hard to maintain, which the other layouts allegedly don't do. Hah!) To do exactly what OP wants, AbsoluteLayout would work, but not anything else out of the box. (A custom layout, of course, can basically re-create AbsoluteLayout. Nobody's deprecated them.) – Ted Hopp Feb 10 '11 at 04:55
  • Whats the chance they actually remove AbsoluteLayout and I have to completely rewrite the app? :) –  Feb 10 '11 at 05:01
  • This is what I'm going with unless someone changes my mind. I'll just copy AbsoluteLayout as a custom layout if it goes away. Still not straightforward to set X and Y though. –  Feb 10 '11 at 05:06