[Accessibility] Missing contentDescription attribute on image
What does this error mean and how can I fix it?
<ImageButton
android:id="#+id/callbannerbuttonpg1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="157dp"
android:background="#null"
android:src="#drawable/callbanner"
android:visibility="invisible" />
Designing for Accessibility
"Defines text that briefly describes content of the view. This property is used primarily for accessibility. Since some views do not have textual representation this attribute can be used for providing such."
And this is not error just a warning. Usage of this attribute is optional.
Add to the imageButton
android:contentDescription="#string/desc"
Just add this:
android:contentDescription="#string/description"
then go to yours Strings.xml and add this:
<string name="description"> YOUR_DESCRIPTION_HERE </string>
as of your code:
<ImageButton
android:id="#+id/callbannerbuttonpg1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="157dp"
android:background="#null"
android:src="#drawable/callbanner"
android:visibility="invisible"
android:contentDescription="#string/description" />
You can ignore such warnings as well by adding this in the imageButton tag :
tools:ignore="contentDescription"
Related
In my app, Talkback is not pronouncing the name of the button. Instead it's just pronouncing as "button".
Can anyone help me to do the task?
I tried the following xml
<Button
android:id="#+id/loginsuccess"
android:scaleType="fitXY"
android:layout_width="100dp"
android:layout_height="30dp"
android:background="#drawable/submit_button"
android:layout_alignParentRight="true"
/>
that's not working
You should add the following in button xml declaration:
android:contentDescription="buttonName"
I simply added a property android:contentDescription=" " in the xml file, Like the following
<Button
android:text="Login"
android:textSize="0sp"
android:id="#+id/loginsuccess"
android:scaleType="fitXY"
android:layout_width="100dp"
android:layout_height="30dp"
android:background="#drawable/submit_button"
android:contentDescription="login"
android:layout_alignParentRight="true"
/>
that solved the problem.
For reference it was because you didn't have android:text so there was nothing to read out.
You must Content description is set on Button. Like this android:contentDescription="login"
I've got a button and a drawable on the left of the text, but I want the drawable to be closer to the text. So I need to move the drawable.
I've defined android:drawableLeft but the content of the button is not centered.
Here is my code:
<Button
android:id="#+id/addsubject"
android:layout_width="250dp"
android:layout_height="wrap_content"
android:drawableLeft="#drawable/ic_action_add"
android:layout_centerHorizontal="true"
android:layout_alignParentTop="true"
android:layout_marginTop="20dp"
android:text="#string/addsubject"
android:textColor="#color/white"
android:background="#drawable/custombutton1" />
Here is how it looks now:
And here is how I'd like it to be:
Thank you!
Use the app:iconGravity="textStart" attribute in the MaterialButton
<com.google.android.material.button.MaterialButton
app:icon="#drawable/ic_add_24px"
app:iconGravity="textStart"
../>
If you want to reduce the padding between the icon and the text just use the app:iconPadding attribute:
<com.google.android.material.button.MaterialButton
app:icon="#drawable/ic_add_24px"
app:iconGravity="textStart"
app:iconPadding="0dp"/>
try this
<Button
android:id="#+id/addsubject"
android:layout_width="160dip"
android:layout_height="60dip"
android:layout_gravity="center"
android:drawableLeft="#drawable/ic_action_add"
android:drawablePadding="2dip"
android:gravity="center"
android:paddingLeft="30dip"
android:paddingRight="26dip"
android:singleLine="true"
android:text="#string/addsubject"
android:textSize="13dip" />
I got this result by simply putting these properties to the button specifically
<Button
...
android:paddingLeft="60dp"
android:drawablePadding="-50dp"
android:gravity="center"
android:drawableLeft="#drawable/your_button_icon"
/>
You can tune in with your own values for your desired results.
Cheers :-)
I done this simply using paddingLeft and paddingRight. Its works!
...
android:paddingLeft="25dp"
android:paddingRight="25dp"/>
You can use LinearLayout as your button by adding OnClickListener and customize however you want.
<LinearLayout
android:layout_width="160dip"
android:layout_height="60dip"
android:orientation="horizontal"
android:background="#drawable/custombutton1">
<ImageView android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:src="#drawable/ic_action_add"
android:layout_gravity="center"
android:layout_weight="1"
android:scaleType="center">
</ImageView>
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/addsubject"
android:layout_gravity="center"
android:layout_weight="1"/>
</LinearLayout>
The padding between the left border of button and right border of the icon is controlled by android:paddingLeft property. The padding between the icon and the text is defined by android:drawablePadding property.
Add these two properties to you button and set the values you are happy with.
<Button
...
android:paddingLeft="24dp"
android:drawablePadding="8dp"
/>
There's also other possibilities that you want to inflate the drawable/icon programatically, after reading a while and using a lot of combination using XMLs, but still doesn't works,
You might want to fork below library instead, to suits your use-cases, such as, customizing the view, position, etc programatically, see below reference:
Drawable/icon aligned with text, see sample here
Button like facebook sign-in, see sample here
For RadioButton, see full gist code here
P.S: Copyrighted and regards to the author
for Materialbutton, you have to use the icon attribute and you can also change its tint with iconTint like this example:
<com.google.android.material.button.MaterialButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:icon="#drawable/ic_cancel"
app:iconTint="#color/black"
android:text="text" />
<TextView
android:id="#+id/first_textview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/hello_world" />
<TextView
android:id="#+id/second_textview"
android:layoutbelow="#id/first_textview"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="This is my first Android Application!" />
<Button
android:id="#+id/first_button"
android:layoutbelow="#id/second_textview"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="And this is a clickable button!" />
I got error when tried to run.
There is a "red" cross at the lines of:
- second
Any idea?
At following two places:
android:layoutbelow="#id/first_textview"
android:layoutbelow="#id/second_textview"
Change layoutbelow to layout_below. Note the use of underscore: _. If you use Ctrl + Spacebar while writing these attributes, eclipse will show you the options available(In case you use eclipse). This way you decrease the chances to such typo mistakes. Hope this helps.
Replace your textview and button line-
android:layoutbelow="#id/first_textview"
with this one-
android:layout_below="#+id/first_textview"
Use "#+id" in giving reference, not the "#id" here and you are missing the "_" in the layoutbelow attribute.
I am an entry level software developer, and have found tons of great answers from this site, but I can't seem to find how to hide the 'box' of a checkbox in Android. I just want the check mark to show, when a user selects an option. Here are some of the most recent things I have tried.
chkSortOrder.setBackgroundResource(android.R.color.transparent);
chkSortOrder.setBackgroundResource(android.R.drawable.checkbox_off_background);
Both of these still show the box.
put the following attribute in your checkbox tag in XML.
android:button="#android:color/transparent"
It's quite late but in any case, if it helps anyone. If you wanna set custom background to RadioButton programmatically, then you can set it like this,
checkbox.setButtonDrawable(null);
checkbox.setBackgroundResource(R.drawable.your_custom_drawable);
It's 2020! and many things have changed. But if you are struggling with this like me and tried everything here (and other solutions) with no luck, then try this one too. What I've got with other solutions came to this (Emulator, API 16):
Weird behavior! Two drawables, right is my custom box and default one on the left. And then accidentally i added this:
app:buttonCompat="#null"
and the second one is gone finally!
Here is the complete definition:
<CheckBox
android:id="#+id/cb_fs_ExactSearch"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:button="#null"
android:gravity="center_vertical"
android:padding="5dp"
android:text="#string/fs_options_exact"
android:textColor="#color/textPrimary"
android:textSize="#dimen/textSmall"
app:buttonCompat="#null"
app:drawableRightCompat="#drawable/drw_checkbox" />
You need to set both button and buttonCompat to null.
I found this error when updating from androidx.appcompat:appcompat:1.0.0 to 1.1.0.
None of the other answers were useful to me as setting button attribute to #null or #android:color/transparent doesn't work on Android API Level 20 or lower.
What I did is to change my CheckBox to ToggleButton and set textOn and textOff attributes to an empty string
<ToggleButton
...
android:background="#drawable/custom_selector"
android:textOff=""
android:textOn="" />
This way I could update to androidx appCompat 1.1.0
You can also toggle between a CheckBox and a TextView. Just make one view visible and another one hidden. This also makes sense because the CheckBox contains paddings around the check mark.
<FrameLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
>
<CheckBox
android:id="#+id/checkbox"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:checkMark="#null"
android:checked="true"
android:gravity="center_vertical"
android:paddingStart="3dp"
android:paddingLeft="3dp"
android:text="My text"
android:textColor="#color/black"
android:textSize="12sp"
/>
<TextView
android:id="#+id/label"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="My text"
android:textColor="#color/black"
android:textSize="12sp"
/>
</FrameLayout>
I am trying to replace my previous ugly text button with an imagebutton. However, after changing the XML file with the following ImageButton code, my application won't even start. Why?
<ImageButton
android:layout_height="fill_parent"
android:id="#+id/refresh"
android:src="#drawable/refresh"
/>
specify the layout width..
android:layout_width="wrap_content/*some value ex:50dp*/"
It turned out that I forgot the layout_width attribute. It works now.
<ImageButton
android:layout_height="fill_parent"
android:id="#+id/refresh"
android:src="#drawable/refresh"
android:layout_width="wrap_content"
/>