views:

503

answers:

2

Hi folks,

I would like to know if is there a way to call android:layout_gravity property from a java method. I didn't found any method in Android documentation to do it. This is the picture of the layout I want to implement:

alt text

I know to do it through xml, as following:

<FrameLayout
    xlmns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content">

      <Button
         android:layout_gravity="left|center_vertical"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:text="<" />


      <Button
         android:layout_gravity="right|center_vertical"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:text=">" />
</FrameLayout>

But in my situation, I need to do it through Java code, because I'll implement another layout views dinamically. To avoid merging xml layout with Java code, I would prefer make all layout using Java.

Can you help me?

Thanks.

André Leitão

+1  A: 

Maybe I misunderstand you, but here it is:

http://developer.android.com/reference/android/widget/TextView.html#setGravity(int)

Roflcoptr
Unfortunately that's the gravity for the text within the TextView, not the gravity for the layout of the TextView itself.
Steve H
you can also use it for the button. the button inherits it
Roflcoptr
Yes, Steve. =( Unfortunately that's the gravity for the text, the same of android:gravity.
André Leitão
+7  A: 

Well as far as I understand you looking for [this][1] It is for FrameLayout for other layout see appopriate LayoutParams classes. For setting gravity like in your xml use something like:
FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(WRAP_CONTENT,WRAP_CONTENT,Gravity.LEFT | Gravity.CENTRAL_VERTICAL)
followed by addView with newly constructed LayoutParams

[1]: http://developer.android.com/reference/android/widget/FrameLayout.LayoutParams.html#FrameLayout.LayoutParams(int, int, int)

Nikolay Ivanov
Nice!!! Thank you Nikolay! =)
André Leitão