Rellac
Rellac

Reputation: 63

Set an ImageView position - Android

I am attempting to create some interactivity on a simple application so I can reposition an image. After doing some googling I found what should be capable of making this work but I seem unable to get anything to work. With the current code, my client will crash but I will strangely get no errors outputted at all.

Check this link. What have I done wrong?

I am using the following xml to draw the image

I have also attempted to use this alongside the setX and setY methods that are a default in ImageView, yet they aren't recognised at all when I attempt to run. Hence I am looking into more complex code snippets.

Upvotes: 0

Views: 588

Answers (3)

rock_win
rock_win

Reputation: 755

I think the easiest would be ( if using more than 3.0),

imageView.animate().translateX()

the animate() would return you a ViewPropertyAnimator.

If you are using FrameLayout your approach of LayoutParams will also work.

Upvotes: 0

ucsunil
ucsunil

Reputation: 7494

Once you have included a widget (in your case an ImageView) into the xml layout, it gets bound to that view. It is easier (and good practice) to dynamically add widgets when you need to move/remove/replace them during run time. For example you could keep your RelativeLayout container empty in your layout file and add this ImageView in your onCreate() method using RelativeLayout.addView().

Upvotes: 0

Decoy
Decoy

Reputation: 1597

You cannot cast from LinearLayout.LayoutParams to AbsoluteLayout.LayoutParams , this is probably the error you get .

Change the AbsoluteLayout.LayoutParams to LinearLayout.LayoutParams because the imageView is in a linearLayout, also AbsoluteLayout is deprecated.

If you want to move a view (change the position) dynamically, use the translateX and translateY properties.

Hope this helps.

Upvotes: 2

Related Questions