I am working on an Android Library, which makes buttons of different shapes. The Button's XML looks like this:
<com.singh.daman.mybutton.ShapedButton
android:id="#+id/round"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="wrap_content"
app:fill_color="#color/colorPrimaryDark"
app:button_type="round_rectangle"
app:stroke_color="#color/colorAccent"
app:stroke_width="12sp"
app:text="Round Rectangle"
app:text_size="16sp"
app:text_color="#ffff"
android:layout_gravity="center"
android:background="#null" />
In which attribute app:button_type="round_rectangle" have different values like rectangle, star, circle and round rectangle.
The type round_rectangle is supported by only Lollipop and Above android versions.
So, When the library user sets button_type to round_rectangle and the app minimum android version is less than Lollipop, I want to show an error that it is only supported by api 21 and above, How can I do that?
You need a custom lint rule. I've never done this, but there are writeups from Google on the topic.
http://tools.android.com/tips/lint-custom-rules
Related
I have a Material Button (from Google's Material Components) which can cast black shadow normally. But when I change the shadow color, it won't appear at all. Here's the button's XML portion :
<com.google.android.material.button.MaterialButton
android:id="#+id/next"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="#dimen/margin_activity_horizontal"
android:enabled="false"
android:elevation="8dp"
android:layout_margin="16dp"
android:text="#string/setup_next"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintTop_toBottomOf="#id/nickname_entry_wrapper"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
tools:enabled="true"
android:textSize="18sp"
android:textColor="#000000"
android:backgroundTint="#FFFFFF"
android:fontFamily="#font/poppins_semibold"
android:paddingVertical="12dp"
android:outlineAmbientShadowColor="#FFFFFFFF"
android:outlineSpotShadowColor="#FFFFFFFF"
android:outlineProvider="background"
android:translationZ="8dp"
/>
As you can see, outlineAmbientShadowColor and outlineSpotShadowColor are supposed to change the shadow color that results from elevation and Z translation, right ? I tried adding both elevation and Z translation at once but nothing works, the button still has 0 shadow.
The button is a child view of a Constraint Layout that has a black background. As you can see, I even tried using different values for outlineProvider such as bounds or paddedBounds, yet..nothing.
If anyone has experience with these outline attributes, I'd love to have this solved. The problem isn't specific to Android 11 only, I am just saying that I am using Android 11 for testing, since those outline attributes are only available on Android versions later than Oreo.
As Mike M. has mentioned in the comment, those attributes don't really change the color, they just add a little 'tint'.
If you wanna use material buttons with more features, you should take a look at this library :
Github Repo: Carbon by ZieIony
It should fulfill the required objective (casting white shadows) successfully and without issues on all Android APIs.
Take this android layout XML snippet for example:
<ImageView
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_gravity="center"
android:layout_weight="1"
android:padding="10dp"
app:srcCompat="#drawable/bitcoin"
android:contentDescription="#null"
android:importantForAccessibility="no"
tools:ignore="ContentDescription" />
What is the difference between android:contentDescription="#null" and tools:ignore="ContentDescription"?
I know both of them are used to indicate that a certain non-textual element carries no meaning and is only meant for decoration. Is there an advantage of using one over the other, should I use both, is it preference, or is one considered better and newer than the other?
Also, should I use android:importantForAccessibility="no" or is using all three attributes/properties simply overkill?
What is the difference between android:contentDescription="#null"
and tools:ignore="ContentDescription"?
android:contentDescription="#null"
Used to indicate that a certain non-textual element carries no meaning and is only meant for decoration.
tools:ignore="ContentDescription"
For graphical elements, such as ImageView and ImageButton. If you do not set their respective android:contentDescription XML attributes, a lint warning message will be displayed.
"Missing contentDescription attribute on image"
To suppress this lint warning message then you must use tools:ignore="ContentDescription" in XML.
I know both of them are used to indicate that a certain non-textual
element carries no meaning and is only meant for decoration. Is there an advantage of using one over the other, should I use both, is it preference, or is one considered better and newer than the other?
No, they are different from each other in term of usage, for example
<ImageView
android:layout_width="200dp"
android:layout_height="300dp"
android:id="#+id/image_user_avatar"
android:contentDescription="User avatar"
tools:ignore="ContentDescription" />
When running the app with TalkBack, it will speak "User avatar".
Should I use android:importantForAccessibility="no"?
If your app only supports devices running Android 4.1 (API level 16) or higher, you can set these elements' android:importantForAccessibility XML attributes to "no" instead of android:contentDescription="#null.
Update
So basically tools:ignore="ContentDescription" is only for the
compiler and android:contentDescription="#null" is for user user?
Yes, it is.
Also, my 'minSdk' is 14 and my 'targetSdk' is 28. Can I still set both
android:importantForAccessibility="no" and
android:contentDescription="#null"?
Yes, you can set both of them but if you run the app on device whose SDK below 16, android:importantForAccessibility="no" will be ignored.
Will android:contentDescription="#null" have the same effect as
android:importantForAccessibility="no" for devices running Android 4.1
or higher?
They have slightly difference.
android:contentDescription="#null": The view with this attribute still highlighted when users move finger on it and Accessibility Services will speak out loud dummy text such as "Button", etc.
android:importantForAccessibility="no": The view with this attribute is disabled by the app so it will not highlighted when users move finger on and ignored by Accessibility Services as well.
I use this code in order to adding a line between my GUI componenets
<View
android:layout_width="fill_parent"
android:layout_height="2dp"
android:background="#3f9fe0"
android:layout_centerVertical="true"
android:layout_toLeftOf="#+id/titleOfSection"
android:id="#+id/sectionLine"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp" />
but it seemst that View is supporting from android api level 14+. I am using support library so it is possible to change this code in a way that android 2.2+ support the View too?
You are wrong, Views was created since api 1 as stated in the documentation.
I just tried your code with a minimum of 1 and maximum of 8 and it still works maybe you are referring to a different View, because if View does not exist since api 1 then buttons, TextView, etc wont exist as well because they inherits from View.
I'm compiling using API 16, but with the minimum supported API set to 8. I'm trying to set the alpha of an ImageView in XML, but every time I use
<ImageView
android:id="#+id/image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:alpha="0"
.
.
. />
AAPT complains that alpha isn't a valid attribute, even if the documentation in the android site states that ImageView's supposed to inherit it from View.
Is this a bug, or is this really how it's supposed to work. I'd set the Visibility, but that won't work well with my animations to show and hide the image.
Yes. You said that your min is 8. However, the View's alpha xml attribute doesn't appear until API Level 11.
http://developer.android.com/reference/android/view/View.html#setAlpha(float)
I have a fairly simple xml file that has an image button in it. The image shows up fine on the Graphical Layout xml designer, shows up fine when I run a development build, but as soon as I create the signed apk file and run it, the image no longer shows up. It's just an empty button. I can't think of a reason why, any ideas? The xml file looks like this:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/navigation_root"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TextView
android:id="#+id/navigation_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="5dp"
android:text="TextView"
android:textAppearance="?android:attr/textAppearanceLarge" />
<SeekBar
android:id="#+id/navigation_seekbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_weight="1"
android:padding="5dp" />
<LinearLayout
android:id="#+id/linearLayout1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="5dp" >
<ImageButton
android:id="#+id/part_select_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/chapter_select" />
<Button
android:id="#+id/navigation_ok_button"
android:layout_width="75dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="#string/ok" />
<Button
android:id="#+id/navigation_cancel_button"
android:layout_width="75dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="#string/cancel" />
</LinearLayout>
</LinearLayout>
The image #drawable/chapter_select is a fairly small (41*41) png file that is in the res/drawable folder.
Seems like this is a bug with android, where sometimes the first image in the drawable folder doesn't show up. Added a dummy image called aaaa.png to the drawable folder and problem was solved. Found the answer here: ImageButton does not display a particular drawable
One of the reason is:
If you are using Vector file as a drawableLeft or drawableRight (or drawableStart or drawableEnd) in layout.xml, then you have to use androidx.appcompat.widget.AppCompatButton (formerly android.support.v7.widget.AppCompatButton) instead of Button.
Simple View like Button or Textview doesn't support Vector file as a drawableLeft or drawableRight (or drawableStart or drawableEnd) in my case.
Had the same issue and resolved it by removing all special characters. In my case it was dashes '-' in the filename:
background-720.png => background.png.
try to put the image in drawable-hdpi and drawable-mdpi folder
depends on what device you run you app , the image is searched in these folders...
But puting in drawable means that the image should be available everywhere, but somethimes (depends on your manifest settings) this could not be true, I mean you can turn of the compatibility mode.
also you can try dinamically at run time to set the image to the view
iv.setImageResource(R.drawable.somethig);
My situation was weird.Everything was correct until integrating FireBase Crash report to my Application.
I just added compile 'com.google.firebase:firebase-crash:11.0.1' & DrawableLeft vanished .When i went through the xml , noticed a warning (In lined below).
So added android:drawableStart & issue gone.
Still I am wondering about the relation of FireBase Crash reporting to the same.
Using left/right instead of start/end attributes Using Gravity#LEFT
and Gravity#RIGHT can lead to problems when a layout is rendered in
locales where text flows from right to left. Use Gravity#START and
Gravity#END instead.
Similarly, in XML gravity and layout_gravity attributes, use start
rather than left. For XML attributes such as paddingLeft and
layout_marginLeft, use paddingStart and layout_marginStart.
NOTE: If your minSdkVersion is less than 17, you should add both the
older left/right attributes as well as the new start/right attributes.
On older platforms, where RTL is not supported and the start/right
attributes are unknown and therefore ignored, you need the older
left/right attributes.
There is a separate lint check which catches that type of error.
(Note: For Gravity#LEFT and Gravity#START, you can use these constants
even when targeting older platforms, because the start bitmask is a
superset of the left bitmask. Therefore, you can use gravity="start"
rather than gravity="left|start".)
Check your image size. If you're using an unnecessarily large asset when actually deployed it might just not show despite looking correct in the designer.
Well ! in my case setting MinifyEnabled false and shrinkResources false is working fine now.
i was getting image from drawable. it was working pretty nice in debug version but after release version of apk it was showing sometime blank ImageView.
minifyEnabled false
shrinkResources true
**
See the Screenshots
**
Hope this may help anyone.
I had a similar problem where a drawable png was not showing up in Android Studio. Deleted the file and added it again in the drawable folder and it Worked for me.