<ImageView style="#style/LoaderPrevNext.Next" />
using the styles
<style name="LoaderPrevNext">
<item name="android:layout_width">40dp</item>
<item name="android:layout_height">80dp</item>
<item name="android:layout_weight">0</item>
<item name="android:scaleType">fitXY</item>
</style>
<style name="LoaderPrevNext.Next">
<item name="android:src">#drawable/next_page</item>
<item name="android:contentDescription">#string/img_desc_next_page</item>
</style>
annoys me with the [Accessibility] Missing contentDescription attribute on image warning.
It disappears if I move or copy the contentDescription from the style to the ImageView tag, but as the src is defined in the style, I'd love to keep them together.
Guess it's simply an SDK bug, but some might got a workaround...
Clarification: The ImageView in question do have a content-description, defined in the LoaderPrevNext.Next style. The warning is false. I'm not interested in ideas on how to set the content description, or how to hack them empty.
In Eclipse Window->Preferences->Android->Lint Erroe Checking->Right Side Scroll Down Until Accessibility->Content Description->Severity->Select Ignore->Apply->yes
How To Suppress Android Accessibility Warning Missing contentDescription
Both of the existing answers to this question are technically correct, but I believe there is a better way. The first answer suggests turning off all contentDescription warnings. While this works, contentDescription is there for a reason, and perhaps should not be globally turned off for all content.
The second answer shows how to specify the contentDescription. While this does make the warning go away, it is not appropriate for all content and technically does not answer the question, although it is a valid work-around.
The Android documentation for lint provides the best solution, IMO, which is a mixture of providing contentDescription for some content, and suppressing the lint warning for other content:
Note that elements in application screens that are purely decorative
and do not provide any content or enable a user action should not have
accessibility content descriptions. In this case, just suppress the
lint warning with a tools:ignore="ContentDescription" attribute.
Therefore, Implement the following solution to remove all of the lint warnings:
Define contentDescription in your XML layout file with android:contentDescription="Your description here." for resources that provide interesting or
useful information to the user. And,
Add tools:ignore="ContentDescription" to purely decorative content or
images.
To disable missing content description warning in the whole project, add this to your build.gradle
android {
...
lintOptions {
disable 'ContentDescription'
}
}
If you do not wish to turn off / ignore the lint warnings you could define an empty string in strings.xml
string.xml:
<string name="empty"></string>
and then in your xml just set the value
<ImageView
android:id="..."
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:contentDescription="#string/empty"/>
contentDescription tag is used for screen reading feature of android. you can add a string about what is your image is or you can just suppress/ignore this warning by adding the following tag.
tools:ignore="ContentDescription"
< ImageView
android:id= ...
android:layout_width= ...
...
android:contentDescription="your description comes here. get from strings xml if its a string"
/>
Why would you want to have image description in Style.
The purpose of styles is to have an ability to change something in one place which is reflected in several places.
Same goes for #string resource. Simply don't put image description in style. Leave #string. Once needed - change that string itself.
I think that the correct solution is to report this as a bug in Android Studio, though I doubt that they will fix it anytime soon (from experience with my bug reports about other things). https://developer.android.com/studio/report-bugs
I'm sorry that so far you have had only off-topic answers that totally ignore the key elements of your question, and answers that claim that styles aren't meant for doing what you want to them (even though the docs never warn against what you are trying to do). I came here because I have the same problem as you have (in 2023 and which I encountered around 2020). I don't see any sign of Android Studio / Android Lint offering an option for taking styles into account. It doesn't even look like there is a point to not taking styles into account, therefore there shouldn't be an option for this, any rule that checks presence or content of specific attributes should also look up the style specified by style="".
Related
1.) Is there any reason to have a default value inside an android xml layout?
Ex.) The TextView below has included a default value of
android:visibility="visible"
`<TextView
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:visibility="visible"/>`
Conjecture: Because this is a default value, it has no effect, and therefore is an unnecessary line of code in the XML file. Is that line of thinking correct?
2.) If there is no reason for default values to exist in Android xml files, is there a lint plugin available to point out default value code lines in android XML files?
It is my thought that a large number of code lines in XML files are default values, serving no purpose. What can we do to reduce these lines of code?
U can create a style with your default values and use it.
For example:
<style name="DefaultTextViewStyle">
<item name="android:visibility">visible</item>
</style>
to use this:
<TextView
style="#style/DefaultTextViewStyle" />
I had some hope that the Lint inspection Redundant default value attribute assignment for xml, run via Android Studio might have done what you're asking. You can run it as specified under the Manually Run Inspections part of the Android docs. i.e.Android Studio -> Analyze -> Run Inspection by name -> enter "Redundant default value attribute assignment", then select the scope for the Lint check.
Sadly though, it doesn't pick up the case you mention above. I'm just wondering if there's something I've missed, or if this isn't intended for Android xml in some way?
I don't know all features of Android Studio, I have tried to search,but found nothing.
I wonder if it is possible to make Android Studio autoinsert attributes required for right-to-left support.
For example I have typed following line
android:layout_marginLeft="10dp"
Is it possible to make Android Studio insert marginStart attribute automatically ?
android:layout_marginLeft="10dp"
android:layout_marginStart="10dp"
And vice-versa(for padding too).
This would save some time.
Maybe someone knows how to get such behavior, I will be grateful for any help. Thx.
Apparently, you can't do that. You have to write them yourself.
The difference between layout_marginLeft and layout_marginStart for example is that layout_marginLeft is executed for left to right languages like English, while `layout_marginStart' is only executed for right to left languages like Arabic.
So if you are going to add string translations to your app including arabic or any other right to left languages, you will need to write marginStart or marginEnd attributes... So anyway, if you didn't write layout_marginStart for example and your app doesn't support Arabic language or any other right to left languages, no error will occur to the user; it is just a warning that Android Studio tells you.
Definitely the same concept is applied to padding attributes. Hope that helps you.
Edit:
If you don't like to see Android Studio warning you these warnings, you can simply disable that by clicking on the yellow light bulb beside the yellow highlighted warning and selecting Edit 'Using left/right instead of start/end attributes' inspection settings, then uncheck it from the list.
But if you don't want to change the inspection settings, you can just add the following to your View that you don't want to use start/end attributes in it:
tools:ignore="RtlHardcoded"
and add that to your parent layout that contains that View:
xmlns:tools="http://schemas.android.com/tools"
Can anyone explain the question mark means in Android XML attributes?
<TextView
style="?android:attr/windowTitleStyle"
More attributes
/>
The question mark means it's a reference to a resource value in the currently applied theme. See the linuxtopia Android Dev Guide or the android.com Dev Guide for more about it.
\? escapes the question mark.
The ? lets you refer to a style attribute instead of a specific hard-coded resource. See "Referencing style attributes" in the Android Dev Guide for details.
So, how is this actually useful? It makes the most sense when considering multiple themes containing the same custom resource attribute.
Say you have movie-related themes like MyThemeTransformers and MyThemeHobbit, and both have an attribute called movieIcon. And that movieIcon attribute points to a different #drawable resource, say robot.png or hobbit.png, in each theme definition.
You can refer to "?attr/movieIcon" anywhere the theme is in effect (like in a toolbar or dialog or whatever kind of View layout), and it will automatically point to the correct drawable when you switch between themes. You don't need any theme-dependent logic to use the different drawables. You just define the movieIcon attribute for each theme and the Android framework takes care of the rest.
Can anyone explain the question mark means in Android XML attributes?
<TextView
style="?android:attr/windowTitleStyle"
More attributes
/>
The question mark means it's a reference to a resource value in the currently applied theme. See the linuxtopia Android Dev Guide or the android.com Dev Guide for more about it.
\? escapes the question mark.
The ? lets you refer to a style attribute instead of a specific hard-coded resource. See "Referencing style attributes" in the Android Dev Guide for details.
So, how is this actually useful? It makes the most sense when considering multiple themes containing the same custom resource attribute.
Say you have movie-related themes like MyThemeTransformers and MyThemeHobbit, and both have an attribute called movieIcon. And that movieIcon attribute points to a different #drawable resource, say robot.png or hobbit.png, in each theme definition.
You can refer to "?attr/movieIcon" anywhere the theme is in effect (like in a toolbar or dialog or whatever kind of View layout), and it will automatically point to the correct drawable when you switch between themes. You don't need any theme-dependent logic to use the different drawables. You just define the movieIcon attribute for each theme and the Android framework takes care of the rest.
Any searches for any information on how to provide your own custom state for use in a drawable state list selector pulls up very little but almost all of them (here and elsewhere) refer to this google groups post.
I have getters and setters like this: (Not included in the above post, but using their wording to keep it simple)
public void setFried(boolean fried){
if(mFried != fried){
mFried = fried;
refreshDrawableState();
}
}
public void isFried(){
return mFried;
}
I have been trying to get it to work for the last couple hours and nothing seems to be working. It just simply does not change the appearance. I watched what happened as it called onCreateDrawableState(), and I watched what would come out of getDrawableState() after I changed the custom state. The custom state values are in fact appearing in the drawableState array.
Since I can see the state is actually being merged into the array, and since it seems to be completely ignoring any of my custom state in the selector, I think the xml must be wrong.
Here is what the post suggested:
<selector xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res/my.app.package">
<item android:drawable="#drawable/item_baked" state_baked="true"
state_fried="false" />
<item android:drawable="#drawable/item_fried" state_baked="false"
state_fried="true" />
<item android:drawable="#drawable/item_overcooked" state_baked="true"
state_fried="true" />
<item android:drawable="#drawable/item_raw" state_baked="false"
state_fried="false" />
</selector>
Can you really just write state_fried and state_baked or do they need a prefix like app:state_fried? If I try adding a prefix I get a Console error like: No resource identifier found for attribute 'state_fried' in package my.app.package
Has anyone actually got custom states to work? Is the referenced post all you need to get this to work or is there something wrong with it or missing?
I don't know if it makes any difference but I am using an Android Library Project and the selector and the attr.xml is in the Library project.
Thanks
Update
Looks like the problem is that Library Projects don't play well with custom attributes. See here, here, here.
Haven't seen much of a workaround unfortunately...
I wrote that original question. IIRC, that was when Android was in beta, and things have changed a bit. Custom attributes do indeed work; I use them. You do indeed need the app: prefix for custom state attributes. You also need to substitute your app's package in place of my.app.package in the xmlns declaration.
use
xmlns:app="http://schemas.android.com/apk/lib/my.app.package
for attributes declared in the library.