How to set layout_weight attribute dynamically from code?
You can pass it in as part of the LinearLayout.LayoutParams
constructor:
LinearLayout.LayoutParams param = new LinearLayout.LayoutParams(
LayoutParams.MATCH_PARENT,
LayoutParams.MATCH_PARENT,
1.0f
);
YOUR_VIEW.setLayoutParams(param);
The last parameter is the weight.
How to set weight attribute dynamically from Kotlin code?
You need to remember that layout parameters are passed from child to parent view. You probably want to change layout_weight
of some child of LinearLayout.
In your case you probably want to do something like this
(orangeLineFiveStars.layoutParams as LinearLayout.LayoutParams).weight = newOrangeWeight
(whiteLineFiveStars.layoutParams as LinearLayout.LayoutParams).weight = newWhiteWeight
It may be necessary to also request re-layout
parentLinearLayout.requestLayout()
How to set layout_weight programmatically?
It's under the respective Spec
.
cbButtonLayoutParams.RowSpec.weight = 1; //might be Weight in C#
cbButtonLayoutParams.ColumnSpec.weight = 1;
I'm not sure if you want the row weight or the column weight, but that's how you do it for both.
EDIT:
I believe that InvokeSpec()
is equivalent to spec()
, which means the second parameter should be the weight, as long as both parameters are Int32
.
Also, layout_weight
is usually only for when the parent of that element is a LinearLayout. Are you sure that the CheckBox is actually being added to the GridLayout and not a LinearLayout child?
layout_weight programmatically explainedv
It depends on your view if you want to split the view horizontally between them you can use something like this.
TextView secondTV = findViewById(R.id.secondTextView);
TextView firstTV = findViewById(R.id.firstTextView);
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(0, ViewGroup.LayoutParams.MATCH_PARENT, 3);
LinearLayout.LayoutParams layoutParams1 = new LinearLayout.LayoutParams(0, ViewGroup.LayoutParams.MATCH_PARENT, 1);
firstTV.setLayoutParams(layoutParams);
secondTV.setLayoutParams(layoutParams1);
And your layout looks like this
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<TextView
android:id="@+id/firstTextView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Hello" />
<TextView
android:id="@+id/secondTextView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/icon"
android:text="Hello" />
</LinearLayout>
but if you want to split the view vertically you can use something like this.
TextView secondTV = findViewById(R.id.secondTextView);
TextView firstTV = findViewById(R.id.firstTextView);
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, 0, 3);
LinearLayout.LayoutParams layoutParams1 = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, 0, 1);
firstTV.setLayoutParams(layoutParams);
secondTV.setLayoutParams(layoutParams1);
And your layout looks like this
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="@+id/firstTextView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Hello" />
<TextView
android:id="@+id/secondTextView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/icon"
android:text="Hello" />
</LinearLayout>
android Layout weight programmatically
//set as like this below for different view set different float value.
myview.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT,5f));
programmatically code for layout_weight for ImageView in kotlin
Use This:
var params = LinearLayout.LayoutParams(
LayoutParams.MATCH_PARENT,
LayoutParams.MATCH_PARENT,
1.0f
)
imageView1.setLayoutParams(params)
in this line: var param = LinearLayout.LayoutParams(
We are using LinearLayout
because your image view is inside a LinearLayout
Related Topics
How to Find and Run the Keytool
Android Asynctask for Long Running Operations
Android. Fragment Getactivity() Sometimes Returns Null
How to Build the Android Sdk with Hidden and Internal APIs Available
How to Maintain Fragment State When Added to the Back Stack
Decimal Separator Comma (',') with Numberdecimal Inputtype in Edittext
Determine Whether JSON Is a JSONobject or JSONarray
Android, Canvas: How to Clear (Delete Contents Of) a Canvas (= Bitmaps), Living in a Surfaceview
Notificationcompat.Builder Deprecated in Android O
Android HTML Imagegetter as Asynctask
How to Show Dependencies Tree in Android Studio
Android - Detect Phone Unlock Event, Not Screen On
Android Sample Bluetooth Code to Send a Simple String via Bluetooth
Custom Filtering in Android Using Arrayadapter
How to Configure a Static Ip Address, Netmask, Gateway Programmatically on Android 3.X or 4.X
Error Android Emulator Gets Killed in Android Studio