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.
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:
followed by
addView
with newly constructedLayoutParams
before adding the button, you should change the orientation of your layout
To change the
layout_gravity
attribute for an existing view:The button is contained in a
FrameLayout
which has agravity
attribute corresponding to thelayout_gravity
XML attribute. Documentation link.Maybe I misunderstand you, but here it is:
http://developer.android.com/reference/android/widget/TextView.html#setGravity(int)
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.
as opposed to
and this call, despite not producing compiler error is actually wrong: