Make child of scrollview 1/3 size - android

I have this:
my problem is that i have a Linearlayout wrapper with a scrollview and a Linearlayout inside.. i want every item to be 1/3 of the scrollviews height no matter how many items i put into it..
When i add items to the Linearlayout it is linearlayouts, if i add 3 'items' it is almost fine.. but if i add 8 items, they are all quite small and scrollable. This is what i want, but i want them to have the height of 1/3 of the scrollview/linearlayout.
i want it to align so its as big as the buttons in the 2nd layout i have.
i just cant seems to make it right, hope its just a simple solution i have refused to see.
my code so far:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dip"
android:orientation="vertical"
android:layout_weight="75">
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/scrollView2"
android:layout_gravity="center_horizontal"
android:fillViewport="true"
android:scrollbarSize="5dp"
android:fadeScrollbars="false"
android:layout_marginRight="5dp"
android:layout_marginLeft="5dp"
android:scrollbarThumbVertical="#drawable/custom_scroll_style">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/coats"
android:orientation="vertical"
android:weightSum="75">
</LinearLayout>
</ScrollView>
</LinearLayout>
This is my way of adding 'children' to the layout wrapped by the scrollview
LinearLayout w0 = new LinearLayout(this);
LinearLayout w1 = new LinearLayout(this);
ImageView w2 = new ImageView(this);
TextView w3 = new TextView(this);
TextView w4 = new TextView(this);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT, 25);
params.setMargins(pixelToDp(0),pixelToDp(5),pixelToDp(0),pixelToDp(5));
w0.setLayoutParams(params);
w0.setOrientation(LinearLayout.HORIZONTAL);
w0.setClickable(true);
w0.setBackgroundColor(Color.argb(100, 100, 100, 100));
w0.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
for (int i = 0; i < LLCoats.getChildCount(); i++) {
View vv = LLCoats.getChildAt(i);
vv.setSelected(false);
vv.setBackgroundColor(Color.argb(100, 100, 100, 100));
}
LinearLayout LL = (LinearLayout)LLCoats.getChildAt(i);
TextView TV = (TextView)LL.getChildAt(1);
TV.requestFocus();
if(TV.getError() != null)
{
Snackbar snackbar = Snackbar
.make(findViewById(android.R.id.content), ""+TV.getError(), Snackbar.LENGTH_LONG);
snackbar.show();
}
v.setSelected(true);
v.setBackgroundColor(Color.argb(255,255,255,255));
}
});
LinearLayout.LayoutParams params2 = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT, 2);
w1.setLayoutParams(params2);
w1.setOrientation(LinearLayout.VERTICAL);
w1.setClickable(false);
LinearLayout.LayoutParams params3 = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT,1);
w2.setLayoutParams(params3);
w2.setScaleType(ImageView.ScaleType.FIT_CENTER);
LinearLayout.LayoutParams params4 = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT,1);
w3.setLayoutParams(params4);
w3.setTextSize(25f);
w3.setGravity(Gravity.CENTER);
w3.setClickable(false);
LinearLayout.LayoutParams params5 = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT,1);
w4.setLayoutParams(params5);
w4.setTextSize(45f);
w4.setGravity(Gravity.CENTER);
w4.setClickable(false);
w1.addView(w2);
w1.addView(w3);
w0.addView(w1);
w0.addView(w4);
LLCoats.addView(w0);
UPDATE!:
Now its getting really weird, as i have found a way to calculate the height and add it with:
DisplayMetrics display = this.getResources().getDisplayMetrics();
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, pixelToDp((int)(display.heightPixels* .8 * 0.25)));
This works really well on my PI64 and a ASUS tablet, but my phone makes the height 100% the same as the scrollview, how is happening??

I made it works like this, getting the screen size *.8 because the screen is used as a popup and then 0.25 as items are 1/4 of the screen and in the end -10 for my margins
DisplayMetrics display = this.getResources().getDisplayMetrics();
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, (int)(display.heightPixels* .8 * 0.25 - pixelToDp(10)));
params.setMargins(pixelToDp(0),pixelToDp(5),pixelToDp(0),pixelToDp(5));

