So here is what I'm trying to accomplish, above is what the view looks like, I'm trying to limit the position of the button to the red square.
Here is the code I have tried so far:
ImageButton charButton = (ImageButton) findViewById(R.id.goodIcon);
charButton.setImageDrawable(x);
DisplayMetrics displayMetrics = context.getResources().getDisplayMetrics();
int width = displayMetrics.widthPixels;
int height = displayMetrics.heightPixels;
width = width - 100; //Wont go within 100 of the screen edge
height = height - 100; //Wont go within 100 of bottom edge
Random r1 = new Random();
int Button1H = r1.nextInt(height - 200) + 200;
if (Button1H<100) {Button1H = 100;}
if (Button1H >= displayMetrics.heightPixels-100){Button1H = displayMetrics.heightPixels-100;}
int Button1W = r1.nextInt(width - 50) +50;
if (Button1W >= displayMetrics.widthPixels-100){Button1W = displayMetrics.widthPixels - 300;}
charButton.setX(Button1W);
charButton.setY(Button1H);
I realize this is most likely not the best way of handling it, but I really don't want the icon going over the views at the top and bottom, or appearing on outside on the right hand side.
Any help? Using Android Studio. Picture at the top shows the range of where I want it to be able to appear.
Aucun commentaire:
Enregistrer un commentaire