Set View height to 0dp and weight to 1 programatically - android

When creating GridView via xml I use the below code, but how could I achieve that programmatically during runtime?
android:layout_height="0dp"
android:layout_weight="1"
Thanks.

use this
GridView YOUR_GRID_VIEW =(GridView )findViewById(R.id.GridView )
LinearLayout.LayoutParams param = new LinearLayout.LayoutParams(
/*width*/ ViewGroup.LayoutParams.MATCH_PARENT,
/*height*/ 0,
/*weight*/ 1.0f
);
YOUR_GRID_VIEW.setLayoutParams(param);
LayoutParams.MATCH_PARENT is for width, 0 for Height and 1.0f is for weight

If you have layout_weight in xml, that means, that the parent layout of GridView is LinearLayout.
Having a reference to a GridView, that is already attached to view hierarchy, you can simply get LayoutParams of that view, cast it to LinearLayout.LayoutParams and perform necessary changes:
GridView gridView = ...;
LinearLayout.LayoutParams params = gridView.getLayoutParams();
params.weight = 1;
params.height = 0;
gridView.setLayoutParams(params);
Otherwise, if you are constructing GridView from scratch, you should create LinearLayout.LayoutParams on your own:
LinearLayout linearLayout = ...;
GridView gridView = new GridView(context);
LinearLayout.LayoutParams params =
new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,
/* height */ 0,
/* weight */ 1);
linearLayout.addView(gridView, params);

Related

I programmatically created a TextView in TableLayout, it automatically sets the height and width

I programmatically created a TextView in TableLayout, it automatically sets the height and width.
Can I change the height and width of the TextView manually inside the TableLayout?
Use
TableLayout.LayoutParams params = new TableLayout.LayoutParams();
params.height = HEIGHT;
params.width = WIDTH;
textView.setLayoutParams(params);
same for other parameters like margin, padding etc
Use
LayoutParams lParams = new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT, 1);
lParams.setMargins(leftMargin, topMargin, rightMargin, bottomMargin);
You can set the height and width programmatically using the following code.
TableRow.LayoutParams param = new TableRow.LayoutParams();
paramColumn1.height = TableRow.LayoutParams.WRAP_CONTENT;
paramColumn1.width = TableRow.LayoutParams.WRAP_CONTENT;
textView1.setLayoutParams(param);
You can also set other properties using this way.

Programmatically LinearLayout Weight

I'm trying to develop similar to grid but using LinearLayout. I would like to have 3 images and exact bottom text after images in single row.
What I have tried:
LinearLayout layout = new LinearLayout(context);
layout.setOrientation(LinearLayout.VERTICAL);
ImageView icon = new ImageView(context);
Item item = getItem(page, index);
Log.e("tag", item.getDrawable());
imageLoader.displayImage(item.getDrawable(), icon, R.drawable.ic_launcher);
icon.setPadding(15, 15, 15, 15);
layout.addView(icon);
TextView label = new TextView(context);
label.setTag("text");
label.setText(item.getName());
label.setTextColor(Color.BLACK);
label.setGravity(Gravity.TOP | Gravity.CENTER_HORIZONTAL);
label.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
layout.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
layout.setWeightSum(3f);
layout.addView(label);
I've a view method which returns Viewso I return return layout; at the end of the method.
Here I 've given weight 3 button this is not working for me. And code show more then 3 images in row with text but would like to have weight 3 images and bottom text .
LinearLayout.LayoutParams param = new LinearLayout.LayoutParams(
LayoutParams.MATCH_PARENT,
LayoutParams.MATCH_PARENT, 1.0f);
or
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.MATCH_PARENT);
params.weight = 1.0f;
Button button = new Button(this);
button.setLayoutParams(params);
I would define a TableLayout with as many TableRows as you need. On each of those TableRows, I'd add a LinearLayout with VERTICAL orientation consisting of the two Views you need: ImageView and TextView.
This is the LinearLayout where you should set a weight of 1 (to all of them). You will have to get the screen's width and see whether the new LinearLayout to be added still fits the current row. If not, simply start a new TableRow.
To get the width of the screen you can use this:
final Display display = getWindowManager().getDefaultDisplay();
final Point size = new Point();
display.getSize(size);
final WindowManager.LayoutParams params = getWindow().getAttributes();
The screen's width might be accessed via the params.width attribute, just compare it with your LinearLayout's .getWidth() method and keep an incremental account (a float) of the current TableRow's width to help you determine if the current item should be placed in an existing row or a new one.
Programmtically setting Linear Layout weight
LinearLayout.LayoutParams param = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT, 1.0f);

