Edit position of an Android button created with a linearLayout - android

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...

Related

How to set Margins for buttons Programmatically in 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.

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.

How do I get space between consecutive RelativeLayouts inside a LinearLayout?

I'm trying to create a list of clickable image buttons w/ text that fit inside a HorizontalScrollView. The images/content will be set programmatically. The best way to do this seemed to be a LinearLayout, that then contained a series of RelativeLayouts that contained the views to display the relevant content. However, I'm having trouble getting space between each RelativeLayout. Even though I've set margins in xml and programmatically they seem to be ignored and the RelativeLayout objects are squished together.
Some code:
<RelativeLayout
android:id="#+id/details_image_button"
android:layout_width="75dp"
android:layout_height="100dp"
android:layout_marginLeft="10dp"
android:background="#00ff78">
<ImageView
android:id="#+id/loadable_image_view"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>
<TextView
android:id="#+id/details_text_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#b083ef"
android:text="PH - Info about title"
android:layout_alignParentBottom="true"
/>
//Code below is looped through several times
RelativeLayout imageButtonLayout = (RelativeLayout) inflater.inflate(R.layout.details_image_button, null);
RelativeLayout.LayoutParams imageButtonLayoutParams = new RelativeLayout.LayoutParams(100, 100);
imageButtonLayoutParams.setMargins(10, 10, 10, 10);
imageButtonLayout.setLayoutParams(imageButtonLayoutParams);
The current result that I am getting is a solid green (background color of the RelativeLayout) rather than the expected result of a group of RelativeLayouts with a space between each. How can I best get a margin or buffer between each RelativeLayout?
If your RelativeLayout is inside a LinearLayout, the LayoutParams you need to use would be LinearLayout.LayoutParams:
RelativeLayout imageButtonLayout = (RelativeLayout)
inflater.inflate(R.layout.details_image_button, null);
LinearLayout.LayoutParams imageButtonLayoutParams = new
LinearLayout.LayoutParams(100, 100);
imageButtonLayoutParams.setMargins(10, 10, 10, 10);
imageButtonLayout.setLayoutParams(imageButtonLayoutParams);
LayoutParams come from the parent, not the child.

Change Relative layout width and height dynamically

Hi i am using following layout structure inside LinearLayout
<TableLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<TableRow
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
</TableRow>
<TableRow
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<TextView
android:id="#+id/tv1"
android:layout_height="wrap_content"
android:gravity="left"
android:paddingLeft="10dp"
android:textColor="#000" />
<TextView
android:id="#+id/tv2"
android:layout_height="wrap_content"
android:layout_weight="1.0"
android:gravity="right"
android:paddingRight="10dp"
android:textColor="#000" />
</TableRow>
</TableLayout>
<ImageView
android:id="#+id/img1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center" />
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="320px"
android:layout_height="320px"
android:gravity="center" >
</RelativeLayout>
and want to set relative layout width and height dynamically from java file instead of setting it to 320px in the xml file but not able to do that , Orientation change is not an issue for as i restricting it to only in Portrait mode. It is possible to set the relative layout on full screen by using match_parent but i have to put another views on the screen so is it possible or i have to achieve it another way...
Android does NOT refresh layout of views with wrap_content once it has been displayed.
Even with invalidate() or requestLayout().
So if you add a child view, or modify the content dynamically, you're screwed.
getLayoutParams().height = x plus requestLayout() are a good solution if you know the "x", and if you need to do it ONLY ONCE.
After that the wrap_content in LayoutParams is lost, since you have overridden it.
To solve that, I've written a static class that recomputes the sizes and forces the update of the layout for the views with wrap_content. The code and instructions to use are available here:
https://github.com/ea167/android-layout-wrap-content-updater
Enjoy!
try using this
getLayoutParams().height= x;
requestLayout(); or invalidate();
give an id to your relative layout in .xml :
android:id="#+id/relativelayout"
now in your java file write this:
RelativeLayout Rl = (RelativeLayout) findViewById(R.id.relativelayout);
RelativeLayout.LayoutParams layout_description = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.FILL_PARENT or YourUserDefinedwidth,
RelativeLayout.LayoutParams.FILL_PARENT or YourUserDefinedHeight);
Rl.setLayoutParams(layout_description);
From below code u can get device height and width:-
Display display = getWindowManager().getDefaultDisplay();
int width = (display.getWidth() );
int height = (display.getHeight() );
paramsTop ur realative layout:-
Now u can set height or width what you want.
paramsTop = new RelativeLayout.LayoutParams(width, height);
You can set views dimensions dynamically using LayoutParams.
Something like this:
RelativeLayout layout = (RelativeLayout) findViewById(R.id.yourlayout_id);
// Gets the layout params that will allow you to resize the layout
LayoutParams params = layout.getLayoutParams();
params.height = 320;
params.width = 320;
I faced this problem, the best solution for Change Relative layout width and height dynamically like this
RelativeLayout relMain = (RelativeLayout) convertView.findViewById(R.id.relMain);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
params.width =200;
params.height =200;
params.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
relMain.setLayoutParams(params);
If you intending to change Height, then this would make a trick.
RelativeLayout relativeLayout = (RelativeLayout) findViewById(R.id.yourId);
relativeLayout.getLayoutParams().height = 100;
relativeLayout.getLayoutParams().width = 100;
I encountered same issue when working with LinearLayout which has wrap_content and one child as TextView which had match_parent.
Remove the TextView programatically and then add it again.
linearLayout.removeView(textView)
linearLayout.addView(textView)
I know it sound stupid but it works.
In my case calling invalidate didn't work, only this worked.
Depending on your implementation you need to take care of view index inside its parent

How to control addView to a layout

I add a graphview programatically :
LinearLayout.LayoutParams Lparams = new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, p.y/2);
//same result with fill_parent or match_parent
Lparams.gravity = Gravity.LEFT;
firstGraph.setPadding(10, 0, 10, 0);
firstGraph.setLayoutParams(Lparams);
LinearLayout RelLayout = (LinearLayout) findViewById(R.id.relLayout);
RelLayout.setGravity(Gravity.LEFT);
RelLayout.addView(firstGraph, 0);
I obtain this:
What i want is to eliminate that left gap of the graphview, I don't know why is there, I only set a padding of 10 that is what you have in right side.
Any Suggestions? Thansk in advance.
the layout xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/relLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="left"
android:orientation="vertical" >
<Button
android:id="#+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" />
</LinearLayout>
That space most likely appears because it is the space reserved for the axis labels(and as you probably don't set any labels it's an empty space). If you do need to remove it you'll have to modify the graph view's code and change the values in GraphViewConfig(as those seem to be the values used).
setPadding(int left, int top, int right, int bottom) is the sytax of setPadding. The first argument is for the left padding. You are giving 10. Try setPadding(0, 0, 10, 0)

Categories

Resources