Change android:layout_marginTop dynamically - android

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 :)

Related

Set margins and other layout properties of ImageView programmatically

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);

Programmatically add views to LinearLayout with both gravity and weight

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.

layout_below and weightsum of LinearLayout Programmatically

I am creating LinearLayout in RelativeLayout programmatically. What I am trying to do is to assign the weigtsum in LinearLayout and want to set the layout_above attribute as well. The problem is weightSum is available in LinearLayout.LayoutParams and layout_above is available in RelativeLayout.LayoutParams. Currently,, I am doing
LinearLayout ll_menu_code = new LinearLayout(this);
ll_menu_code.setId(1001);
LinearLayout.LayoutParams parmas_ll_menu_code = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT, 2f);
parmas_ll_menu_code.setMargins(0, 20, 0, 0);
ll_menu_code.setLayoutParams(parmas_ll_menu_code);
Which sets the weightSum. How to set the layout_above? What should the way to set the both attributes?
I want to create the following xml layout programmatically
<LinearLayout
android:layout_below="#id/ll_menu_code"
android:id="#+id/ll_quantity"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="20dp"
android:weightSum="5">
If you want to add LinearLayout into RelativeLayout programmatically, then you must create instance of RelativeLayout.LayoutParams rather than LinearLayout.LayoutParams since LinearLayout is a child to Relativelayout
So your code must be
LinearLayout ll_menu_code = new LinearLayout(this);
ll_menu_code.setId(1001);
ll_menu_code.setWeightSum(2f);
RelativeLayout.LayoutParams parmas_ll_menu_code = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
parmas_ll_menu_code.setMargins(0, 20, 0, 0);
parmas_ll_menu_code.addRule(RelativeLayout.BELOW, 1001);
ll_menu_code.setLayoutParams(parmas_ll_menu_code);
I Hope it helps..

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.

Categories

Resources