How to set Margins for buttons Programmatically in Android? - android

I have Programmatically added more than 50 buttons in a GridLayout which contains ScrollView and LinearLayout as GridLayout Parents. I need to set margins for every button. I tried setMargins() method. But, it doesn't work. Can anyone Please help me?
XML
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="10dp"
android:layout_marginBottom="10dp">
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scrollbars="none">
<GridLayout
android:id="#+id/levelsGridLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:columnCount="5"
android:rowCount="10">
</GridLayout>
</ScrollView>
</LinearLayout>
Code to create Buttons.
FrameLayout.LayoutParams params = FrameLayout.LayoutParams(
FrameLayout.LayoutParams.WRAP_CONTENT,
FrameLayout.LayoutParams.WRAP_CONTENT
);
for (int i = 1; i <= 100; i++) {
Button button = new Button(this);
button.setText(Integer.toString(i));
id = getResources().getIdentifier("button" + i, "id", getPackageName());
button.setId(id);
button.setTag(Integer.toString(i));
button.setTextAlignment(View.TEXT_ALIGNMENT_CENTER);
button.setTextColor(Color.WHITE);
button.setBackgroundResource(R.drawable.levels_button_background);
params.setMargins(5, 5, 5, 5);
button.setLayoutParams(params);
allLevelButtons.add(button);
levelsGridLayout.addView(button);
button.getLayoutParams().width = oneButtonWidth;
}

When you add the view, you need to add it with the 2 parameter addView(View, LayoutParameters) version. Otherwise you don't get the params you just set, you get a new params object. Also, you need to move the creation of the params object inside the loop, each one should get its own, or if you ever change it you'll get weird results (they'd all change).
Of course you should probably be using a GridLayout or RecyclerView with a GridLayoutManager rather than adding views one by one, especially if you have more than half a dozen or so.

Related

How to change view margins dynamically in "Android Flowlayout"

I have an Android Flow layout hash_tag_layout
<org.apmem.tools.layouts.FlowLayout
android:id="#+id/hash_tag_layout"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="#dimen/view_padding"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin">
</org.apmem.tools.layouts.FlowLayout>
The problem is I cannot change the margins on dynamically added views.
int pixels = convertDensityPixelsToPixels(5);
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
lp.setMargins(0,0,pixels,pixels); //<---<< This line has no effect
TextView tv1 = new TextView(ctx);
tv1.setPadding(pixels,pixels,pixels,pixels); //int left top right bottom
tv1.setBackground(ctx.getResources().getDrawable(R.drawable.hash_tag_bubble_format));//drawable
tv1.setText("# Asd");
tv1.setLayoutParams(lp);
layout.addView(tv1);
If I change hash_tag_layout layout to a regular linear layout, than I get margins!
<LinearLayout
android:id="#+id/hash_tag_layout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
</LinearLayout>
So I am lead to conlude that Android Flow layout does not allow me to add margins to views added dynamically!
Any idea how to change the source code or find a way around this? Maybe add a transparent border around the dynamically added views?
Just change your LinearLayout.LayoutParams to FlowLayout.LayoutParams.
Here is an issue discussion on GitHub.
I see that you asked a question 3 years ago, but I have just faced the same problem :)

Adding a few TextViews on RelativeLayout

I'm trying to programmatically add many TextView to a RelativeLayout but I am unable to do that when TextView reach the end of the display right next TextView inflate in a new line.
RelativeLayout:
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:id="#+id/tag_cloud"
android:padding="10dp">
</RelativeLayout>
Code:
if (categoriesCursor.moveToFirst()){
do {
TextView tagElement = (TextView) getLayoutInflater().inflate(R.layout.tag, null);
tagElement.setText(categoriesCursor.getString(2));
LinearLayout.LayoutParams llp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
llp.setMargins(0, 0, pixels, pixels); // llp.setMargins(left, top, right, bottom);
tagElement.setLayoutParams(llp);
tagCloudLayout.addView(tagElement);
} while (categoriesCursor.moveToNext());
}
tag.xml
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#ffffff"
android:textColor="#ff000000"
android:textStyle="bold"
android:paddingLeft="5dp"
android:paddingRight="5dp"/>
Thanks
It´s not really clear what You are asking, but If I understand You the right way, You want to have only one line with textViews, or? Also, You are using wrong Layout Params, if You want to add some views to a relativeLayout side by side, I think You will get it with:
RelativeLayout.LayoutParams param = new RelativeLayout.LayoutParams(
LayoutParams.MATCH_PARENT,
LayoutParams.MATCH_PARENT, 1.0f);
Where the last parameter stands for the layout weight attribute. With LayoutWeight and MATCH_PARENT, all views will be drawn with equal size.
Consider using a ListView which contains TextView, and that way you'll be able to take advantage of cell re-use etc. Here's a good tutorial
Finally I put one LinearLayout vertical oriented and inside LinearLayouts horizontal oriented with three TextView inside. Isn't the best solution but it works for me.

