Unknown attribute android:elevation - android

I'm trying to follow a tutorial that suppose to show how to add a floating button and in the tutorial it says to add the attribute android:elevation to the buttons xml like this:
<ImageButton
android:layout_width="30dp"
android:layout_height="30dp"
android:id="#+id/editButton"
android:layout_gravity="center|right"
android:clickable="false"
android:background="#drawable/edit_grey"
android:layout_marginRight="1dp"
android:elevation="#dimen/elevation_low"/>
but it dosen't recognize that attribute...i believe it has something to do with my project target or sdk...can somebody help me?

To use android:elevation, just as with any other Android 5.0 API, you must compile against Android 5.0 (API 21). This does not mean you have to change your target SDK level or minimum SDK level.
Note: your XML file may still give a warning that android:elevation only works on Android 5.0 or higher. This warning just serves to tell you that previous versions of Android will not have an elevation shadow on the floating action button. However, that does not cause an error - previous versions of Android will ignore XML attributes they do not understand.

The Elevation attribute is pretty new. It defines the lift of the view it is applied to. It is used for the Material Design in the newest Android Versions.
Material Design
You can use it on API Level 21, I guess your targeted Level is lower than this.
If you just want to achieve some shadowing you can use this:
Shadow Drawables for Views
Or you can use the SupportLibrary with CardViews or something like that, they support elevation from API Level 7:
[How-to] Use the v21 Support Libs on Older Versions & Target L While Remaining Backwards-Compatible
Hope this helps.

Related

android:tint on background drawable of a layout on sdk<21

I know there are many topics discussing tint functionality prior to sdk 21, but
they either use some kind of imageviews (which I dont) or solve it programmatically (which I dont want to).
My case:
I have a constraint layout with a 9-patch drawable as background:
<android.support.constraint.ConstraintLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/testDrawable">
This is my drawable:
<nine-patch xmlns:android="http://schemas.android.com/apk/res/android"
android:src="#drawable/ninepatchdrawable"
android:tint="#color/lightBlue"
android:tintMode="multiply" />
This works fine for updated devices, but as I said the tint isn't working for devices sdk < 21. I don't get any kind of error message either.
I already tried changing my layout to:
<android.support.constraint.ConstraintLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/testDrawable"
android:backgroundTint="#color/lightBlue"
android:backgroundTintMode="multiply">
or using app:backgroundTint as suggestet in another question.
Thanks in advance
You can try something like this:
ImageView.getDrawable().setColorFilter(new PorterDuffColorFilter(context.getResources().getColor(**color**), PorterDuff.Mode.SRC_IN));
Don't believe it'll work with a lower API -> but its a good way to change tint on the fly
There is an obvious reason why.
If you check the docs. The first part of the first line is:
With Android 5.0 (API level 21) and above,
Because android:tint was added with material design in API 21. And because it was added in API 21, it is a tag that has no meaning on API 20 and lower.
Using the support library (appcompat library) should allow you to use it. You need to add another namespace, but that isn't the biggest job to do. You can also do it in styles.xml
And backgroundTint is something completely different as you can see in this question

Show error for unsupported custom attribute

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

Can't set alpha of ImageView in XML

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)

UnsupportedOperationException in custom view

I have a custom view which throws on some devices the UnsupportedOperationException. It looks like that:
java.lang.UnsupportedOperationException
at android.view.GLES20Canvas.clipPath(GLES20Canvas.java:287)
[...]
I know there's the setLayerType method but this is only supported since API level 11. How do I fix that problem for API level 8?
Here's a similar thread but the solution doesn't work for me because I'm using API level 8.
You can define layerType in layout XML.
<com.nth.android.ui.view.CircularProgressBar
android:layout_width="96dp"
android:layout_height="96dp"
android:background="#android:color/white"
android:src="#android:color/black"
android:layerType="software" />
The project where I've used features unsupported by hardware acceleration had target SDK version set to 14 and minimum SDK version was 8. Attribute layerType="software" worked on both Honeycomb (Android 3.2) and Gingerbread (Android 2.3.3).
Also, in some cases layerType="software" has to be set for view groups (layouts) that contain view in question.

Setting Alpha level on view doesn't work

I am trying to set the alpha level of a view.
I am reading the documentation here:
http://developer.android.com/reference/android/widget/RelativeLayout.html
and it seems as if it should be possible even on android 2.1 which I am developing for (as I cant see it saying otherwise) but when I add it to my view
<View
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_margin="20sp"
android:background="#ffffff"
android:alpha="0.9"></View>
I get an error saying:
No resource identifier found for attribute 'alpha' in package
'android'
How do I set the alpha level of a view?
you can set the alpha with the background properties like this way
android:background="#50ffffff"
here the first 50 value is set the alpha value
On which android version are you running this?
setAlpha is only available on api level 11. You might try some other way to do this if you want to do this in previous versions. Probably using a semi-transparent background could do it.

Categories

Resources