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.
Related
I got 4 Imageviews in a Tablerow. They are scaled through the layout_weight command. I need to write in this Imageviews now. Is there any possibility to do this without having to use alignparent meaning using a relative layout?
This is my code at the Moment:
TableRow.LayoutParams params = new TableRow.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT, 1f);
TableRow.LayoutParams params1 = new TableRow.LayoutParams(0,ViewGroup.LayoutParams.WRAP_CONTENT,1.2f);
TableRow.LayoutParams params2 = new TableRow.LayoutParams(0, ViewGroup.LayoutParams.WRAP_CONTENT, 2.4f);
TableRow.LayoutParams params3 = new TableRow.LayoutParams(0, ViewGroup.LayoutParams.WRAP_CONTENT, 3.6f);
TableRow.LayoutParams params4 = new TableRow.LayoutParams(0, ViewGroup.LayoutParams.WRAP_CONTENT, 4.8f);
TableRow tableRow = new TableRow(this);
tableRow.setLayoutParams(params);
ImageView rank = new ImageView(this);
rank.setAdjustViewBounds(true);
rank.setLayoutParams(params1);
rank.setImageResource(R.drawable.whitebutton);
Layout[i] = new ImageView(this);
Layout[i].setLayoutParams(params2);
Layout[i].setImageResource(R.drawable.width2);
Layout[i].setAdjustViewBounds(true);
Layout[i+10] = new ImageView(this);
Layout[i+10].setLayoutParams(params3);
Layout[i+10].setImageResource(R.drawable.width3);
Layout[i+10].setAdjustViewBounds(true);
Layout[i+20] = new ImageView(this);
Layout[i+20].setLayoutParams(params4);
Layout[i+20].setImageResource(R.drawable.width4);
Layout[i+20].setAdjustViewBounds(true);
tableRow.addView(rank);
tableRow.addView(Layout[i]);
tableRow.addView(Layout[i+10]);
tableRow.addView(Layout[i+20]);
t.addView(tableRow);
This is the Interface I want to create:
well, in addition the same sized 4 pictures with the actual highscore in it
use a TextView instead of ImageView
<TextView
android:layout_width="300dp"
android:layout_height="200dp"
android:id="#+id/b1"
android:text="hai every one !"
android:textColor="#ffff"
android:textSize="20sp"
android:gravity="center"
android:background="#EF5350"
android:padding="20dp"
android:drawableLeft="#mipmap/ic_launcher"
android:drawableTop="#mipmap/ic_launcher"
android:drawableRight="#mipmap/ic_launcher"
android:drawableBottom="#mipmap/ic_launcher"
android:layout_centerInParent="true"/>
here you can use the Text, Background as well as 4 drawables.You can arrange the drawables using the paddingLeft, paddingTop, paddingRight and paddingBottom.
oR
use RelativeLayout instead of ImageView.Add the TextView inside the RelativeLayout
i prefer the first approach.
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
I tryning to get a LinearLayout in a ScrollView like this:
The greenspace should be wrap_content and the red one should take the remaining space.
But this is my Result:
This is my Code:
foreach(Ele el : elements) {}
LinearLayout layout = new LinearLayout(getActivity());
LayoutParams layout_parm = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
layout.setOrientation(LinearLayout.HORIZONTAL);
layout.setLayoutParams(layout_parm);
TextView tv = new TextView(getActivity());
tv.setLayoutParams( new LayoutParams(0, LayoutParams.WRAP_CONTENT, 1) );
tv.setBackgroundColor(getActivity().getResources().getColor((R.color.red)));
tv.setTextSize(TypedValue.COMPLEX_UNIT_SP, 16);
tv.setText("Name...");
tv.setPadding(0, 0, 0, getDP(5));
layout.addView(tv);
TextView iView = new TextView(getActivity());
iView.setText("OTO");
iView.setTextSize(TypedValue.COMPLEX_UNIT_SP, 16);
iView.setBackgroundColor(getActivity().getResources().getColor((R.color.green)));
iView.setLayoutParams( new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, 0) );
layout.addView(iView);
frg_layout.addView(layout);
}
I am so confused! Maybe you can help me to find out my fail...
Thank you!
The Parent-ViewGroup:
<LinearLayout
android:id="#+id/frg_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="top"
android:gravity="top"
android:orientation="vertical" />
you change the layout orientation
LinearLayout layout = new LinearLayout(getActivity());
LayoutParams layout_parm = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
layout.setOrientation(LinearLayout.VERTICAL);
layout.setLayoutParams(layout_parm);
uset this type in xml
<LinearLayout
android:id="#+id/frg_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="#+id/TextView1"
android:layout_width="250dp"
android:layout_height="60dp"/>
<TextView
android:id="#+id/textview2"
android:layout_width="250dp"
android:layout_height="60dp"
/>
</linearlayout>
I think you've mostly got it right, just need a few minor tweaks:
for each(Ele el:elements){
LinearLayout layout = new LinearLayout(getActivity());
LayoutParams layout_parm = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
layout.setOrientation(LinearLayout.HORIZONTAL);
layout.setLayoutParams(layout_parm);
TextView tv = new TextView(getActivity());
//adjust the last number here to make the first block wider or larger as you want
tv.setLayoutParams(new LayoutParams(0, LayoutParams.WRAP_CONTENT, 2));
tv.setBackgroundColor(getActivity().getResources().getColor((R.color.red)));
tv.setTextSize(TypedValue.COMPLEX_UNIT_SP, 16);
tv.setText("Name...");
tv.setPadding(0, 0, 0, getDP(5));
layout.addView(tv);
TextView iView = new TextView(getActivity());
iView.setText("OTO");
iView.setTextSize(TypedValue.COMPLEX_UNIT_SP, 16);
iView.setBackgroundColor(getActivity().getResources().getColor((R.color.green)));
//adjust your layout param to be a weight instead of wrap_content
iView.setLayoutParams(new LayoutParams(0, LayoutParams.WRAP_CONTENT, 1));
layout.addView(iView);
frg_layout.addView(layout);
}
See if that works! An alternative would be to build an xml layout which I think is generally cleaner, but programatically it should work roughly the same way.
Using LayoutInflater with xml will bring the solutuion!
Thank you!
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.
I am trying to add textview dynamically to the right of the textView , but textView is not getting displayed.
The statement "params2.addRule(RelativeLayout.RIGHT_OF, 1);" in the below code is creating a problem if i remove that line the textview is getting displayed at random position.
Need to implement this xml dynamically
<TextView
android:id="#+id/stationnameVal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:text="KANYAKUMARI (CAPE)"
android:textColor="#color/black"
android:textSize="12sp" />
<TextView
android:id="#+id/arrivaltimeVal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_marginLeft="45dp"
android:layout_toRightOf="#+id/stationnameVal"
android:text="SRC"
android:textColor="#color/black"
android:textSize="12sp" />
Code is :
RelativeLayout rel = new RelativeLayout(this);
// first TextView
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
TextView stateName = new TextView(DisplayActivity.this);
stateName.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT));
params.setMargins(0, 5, 0, 0);
stateName.setText("KANYAKUMARI (CAPE)");
stateName.setId(1);
stateName.setTextColor(R.color.black);
stateName.setTextSize(12);
rel.addView(stateName, params);
// Adding Second TextView
RelativeLayout.LayoutParams params2 = new RelativeLayout.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
TextView arr = new TextView(DisplayActivity.this);
arr.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT));
arr.setText("SRC");
arr.setTextColor(R.color.white);
arr.setTextSize(12);
params2.addRule(RelativeLayout.ALIGN_PARENT_TOP, stateName.getId());
params2.addRule(RelativeLayout.RIGHT_OF, 1);
params2.setMargins(45, 0, 0, 0);
rel.addView(arr, params2);
How about changing the "LayoutParams.FILL_PARENT" to "LayoutParams.WRAP_CONTENT"?
Regards
Ziteng Chen
change this:
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
or this:
rel.addView(stateName, params);
to
rel.addView(stateName);
and also change this:
params2.addRule(RelativeLayout.ALIGN_PARENT_TOP, stateName.getId());
to
params2.addRule(RelativeLayout.ALIGN_PARENT_TOP);
and do the same for param:
params.addRule(RelativeLayout.ALIGN_PARENT_TOP);
otherwise remove it, because Kanyakumari and src are not aligned properly.
actually do this to align them:
params2.addRule(RelativeLayout.ALIGN_TOP, 1);