Android Radial Gradient does not work properly - android

I'm working on an Android application in Xamarin. For button backgrounds, I've created a simple XML. The code is:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape android:shape="oval">
<gradient android:startColor="#ff000000"
android:centerColor="#ffffffff"
android:endColor="#00ffffff"
android:type="radial"
android:gradientRadius="100"
/>
</shape>
</item>
</selector>
When I use linear or sweep in android:type code, everything is fine. A good transparent gradient appear in my device (My device is HTC One M8). But when I use radial, a bad solid color appears, no transparency. I want to use this technique for the shadow under buttons, but if you check the screenshot below, a very bad harshy solid color appear under the buttons.
If the code uses RGB or ARGB color, it draws a buggy solid gradient!
Is it a bug in the code, in my device or in the Xamarin deploy setting?

Related

How to have multi-layered kind of shadow/gradient in Android

I am trying to achieve this layout. The upper part is a bit dark and it decreases as we move down making it complete transparent. I tried a couple of gradient variations but didn't get the desired results. Does anybody have idea of how to achieve this. Is it gradient or shadow ?
Create a gradient file and use this code you can increase or decrease transparency as you want
layout-->new file-->layout resource file--> give any name
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape>
<gradient android:startColor="#B71A1A1A"
android:angle="270"
android:centerColor="#00FFFFFF"
android:endColor="#00FFFFFF"/>
</shape>
</item>
</selector>

Color overlay on drawable Android

I've been following this tutorial here Medium - Diagonal Cut View to get that diagonal view effect
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#color/colorPrimary" />
<item>
<bitmap
android:src="#drawable/bebe"
android:gravity="center"
android:alpha="0.1" />
</item>
<item android:top="260dp"
android:bottom="-100dp"
android:left="0dp"
android:right="-260dp">
<rotate
android:fromDegrees="-10"
android:pivotX="0%"
android:pivotY="100%">
<shape
android:shape="rectangle">
<solid
android:color="#android:color/white"/>
</shape>
</rotate>
</item>
</layer-list>
So far the code is the almost the same and the effect is achieved, but only works on Lollipop+, I have searched but cannot find how to have a color overlay on top of an drawable to achieve this same effect and all my tries has being in vain.
The drawable goes in the background property of a RelativeLayout. I have tried to make it in 2 separated ImageView, one for the background image and one for the color overlay, but that doesn't apply the diagonal style right.
How can one achieve this effect for pre-lollipop versions?
Drawable background = relativeLayout.getBackground();
background.setColorFilter(getResources().getColor(R.color.colorAccent), PorterDuff.Mode.SRC_IN);
you can also try SRC_ATOPor MULTIPLY depending on desired effect.
========= EDIT ========================
Ok, I think I now better understand what you are asking. It wasn't entirely clear at first.
You aren't asking about a color overlay per-say, or rather, that is not what your problem is. Your problem lies in your reliance on the alpha attribute.
Do this, I have reordered your elements, so that the colored shape goes on top of the image, and instead of making the image transparent, we make the colored shape's color have an specified alpha byte. You can change the color and alpha as you like.
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item>
        <bitmap
            android:gravity="center"
            android:src="#drawable/muse15fence_750"/>
    </item>
    
    <item>
        <shape android:shape="rectangle">
            <solid android:color="#cc3F51B5"/>
        </shape>
    </item>
    <item
        android:bottom="-100dp"
        android:left="0dp"
        android:right="-260dp"
        android:top="260dp">
        <rotate
            android:fromDegrees="-10"
            android:pivotX="0%"
            android:pivotY="100%">
            <shape
                android:shape="rectangle">
                <solid
                    android:color="#android:color/white"/>
            </shape>
        </rotate>
    </item>
</layer-list>
This is what it looks like in Jelly Bean.

LinearLayout in Android API Level 10 (2.3), background affects whole element