Related

how can we divide screen in two equal width in android programatically

i was developing android application but based on the screen sizes element size getting changed.is there is any solutions to divide the screen sizes equally.i am getting different sizes in different screens like 5inches and 3.7 inches
My Code is...
lView.setBackgroundColor(Color.parseColor("#FFFFFF"));
lView.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT));
LinearLayout.LayoutParams layoutParams2 = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, 220);
GradientDrawable gdtitle = new GradientDrawable();
gdtitle.setCornerRadius(5);
ImageView title = new ImageView(Main2Activity.this);
title.setImageResource(R.drawable.logo);
title.setLayoutParams(layoutParams2);
title.setBackgroundDrawable(gdtitle);
lView.setOrientation(LinearLayout.VERTICAL);
lView.addView(title);
GradientDrawable gd = new GradientDrawable();
gd.setColor(Color.parseColor("#FFFFFF")); // Changes this drawbale to use a single color instead of a gradient
gd.setCornerRadius(5);
gd.setStroke(1, 0xFF000000);
int width = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 200, getResources().getDisplayMetrics());
int Height = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 50, getResources().getDisplayMetrics());
TextView uname1 = new TextView(Main2Activity.this);
uname1.setText("Hello , " + Sessionname);
uname1.setGravity(Gravity.CENTER);
uname1.setTextColor(Color.parseColor("#003366"));
uname1.setTextSize(20);
uname1.setWidth(width);
uname1.setLayoutParams(l2);
et1 = new TextView(Main2Activity.this);
et1.setHeight(Height);
et1.setWidth(width);
et1.setTextColor(Color.WHITE);
et1.setHintTextColor(Color.WHITE);
et1.setGravity(Gravity.CENTER);
et1.setHint("Select Date");
et1.setBackgroundDrawable(gd3);
et1.setTextSize(15);
et1.setLayoutParams(l2);
LinearLayout.LayoutParams l4 = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
l4.gravity = Gravity.CENTER;
lHorizontalView1.setOrientation(LinearLayout.HORIZONTAL);
lHorizontalView1.setLayoutParams(l4);
lHorizontalView1.addView(uname1);
lHorizontalView1.addView(et1);
lView.addView(lHorizontalView1);
GradientDrawable gd4 = new GradientDrawable();
gd4.setCornerRadius(30);
gd4.setColor(Color.parseColor("#5CB85C"));
gd4.setStroke(3, 0xFFFFFFFF);
Intime = new TextView(Main2Activity.this);
Intime.setHint("In Time");
Intime.setTextColor(Color.WHITE);
Intime.setHintTextColor(Color.WHITE);
Intime.setTextSize(15);
Intime.setHeight(Height);
Intime.setWidth(width);
Intime.setGravity(Gravity.CENTER);
Intime.setLayoutParams(l2);
Intime.setBackgroundDrawable(gd4);
lView.setOrientation(LinearLayout.HORIZONTAL);
Outtime = new TextView(Main2Activity.this);
Outtime.setHint("Out Time");
Outtime.setTextSize(15);
Outtime.setHeight(Height);
Outtime.setWidth(width);
Outtime.setGravity(Gravity.CENTER);
Outtime.setTextColor(Color.WHITE);
Outtime.setHintTextColor(Color.WHITE);
Outtime.setLayoutParams(l2);
Outtime.setBackgroundDrawable(gd4);
lView.setOrientation(LinearLayout.HORIZONTAL);
LinearLayout lHorizontalView = new LinearLayout(Main2Activity.this);
LinearLayout.LayoutParams l3 = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
l3.gravity = Gravity.CENTER;
lHorizontalView.setOrientation(LinearLayout.HORIZONTAL);
lHorizontalView.setLayoutParams(l3);
lHorizontalView.addView(Intime);
lHorizontalView.addView(Outtime);
lView.addView(lHorizontalView);
You should use DisplayMetrics
DisplayMetrics metrics = getResources().getDisplayMetrics();
int DeviceTotalWidth = metrics.widthPixels;
int DeviceTotalHeight = metrics.heightPixels;
Example
RelativeLayout rl_firstObj=(RelativeLayout)findViewById(R.id.rl_first);
rl_firstObj.getLayoutParams().width=DeviceTotalWidth*50/100;
RelativeLayout rl_secondObj=(RelativeLayout)findViewById(R.id.rl_second);
rl_secondObj.getLayoutParams().width=DeviceTotalWidth*50/100;
For buttons parent view use linearlayout and set
param.weight = 1
for each button.
Using LinearLayouts and assigning weights to the views inside it will divide the seperate the views with the same ratio no matter the screen size. Here, I've assigned weight 1 to both the views, thus they will be shown side my side divided equally on the screen.
<LinearLayout
android:id="#+id/linearLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:weightSum="2">
<Button
android:id="#+id/button1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Button1"
android:textSize="16sp" />
<Button
android:id="#+id/button2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Button2"
android:textSize="16sp" />
</LinearLayout>
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT, 1);
textView1.setLayoutParams(layoutParams);
LinearLayout.LayoutParams layoutParams1 = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT, 1);
textView2.setLayoutParams(layoutParams1);
I have taken a linear layout with parameter weight as 1 and divide that in two views.Here i used textviews u can take any view in your code.Please let me know if face any issue

