0

I want to set a watermark to certain views (mostly viewgroups) of my app.

I managed to create the watermark with some canvas redrawing within the onDraw() method, but to apply my demo I needed to create a sublass of FragmentLayout in order to replace the FragmentLayout in the layout with the WatermarkFragmentLayout.

In the project, the views that need to be applied range from ConstraintLayout, FrameLayout, LinearLayout, CardViews, RecyclerViews, etc. Additionally there are probably 100+ screens where the watermark needs to be applied in these views.

I wouldn't want to create a subclass for each viewgroup, and then change all the layouts to use that class.

Is there a better way to do so?

Just to add, the watermark is meant to be "bellow" the widgets, in a similar fashion to a background but with some logic applied to it so that watermark text varies:

watermark example

htafoya
  • 18,261
  • 11
  • 80
  • 104
  • 1
    Subclass `Drawable` and put your `Canvas` code in its `draw()` override, then add that `Drawable` to each `View[Group]`'s overlay: https://developer.android.com/reference/android/view/ViewOverlay. – Mike M. Mar 09 '23 at 17:14
  • 1
    Also, I just realized, if your `minSdk` is at least 24 (I think), you might be able to specify your custom `Drawable` class in a `drawable` resource, which would allow you to set it via the `android:foreground` attribute without the additional overlay code, and you'd also be able to use it in a `style`. I've not tested that yet, though. – Mike M. Mar 09 '23 at 18:18
  • ^That does work, btw, and it is from API level 24, as I'd remembered. Beginning with that version, you can do something like `` in a resource drawable XML file, and use that for `android:foreground` on a `` or in a `style`. For this setup, the `CustomDrawable` class must have a public empty constructor, so any parameters you might've needed would have to be handled through XML attributes. Those attributes are accessed in code in `Drawable`'s `inflate()` overrides, where you'd handle them similarly to how it's done for a custom `View`. – Mike M. Mar 10 '23 at 00:22
  • Thank you @MikeM., the issue with the overlay is that it is OVER the view, which well that is normal for a watermark but actually I need it to be bellow the other components (kind of a background with a watermark) – htafoya Mar 11 '23 at 05:20
  • I just read the following comments, that seem like a good idea, current version is 23 but maybe I can convince to upgrade it. – htafoya Mar 11 '23 at 05:27
  • You could use `CustomDrawable` as the background instead, then, yeah? Still better than subclassing each one, at least. At 24, you would use it in a `style`'s `android:background`. – Mike M. Mar 11 '23 at 05:32
  • @MikeM.so just to apply it, how could I get the height of the view or estimated height and width when I apply the draw method of CustomDrawable so that I can perform my canvas drawing – htafoya Mar 23 '23 at 03:19
  • 1
    A background drawable fills the `View`, so its bounds will be the same size as the `View`. You can just use `getBounds()` or `bounds`, depending on your language, and then use its `width()` and `height()`. – Mike M. Mar 23 '23 at 03:27

0 Answers0