Weight inside Scrollview

I'm having trouble calculating the weight inside of an scroll view on my android app. I want the app's content to resize proportionally to the screen size. How can I calculate the weight inside a scroll view?
Here some of my code:
LinearLayout layout_principal = (LinearLayout) findViewById(R.id.layout_items);// is horizontal layout and layout of Scrollview
LinearLayout layout_titulo_opcion = new LinearLayout(this);
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.FILL_PARENT,
0,
1f);
layout_titulo_opcion.setLayoutParams(layoutParams);
LinearLayout layout_titulo_opcion2 = new LinearLayout(this);
LinearLayout.LayoutParams layoutParams2 = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.FILL_PARENT,
0,
1f);
layout_titulo_opcion2.setLayoutParams(layoutParams2);
layout_principal.addView(layout_titulo_opcion);
layout_principal.addView(layout_titulo_opcion2);
To get/set the weight, use this:
LinearLayout.LayoutParams mylp = (LinearLayout.LayoutParams)myLayout.getLayoutParams();
int myWeight = mylp.weight; // get weight
mylp.weight = 4; // set weight
To set the weight sum, use this:
myLayout.setWeightSum(10);

How to set a button's parameters programmatically

I'm trying to add a bunch of buttons to a layout like this:
for( int i = 0; i < 10; i++ ) {
Button button = new Button( this );
button.setText( "" + i );
( ( LinearLayout )dialog.findViewById( R.id.Buttons ) ).addView( button );
}
My problem is how do I do this programmatically to all the buttons:
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:textSize="32dip" />
I've been looking at LayoutParams but it doesn't look complete. Like how do I set the textSize to 32 dip?
Set your attributes using the following code:
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT);
button.setLayoutParams(params);
button.setGravity(Gravity.CENTER_HORIZONTAL);
button.setTextSize(32);
If you want to specify the text size units use:
button.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 32);
LayoutParams relates to the parent ViewGroup which will contain the view. So in your case it's a LinearLayout so you need to create parameters for that one. Here's what I'm talking about:
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
lp.weight = 1f;
Button button = new Button(this);
button.setLayoutParams(lp);
button.setText("" + i);
((LinearLayout)dialog.findViewById(R.id.Buttons)).addView(button);
Use LayoutParams for the height, width and gravity with
LinearLayout.LayoutParams (int width, int height)
where you can use WRAP_CONTENT for the ints.
Then there are Button.setGravity() and Button.setTextSize() for the last two.
Hope this helps.
You'd use a LayoutParams object for the layout settings, and to set the text size use setTextSize() from the Button class.
You can set the gravity with setGravity() too.
TextSize is not inside Layout params. To set textSize you have to
button.setTextSize(32);

How to set layout_weight attribute dynamically from code?

