How to set layout_gravity programmatically?
Java
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.MATCH_PARENT);
params.weight = 1.0f;
params.gravity = Gravity.TOP;
button.setLayoutParams(params);
Kotlin
val params = LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT
).apply {
weight = 1.0f
gravity = Gravity.TOP
}
For gravity values and how to set gravity check Gravity.
Basically, you should choose the LayoutParams
depending on the parent. It can be RelativeLayout
, LinearLayout
etc...
Programmatically set layout_gravity for a FrameLayout's Child?
The layout_gravity
attribute lands on the FrameLayout.LayoutParams
, not on the view itself. You'll need something like:
mCameraPreview = new CameraPreview(this, recordMode);
FrameLayout preview = (FrameLayout) findViewById(R.id.camera_preview_wrapper);
FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(
FrameLayout.LayoutParams.MATCH_PARENT,
FrameLayout.LayoutParams.MATCH_PARENT,
Gravity.BOTTOM);
preview.addView(mCameraPreview, params);
Android - Set Layout_Gravity programmatically for LinearLayout
Do somethings like this :
LayoutParams lp = new LayoutParams();
lp.gravity= Gravity.CENTER_HORIZONTAL;
myImg.setLayoutParams(lp);
UPDATE :
Another way to do this :
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
params.weight = 1.0f;
params.gravity = Gravity.CENTER;
imageView.setLayoutParams(params);
How to set both gravity and layout gravity of a LinearLayout programatically
Short answer
Set gravity
linearLayout.setGravity(Gravity.CENTER);
Set layout gravity
// the LinearLayout's parent is a FrameLayout
FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(400, 400);
params.gravity = Gravity.TOP|Gravity.RIGHT;
linearLayout.setLayoutParams(params);
Background
Previously, I have explained the difference between 'gravity' and `layout_gravity' for views within a layout.
Setting the gravity
of a LinearLayout
itself changes the location of the views within it. Setting the layout_gravity of a LinearLayout
changes how the LinearLayout
is arranged within its parent layout.
This image shows a LinearLayout
(brown) within a FrameLayout
(white). The LinearLayout
's gravity
is set to center_horizontal
and its layout_gravity
is set to right|bottom
.
Here is the xml:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout
android:id="@+id/llExample"
android:layout_width="200dp"
android:layout_height="200dp"
android:layout_gravity="right|bottom"
android:background="#e3e2ad"
android:gravity="center_horizontal"
android:orientation="vertical" >
<TextView
android:id="@+id/textView1"
android:layout_width="100dp"
android:layout_height="50dp"
android:background="#bcf5b1"
android:text="TextView 1" />
<TextView
android:id="@+id/textView1"
android:layout_width="100dp"
android:layout_height="50dp"
android:background="#aacaff"
android:text="TextView 2" />
</LinearLayout>
</FrameLayout>
Changing things programmatically
The following code shows how to change both the gravity
and the layout_gravity
of the LinearLayout
.
public class LinearLayoutGravity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.linear_layout_gravity);
// Change the gravity (not layout_gravity) of the LinearLayout
LinearLayout ll = (LinearLayout) findViewById(R.id.llExample);
ll.setGravity(Gravity.CENTER);
// Change the layout_gravity (not gravity) of the LinearLayout
FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(400, 400);
params.gravity = Gravity.TOP|Gravity.RIGHT;
ll.setLayoutParams(params);
}
}
And here is the result:
See also
RelativeLayout
andLinearLayout
with gravity: What is the effect?
Set gravity for LinearLayout programmatically
Remember that gravity
sets the positioning of a view's children, while layout_gravity
sets the positioning of a view within its parent. So in your case, you want to set the gravity of the LinearLayout, which can be done via member methods. You should also set the orientation.
Your run() method should look something like:
public void run(){
layout = new LinearLayout(activity);
layout.setGravity(Gravity.BOTTOM);
layout.setOrientation(LinearLayout.VERTICAL);
layout.addView(adView);
LinearLayout.LayoutParams lllp = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
activity.addContentView(layout, lllp);
}
Android : Set gravity of a LinearLayout programmatically
I think, you are looking for
LinearLayout one = new LinearLayout(this);
one.setGravity(Gravity.BOTTOM | Gravity.CENTER); //to show LinearLayout gravity
Button myButton1 = new Button(this);
one.addView(myButton1,
new LinearLayout.LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT,
0));
myButton1.setText("First");
myButton1.setGravity(Gravity.CENTER); //to show text gravity in button
Button myButton2 = new Button(this);
one.addView(myButton2,
new LinearLayout.LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT,
0));
myButton2.setText("Second");
myButton2.setGravity(Gravity.CENTER); //to show text gravity
setContentView(one);
icicle
is sometimes used as the name of the parameter.It's just a
placeholder for one of the formal parameters onCreate takes one Bundle parameter. It's up to you what to call it.
Some properties of LinearLayout https://stackoverflow.com/a/19065951/2826147
Related Topics
Updating Android UI Using Threads
Letting Webview on Android Work with Prefers-Color-Scheme: Dark
Customize Android Facebook Login Button
Android: Bitmapfactory.Decodestream() Out of Memory with a 400Kb File with 2Mb Free Heap
Starting an Activity from a Service After Home Button Pressed Without the 5 Seconds Delay
Recognizing Multiple Keywords Using Pocketsphinx
Why Is My Button Text Forced to All Caps on Lollipop
Non-Scrollable Listview Inside Scrollview
How to Save Custom Arraylist on Android Screen Rotate
Best Place to Close Database Connection
Cannot Get Folderid That I Just Created on Google Drive
Android Custom Dropdown/Popup Menu
How Permission Can Be Checked at Runtime Without Throwing Securityexception
How to Create a Notification with Notificationcompat.Builder
How to Start an Activity from a Service
What Is Exactly a "Clock Tick" in the Context of Android CPU Usage
How Much Support Is There for -Webkit-Overflow-Scrolling:Touch