I have an app that requires scrollbars to be enabled in one landscape orientation but not in portrait orientation. I thought the easiest way to do this would be to make an attribute indicating whether the scrollbar is enabled or not, such as the following:
<ScrollView
a:layout_width="fill_parent"
a:layout_height="fill_parent"
a:scrollbars="?my_activity_scrollbars"
a:fadingEdge="none"
>
I would then define separate values of the attribute for landscape and portrait modes. Seemed easy.
So I then defined this attribute in attrs.xml:
<resources>
<declare-styleable name="Theme">
<attr name="my_activity_scrollbars" format="enum" />
</declare-styleable>
</resources>
And added it to my app's styles.xml:
<style name="MyApp" parent="#android:style/Theme.Light">
<item name="add_credit_card_scrollbars">none</item>
</style>
However, when I try to compile my app, I get the following error:
styles.xml:8: error: Error: String types not allowed (at 'my_activity_scrollbars' with value 'none').
It seems clear that it's treating "none" as a string rather than an enumerated value. Rather than using "none" I tried things like "?android:attr/scrollbars/none", "?android:attr/none", etc, but those didn't work.
How can I specify the "none" value as an enumerated value instead of a string?
I believe you have your attr definition set incorrectly:
This is an attr enum from the android source:
<attr name="ellipsize">
<enum name="none" value="0" />
<enum name="start" value="1" />
<enum name="middle" value="2" />
<enum name="end" value="3" />
<enum name="marquee" value="4" />
</attr>
It appears you do not define format="enum" and instead specify the enum values within the attr tag itself.
Related
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?
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
I develop a custom view in Android Studio. This custom view has a custom property. The possible values of this property are declared as an enum-formatted attr in a declare-stylable tag within a resources tag, all that in an xml file (see below).
Even though there are quite some questions (including answers) about accessing xml-defined enums in this forum, none of those actually helps me in solving my concrete problem.
From within the file customview_values.xml:
<declare-styleable name="CustomView">
<attr name="customViewMode" format="enum">
<enum name="CUSTOMVIEW_UNDEFINED" value="0" />
<enum name="CUSTOMVIEW_BLINKING" value="1" />
<enum name="CUSTOMVIEW_ROLLING" value="2" />
<enum name="CUSTOMVIEW_PULSING" value="3" />
<enum name="CUSTOMVIEW_NOISE" value="4" />
</attr>
</declare-styleable>
You might accept the following circumstances: Whenever an enum's value in this xml file is changed, this change shall propagate consistently to the whole application. There should be no need to have to adapt a programmatical list somewhere else in the code, whenever one changes this xml file, because this would be very error prone (and hence not in the interest of a reasonable developer).
For this reason, I need a solution, where I can get the value of each entry of the above attr list by its name, programmatically (!).
A second application of what I am looking for is testing: I want to write a test, which checks the consistence of the above-defined list, this test necessarily needs to read the respective values by their names from the xml file.
Please help me, my fellow geniuses, you're my only hope!
I've made a custom controller to check how android works with attributes, so to do that I also created a file named attrs.xml and set it like below
attrs.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="durationTextView">
<attr name="showtype" format="enum">
<enum name="hhmmss" value="0" />
<enum name="hhmm" value="1" />
<enum name="mm" value="2" />
</attr>
</declare-styleable>
</resources>
activity.xml
<com.example.mustafaguven.widgets.DurationTextView
android:id="#+id/lblDuration"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
custom:showtype="mm"
/>
What I'm gonna ask is that why I'm getting an error like I shared as title if I set showtype as "mm". Android has some problems specifically with the name "mm", and it's really weird because if I changed "mm" with another name, there is no any error. It works perfectly.
I'm just curious about what makes it believe its an error. please try it and see it yourself.
I have 2 attributes (amongst many others) in a styleable in attrs.xml.
Given these two:
<attr name="enableScrolling" format="boolean"/>
<attr name="showPadlock" format="boolean"/>
Is there any way to make them mutually exclusive? The design is for a generic scale to display alongside a graph. The specific use case for an instance of the generic scale is that if scrolling is enabled, the scale displays a padlock icon which toggles between locked and unlocked and disables/enables the scrolling at runtime.
Showing the padlock is not meaningful if enableScrolling is false. I deal with this in code but it would be much cleaner if I could somehow convey the semantics in XML.
I'm guessing that the answer is no since there is no UX paradigm in the IDE to deal with it but I welcome any thoughts.
Thanks.
You have 3 ways:
just ignore showPadlock if it has no sense
check attributes in code. Show warning message or throw an exception if you notice invalid combination.
create enum attr. For instance:
<attr name="scrollType">
<enum name="simpleScroll" value="1" />
<enum name="scrollWithPadlock" value="2" />
<enum name="none" value="3" />
</attr>
In my application I'm using the following custom attribute for a custom view:
<attr name="direction">
<enum name="up" value="1" />
<enum name="down" value="2" />
</attr>
The thing is that in my custom view I must compare the current direction with the possible
ones. Is there a way to access the values for the up & down attributes?
I have stumbled upon this AttributeSet class . It should help you to retrieve data from compiled XML files.