Exchange the position of button in android - java

So what I want to achieve is to change the position of my buttons. this is my sample picture:
Note:
My buttons have background drawable, for example sake, I just uploaded without the background images on buttons.
Default
When shuffled:
So this is the code I'm working on:
List<Integer> objects = new ArrayList<Integer>();
objects.add(0);
objects.add(1);
objects.add(2);
objects.add(3);
objects.add(4);
// Shuffle the collection
Collections.shuffle(objects);
List<Button> buttons = new ArrayList<Button>();
buttons.add((Button) findViewById(R.id.bObject1Stage2_1));
buttons.add((Button) findViewById(R.id.bObject2Stage2_1));
buttons.add((Button) findViewById(R.id.bObject3Stage2_1));
buttons.add((Button) findViewById(R.id.bObject4Stage2_1));
buttons.add((Button) findViewById(R.id.bObject5Stage2_1));
Collections.shuffle(buttons);
for (int i = 0; i < objects.size(); i++) {
//buttons.get(i).setText(objects.get(i).toString());
buttons.get(i);
}
But it's not changing the position of buttons. What am I missing in here? Any help is truly appreciated. Thanks.

Use RelativeLayout.LayoutParams to switch values between buttons You wish to move and apply them using setLayoutParams method
This will allow You to switch position of 2 buttons. To shuffle them You have to prepare some algorithm to make number of switches.
RelativeLayout.LayoutParams params1 = (RelativeLayout.LayoutParams) b1.getLayoutParams();
RelativeLayout.LayoutParams params2 = (RelativeLayout.LayoutParams) b2.getLayoutParams();
b1.setLayoutParams(params2);
b2.setLayoutParams(params1);
This will work only if these button are not relative to each other.
If for example b2 was relative to b1 so it is below it You would have to set b1 to be below b1 using addRule method and removeRule on b2 so it is not relative to b1 any more.
for example You have in xml
<Button
android:id="#+id/bObject2Stage2_1"
/*...*/
android:layout_toRightOf="#+id/bObject1Stage2_1" />
to set this rule You would write
params.addRule(RelativeLayout.RIGHT_OF, R.id.bObject1Stage2_1);

Related

How to align imageViews which are created dynamically inside a class and not the xml file

My Problem: I want to create a for loop in the onCreate() method to save imageViews dynamically. I am being able to do everything properly it's just that the imageView is displayed at the top left most corner, i.e i am not being able to assign it it's alignment.
My Code:
RelativeLayout layout = findViewById(R.id.relativeLayout);
for(int i=0;i<3;i++)
{
ImageView image = new ImageView(this);
image.setMaxHeight(200);
image.setMaxWidth(200);
//CODE TO ADD THE ALIGNMENT OF THE IMAGE
image.setImageResource(R.drawable.red);
layout.addView(image);
}
Try this one:
for(int i=0;i<3;i++)
{
ImageView image = new ImageView(this);
image.setMaxHeight(200);
image.setMaxWidth(200);
//CODE TO ADD THE ALIGNMENT OF THE IMAGE
image.setImageResource(R.drawable.red);
RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(RelativeLayout.MarginLayoutParams.MATCH_PARENT, 200);
layoutParams.setMargins(40, 40, 40, 40);
image.setLayoutParams(layoutParams);
layout.addView(image);
}
The legal values can be found at https://developer.android.com/reference/android/view/View.html#setTextAlignment(int)
But this has no effect on an ImageView. I'm not sure what you're actually truing to do, so I can't help you. My best guess is you're trying to arrange views within the parent layout somehow, but to help with that we'd need to know the type of layout the parent is and what you want to do.

Looping through specific widgets

Suppose I have a Table layout and i have a button called 'add people' which dynamically adds a row with two EdiTexts in it each time I click it. now there is another button which is supposed to save the first editText values in database and Add(sum) all the values of second editText .in HTML is very straight forward I can simply add class attribute to both inputs and loop through each class name. but I have no idea how i am supposed to do it in android.
What about saving the references in two lists? Then you can simply iterate over the lists to either store the values in a db or to sum them.
original answer check
public void init(){
TableLayout tablelayout = (TableLayout) findViewById(R.id.displayLinear);
for (int i = 0; i <2; i++) {
//Dynamic Button
Button myButton = new Button(this);
myButton.setText("Push Me");
LinearLayout ll = (LinearLayout)findViewById(R.id.buttonlayout);
LayoutParams lp = new LayoutParams(LayoutParams.MATCH_PARENT,
LayoutParams.WRAP_CONTENT);
ll.addView(myButton, lp);
}
}

aligning dynamic buttons in gridlayout