Issue with layout weight in Android LinearLayout

The way my program works is that I take user input for 3 items, create a horizontal LinearLayout programmatically with those three items. Then place that LinearLayout inside a RelativeLayout that is defined in XML.
User can keep adding these 3-item horizontal LLs to the bottom of the RelativeLayout, which acts as a list in this case.
I am specifying a weight in the setLayoutParams method for each TextView object
TextView quantityText = new TextView(MainActivity.this);
quantityText.setLayoutParams(new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT, 1f));
quantityText.setText("" + currentIngredientMeasurementQuantity);
TextView typeText = new TextView(MainActivity.this);
typeText.setLayoutParams(new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT, 1f));
typeText.setText(currentIngredientMeasurementType);
TextView nameText = new TextView(MainActivity.this);
nameText.setLayoutParams(new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT, 4f));
nameText.setText(currentIngredientName);
LinearLayout ll = new LinearLayout(MainActivity.this);
ll.setId(View.generateViewId());
RelativeLayout relativeLayout = (RelativeLayout) findViewById(R.id.ingredient_list_relativelayout);
RelativeLayout.LayoutParams relativeParams = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
//Find last member of RelativeLayout
View lastIngredient = (View) relativeLayout.getChildAt(totalIngredientQuantity - 1);
relativeParams.addRule(RelativeLayout.BELOW, lastIngredient.getId());
relativeParams.setMargins(0, 0, 0, 0);
ll.addView(quantityText);
ll.addView(typeText);
ll.addView(nameText);
relativeLayout.addView(ll, relativeParams);
The problem is, the TextViews aren't being displayed the way I'd like them to. I'd like each of the three items to line up vertically, like a proper list.
Instead, I get something like this. You can see that the length of the strings is somehow affecting the way the layout is stored/displayed.
I know it's a big block of code, but any help would be appreciated!
Try this:-
Create a custom layout insteadof creating controls as dynamically
custom.xml
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:weightSum="90">
<TextView
android:id="#+id/textview1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="30"
android:gravity="center"
android:text="text1"/>
<TextView
android:id="#+id/textview2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:gravity="center"
android:layout_weight="30"
android:text="text2"/>
<TextView
android:id="#+id/textview3"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:gravity="center"
android:layout_weight="30"
android:text="text3"/>
</LinearLayout>
Add this view in RelativeLayout
View view = factory.inflate(R.layout.custom, null);
relativeLayout.addView(view);
You need to set the layout_width property to 0 for layout weight property to work well. I made a couple to changes to your code to make it work well for me.
Try this,
private void addNewRow(float currentIngredientMeasurementQuantity, String currentIngredientMeasurementType, String currentIngredientName) {
TextView quantityText = new TextView(MainActivity.this);
quantityText.setLayoutParams(new LinearLayout.LayoutParams(0, LinearLayout.LayoutParams.WRAP_CONTENT, 1f));
quantityText.setText("" + currentIngredientMeasurementQuantity);
TextView typeText = new TextView(MainActivity.this);
typeText.setLayoutParams(new LinearLayout.LayoutParams(0, LinearLayout.LayoutParams.WRAP_CONTENT, 1f));
typeText.setText(currentIngredientMeasurementType);
TextView nameText = new TextView(MainActivity.this);
nameText.setLayoutParams(new LinearLayout.LayoutParams(0, LinearLayout.LayoutParams.WRAP_CONTENT, 4f));
nameText.setText(currentIngredientName);
LinearLayout ll = new LinearLayout(MainActivity.this);
if (Build.VERSION.SDK_INT > Build.VERSION_CODES.JELLY_BEAN) {
ll.setId(View.generateViewId());
} else {
ll.setId(totalIngredientQuantity);
}
RelativeLayout relativeLayout = (RelativeLayout) findViewById(R.id.ingredient_list_relativelayout);
RelativeLayout.LayoutParams relativeParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
//Find last member of RelativeLayout
View lastIngredient = (View) relativeLayout.getChildAt(totalIngredientQuantity - 1);
if (lastIngredient != null) {
relativeParams.addRule(RelativeLayout.BELOW, lastIngredient.getId());
relativeParams.setMargins(0, 0, 0, 0);
}
ll.addView(quantityText);
ll.addView(typeText);
ll.addView(nameText);
relativeLayout.addView(ll, relativeParams);
}
You have to set the Gravity to Left (or Start) on your TextViews before adding them to the LinearLayout, just like this:
LinearLayout.LayoutParams name_lp = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT
);
name_lp.gravity = Gravity.LEFT;
nameText.setLayoutParams(name_lp);
Try to tell your your Textviews to use all the available space they can get. So they will fill the whole LinearLayout.
Instead of WRAP_CONTENT use MATCH_PARENT or FILL_PARENT
In XML you could use 0dip like described here: In android layouts what is the effect/meaning of layout_height="0dip"
Another idea would be to use TableLayout or GridLayout http://developer.android.com/reference/android/widget/GridLayout.html
http://developer.android.com/reference/android/widget/TableLayout.html
Have Fun

