0

In this problem i wanted to change the position of the ImageButton frequently. After the first initialization the ImageButton is not changing its position. Here is the code...

runOnUiThread(new Runnable() {
        public void run()
        {
            for(int i=0;i<60;i++)
            {
                try {
                    Thread.sleep(500);
                } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
                }
                img.refreshDrawableState();

                Random r=new Random();
                int a=r.nextInt();
                int b=r.nextInt();

                if(a<0)
                    a=a*(-1);
                if(b<0)
                    b=b*(-1);

                w=(a%(width-50))-72;
                h=(b%(height-50))-30;



                img.refreshDrawableState();
                img.setLeft(w);
                img.setTop(h);

                Toast.makeText(getApplicationContext(), "width : "+w+"\theight : "+h, Toast.LENGTH_SHORT).show();
            }
        }
    });

The variables height & width are height & width of the screen

  • Is your imagebutton in a AbsoluteValue layout? – wyoskibum Aug 22 '13 at 14:09
  • @wyoskibum : It is in the RelativeLayout. – user2707621 Aug 22 '13 at 14:14
  • Then it's logical that you button doens't move.. the relativelayout works as designed – Tobrun Aug 22 '13 at 14:29
  • @user1281750 : then which layout should i use ? – user2707621 Aug 22 '13 at 14:32
  • Learn about the different types of viewgroups here: http://developer.android.com/reference/android/view/ViewGroup.html (different types of viewgroups can be selected in the known subclasses item). If you want to use x,y coordinates then absoluteLayout will the way to go, but this is not the android way (not anymore atleast hence it deprecated) – Tobrun Aug 22 '13 at 14:39

1 Answers1

0

You should use an Absolute Layout and set the layout parameters. I found this question/answer useful when I needed to do something similar.

Community
  • 1
  • 1
wyoskibum
  • 1,869
  • 2
  • 23
  • 43