mardi 28 juillet 2015

how to set button in random position when the app start and after click

I've tried this code to change randomly the button position. But i'ts just dimensions, on the first click the button change position but on the other click not. Why?? some help? Thanks

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
final Button btn = (Button) findViewById(R.id.run_button);
        RelativeLayout.LayoutParams absParams = (RelativeLayout.LayoutParams) btn
                .getLayoutParams();

        DisplayMetrics displaymetrics = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
        int width = displaymetrics.widthPixels;
        int height = displaymetrics.heightPixels;

        Random r = new Random();

absParams.width = r.nextInt(width);
        absParams.height = r.nextInt(height);
        btn.setLayoutParams(absParams);

        btn.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                Random r = new Random();
                btn.getX();
                btn.getY();

                btn.setX(r.nextFloat());
                btn.setY(r.nextFloat());

            }

        });




Aucun commentaire:

Enregistrer un commentaire