I am trying to align dynamic buttons in Grid layout.
Problem:
However, if text is more than one line the formation of buttons in row
is not as needed.
String item[] = new String[]{"item1", "item2", "item3 yes",
"item4","item5"};
Button[] myButton = new Button[item.length];
GridLayout scrViewButLay = (GridLayout) findViewById(R.id.grid);
scrViewButLay.setColumnCount(4);
scrViewButLay.setRowCount(10);
// GridLayout.LayoutParams leftMarginParams = new GridLayout.LayoutParams(
// );
for (int index = 0; index < item.length ; index++) {
myButton[index] = new Button(this); //initialize the button here
myButton[index].setText(item[index]);
myButton[index].setHeight(100);
myButton[index].setWidth(100);
myButton[index].setTag(index);
scrViewButLay.addView(myButton[index]);
}
}
`
I recommend to use an Adapter and then customize however you want
check out this example
http://androidexample.com/Custom_Grid_Layout_-_Android_Example/index.php?view=article_discription&aid=76
I think using a recyclerView with GridLayoutManager should help you out.
This post https://inducesmile.com/android/android-gridlayoutmanager-with-recyclerview-in-material-design/ should help you out.
You only need to apply a layout_gravity as the default seems to be to align the text to be the same height (this is why it only happens with the item 3 with two lines).
GridLayout.LayoutParams param =new GridLayout.LayoutParams();
param.height = GridLayout.LayoutParams.WRAP_CONTENT;
param.width = GridLayout.LayoutParams.WRAP_CONTENT;
param.setGravity(Gravity.BOTTOM);

How to change Button size in Android?

I am trying to change the button size dynamically, but after using setWidth() and setHeight() nothing happens.
This is my code...
GridLayout grid = (GridLayout)findViewById(R.id.grid);
grid.setColumnCount(10);
grid.setRowCount(10);
cells = new Button[100];
for (int i = 0; i < cells.length; i++) {
cells[i] = new Button(this);
cells[i].setWidth(grid.getWidth()/10);
cells[i].setHeight(grid.getHeight()/10);
grid.addView(cells[i]);
}
Any ideas/solutions?
LayoutParams should solve the issue:
This in an example of how it should work taken from another stackOverflow post:
ViewGroup.LayoutParams params = myButton.getLayoutParams();
params.width = 400;
myButton.setLayoutParams(params);
I figured this out on my own and it was very simple, I feel stupid for overlooking it.
grid.addView(cells[i], display.getWidth()/grid.getColumnCount(), display.getWidth()/grid.getColumnCount());
In this line of code I add the button to the layout with the "cells[i]" while the trailing two parameters tell the view what size I want the button to be.
Another example:
addView(myButton, 100, 200);
Above the addView is adding myButton with 100 pixel width by 200 pixel height.
Maybe try to call invalidate method. If the view is visible, onDraw(android.graphics.Canvas) will be called at some point in the future. This must be called from a UI thread. To call from a non-UI thread, call postInvalidate().
Source: http://developer.android.com/reference/android/view/View.html#invalidate%28%29

Move view dynamically in relativelayout

I am doing an pageindex, and the current page should be highlighted with a arrow image (imageview).
The index is a RelativeLayout with 25 textviews added to it:
for (int i = 0; i < 25; i++) {
TextView tv = new TextView(this);
tv.setText(Integer.toString(i+1));
tv.setGravity(Gravity.CENTER);
int id = 2000+i;
tv.setId(id);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.FILL_PARENT);
if(i==0)
params.addRule(RelativeLayout.ALIGN_PARENT_LEFT, RelativeLayout.TRUE);
else
params.addRule(RelativeLayout.RIGHT_OF, prevViewId);
prevViewId = id;
rl.addView(tv,params);
}
And when the page changes I do something like this:
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.ALIGN_BOTTOM, 2000+i);
params.addRule(RelativeLayout.ALIGN_RIGHT, 2000+i);
params.addRule(RelativeLayout.ALIGN_LEFT, 2000+i);
arrowImageView.setLayoutParams(params);
rl.requestLayout();
rl.invalidate();
Everything looks correct, I can place the arrow at arbitrary page at "start up", but the arrow wont move when page is changed. I have debuged and verified that the code is run and everything looks correct, but still the arrow is stuck in the first position. If I force delete and add a new imageview instead of updating the LayoutParams the arrow will disappear totally.
I have the same problem.. I also want to move my views around at run-time inside a reference layout. So if anyone ca help that would be awesome.
What i believe is happening above is that the arrow IS having its location changed, however it doesn't get updated to the screen. Correct me if I'm wrong, please, this is just my guess as I am too having the same problem.
-edit-
After some messing around I've found what works for me is to remove first then add.
rl.removeView(tv);
rl.addView(tv,params);
-edit-
also, u can save the params for the moving view into a unique variable so that way all ude have to do is change the margins...
for example: instead of params, have it be its own name "arrowParams"
then to move it ude just need: arrowParams.leftMargin = 2000+i; and so on...

Categories