I'm trying to add a few images to a LinearLayout programmatically.
the xml of the image looks like this:
<ImageView
android:id="#+id/iv_card27"
android:layout_width="50dp"
android:layout_height="match_parent"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:scaleType="centerInside"
app:srcCompat="#drawable/back" />
and this is my the java code I tried already:
ImageView card = new ImageView(this);
card.getLayoutParams().width = 50;
card.getLayoutParams().height = ViewGroup.LayoutParams.MATCH_PARENT;
card.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
card.setImageResource(R.drawable.back);
bottomRow.addView(card);
However I have been struggling to add the Margins, Also Im worried about the width which I set to 50. But it should actually be 50dp. How can I accomplish this?
You can use ViewGroup.MarginLayoutParams , RelativeLayout.LayoutParams or LinearLayout.LayoutParams to set layout margin.
LayoutParams params = new LayoutParams(
LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT);
params.setMargins(left, top, right, bottom);
card.setLayoutParams(params);
https://developer.android.com/reference/android/view/ViewGroup.MarginLayoutParams
You have to arrange margins using particular code:
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
params.setMargins(xxx,xxx,xxx,xxx);
card.setLayoutParams(params);
for setting margin to your ImageView:
MarginLayoutParams marginParams = new MarginLayoutParams(image.getLayoutParams());
marginParams.setMargins(left_margin, top_margin, right_margin, bottom_margin);
RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(marginParams);
card.setLayoutParams(layoutParams);
Related
I have LinearLayout generated in xml, this one:
<LinearLayout
android:id="#+id/available_themes_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:gravity="center"
android:paddingTop="6dp"
android:paddingBottom="6dp"/>
I'm trying to add some custom views to this layout and place them evenly along layout:
BiColorCircleView lightOrangeCircleView = new BiColorCircleView(getCurrentActivity());
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT,
1.0f);
params.gravity = Gravity.CENTER;
lightOrangeCircleView.setLayoutParams(params);
availableThemesLayout.addView(lightOrangeCircleView);
But at the end I achieve that only weight is applied and my views is aligned to left side. Thought, if weight is not set - gravity works nice.
Could somebody point me to way of solving this problem?
LinearLayout availableThemesLayout = new LinearLayout(null);
ImageView lightOrangeCircleView = new ImageView(null);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
0,
LinearLayout.LayoutParams.WRAP_CONTENT,
1.0f);
FrameLayout frameLayout = new FrameLayout(null);
availableThemesLayout.addView(frameLayout, params);
FrameLayout.LayoutParams frameParams = new FrameLayout.LayoutParams(
FrameLayout.LayoutParams.WRAP_CONTENT,
FrameLayout.LayoutParams.WRAP_CONTENT);
frameParams.gravity = Gravity.CENTER;
frameLayout.addView(lightOrangeCircleView, params);
Try this.
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
How do I change this attribute of .xml file dynamically?:
android:layout_marginTop="60dp"
I use RelativeLayout
you can set layout params on your view and set margins on your params http://developer.android.com/reference/android/view/ViewGroup.LayoutParams.html
for example, this is how you can do it for a LinearLayout
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
layoutParams.setMargins(left, top, right, bottom);
and then just setLayoutParams
For a RelativeLayout you can just change LinearLayout with RelativeLayout :)
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 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.