Arjun Arjun - 14 days ago 8
Android Question

How to position ImageView offscreen in xml

Description : ConstraintLayout that has Imageview. The ImageView source has a larger width then the screen by design.

Problem : Can easily position the ImageView so the first half of the image is visible and the remaining half is offscreen .. however, in a further state, the first half of the image should be hidden and the second half showing.

Just for illustration purposes:
Image : XXXXXX Screen : [] First state : [XXX]XXX Second state : XXX[XXX]

I am trying to do this is XML only and don't want to do this programmatically.

Any solutions I can try out ??

Answer

I don't think you're gonna get around programming anything. What you could do in order to avoid adding / editing the imageView in your activity's function you could make yourself a custom class to include into your xml. Have a look here: https://developer.android.com/training/custom-views/create-view.html

If you want to strictly stay xml, you could set a negative padding. This way, the imageView would stay the same size, but the content is shiftet left or right. The problem with this is that you have to pretty much hardcode the sizes into the xml. Of course you could create dimension variables in xml for all possible screen sizes, but I think coding would be a lot easier. Anyway, xml for this could look like this:

<ImageView
    app:srcCompat="@your_src"
    android:id="@+id/imageViewID"
    android:scaleType="fitXY"
    android:layout_width="width"
    android:paddingLeft="-width"
    android:layout_height="height*2" />