21

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:

http://www.anddev.org/resources/image/2234

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 dynamically. To avoid merging XML layout with Java code, I would prefer make all layout using Java.

Yi Jiang
  • 49,435
  • 16
  • 136
  • 136
André Leitão
  • 240
  • 1
  • 3
  • 9

6 Answers6

30

Well as far as I understand you looking for this It is for FrameLayout for other layout see appropriate LayoutParams classes. For setting gravity like in your XML use something like:

FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT,Gravity.LEFT | Gravity.CENTER_VERTICAL)

followed by addView with newly constructed LayoutParams

BlaShadow
  • 11,075
  • 7
  • 39
  • 60
Nikolay Ivanov
  • 8,897
  • 3
  • 31
  • 34
2

Watchout! This wouldn't work with LinearLayout though. Because LinearLayout.LayoutParams(...) constructor is different from FrameLayout.LayoutParams(...) constructor. The third parameter is not gravity but weight.

     LinearLayout.LayoutParams(int width, int height, float weight)

as opposed to

     FrameLayout.LayoutParams(int width, int height, int gravity)

and this call, despite not producing compiler error is actually wrong:

lparams = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, Gravity.LEFT);
Dimitry K
  • 2,236
  • 1
  • 28
  • 37
  • Although you cant set the gravity via the constructor but the gravity field of the LinearLayout.LayoutParams is public so you can use the gravity from there –  Dec 10 '14 at 14:34
2

Maybe I misunderstand you, but here it is:

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

1
button.setBackgroundResource(R.drawable.sound_on);
        button.setText("On");
        button.setGravity(Gravity.CENTER_VERTICAL| Gravity.RIGHT);
        button.invalidate();
all-ok
  • 315
  • 1
  • 9
  • layout_gravity is not the same thing as gravity. Gravity is the gravity of your children. Layout gravity is you telling your parent layout what you want your gravity to be. – MinceMan Feb 26 '14 at 20:06
1

To change the layout_gravity attribute for an existing view:

Button button = (Button) findViewById(R.id.some_button);
FrameLayout.LayoutParams params;
params = (LayoutParams) button.getLayoutParams();

params.gravity = Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL;
button.setLayoutParams(params);

The button is contained in a FrameLayout which has a gravity attribute corresponding to the layout_gravity XML attribute. Documentation link.

Lekensteyn
  • 64,486
  • 22
  • 159
  • 192
-2

before adding the button, you should change the orientation of your layout

yourbutton.setGravity(Gravity.RIGHT);
LayoutParams layout = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, Gravity.RIGHT);
layout.gravity = Gravity.RIGHT;
yourbutton.setLayoutParams(layout);


yourlayout.setGravity(Gravity.RIGHT);
yourlayout.addView(yourbutton);
Nick Volynkin
  • 14,023
  • 6
  • 43
  • 67