Android: TextView ignores Gravity and Margin

Here is my code:
//define tablerows. each table row has max 3 buttons
LinearLayout llRow1 = (LinearLayout)findViewById(R.id.llRow1); llRow1.removeAllViews();
float scale = getResources().getDisplayMetrics().density;
int padding_5dp = (int) (5 * scale + 0.5f);
//Define FrameLayout
FrameLayout flTmp = new FrameLayout(this);
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(FrameLayout.LayoutParams.FILL_PARENT, FrameLayout.LayoutParams.FILL_PARENT, 1f);
lp.setMargins(0, 0, padding_5dp, padding_5dp);
flTmp.setLayoutParams(lp);
//Add dynamically TextView
TextView tvTmp = new TextView(this);
tvTmp.setText("Test");
LinearLayout.LayoutParams tvPara=new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT );
tvPara.gravity = Gravity.BOTTOM | Gravity.LEFT;
tvPara.setMargins(padding_5dp, 0, 0, 0);
tvTmp.setLayoutParams(tvPara);
//Add dynamically Buttons for juice
ImageButton btnTmp = new ImageButton(this);
[...]
//Add Button and TextView to FrameLayout
flTmp.addView(btnTmp);
flTmp.addView(tvTmp);
llRow1.addView(flTmp);
What i try to do is create an imagebutton dynamically with a textview description like following xml code:
<FrameLayout>
<ImageButton android:background="#null" android:id="#+id/button_x" android:layout_width="fill_parent" android:layout_height="fill_parent" android:scaleType="fitXY" android:src="#drawable/button_graphic"></ImageButton>
<TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:clickable="false" android:text="TEST TEST"></TextView>
</FrameLayout>
works fine with my code, but the textview ignores margin and gravity parameters.
its located on upper left corner without margins.
Any hint?
The LayoutParams you use need to be of the type of parent that the child is contained in. You're FrameLayout contains the TextView, but you're assigning a LinearLayout.LayoutParams reference to it. Make it a FrameLayout.LayoutParams or change to a LinearLayout. If I had to guess, it's silently catching this error and just ignoring it.

