Suppose I want to show an image of the something using an ImageView, and I want to put new views on top of it (animated ImageViews that show pins on a world map image, for example, or a Switch view on top of a smartphone image).
This means that no matter how the ImageView shows the image, the views should be in it, inside correct spot, with the same size as specified, or in a size related to the imageView itself
As opposed to other views, the ImageView can have a certain size, but its content is something else (to keep aspect ratio).
I tried to use ConstraintLayout, but this can't really help, because the ImageView can change its size (for example when changing orientation), and thus ruining the position I've given the views compared to the ImageView.
I've found some libraries that can handle a similar thing (like here) and I even asked a similar question before (here), but all those solutions are for a static image within the ImageView, yet what I search for is adding a view on top of an ImageView.
How do I put the views on top of the ImageView's content correctly?
How do I also scale the views down/up compared to the size of the ImageView's content ?
Can ConstraintLayout be used to change the scale of the views according to the ImageView's size ?
Before Coordinator Layout, Frame Layout was used, but using more than one views in Frame Layout results in overlapping of views over one another.
Tap Edit and choose an image from your Photo Library or Stock Images. Scroll through the bottom menu and tap on Overlays.
You can make a view clickable, as a button, by adding the android:onClick attribute in the XML layout. For example, you can make an image act like a button by adding android:onClick to the ImageView .
image); image. setAlpha(150); // Value: [0-255]. Where 0 is fully transparent // and 255 is fully opaque. Set the value according // to your choice, and you can also use seekbar to // maintain the transparency.
Make FrameLayout
with wrap_content
around ImageView
. Then you could set SwitchView
on top of ImageView
. You could align it to center, side or corners and using margins to get some fine position.
It still won't scale with image, but you can get pretty good results. If that doesn't fit you, you can programatically get width/height of ImageView
and alter position (or margins) of SwitchView
accordingly.
With below you can manage width of switch or any other view as per image view width
android:layout_alignLeft="@+id/imageView"
android:layout_alignRight="@+id/imageView"
<Switch
android:id="@+id/swi"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/imageView"
android:layout_alignRight="@+id/imageView" />
<ImageView
android:id="@+id/imageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/swi"
android:src="@drawable/download" />
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With