Is there an alternative to Android's ViewOverlay? - android

Android's ViewOverlay allows easy overlaying of Drawables over a conventional view element without having to interfere with the layout, but it is only available since API 18 (and as far as I can see, there's no compatibility layer, that backports this).
What would be the easiest alternative for an older API level (specifically: 15) to overlay a Drawable over a view element without modifying the layout? In my case, it would be enough to deal with a single Drawable; I don't need to replicate support for multiple Drawables that the new overlay API allows.

Did you try FrameLayout's foreground attribute?
FrameLayout's foreground is drawn over it's contents. And this works with API level 15.
In your xml:
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:foreground="#drawable/your_drawable">
...
Your contents.
...
</FrameLayout>
Or in your source code:
frameLayout.setForeground(drawable);

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

"Ugly" CardView on Pre-Lollipop devices

I am using CardView as custom item for RecyclerView. They looks good on Android 5+ but so different on older Android versions.
On Android 5 +
On Android < 5
The code is the same:
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minHeight="?android:attr/listPreferredItemHeight"
card_view:cardCornerRadius="1dp"
card_view:cardElevation="1dp">
... other items ...
</android.support.v7.widget.CardView>
Is there a way to achieve the Android 5+ behavior on pre-Lollipop devices?
Using the support CardView? No.
Personally I think that the support CardView is broken and shouldn't be used at all. It looks and works a little bit different on Lollipop and on older systems. The shadow is different, the padding is different, content clipping doesn't work on pre-Lollipop devices, etc. The API is also weird and confusing. That's why it's hard to get good results on all platforms. If you can live without cards, I would go that way.
Of course it's possible to create a custom, nice-looking, backwards-compatible card, but it's a bit complex task. To create a card on your own you have to implement:
rounded corners with content clipping (doesn't work in the support CardView). Here's how to do it properly.
shadows drawn outside the card (not inside, like the support CardView). This one depends on your needs. I would override drawChild(...) in a parent container, where I could draw shadows around cards freely. Shadow generation method doesn't matter - it could be a gradient, a static 9-patch or a RenderScript-blurred black shape.
I was frustrated by the look and the API of CardView as well, so I created my own implementation. It can be found on GitHub - the library is called Carbon and using it is probably the easiest way to get a decent card. After importing the library simply add style="?attr/carbon_cardViewStyle" to any layout to make it look like a card:
<carbon.widget.RelativeLayout
style="?attr/carbon_cardViewStyle"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>

Adding View in xml layout android 2.2+

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.

Android Unable to remove the color at the top and bottom

I have added a scrollView and if deploy on the android tablet,it has some problems. But it works fine on cellphone.
When users move to the top or bottom of the whole page,it will automatically show the blue shadow which indicates users reaching the bottom of the page.
I want to remove those indicator since it affects the UI.
Is there any way to remove or set in the XML?
I have tried different parameter to set on the scrollview but it doesn't work.
Please help.
fadingEdge is deprecated. Use this in your ScrollView: android:overScrollMode="never"
Add this extra line to your ScrollView definition:
android:overScrollMode="never"
or add this to your code:
findViewById(R.id.sobreScrollView).setOverScrollMode(ScrollView.OVER_SCROLL_NEV‌​ER);
Try this:
android:overScrollMode="never"
Add this to the scrollView
android:fadingEdge="none"
Pre API level 14:
android:fadingEdge="none"
API level 14+:
android:requiresFadingEdge="none"
See this:
https://developer.android.com/reference/android/R.attr.html#fadingEdge
This attribute is deprecated and will be ignored as of API level 14 (ICE_CREAM_SANDWICH). Using fading edges may introduce noticeable performance degradations and should be used only when required by the application's visual design. To request fading edges with API level 14 and above, use the android:requiresFadingEdge attribute instead.
setOverScrollMode(View.OVER_SCROLL_NEVER)
As mentioned by Romain Piel on this post: https://stackoverflow.com/a/7106752/956975, Pim Reijersen pointed out in the comments that android:fadingEdge is deprecated and will be ignored as of API level 14.
Remove the shadow on a ListView/GridView/ScrollView like so
in XML:
android:fadingEdgeLength="0dp"
in Java:
view.setFadingEdgeLength(0);

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