position views programmatically in Android

I have a method that creates 3 textViews in a vertical LinearLayout:
public LinearLayout CreateLayout() {
LinearLayout aLap = new LinearLayout(this);
LinearLayout.LayoutParams TextParams = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
TextView txtName = new TextView(this);
txtName.setText(result1);
txtName.setLayoutParams(TextParams);
txtName.setGravity(Gravity.CENTER);
txtName.setPadding(20, 8, 20, 4);
txtName.setTextSize(20);
txtName.setTextColor(Color.parseColor("#000000"));
TextView txtTime = new TextView(this);
txtTime.setText(result2);
txtTime.setLayoutParams(TextParams);
txtTime.setGravity(Gravity.CENTER);
txtTime.setTextSize(20);
txtTime.setPadding(20, 4, 10, 4);
txtTime.setTextColor(Color.parseColor("#000000"));
android.view.ViewGroup.LayoutParams Params = new ViewGroup.LayoutParams(
android.view.ViewGroup.LayoutParams.MATCH_PARENT, 150);
TextView rep = new TextView(this);
String Rep = sharedPrefs.getString("R_PREFS", "2");
rep.setLayoutParams(TextParams);
rep.setGravity(Gravity.CENTER);
rep.setTextSize(20);
rep.setPadding(20, 4, 10, 4);
rep.setTextColor(Color.parseColor("#000000"));
rep.setText("Test: " + Rep);
aLap.setLayoutParams(Params);
aLap.setBackgroundResource(R.drawable.bg);
aLap.setOrientation(LinearLayout.VERTICAL);
aLap.setPadding(3, 3, 3, 3);
aLap.addView(txtName);
aLap.addView(txtTime);
aLap.addView(rep);
return aLap;
}
I use a vertical layout since I want the textViews to be placed under each other. Now I want an ImageView to be placed to the right of this textViews and centered vertically. Is that possible?
You can not achieve desired output with a single LinearLayout. I would suggest you use RelativeLayout. It's more flexible. You can add more child-views to RelativeLayout at desired positions by adding rules to the views.
Check this post on how to add rules when working with Relative Layout at runtime: How to lay out Views in RelativeLayout programmatically?.
<LinearLayout orientation="horizontal">
<LinearLayout orientation="vertical">
<TextView view1/>
<TextView view1/>
<TextView view1/>
</LinearLayout>
<ImageView height=fill_parent scaleType="center"/>
</LinearLayout>
Is more or less what you want I think

android Layout weight programmatically

