android , moving view in linearlayout

949 views Asked by At

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.

2

There are 2 answers

0
Parham Zahir On BEST ANSWER

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);
0
Scott Merritt On

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.