I have the following element
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:background="#drawable/black_border">
And black_border is:
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >
<stroke
android:width="1px"
android:color="#000"/>
</shape>
The desired result is a 1px black border around the elements inside the LinearLayout.
On my device (running 5.0/Level 21) everything looks good, the contents of that element are grouped by a 1px black border.
However, the min sdk my app will support is level 10/2.3 and when I run the app in the emulator the whole background of the LinearLayout is black. It seems like
it is treating the stroke as a fill.
Has anyone come across this?
Edit: I gave the black_border a stroke of white, this somewhat solved my problem. TextViews inside the LinearLayout had no visible border with this change, so a subsequent step was needed to give them a border.
I had the same issue with api level 9.
You have to explicity tell that the solid is transparent.
Try this:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="oval">
<stroke android:color="#color/colorPrimaryDark" android:width="2dp" android:height="2dp"/>
<solid android:color="#color/trasparent" android:width="2dp" android:height="2dp"/>
<size
android:width="10dp"
android:height="10dp"/>
</shape>
where colorPrimaryDark is defined as black (#FF000000) and transparent is #00000000
I suggest you to use dp for dimensions instead of px.
I got round this by giving black_border a stroke of white. Subsequently I needed to give any TextViews inside the LinearLayout a border as having a white background made them borderless.

Android unbounded ripple and background

it is possible to create a RippleDrawable defining an unbounded ripple and at the same time a background color?
I've tried everything but when i define a shape and its color the ripple is not unbounded anymore.
Also in this page https://developer.android.com/reference/android/graphics/drawable/RippleDrawable.html there is no reference about adding an unbounded ripple over a shape.
I've tried with this layer-list but the result is awful
<layer-list
xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<ripple
android:color="#color/android_l_light_gray">
</ripple>
</item>
<item>
<shape
android:shape="rectangle">
<solid android:color="#color/android_l_dark_gray" />
</shape>
</item> </layer-list>
this is what i get
First off keep in mind that layer-list drawable works similar to FrameLayout, it stacks items as they appear so if you want to have your ripple in front just move it down
<?xml version="1.0" encoding="utf-8"?>
<layer-list
xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape
android:shape="rectangle">
<solid android:color="#color/android_l_dark_gray"/>
</shape>
</item>
<item>
<ripple android:color="#color/android_l_light_gray"/>
</item>
</layer-list>
Produces something like
Now you notice how it gets "clipped" by the other buttons? it is actually being overdrawn, this is because of draw order, the other views' background get drawn on top, not that the ripple is actually getting clipped, so I can think of a few solutions for your particular layout:
a. Get rid of the layer-list drawable and use just ripple as your button background, use your shape or color drawable as your ViewGroup background, producing:
Notice that you lost your grid like effect due your other background, but take a look at other number pads on lollipop and you will notice they don't have grid dividers, if you still want them use your ViewGroup to draw them.
b. Use/draw your ripple as foreground/overlay/drawableTop drawable, and the other one as background, but I think you might run into a similar issue with drawing order as before.
c. Had another one in mind but I just forgot, might come back as a dream ¯\_(ツ)_/¯
Check out Calculator from AOSP, you might want to borrow CalculatorPadLayout for your grid, or just learn how they do it :)
XML
<Button
android:id="#+id/btn_key_seven"
style="#style/AppTheme.KeypadButton"
android:text="#string/str_one" />
STYLE
<style name="AppTheme.KeypadButton" parent="Base.Widget.AppCompat.Button.Borderless">
<item name="android:background">#drawable/button_ripple_effect_unbounded</item>
<item name="android:colorBackground">#color/colorPrimaryDark</item>
</style>
Drawable
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<ripple android:color="#color/colorPrimaryDark"/>
</item>
</layer-list>```
Working awesome for me

Android Shape - Gradient should go to the right side

What I try to do
I'm trying to create a Backgroud-Shape for my App. For This I created a gradient witch starts in the middle, but I want that it starts on the right side. You can imagine you this like the gradient comes in from the right side.
Question
What do I need to change in my Shape or Gradient that this works?
Code
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<gradient android:gravity="right" android:type="radial" android:gradientRadius="280"
android:startColor="#FFFFFF" android:endColor="#CBCBCB" /><!-- #e0dede" -->
</shape>
Image
This how it looks at the moment!
Like you see the gradient is in the middle, I want that exactly this gradient starts from the right side into the middle of the screen.
Try this and see the magic:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<gradient android:layout_gravity="right" android:type="radial" android:gradientRadius="280"
android:centerX="100%" android:centerY="50%"
android:startColor="#FFFFFF" android:endColor="#CBCBCB" /><!-- #e0dede" -->
</shape>
Tags android:centerX and android:centerY lets you start your gradient from any point on your screen.You just need to give related % values as i did here.It worked like magic for me!
I didn't change other tags settings of your code but you will have to change the radius as per how much white portion you need to make visible.
Hope,It helped.

Categories

Resources