Color gradient for textcolor - android

I have a textview. I want to change color of text by gradient but it's not work
<shape android:shape="rectangle" >
<gradient
android:angle="90"
android:startColor="#eabd00"
android:centerColor="#fef1b2"
android:endColor="#f9e273"
android:type="linear" />
</shape>
Can somebody help me? Thanks All.

try this
TextView textView = findViewById(R.id.btnLogin)
Shader textShader=new LinearGradient(0, 0, 0, 20,
new int[]{R.color.colorAccent,R.color.colorPrimary},
new float[]{0, 1}, TileMode.CLAMP);
textView.getPaint().setShader(textShader);
or this
TextView txt1 = (TextView) findViewById(R.id.textview);
int[] color = {Color.DKGRAY,Color.CYAN};
float[] position = {0, 1};
TileMode tile_mode = TileMode.MIRROR; // or TileMode.REPEAT;
LinearGradient lin_grad = new LinearGradient(0, 0, 0, 50,color,position, tile_mode);
Shader shader_gradient = lin_grad;
txt1.getPaint().setShader(shader_gradient);

Related

Custom Background Shape of Button via Code

I have managed to create a custom button background shape via XML code
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:state_pressed="true" >
<shape android:shape="rectangle" >
<corners android:radius="24dp" />
<stroke android:width="2dp" android:color="#color/colorWhite" />
<solid android:color="#color/colorPrimary" />
</shape>
</item>
<item android:state_focused="true">
<shape android:shape="rectangle" >
<corners android:radius="24dp" />
<stroke android:width="2dp" android:color="#color/colorWhite" />
<solid android:color="#color/colorPrimary" />
</shape>
</item>
<item >
<shape android:shape="rectangle" >
<corners android:radius="24dp" />
<stroke android:width="2dp" android:color="#color/colorWhite" />
<solid android:color="#color/colorWhite" />
</shape>
</item>
</selector>
But I am wondering what is the Java code equivalent to that? Any guides please?
It's a lot more verbose to do it in Java, but here are the things you would need to do.
Create a new StateListDrawable()
For each state:
Create a new ShapeDrawable(new RoundRectShape(...)). I don't recall exactly how the constructor args work, but you can experiment.
Use shapeDrawable.getPaint() to obtain its Paint object and make modifications. You'll probably use setColor(), setStyle(), and setStrokeWidth().
Construct a state set. This is an array of integers composed of various android state attributes, like android.R.attr.state_pressed, for the state you want.
Call stateListDrawable.addState(stateSet, shapeDrawable). You can use StateSet.NOTHING (or an empty int[]) for the default state. Make sure you add them in the order they would appear in XML.
Something like this:
StateListDrawable stateListDrawable = new StateListDrawable();
Shape roundRect = new RoundRectShape(...);
// Add states in order. I'll just demonstrate one.
ShapeDrawable pressed = new ShapeDrawable(roundRect);
Paint paint = pressed.getPaint();
paint.setColor(Color.BLUE);
paint.setStyle(Style.STROKE);
paint.setStrokeWidth(10f); // this is in pixels, you'll have to convert to dp yourself
int[] pressedState = { android.R.attr.state_pressed };
stateListDrawable.addState(pressedState, pressed);
You can handle button states like this:
StateListDrawable states = new StateListDrawable();
states.addState(new int[] { android.R.attr.state_pressed }, getResources().getDrawable(R.drawable.img_pressed));
states.addState(new int[] { android.R.attr.state_focused }, getResources().getDrawable(R.drawable.img_focused));
states.addState(new int[] {}, getResources().getDrawable(R.drawable.img_normal));
button.setBackgroundDrawable(states);
See this sample for reference:
StateListDrawable states = new StateListDrawable();
states.addState(new int[] { android.R.attr.state_pressed }, getSelectedBackground());
states.addState(new int[] { android.R.attr.state_focused }, getSelectedBackground());
states.addState(new int[] {}, getNormalBackground());
button.setBackgroundDrawable(states);
private ShapeDrawable getNormalBackground() {
int r = 10;
float[] outerR = new float[] { r, r, r, r, r, r, r, r };
RoundRectShape rr = new RoundRectShape(outerR, null, null);
ShapeDrawable drawable = new ShapeDrawable(rr);
drawable.getPaint().setColor(Color.GRAY);
return drawable;
}
private ShapeDrawable getSelectedBackground() {
float[] outerR2 = new float[] { 10, 10, 10, 10, 10, 10, 10, 10 };
RectF inset2 = new RectF(3, 3, 3, 3);
float[] innerR2 = new float[] { 9, 9, 0, 0, 0, 5, 0, 0 };
ShapeDrawable sh2 = new ShapeDrawable(new RoundRectShape(outerR2, inset2, innerR2));
return sh2;
}
Check this once Defining Drawable Shape with in JAVA code

