Android: set button to random position - java

How can I as easy as possible set the position of my Button to a random place?
I've tryed to use this: (My screen resolution is 800x480)
Button btn = (Button) findViewById(R.id.button1);
Random r = new Random();
int x = r.nextInt(480);
int y = r.nextInt(800);
btn.setX(x);
btn.setY(y);
But when i use this, it seems that the button some times get located outside my application or so? is it possible to prevent this and keep the button inside the app?

Pretty sure your problem is you are not accounting for the width and height of the button, and so if it randoms to 480*800 it will be off the screen. Try something similar to:
int x = r.nextInt(480 - buttonWidth);
int y = r.nextInt(800 - buttonHeight);

Related

Android animation in button

i am moving my button by animation and button moving visually but when i press the button at new position i don't find it there.
final Animation animation1 = new TranslateAnimation(0,0,0,100);
animation1.setDuration(5000);
animation1.setFillAfter(true);
alphabet.startAnimation(animation1);
humanbody.startAnimation(animation1);
poem.startAnimation(animation1);
game.startAnimation(animation1);
When I press alphabet button,it took me to human-body activity And I press somewhere top of alphabet button it took me to alphabet activity
I'm still a beginner at this, but I think that using the animation class only animates the view's drawable state, not the its bound/touch area.
Try using this:
int t = 5000;
int y = 100;
alphabet.animate().setDuration(t).translateY(y).start();
humanbody.animate().setDuration(t).translateY(y).start();
poem.animate().setDuration(t).translateY(y).start();
game.animate().setDuration(t).translateY(y).start();

Avoid dynamic views from overlapping

I am developing an app that requires multiple buttons to be created dynamically (onCreate of MainActivity class). Each button has a dimension of 100x100dp.
Here's the code:
for (int i = 0; i < count; i++) {
buttons[i] = new Button(this);
RelativeLayout ll = (RelativeLayout)findViewById(R.id.maincontainer);
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
buttons[i].setY(i * 150); // should be random
buttons[i].setX(i * 120); // should be random
String temp = Character.toString(input.charAt(i));
buttons[i].setText(temp);
buttons[i].setOnClickListener(this);
buttons[i].setId(i);
test1.setText(Integer.toString(buttons[i].getId()));
ll.addView(buttons[i],lp);
}
The position of these buttons should be completely random within the layout which can be easily achieved by generating random values for x and y coordinates. But I need the buttons not to overlap other buttons. Also due to 100x100dp dimension, sometimes the previously generated buttons are being partially overlapped by new ones.
You can actually figure this out programatically. Keep the co-ordinates of the views that you generate stored in a list. Then simply compare the co-ordinates to see if the new view intersects. You can use the following for a visualisation:
http://silentmatt.com/rectangle-intersection/
I hope you try writing code than copying it from someplace. :)
A SO link to help you out: Determine if two rectangles overlap each other?

Button position from dialog to activity

I've been trying to get the position from a button inside a dialog so that i can use it to place a button below that button in the activity.
I've tried a few things but none of them seem to work
int loc1[] = new int[2];
button.getLocationInWindow(loc1);
int loc2[] = new int[2];
button.getLocationOnScreen(loc2);
I've also tried
button.getX();
button.getY();
The button was never placed below the button in the dialog.
(I should note that i only mentioned the methods to get the postiion)
Can anybody help me ?
Thanks !
You need to wait for the callback when the Button has placed. The code you are using not returning the proper value because the value is returned before the Button is placed. So add the Button in a layout with height/width as wrap_content and Use this code:
layout.getViewTreeObserver().addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
public void onGlobalLayout() {
getViewTreeObserver().removeGlobalOnLayoutListener(this);
int[] locations = new int[2];
layout.getLocationOnScreen(locations);
int x = locations[0];
int y = locations[1];
}
});
Make layout params, they depend on the parent, so you should probably use LinearLayout.LayoutParams or RelativeLayout.LayoutParams (there are other cases).
And add layout_below(button1) to your LayoutParams object. And set that LayoutParams to new button you want to appear below first button1.

Show image randomly when button is clicked

I made 9 imagebuttons and 1 button.
I want only 4 buttons shows images when button is clicked, and it shows randomly every time button is pressed, how will that be possible?
try this
// add your image buttons to this list
ArrayList<ImageButton> allImageButtons = new ArrayList<ImageButton>();
allImageButtons.add(imgbtn1); //etc... for all 8 image buttons
Then in your button click method
Random rnd = new Random();
ArrayList<Integer> randomNumbers = new ArrayList<Integer>();
while (randomNumbers.size() < 4)
{
int num = rnd.nextInt(9);
if (!randomNumbers.contains(num))
randomNumbers.add(num);
}
for (int i=0;i<allImageButtons.size();i++)
{
if (randomNumbers.contains(i))
allImageButtons.get(i).setVisibility(View.VISIBLE);
else
allImageButtons.get(i).setVisibility(View.GONE);
}
You can create an array that include image's src name and you can use
string imageArray = Name of yur images
int random = (int )(Math.random() * 9);
Then you can chance your image button images according to this random number.
imgButton.setBackgroundResource(imageArray[random]);
I hope it is clear and helpful.

Android/Java "Convert" String to Button

I have this code:
Button button1 = (Button) findViewById(R.id.button1);
Button button2 = (Button) findViewById(R.id.button2);
String object = "button";
int num;
num = r.nextInt(3 - 1) + 1;
String total = object + num;
I want do set the text for one of the buttons chosen randomly. Something like this:
button<num>.setText(some_text);
^ here instead of <num> should be 1 or 2
and has to be chosen randomly
Like Ondkloss said, you can add your buttons to an array, then randomly select one from that array.
Button[] buttonArray = new Button[2];
buttonArray[0] = button1;
buttonArray[1] = button2;
Random r = new Random();
buttonArray[r.nextInt(2)].setText(someRandomText);
Keep in mind that if you change the number of buttons you will need to change the numbers that I have used (new Button[2] & r.nextInt(2)). My solution works specifically for an array of length 2 containing only 2 buttons. But other than changing the numbers in the array creation and the random number generation to match the number of buttons you have, this solution should work just fine.
No, i want change the text of the button.
Then just do something like this
button1.setText("Just some strings here");

Categories

Resources