Android TextView background with transparent fading borders - android

I have a TextView, to which I want to add a solid color background. I want this background only have the borders fading to transparency. Like this picture here, but instead of a photo, it's only a solid color. http://i.imgur.com/zAoazUy.png (Sorry I cannot post images yet)
This TextView is dynamically generated so its size can vary. The gradient has to end to transparent.
Is this possible? I fiddled a lot with XML Drawable ressources but got to nothing remotely close to what I want.
Thanks in advance
PS: (Picture stolen from Draw transparent gradient with alpha transparency from 0 to 1)

You have to define gradient in drawable folder ,startColor can be any color of your choice but for endColor use argb value to get transparent effect at corner
grad.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<gradient android:type="radial"
android:gradientRadius="250"
android:startColor="#0000ff"
android:endColor="#64ffffff"/>
</shape>
and then simply use in textView
textView.setBackground(getApplicationContext().getDrawable(R.drawable.grad));

Related

How to set background color of an image, but within the border?

My goal is to display a circular image, and allow the user to set its foreground and background colors respectively, for example:
My attempt was to create an image asset with transparent background, then use ImageView::setColorFilter to change its foreground, and use ImageView::setBackgroundColor to set its background. The image asset looks like this:
My problem is that pixels outside of what we humans call 'border' are also transparent, so the result looks like this:
How do people usually deal with this issue? Although I was doing Android development, but any ideas or code snippets in any language are appreciated!
at the first, you must create a new drawable file and write below code for creating the circle with yellow color with blue border:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval">
<solid android:color="#cab816" />
<size android:width="50dp"
android:height="50dp"/>
<stroke android:width="2dp"
android:color="#color/darkBlue"/>
</shape>
and now you must set this drawable to the background of your imageview and set your image with src.
if your image covered all of this circle, use padding for decrease image size.

Align XML drawable background to bottom of view

I am trying to provide a simple solid-color underline to a TextView header. I want this to be reusable and to work with any view in the future, regardless of height. I am trying to favor a background drawable so I can simply apply it to view. I can draw a line without any problem:
<shape
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:shape="line"
>
<stroke
android:color="#8a9299"
android:width="1dp"
/>
</shape>
This line is, however, centered in the background of the view. I see a bunch of online tutorials that use layers to draw a background-colored rectangle and then "peek" another rectangle from behind, however I don't know what background color this header element type will be used on, and transparent rectangle backgrounds show the color rectangle below. Is there any way to stick to a line but give it a bottom gravity inside the view it is applied to?
This can be achieved using gradientDrawable. Here you go:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<gradient
android:angle="90"
android:startColor="#000000"
android:centerColor="#android:color/transparent"
android:centerX="0.1" />
</shape>
Increase/Decrease centerX to increase/decrease width of your underline. Change startColor to change the color of the underline.
Explanation:
angle is the angle of the gradient. 0 is left to right. 90 is bottom to top.
startColor is the start color of the gradient. Our angle is 90 so the gradient starts from bottom (and so it appears like an underline)
centerColor is the centerColor which is transparent.
centerX is the X position of the gradient as a fraction of the width. Keep it small (<0.1) for a good looking underline. Anything above 0.1 looks bad (nobody is stopping you though!).

Android linear gradient incorrectly fades in the middle?

I have a linear gradient defined as a drawable in xml with the following:
<?xml version="1.0" encoding="utf-8" ?>
<shape
xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<gradient
android:type="linear"
android:startColor="#android:color/transparent"
android:centerColor="#color/CenterColor"
android:endColor="#android:color/transparent"
android:gradientRadius="100"
android:centerX="0.5"/>
</shape>
CenterColor is defined as #E6E7E8.
My view is defined as:
<View
android:id="#+id/addressGradientDivider"
android:layout_width="match_parent"
android:layout_height="100dp"
android:layout_below="#+id/addressTextView"
android:layout_marginTop="#dimen/NormalVerticalMargin"
android:layout_marginLeft="#dimen/SmallHorizontalMargin"
android:layout_marginRight="#dimen/SmallHorizontalMargin"
android:background="#drawable/DetailsGradientDivider"/>
The result is a gradient like the following:
For whatever reason the gradient is fading in the middle and I can't figure out why. I have also tried setting the gradient programmatically and got the same results.
Why is the gradient fading in the middle and how can I get the darkest part of the gradient to be in the center?
I think the reason is #android:color/transparent is #00000000 the transparent of black (#000000) and also your centerColor is very light gray.
The center color that you see as fading is actually #E6E7E8.
So, if you blend this transparent of black and very light gray, you will see slightly darker gray in between when the alpha is not fully 00.
If you change the startColor to #00FFFFFF (transparent of white), you will not see the gray gradient at 25% and 75% position. Though, the centerColor is very close to white, so it is even hard to see the gradient.

Trying to change the color of a shape used in textView background

I have a drawable oval defined in xml with a white background. I am setting this circle as the background resource of my textView inside my gridAdapter.
I want to change the color of the drawable circle by code, and I'm not having much luck. I have used .setColor , .setColorFilter and a variety of other hacks, but none have worked. The circle loads in the background of textView as white, it never seems to change.
Here I try to load the drawable and modify its color, and then add it as a background resource to the textView:
int color = Color.parseColor(locationModel.col_line_color); // Color of the line
GradientDrawable bgcircle = (GradientDrawable) gridContext.getResources().getDrawable(R.drawable.circle);
bgcircle.mutate();
// bgcircle.setColor(color);
bgcircle.setColorFilter(color, Mode.SRC_ATOP);
viewHolder.textView.setText((locationModel.col_line).toString());
viewHolder.textView.setBackgroundResource(R.drawable.circle);
The XML for the drawable shape:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval">
<solid
android:color="#android:color/white"/>
<size
android:width="120dp"
android:height="120dp"/>
</shape>
A lot of the tutorials call for .mutate() then to implement .setColorFilter(), but I haven't had much luck. I've tried changing the PorterDuff Mode to some alternatives, but the circles continue rendering white.
Any help/guidance would be appreciated.
Instead of tyring to modify the current xml drawable, Create the drawable in your code. you can also use ShapeDrawable if you dont need gradient.
ShapeDrawable drawable = new ShapeDrawable(new OvalShape());
drawable.getPaint().setColor(newColor);

Round ring button in Android

I want to create a button background (or button itself) that look exactly like the below one.
I'm doing it currently with an image. I tried to create a similar one using the following XML, but it doesn't look as expected.
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval">
<solid
android:color="#f0600000"/>
<stroke
android:width="10dp"
android:color="#FFFF6666"/>
</shape>
Actually I want a round button with 3 pixel shadow and 1/4 width stroke in red color around the white circle. I have not succeeded with the shadow part at all. Thanks for any sort of help.
Well, the solid fills the middle and stroke paints the border. Since you want 3 colors you will have to use 2 drawables, painting white circle over the red circle with grey border. You can then use LayerDrawable to keep these as one unit

Categories

Resources