i have hard-coded the layout_weight on layout xml.but now i want to give the layout_weight and weightSum from java code.
How we do that from our class?
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:weightSum="25" >
<TextView
android:id="#+id/textView1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="5"
android:background="#F51215"
android:paddingLeft="5dip"
android:text="5" />
<TextView
android:id="#+id/textView2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="20"
android:background="#1AC92B"
android:paddingLeft="5dip"
android:text="20" />
</LinearLayout>
Something like this:
......
LinearLayout layout = (LinearLayout)findViewById(YOUR_LAYOT_ID);
layout.setWeightSum(25f);
LinearLayout.LayoutParams lParams = (LinearLayout.LayoutParams) layout.getLayoutParams(); //or create new LayoutParams...
lParams.weight = 0.5f;
.......
someView.setLayoutParams(lParams);
.......
//set as like this below for different view set different float value.
myview.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT,5f));
I just wanted to add an example of how to use the weightsum with e.g a TableRow with TextViews inside:
TableRow row = new TableRow(this);
TableRow.LayoutParams params1 = new TableRow.LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT, 10f);
row.setLayoutParams(params1);
row.setPadding(10, 10, 10, 10);
row.setBackgroundColor(R.color.black);
TextView tv = new TextView(this);
TableRow.LayoutParams params2 = new TableRow.LayoutParams(0, LayoutParams.WRAP_CONTENT, 6f);
tv.setLayoutParams(params2);
tv.setTextSize(30);
tv.setBackgroundResource(R.color.white);
TextView tv2 = new TextView(this);
TableRow.LayoutParams params3 = new TableRow.LayoutParams(0, LayoutParams.MATCH_PARENT, 2f);
tv2.setLayoutParams(params3);
tv2.setBackgroundResource(R.color.green);
TextView tv3 = new TextView(this);
TableRow.LayoutParams params4 = new TableRow.LayoutParams(0, LayoutParams.MATCH_PARENT, 2f);
tv3.setLayoutParams(params4);
tv3.setBackgroundResource(R.color.blue);
Produces the TableRow underneath. The parent whose weightsum is 10, and the children's widths are then equal to 60%, 20% and 20% of the width. The height is here determined by the TextView, tv, which has a height of 30. Hope it helps someone. :-)
If You want to change parent weight programmatically and it's child property as it is then use below
Here's how you set it.
LinearLayout yourLayout=(LinearLayout)findViewById(R.id.container);
yourLayout.setWeightSum(0.6f);
Dynamically Generate TextView with Weight using LinearLayout
LinearLayout lin_hoizontal = new LinearLayout(context);
lin_hoizontal.setOrientation(LinearLayout.HORIZONTAL);
lin_hoizontal.setLayoutParams(new android.widget.LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT, 10f));
lin_hoizontal.setPadding((int) context.getResources().getDimension(R.dimen.d_8), (int) context.getResources().getDimension(R.dimen.d_2), (int) context.getResources().getDimension(R.dimen.d_8), (int) context.getResources().getDimension(R.dimen.d_2));
android.widget.LinearLayout.LayoutParams params_label = new android.widget.LinearLayout.LayoutParams(0, LinearLayout.LayoutParams.WRAP_CONTENT, 2.5f);
TextView txt_label = new TextView(context);
txt_label.setTextColor(context.getResources().getColor(R.color.listing_header_txt_color));//your text color
txt_label.setTextSize(TypedValue.COMPLEX_UNIT_PX, context.getResources().getDimension(R.dimen.d_13));
txt_label.setTypeface(Typeface.create("sans-serif-medium", Typeface.NORMAL));
txt_label.setLayoutParams(params_label);
txt_label.setPadding(0, 0, (int) context.getResources().getDimension(R.dimen.d_2), 0);
txt_label.setText("Label");
android.widget.LinearLayout.LayoutParams params_value = new android.widget.LinearLayout.LayoutParams(0, LinearLayout.LayoutParams.WRAP_CONTENT, 7.5f);
TextView txt_value = new TextView(context);
txt_value.setTextColor(context.getResources().getColor(R.color.listing_header_txt_color));
txt_value.setTextSize(TypedValue.COMPLEX_UNIT_PX, context.getResources().getDimension(R.dimen.d_13));
txt_value.setTypeface(Typeface.create("sans-serif-light", Typeface.NORMAL));
txt_value.setLayoutParams(params_value);
txt_value.setPadding((int) context.getResources().getDimension(R.dimen.d_2), 0, 0, 0);
txt_value.setText("Value");
lin_hoizontal.addView(txt_label);
lin_hoizontal.addView(txt_value);
lin_hoizontal.addView(lin_main);
I like Ajii's answer or:
((LinearLayout)view).setWeightSum(n);
and for individual view weight:
((LinearLayout.LayoutParams)view.getLayoutParams()).weight = n;
of course you can do.. LinearLayout view = findViewById(R.id.view_name);
(and drop the dynamic casting (LinearLayout))
or substitute...……….. findViewById(R.id.view_name) for view above.
These both worked for me.
And if you want value in dimens file:
<item name="new_weight" format="float" type="dimen">(your number)</item>
and: float n = getResources().getFloat(R.dimen.new_weight);
I'm sure you already know most of this but... I was a newbie once and I always appreciated the extra descriptions.

Categories

Resources