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.
Related
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
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
Here is my EditText.
My issue is that Android ignores the layout_marginEnd in Lollipop and above
and uses layout_marginRight instead.
See two screenshots below:
<EditText
android:id="#+id/foo_count_edit_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_below="#+id/foo_container"
android:layout_marginEnd="20dp"
android:layout_marginRight="100dp"
android:layout_marginTop="-6dp"
android:hint="#string/foo_default_value"
android:imeOptions="actionDone"
android:inputType="number"/>
As can be seen, in API 21 (left) the margin is 100dp, taken from layout_marginRight and not layout_marginEnd.
Edit:
#ErnirErlingsson Nailed it: Need to enable android:supportsRtl in the manifest in order to use layout_marginEnd instead of layout_marginRight.
Thanks.
Edit 2:
Note however that in API 17 (right screenshot) the right margin is taken from layout_marginEnd while the android:supportsRtl is false.
One replaces the other, if you decide to support RTL now or in the future you should always use marginStart and marginEnd instead of using marginRight and marginLeft, otherwise you are fine with the latter.
http://developer.android.com/about/versions/android-4.2.html#RTL
According to the corresponding docs RTL support should be disabled per default and therefore the left/right margins should be used. To my knowledge this does not differ between the OS's versions that are visible on the screenshots that you posted with your question, so as to exactly why this is happening is a bit of a mystery. My first guess is that this has something to do with the emulator and I would confirm this on actual devices with the respective OS versions.
EDIT
When supporting older OS versions you do indeed need to add both to your layout but to make sure that marginStart and marginEnd are used you need to set the android:supportsRtl attribute to true. Check out the first link I posted above for more information.
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.
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)