tags:

views:

19

answers:

1

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.

A: 

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

kcoppock
I want to postion it with its x and y coordinate in java code. i want to know how to do that. Thankyou for your help.
Jim
I have added an example for you.
kcoppock
Thankyou for your valuable information. Can you please convert the above code for RelativeLayout.please........
Jim
There is no direct equivalent in RelativeLayout. By definition, items are placed into the layout relative to one another. So you could place the EditText aligned to the top left of the view, with a top and left margin of 10dp. Alternately you can place it directly beneath another View by using layout_below in the XML layout. You'll have to be more specific with your question for a RelativeLayout suggestion.
kcoppock
Actually i want to place my editext in for eg: x = 50 and y = 100 How will I do that using relativeLayout
Jim
You can't. It's just not the way to do things on Android. Think about it this way: You have an ldpi device at as low as 100 dpi, and an hdpi device at 240 dpi. On the low dpi device, if you put your view 100px from the top, it will appear 1 inch from the top of the layout. Whereas the same layout (100px from the top) on the high dpi device will be only .41 inches from the top of the screen, leading to a very different experience for each device it's being seen on. Also, let's say you put something 500px from the top. On a hdpi device, it will show, but it won't even be visible on 480x320.
kcoppock