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));
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 the layout_weight of a TextView in code?
Firstly, the LayoutParams
constructors use pixels (according to the documentation).
To set the layout_weight
programmatically, you need to use LinearLayout.LayoutParams
. ViewGroup.LayoutParams
does not have a third argument, as you pointer out, but the former does.
Try this:
create.setLayoutParams(new LinearLayout.LayoutParams(0, 30, 1.0f));
ViewGroup.LayoutParams
is a set of layout parameters for any View
within any kind of layout (since these layouts are all ViewGroup
s). When you have a specific layout, for example a LinearLayout
, you can use LinearLayout.LayoutParams
to get access to things specific to that type of layout. In this case, the layout_weight
is particular to LinearLayout
s, therefore you must be using the LinearLayout.LayoutParams
to access this weight parameter.
You should also remove the layout_weight="0"
from the TextView
in your XML. If this still doesn't fix it, give your LinearLayout
a background color and see if it's even visible at all, then edit your OP with your findings and any changed/new code.
Set button and textView weight programmatically
I found another way around to accomplish what I wanted, so instead of using weight I used the screen width in order to get a relative width. and to add margin, I just added an empty row before the row that contains the button and text. Here is the full code for the interested.
private fun addRow(content: Editable) {
val tl = findViewById<TableLayout>(R.id.main_table)
val margin = TableRow(this)
val tr_head = TableRow(this)
margin.id = View.generateViewId()
tr_head.id = View.generateViewId()
val tabParams = TableRow.LayoutParams(
TableRow.LayoutParams.MATCH_PARENT,
84
)
tr_head.setLayoutParams(tabParams)
val label = TextView(this)
label.id = View.generateViewId()
label.text = content
label.setTextColor(Color.WHITE)
val displayMetrics = DisplayMetrics()
windowManager.defaultDisplay.getMetrics(displayMetrics)
label.width = (displayMetrics.widthPixels / 3)*2
label.setPadding(50, 0,0,0)
tr_head.addView(label)
val btn = Button(this)
btn.id = View.generateViewId()
btn.width = (displayMetrics.widthPixels / 3) - 50
btn.setBackgroundColor(Color.RED)
btn.setText("Remove")
btn.setOnClickListener {
tl.removeView(tr_head)
tl.removeView(margin)
}
label.id = View.generateViewId()
val filler = TextView(this)
filler.id = View.generateViewId()
margin.addView(filler)
tr_head.addView(btn)
tl.addView(
margin, TableLayout.LayoutParams(
TableRow.LayoutParams.MATCH_PARENT,
20
)
)
tl.addView(
tr_head, tabParams
)
}
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()
Programmatically create a textview with 0dp and layout weight 1
What I should probably mention, don't know if it makes a difference, is the TableView is within a HorizontalScrollView.
It is a logical error to tell a child view inside a scrolling container to match_parent
or fill the available space in any way. A scrolling container, like ScrollView
or HorizontalScrollView
measures its one-and-only child view as UNSPECIFIED
so that the child can grow beyond the parent bound in that one direction (and, thus, be scrollable).
The child of a scrolling container is not given what the "available space" would be for it to fill, and operations like layout weight have no effect if the parent's dimension is not well-defined.
The row should fit in the width of the screen, if the data is smaller than the screen, but if the row won't fit, then the view will be horizontally scrollable.
The functionality you are looking for is fillViewport
(docs link), which tells the scrolling container to force the child to match it's size if the measured width (in the horizontal case) is less than the parent. Use this in place of applying a weight.
Setting the layout_weight of the TextView under the TableRow
Thank you everyone!
Finally, I was able to solve my problem by referencing to the following posts.
android: two issues using Tablerow+TextView in Tablelayout
How to make a TableLayout from XML using programmable way ?
Refer below for my working code.
This is where the dynamic adding of rows is done.
TableLayout tl = (TableLayout)findViewById(R.id.table_layout);
private void addTableRow(int resIdTitle, String strValue){
LayoutInflater inflater = getLayoutInflater();
TableRow tr = (TableRow)inflater.inflate(R.layout.table_row, tl, false);
// Add First Column
TextView tvTitle = (TextView)tr.findViewById(R.id.tvTitle);
tvTitle.setText(resIdTitle);
// Add the 3rd Column
TextView tvValue = (TextView)tr.findViewById(R.id.tvValue);
tvValue.setText(strValue);
tl.addView(tr);
}
This is the table_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1">
<TableLayout
android:id="@+id/table_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:title="@string/strGetParamTitle"
android:scrollbars="vertical"
android:shrinkColumns="0">
</TableLayout>
</ScrollView>
And finally the table_row.xml. I've set all the layout_params here.
<?xml version="1.0" encoding="utf-8"?>
<TableRow xmlns:android="http://schemas.android.com/apk/res/android">
<TextView
android:id="@+id/tvTitle"
android:paddingRight="3dip"
android:layout_width="0px"
android:layout_weight="0.35"/>
<TextView android:text=":"
android:layout_width="0px"
android:layout_weight="0.05"/>
<TextView
android:id="@+id/tvValue"
android:paddingLeft="3dip"
android:layout_width="0px"
android:layout_weight="0.6"
/>
</TableRow>
adding the views dynamically with layout weight property in Android
Problem is with your layout params. You need to use like below
private void addLayout(String textView1Text, String textView2Text, String checkBoxText) {
LinearLayout.LayoutParams param = new LinearLayout.LayoutParams( LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT, 1.0f);
mLinearLayout.setLayoutParams(param);
TextView tv1 = new TextView(this);
tv1.setText(textView1Text);
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(0, 100);
lp.weight = 0.1f;
tv1.setLayoutParams(lp);
mLinearLayout.addView(tv1);
TextView tv2 = new TextView(this);
tv2.setText(textView2Text);
LinearLayout.LayoutParams lp1 = new LinearLayout.LayoutParams(0, 100);
lp1.weight = 0.6f;
tv2.setLayoutParams(lp1);
mLinearLayout.addView(tv2);
CheckBox cb = new CheckBox(this);
cb.setText(textView2Text);
LinearLayout.LayoutParams lp2 = new LinearLayout.LayoutParams(0, 100);
lp2.weight = 0.3f;
cb.setLayoutParams(lp2);
mLinearLayout.addView(cb);
}
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>
Related Topics
How to Customize Item Background and Item Text Color Inside Navigationview
How to Set Default App Launcher Programmatically
How to Programmatically Force Bluetooth Low Energy Service Discovery on Android Without Using Cache
Programmatically Getting the Gateway and Subnet Mask Details
In Android Edittext, How to Force Writing Uppercase
What Is Android Keystore File, and What Is It Used For
Programmatically Relaunch/Recreate an Activity
How to Use Onsaveinstancestate() and Onrestoreinstancestate()
Android Webview "Tel:" Links Show Web Page Not Found
Android: Resize Only Parts of View with Soft Keyboard on Screen
How to Implement Cookie Handling on Android Using Okhttp
Android - Get Text Out of Webview
Android Adb Devices Unauthorized
How to Get the Current Page Number of a Viewpager for Android
How to Make My Layout Scroll Both Horizontally and Vertically
Retaining Position in Listview After Calling Notifydatasetchanged
Converting Yuv->Rgb(Image Processing)->Yuv During Onpreviewframe in Android