lundi 9 mars 2015

Set a view on screen-properities

In the code below, the button change it's position on click. but sometimes, the button moves out of the screen, result it can't be clicked anymore. how to fix that?



@Override

public void onClick(View v) {


Random r = new Random();
View decorView = getWindow().getDecorView();
int screenWidth = decorView.getWidth();
int screenHeight = decorView.getHeight();
long startTime = SystemClock.elapsedRealtime();
i++;


if (i == 1 ) {

start_time.setX(r.nextInt(screenWidth - start_time.getWidth()));
start_time.setY(r.nextInt(screenHeight - start_time.getHeight()));
}
if (i == 2 ) {
start_time.setX(r.nextInt(screenWidth - start_time.getWidth()));
start_time.setY(r.nextInt(screenHeight - start_time.getHeight()));
}

else if (i == 3) {
long difference = SystemClock.elapsedRealtime() - startTime;

Intent intent = new Intent(Game.this, MainScreen.class);
intent.putExtra("time",difference);

textview1.setText(getIntent().getStringExtra("time"));
finish();
}


}




Aucun commentaire:

Enregistrer un commentaire