vendredi 6 mars 2015

how to set a button's position randomly new?



when I click on the button 'start time', it sometimes moves completely out of the screen which is bad. and also, for me, the button is not placed randomly enough, sometimes on one click, it just moves a little downwards. And at the end instead of getting the time, the app stopps with nullpointer. The Button should stay in the screen, placed randomly at a new place on click and after some clicks, I want to recieve the elapsed time. why is this not working?


code:



@Override

public void onClick(View v) {

Button b = (Button) findViewById(R.id.start_time);

LayoutParams params = (LayoutParams) b.getLayoutParams();
DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
long startTime = SystemClock.elapsedRealtime();
i++;


if (i == 1 ) {

params.leftMargin = b.getWidth()
+ new Random().nextInt(metrics.widthPixels - 2*b.getWidth());
params.topMargin = b.getHeight()
+ new Random().nextInt(metrics.heightPixels - 3*b.getHeight());
b.setLayoutParams(params);
}
if (i == 2 ) {
params.leftMargin = b.getWidth()
+ new Random().nextInt(metrics.widthPixels - 8+8*b.getWidth());
params.topMargin = b.getHeight()
+ new Random().nextInt(metrics.heightPixels - 4+4*b.getHeight());
b.setLayoutParams(params);
}
if (i == 3 ) {
params.leftMargin = b.getWidth()
+ new Random().nextInt(metrics.widthPixels - 2+9*b.getWidth());
params.topMargin = b.getHeight()
+ new Random().nextInt(metrics.heightPixels - 3+4*b.getHeight());
b.setLayoutParams(params);
}
if (i == 4 ) {
params.leftMargin = b.getWidth()
+ new Random().nextInt(metrics.widthPixels - 2+9*b.getWidth());
params.topMargin = b.getHeight()
+ new Random().nextInt(metrics.heightPixels - 3+4*b.getHeight());
b.setLayoutParams(params);
}
if (i == 5 ) {
params.leftMargin = b.getWidth()
+ new Random().nextInt(metrics.widthPixels - 2+9*b.getWidth());
params.topMargin = b.getHeight()
+ new Random().nextInt(metrics.heightPixels - 3+4*b.getHeight());
b.setLayoutParams(params);
}
if (i == 6 ) {
params.leftMargin = b.getWidth()
+ new Random().nextInt(metrics.widthPixels - 2+9*b.getWidth());
params.topMargin = b.getHeight()
+ new Random().nextInt(metrics.heightPixels - 3+4*b.getHeight());
b.setLayoutParams(params);
}
else if (i == 7) {
long difference = SystemClock.elapsedRealtime() - startTime;

Intent intent = new Intent(Game.this, MainScreen.class);
intent.putExtra("time",difference);
// Toast.makeText(getApplicationContext(), getIntent().getStringExtra("time"), Toast.LENGTH_LONG).show();
textview1.setText(getIntent().getStringExtra("time"));
finish();
}


}


and the xml:



<RelativeLayout xmlns:android="http://ift.tt/nIICcg"
xmlns:tools="http://ift.tt/LrGmb4"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clickable="true"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="chdfss.dsfwegg.trhhGame" >

<Button
android:id="@+id/start_time"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="139dp"
android:text="Button" />




</RelativeLayout>



Aucun commentaire:

Enregistrer un commentaire