android shape xml rotated drawable change color programmatically

This is a xml for triangle shape:
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:id="#+id/shape_id">
<rotate
android:fromDegrees="45"
android:toDegrees="45"
android:pivotX="-40%"
android:pivotY="87%" >
<shape android:shape="rectangle" >
<stroke android:width="10dp"/>
</shape>
</rotate>
</item>
</layer-list>
And this is a background of a textview
<TextView
android:id="#+id/headlineSelect_TXT2"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_weight="1"
android:background="#drawable/category_triangle_shape1"
android:visibility="invisible" />
And I want to change color of shape programmatically. I tried this but I am getting null pointer exception
LayerDrawable bgDrawable = (LayerDrawable) getActivity()
.getResources()
.getDrawable(R.drawable.category_triangle_shape1);
final GradientDrawable shape = (GradientDrawable) bgDrawable
.findDrawableByLayerId(R.id.shape_id);
shape.setStroke(10,Color.GREEN);
How can I do that? Thanks for help.
This is somewhat tricky and involves a lot of casts:
TextView view = (TextView) findViewById( R.id.my_text_view );
// Get the drawable from the view, if you're using an imageview src
// element you'll go with view.getDrawable()
LayerDrawable layers = (LayerDrawable) view.getBackground();
// Now get your shape by selecting the id
RotateDrawable rotate = (RotateDrawable) layers.findDrawableByLayerId( R.id.shape_id );
// Finally you can access the underlying shape
GradientDrawable drawable = (GradientDrawable) rotate.getDrawable();
// ... and do you fancy things
drawable.setColor( ... );
Forget about XML and do it by code like that:
TextView txtView = (TextView)findViewById(R.id. headlineSelect_TXT2);
ShapeDrawable.ShaderFactory sf = new ShapeDrawable.ShaderFactory() {
#Override
public Shader resize(int width, int height) {
LinearGradient lg = new LinearGradient(0, 0, 0, txtView.getHeight(),
new int[] {
Color.LIGHT_GREEN,
Color.WHITE,
Color.MID_GREEN,
Color.DARK_GREEN },
new float[] {
0, 0.45f, 0.55f, 1 },
Shader.TileMode.REPEAT);
return lg;
}
};
PaintDrawable p = new PaintDrawable();
p.setShape(new RectShape());
p.setShaderFactory(sf);
txtView.setBackgroundDrawable((Drawable)p);
If you have RotateDrawable in your xml file inside res/drawable then you could try the below code to change the color of rotate drawable.
LayerDrawable layers = (LayerDrawable) getResources().getDrawable(R.drawable.triangle);
RotateDrawable rotateShape = (RotateDrawable) (layers.findDrawableByLayerId(R.id.user_color));
GradientDrawable shape = (GradientDrawable) rotateShape.getDrawable();
shape.setColor(Color.parseColor("#FF0000"));
TextView NotchTV = (TextView) view.findViewById(R.id.bubble_notch);
NotchTV.setBackgroundDrawable(layers);
`

LayerDrawable programmatically

Since my application's color theme is dynamic i can only create background drawables using colors and shapedrawables,
i want to build a edittext background drawable with colors and shapes as shown below.
But i want to do this programatically
How to build this same drawable programatically?
<item>
<shape>
<solid android:color="#android:color/yellow" />
</shape>
</item>
<!-- main color -->
<item
android:bottom="1dp"
android:left="1dp"
android:right="1dp">
<shape>
<solid android:color="#android:color/white" />
</shape>
</item>
<!-- draw another block to cut-off the left and right bars -->
<item android:bottom="10dp">
<shape>
<solid android:color="#android:color/white" />
</shape>
</item>
this is what i tried....
GradientDrawable border = new GradientDrawable();
border.setShape(GradientDrawable.RECTANGLE);
border.setColor(Color.WHITE);
GradientDrawable background = new GradientDrawable();
background.setShape(GradientDrawable.RECTANGLE);
background.setColor(Color.YELLOW);
GradientDrawable clip = new GradientDrawable();
clip.setShape(GradientDrawable.RECTANGLE);
border.setColor(Color.WHITE);
Drawable[] layers = {background, border, clip};
LayerDrawable layerDrawable = new LayerDrawable(layers);
layerDrawable.setLayerInset(0, 0, 0, 0, 0);
layerDrawable.setLayerInset(1, 1, 0, 1, 1);
layerDrawable.setLayerInset(2, 0, 0, 0, 10);
but the result is different....please help....!
I finally got it working. Instead of using GradientDrawable I used ShapeDrawable.
By setting this LayerDrawable as an EditText background you can regenerate default EditText
styles with custom colors.
ShapeDrawable border = new ShapeDrawable();
border.getPaint().setColor(Color.WHITE);
ShapeDrawable background = new ShapeDrawable();
background.getPaint().setColor(Color.BLACK);
ShapeDrawable clip = new ShapeDrawable();
clip.getPaint().setColor(Color.WHITE);
Drawable[] layers = {background, border, clip};
LayerDrawable layerDrawable = new LayerDrawable(layers);
layerDrawable.setLayerInset(0, 0, 0, 0, 0);
layerDrawable.setLayerInset(1, 1, 0, 1, 1);
layerDrawable.setLayerInset(2, 0, 0, 0, 10);
This also works with Gradient Drawables:
GradientDrawable border = new GradientDrawable();
border.setColor(Color.White);
GradientDrawable background = new GradientDrawable();
background.setColor(Color.Black);
GradientDrawable clip = new GradientDrawable();
clip.setColor(Color.White);
GradientDrawable[] layers = {background, border, clip};
LayerDrawable layerDrawable = new LayerDrawable(layers);
layerDrawable.setLayerInset(0, 0, 0, 0, 0);
layerDrawable.setLayerInset(1, 1, 0, 1, 1);
layerDrawable.setLayerInset(2, 0, 0, 0, 10);

Drawing LinearGradient in a layout

i try to draw LinearGradient in a layout but the gradient is not fit into my view.
Instead of gradient i see only one color.
I think its because im not giving the right dimension of the view
Here is my code:
backGroundColorView = (LinearLayout) findViewById(R.id.backGroundColorView);
int[] tempColors = data.getAppBackgroundColor();
LinearGradient test = new LinearGradient(0.f, 0.f, backGroundColorView.getWidth(), backGroundColorView.getHeight(), tempColors, null, TileMode.CLAMP);
ShapeDrawable shape = new ShapeDrawable(new RectShape());
shape.getPaint().setShader(test);
backGroundColorView.setBackgroundDrawable(shape);
Thank for helping
just use GradientDrawable as a background http://developer.android.com/reference/android/graphics/drawable/GradientDrawable.html
You can use this site do create the gradient you want and then add a file in your res/drawable and put the code inside.
After that all you have to do is set the background of your layout to be the drawable file you just created.
If you have any questions just ask ;)
EDIT:
Change your code to this:
GradientDrawable gd = new GradientDrawable(GradientDrawable.Orientation.TOP_BOTTOM,tempColors);
gd.setCornerRadius(0f);
backGroundColorView.setBackgroundDrawable(gd);
Try this
backGroundColorView = (LinearLayout) findViewById(R.id.backGroundColorView);
int[] tempColors = data.getAppBackgroundColor();
GradientDrawable gd = new GradientDrawable(
GradientDrawable.Orientation.TOP_BOTTOM,tempColors);
gd.setCornerRadius(0f);
backGroundColorView .setBackgroundDrawable(gd);
Instead of
backGroundColorView = (LinearLayout) findViewById(R.id.backGroundColorView);
int[] tempColors = data.getAppBackgroundColor();
LinearGradient test = new LinearGradient(0.f, 0.f, backGroundColorView.getWidth(), backGroundColorView.getHeight(), tempColors, null, TileMode.CLAMP);
ShapeDrawable shape = new ShapeDrawable(new RectShape());
shape.getPaint().setShader(test);
backGroundColorView.setBackgroundDrawable(shape);
GradientDrawable
you can create linear gradiant like this:
<?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:centerX="50%"
android:startColor="#FF003333"
android:centerColor="#FF05C1FF"
android:endColor="#FF003333"
android:angle="270"/>
<!-- <gradient
android:centerColor="#FF05C1FF"
android:centerX="50%"
android:centerY="50%"
android:endColor="#FF003333"
android:gradientRadius="50"
android:startColor="#FF003333"
android:type="radial" /> -->
<corners
android:bottomLeftRadius="15dp"
android:bottomRightRadius="15dp"
android:topLeftRadius="15dp"
android:topRightRadius="15dp" />
</shape>
and use it as background of linear layout.
ihope it solves your problem.
see i have used this in my post: customToast
you can use this also:
GradientDrawable grad = new GradientDrawable(Orientation.LEFT_RIGHT,
new int[]{0xffffffff, 0xffff00ff, 0xffffff00,
0xff0000ff, 0xf0f0f0f0, 0xfefefefe});
grad.setBounds(0, 0, 320, 480);

Android Gradient drawable programmatically

I have a gradient drawable defined in xml that I use it as a background, like this:
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:bottom="4dp">
<shape>
<gradient
android:startColor="#color/blue"
android:endColor="#color/dark_blue"
android:angle="270" />
</shape>
</item>
<item android:top="98dp">
<shape>
<gradient
android:startColor="#color/black"
android:endColor="#color/transparent_black"
android:angle="270" />
</shape>
</item>
</layer-list>
I need to implement this programmatically. I have tried to use a GradientDrawable as follows (this method is implemented on a custom view):
int[] colors1 = {getResources().getColor(R.color.black), getResources().getColor(R.color.trasparent_black)};
GradientDrawable shadow = new GradientDrawable(Orientation.TOP_BOTTOM, colors1);
shadow.setBounds(0,98, 0, 0);
int[] colors = new int[2];
colors[0] = getResources().getColor(R.color.blue);
colors[1] = getResources().getColor(R.color.dark_blue);
GradientDrawable backColor = new GradientDrawable(Orientation.TOP_BOTTOM, colors);
backColor.setBounds(0, 0,0, 4);
//finally create a layer list and set them as background.
Drawable[] layers = new Drawable[2];
layers[0] = backColor;
layers[1] = shadow;
LayerDrawable layerList = new LayerDrawable(layers);
setBackgroundDrawable(layerList);
The problem is that it seems that setting the bounds is useless or doesn't work the same way as (android:top, android:bottom xml parameters). The resulting background is each layer painted from top to bottom, one above the other.
I want to generate something like this:
Found the answer!. Possible duplicate Multi-gradient shapes.
Replaced:
backColor.setBounds(0, 0,0, 4);
shadow.setBounds(0,98, 0, 0);
for
layerList.setLayerInset(0, 0, 0, 0, 4);
layerList.setLayerInset(1, 0, 98, 0, 0);

Categories

Resources