How to reuse drawable - android

I have a number of background drawables that the only different is the color of the border or the color of the background.
Is there a way to define a general drawable and then add (or change) the attribute I need.
For example here is a rectangular drawable
XML file
<?xml version="1.0" encoding="utf-8"?>
<shape
xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >
<corners android:radius="#dimen/radius_small"/>
<solid android:color="#color/simple_black" />
<stroke android:width="2dip" android:color="#color/simple_white" />
<padding
android:left="#dimen/two_dp"
android:right="#dimen/two_dp"/>
</shape>
and I would like to change the solid color or the stroke color, and not to create a separate drawable for each one.

This assumes that you want to do it programatically. If all you need is to change the background and/or stroke color, here is how you can do it:
Obtain a reference to the shape
GradientDrawable drawable = (GradientDrawable)context.getResources().getDrawable(R.drawable.shape_id)
To change color
drawable.setColor(Color.RED)
To change stroke color and width (notice that you can only change both of them together, so I recommend keeping a variable with the stroke's width in px)
drawable.setStroke(Util.dpToPx(context, 2), Color.YELLOW)

Related

Android background gradient radial

I want that my app have a background gradient radial, and i Think I did it, but in runtime my app show just one color.
Here my drawable background
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<gradient
android:type="radial"
android:gradientRadius="360"
android:startColor="#color/color_start"
android:endColor="#color/color_end" />
And I call it in my activity XML in this way:
android:background="#drawable/background_app"
PD:Sorry about my bad English
android:gradientRadius="250" will be ignored. You should point to a dimen resource with a px or dp value, like: android:gradientRadius="#dimen/gradient_radius"

Layout Background partially transparent

I want to set a transparent background. Maybe 20% of gray
but i also want to have a small part of the background being completely transparent (a rectangle or a circle in the layout):
Is that possible? later i want the rectangle (or the circle) to be moveable and scaleable.
This is easily achievable with a custom drawable like this:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:innerRadius="0dp"
android:shape="rectangle"
android:thicknessRatio="1.9"
android:useLevel="false">
<solid android:color="#android:color/transparent"/>
<stroke
android:width="125dp"
android:color="#CC000000"/>
</shape>
When you overlay that drawable on top of an image the results would looks something like this:
By creating that drawable dynamically in code you can adjust it at runtime. For example:
ShapeDrawable drawable = new ShapeDrawable(new RectShape());
drawable.getPaint().setColor(0xCC000000);
drawable.getPaint().setStyle(Paint.Style.STROKE);
drawable.getPaint().setStrokeWidth(dpToPixel(125.0f));
someView.setBackground(drawable);
You can influence the shape of the hole in the middle by combining multiple layers in a LayerDrawable.
You can also influence the general shape of the hole by setting the android:shape tag of the <shape /> element in the custom drawable.

Custom Shapes with text in Android

I am new to android. I want to draw a custom shape in android. When someone chooses a date(via date picker) I want to display the result in a square box rather than the regular way.How do I achieve this?
You can use a textview to show the selected date. Since you seem to know the coding for that, I focus on the design you can apply for your textview.
By default, a textview has a rectangular shape with sharp edges and points, but you can achieve a rounded-corner textview using a style , like this one :
<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<stroke
android:width="1dp"
android:color="#color/red"/>
<corners android:radius="7dp" />
<padding
android:left="1dp"
android:right="1dp"
android:top="1dp"
android:bottom="1dp"/>
<solid android:color="#color/white"/></shape>
The corners tag, is responsible for the rounded corners, you can tweak th radius to make more or less rounded.
Stroke tag, defines the width and color of the border for the textbox.
and Solid tag determines the bakcground color of the view.
In order to use this style, you should save it as an XML file and save it in drawable folder (normally drawable-hdpi will do). Then , in your tetxview or edittext you should set the background property as the following :
android:background="#drawable/thesavedxmlfile"

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);

Border texture for a View

Is there any way to set border texture for LinearLayout or any other View? I can't just set an image as the background because of different screen sizes.
Here is the sample of the texture I'm talking about:
I tried to use Nine-patch, but it stretches the white lines that are supposed to be repeated.
Screenshot from Draw 9-patch tool:
You can use shape drawable like this:
<shape android:shape="rectangle">
<solid android:color="#FAFAD2" />
<stroke android:width="1sp" android:color="#000000"
android:dashWidth="7sp" android:dashGap="5sp" />
</shape>
and this will result you something like this:
You can change color, width, border etc according to your requirements.

Categories

Resources