0

I made a linear layout and it contain some Image views . when i move one with (set X(view.get x + 10)) function, it moves... but it moves behind other views. that the view become hidden.

and the other problem is when i get X & Y of the view, its always 0,0. but the view is in the middle of the screen. what should I do??? should i give up with linear layout??

if(wichmov == "right" ){
    if(martin.getX() < width){
        martin.setX(martin.getX()+ deltax);
    }
    else if(wichmov == "left"){
        if(martin.getX() > 0){
            martin.setX(martin.getX()- deltax );
        }
    }
}

this is how i move it.

ricopo
  • 455
  • 6
  • 17
  • I thing that it'd be better to use RealiveLayout instead of LinearLayout, to get te absolute position of a view on the screen you may use View.getLocationOnScreen(). – miibpa May 11 '17 at 18:49
  • By default, Views are drawn in the order they are declared in the layout, so former views appear behind latter views. You can change that by calling View.bringToFront() – BladeCoder May 11 '17 at 19:12
  • @miibpa but i hardly can change in to relative layout. get location on screen is returning 0,0 too. :( – Parham Zahir May 12 '17 at 13:43
  • @bladecoder i try what U say but it is still moving behind... – Parham Zahir May 12 '17 at 13:44

2 Answers2

0

When are you trying to call getX()? This might be something to look into: View getX() and getY() return 0.0 after they have been added to the Activity

If you're making the call in onCreate, it'll be zero.

Community
  • 1
  • 1
Scott Merritt
  • 1,284
  • 2
  • 13
  • 24
0

I just figured it out.

I use a relative layout and then set the linear layout as match parent. After designing the background with the linear layout, I define an image view after the linear layout and inside of the relative layout, and then in Java, I set the position of it in the exact place I want it to (front of the special box of linear layout that I wanted it move), and width and height of it too.

The following code will help you to place your view to the exact place of your screen you want it, and set its width and height:

DisplayMetrics metrics = this.getResources().getDisplayMetrics(); //getting screen size
width = metrics.widthPixels;
height = metrics.heightPixels;

view.setX(width *5/8); //setting the place
view.setY(height/4);

LayoutParams para = view.getLayoutParams(); //set size of view
para.height = (int) (height/2);
para.width = (int) (width/4);
view.setLayoutParams(para);
Pang
  • 9,564
  • 146
  • 81
  • 122