Programmatically set weight of children of LinearLayout [duplicate]

This question already has answers here:
Android Linear Layout Weight Programmatically
(4 answers)
Closed 3 years ago.
I have a vertical LinearLayout LL_p with two children horizontal LinearLayouts LL_1 and LL_2 which in turn have their own children. Based on the visible contents of LL_1 and LL_2, I want to dynamically change their relative weight inside LL_p. I already have an xml layout with a great deal of details that I do not wish to lose, so I only have to make the incremental change to the weights. How do I do that? Here is my xml
…
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight=“0.2”
android:orientation="vertical" >
<LinearLayout
android:id="#+id/ll_1”
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.55"
android:background="#drawable/some_image”
android:orientation="vertical" >
<!—- a number of includes —>
</LinearLayout>
<LinearLayout
android:id="#+id/ll_2”
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.45"
android:layout_marginBottom="#dimen/dim_1”
android:background="#color/some_color”
android:orientation="horizontal" >
<!—- a number of children —>
</LinearLayout>
</LinearLayout>
</LinearLayout>
So in java activity class I figure I need the following method, but I need help completing it. Notice that I am not instantiating new layout params as that would cause me to lose my xml layout details; instead I am using getLayoutParams to grab the one set from xml. so how do I make weight change to the layout as obtained?
private void adjustMYLayout(boolean flip) {
LayoutParams layout1 = mLL1.getLayoutParams();
LayoutParams layout2 = mBLL2.getLayoutParams();
//now what?
if(flip) {//set one weight system
}else {
//set other weight system
}
}
UPDATE for #nKn
private void adjustMYLayout(boolean flip) {
if (flip) {
mLL1.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT, 0.8f));
mLL2.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT, 0.2f));
} else {
mLL1.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT, 0.55f));
mLL2.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.WRAP_CONTENT, 0.45f));
}
}
You can achieve that by declaring a LinearLayout.LayoutParams object
tv.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT, 1f));
The third parameter (1f) is the weight of the layout (in this case 1 (f stands for float)).

Edit position of an Android button created with a linearLayout

I am trying to dynamically create buttons, and they will be of varying size and in varying positions.
I have the code to create a button of varying size, but I am stuck on changing the position.
I am using linearlayout and am trying to use setMargins to move the button around, but it seems to be changing the margin within the button. My code is as follows:
public void button(int a, int b) {
newButton = new Button(this);
newButton.setText("HELLO");
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.FILL_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
layoutParams.setMargins(0, 0, 0, 0);
ViewGroup.LayoutParams params = layout.getLayoutParams();
params.width = a;
params.height = b;
layout.requestLayout();
layout.addView(newButton, layoutParams);
}
Here is my manifest for this bit:
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/textView1"
android:layout_marginTop="58dp"
android:layout_toRightOf="#+id/textView1"
android:text="Button" />
<LinearLayout
android:id="#+id/layout"
android:layout_width="50sp"
android:layout_height="40sp"
android:orientation="vertical" >
</LinearLayout>
Do you understand what a LinearLayout is and why you're using it? Every child of the layout snaps in place. If I have a LinearLayout that's vertical and it has 3 children they will be on top of each other. I can change their gravity so they are attracted to different margins but to "change" its position is impossible depending on what you mean by "change".
Check out the other layouts. You may want to use a RelativeLayout.
Why don´t you use button.setX(float x) and button.setY(float y)? You'll need to use RelativeLayout instead of the LinearLayout. It's more easy but it's only available since api 11...

android how to add a custom horizontal scrollview to list of images

I am going to developing an application where my 1st page is a list of images. I want it should be better visible. in a horizontal srcollview manner having some good visibility.
How can I do that.
Plz give me a sample example for better understanding.
Thank you
Take a look at the Gallery widget.
you can use this,
in XML use horizontalscrollView
<HorizontalScrollView
android:layout_width="match_parent"
android:layout_height="100dp"
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp"
android:paddingLeft="15dp"
android:paddingRight="15dp"
android:id="#+id/hrscroll">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:orientation="horizontal"
android:id="#+id/scrollview_layout">
</LinearLayout>
</HorizontalScrollView>
java code..
LayoutParams params = new LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT);
params.width=110;
LayoutParams params2 = new LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT);
params2.width=100;
LinearLayout gall = (LinearLayout) findViewById(R.id.scrollview_layout);
for(int i=0;i<=5;i++){
LinearLayout Limages = new LinearLayout(this);
Limages.setOrientation(LinearLayout.VERTICAL);
ImageView images = new ImageView(this);
// images.setScaleType(ImageView.ScaleType.FIT_CENTER);
images.setLayoutParams(params2);
images.setBackgroundResource(R.drawable.imagez);
Limages.addView(images);
Limages.setLayoutParams(params);
gall.addView(Limages);
}

Categories

Resources