0

I want to create a new xml parameter for one of my custom views to handle the visibility of something within it. I can do it other ways of course, but I would like to do it in a way that I can use normal android params to change it.

something like:

<MyCustomView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:my_custom_visibility="gone"/>

so I coulg just do

viewToBeHidden.visibility = a.getInt(R.styleable.my_custom_view_my_custom_visibility, View.Visible);

I tried because visibilities are integers but it doesn't let me put gone in the view declaration

<attr name="my_custom_visibility" format="integer" />

using

<attr name="my_custom_visibility" format="reference" />

results in a compilation error:

AAPT: error: 'gone' is incompatible with attribute my_custom_visibility (attr) reference [weak].

the other types available for the attribute don't seem to apply to this case.

is there a way to achieve this?

3
  • 2
    try enum type as described here stackoverflow.com/a/54341705/9241978
    – Pawel
    Commented Jan 29, 2020 at 11:57
  • 1
    the return types is enum for View.Visible | View.Gone.. not ineteger Commented Jan 29, 2020 at 11:58
  • thought there will be a better way to do it thanks to both Commented Jan 29, 2020 at 14:16

1 Answer 1

1

Based on @Pawel and @Sam comments, I created a dedicated enum attribute like this:

<attr name="my_custom_visibility" format="enum">
            <enum name="gone" value="8" />
            <enum name="invisible" value="4" />
            <enum name="visible" value="0" />
        </attr>

and use it like this in the view:

viewToBeHidden.visibility = attrs.getInt(R.styleable.my_custom_view_my_custom_visibility, View.VISIBLE);

so it would feel just as the "original" one

Not the answer you're looking for? Browse other questions tagged or ask your own question.