How can I set the value for the attribute layout_weight for button in android dynamically from java 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.
Use LinearLayout.LayoutParams:
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.MATCH_PARENT);
params.weight = 1.0f;
Button button = new Button(this);
button.setLayoutParams(params);
EDIT: Ah, Erich's answer is easier!
If you already define your view in your layout(xml) file, only want to change the weight programmatically, this way is better
LinearLayout.LayoutParams params = (LinearLayout.LayoutParams)
mButton.getLayoutParams();
params.weight = 1.0f;
mButton.setLayoutParams(params);
new a LayoutParams overwrites other params defined in you xml file like margins, or you need to specify all of them in LayoutParams.
If the constructor with width, height and weight is not working, try using the constructor with width and height. And then manually set the weight.
And if you want the width to be set according to the weight, set width as 0 in the constructor. Same applies for height.
Below code works for me.
LinearLayout.LayoutParams childParam1 = new LinearLayout.LayoutParams(0,LinearLayout.LayoutParams.MATCH_PARENT);
childParam1.weight = 0.3f;
child1.setLayoutParams(childParam1);
LinearLayout.LayoutParams childParam2 = new LinearLayout.LayoutParams(0,LinearLayout.LayoutParams.MATCH_PARENT);
childParam2.weight = 0.7f;
child2.setLayoutParams(childParam2);
parent.setWeightSum(1f);
parent.addView(child1);
parent.addView(child2);
If layoutparams is already defined (in XML or dynamically), Here's a one liner:
((LinearLayout.LayoutParams) mView.getLayoutParams()).weight = 1;
If I someone looking for answer, use this:
LinearLayout.LayoutParams lay = (LinearLayout.LayoutParams) myLayout.getLayoutParams();
lay.weight = 0.5;
If you are initializing your layout from xml file, this will be much more convenient than providing new layout parameters for Linear Layout.
Any of LinearLayout.LayoutParams and TableLayout.LayoutParams worked for me, for buttons the right one is TableRow.LayoutParams. That is:
TableRow.LayoutParams buttonParams = new TableRow.LayoutParams(
TableRow.LayoutParams.MATCH_PARENT,
TableRow.LayoutParams.WRAP_CONTENT, 1f);
About using MATCH_PARENT or WRAP_CONTENT be the same.
If you have already defined your view in layout(xml) file and only want to change the weight pro grammatically, then then creating new LayoutParams overwrites other params defined in you xml file.
So first you should use "getLayoutParams" and then setLayoutParams
LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) mButton.getLayoutParams();
params.weight = 4f;
mButton.setLayoutParams(params);
I have done my task by adding this line of code!
LinearLayout.LayoutParams mylayout = (LinearLayout.LayoutParams)
myview.getLayoutParams();
mylayout.weight = 2;
myview.setLayoutParams(mylayout);
Using Kotlin you can do something like:
import android.content.Context
import android.support.v4.content.ContextCompat
import android.support.v7.widget.CardView
import android.widget.*
import android.widget.LinearLayout
class RespondTo : CardView {
constructor(context: Context) : super(context) {
init(context)
}
private fun init(context: Context) {
val parent = LinearLayout(context)
parent.apply {
layoutParams = LinearLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT,
FrameLayout.LayoutParams.WRAP_CONTENT, 1.0f).apply {
orientation = LinearLayout.HORIZONTAL
addView(EditText(context).apply {
id = generateViewId()
layoutParams = LinearLayout.LayoutParams(0,
LinearLayout.LayoutParams.WRAP_CONTENT, 0.9f).apply {
}
})
addView(ImageButton(context).apply({
layoutParams = LinearLayout.LayoutParams(0,
LinearLayout.LayoutParams.WRAP_CONTENT, 0.1f)
background = null
setImageDrawable(ContextCompat.getDrawable(context, R.drawable.ic_save_black_24px))
id = generateViewId()
layoutParams = RelativeLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT).apply {
addRule(RelativeLayout.ALIGN_PARENT_RIGHT)
// addRule(RelativeLayout.LEFT_OF, myImageButton.id)
}
}))
}
}
this.addView(parent)
}
}
if you want to set weight to LinearLayout then use code mentioned below or set weight to Relative Layout then replace it with Relative Layout
LinearLayout.LayoutParams param = new LinearLayout.LayoutParams(
LayoutParams.MATCH_PARENT,
LayoutParams.MATCH_PARENT,
1.0f
);
YOUR_VIEW.setLayoutParams(param);
You can try this approach.
First, you get the LayoutParams associated with this Button. Perform a type conversion.
LinearLayout.LayoutParams linearLayoutParams = (LinearLayout.LayoutParams) button.getLayoutParams();
Then assign the required value of the "weight" parameter.
linearLayoutParams.weight = 5;
Redraw the layout.
button.requestLayout();

Categories

Resources