Progress bar shadow only on filled part - android

Implementing a progress bar in android, trying to get a drop shadow on the bottom part of the filled section only, like this:
API level is KitKat and up but i wont mind a solution that applies to LOLLIPOP and up only.
Elevation does not seem to work, nor do the card View.
any sugesstions?

Create the following xml shape and place it as your progressBar background.
** The 10dp in the xml assumes the progressBar is 20dp in Height and that you want the shadow to be exactly half of it.
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="#android:id/background">
<layer-list>
<item android:bottom="10dp">
<shape android:shape="rectangle">
<solid android:color="#color/orange" />
</shape>
</item>
</layer-list>
</item>
<item android:id="#android:id/progress">
<clip>
<layer-list>
<item>
<shape android:shape="rectangle">
<solid android:color="#color/green" />
</shape>
</item>
<item android:top="10dp">
<shape android:shape="rectangle">
<gradient
android:angle="90"
android:endColor="#color/black"
android:startColor="#color/white" />
</shape>
</item>
</layer-list>
</clip>
</item>
</layer-list>

Related

Material action bar background using a custom xml

Hi I need to make like an action bar for my custom view background and I tried using
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape android:shape="rectangle">
<solid android:color="#color/button_gradient" />
</shape>
</item>
<item android:top="1dp">
<shape android:shape="rectangle">
<solid android:color="#color/list_pressed" />
</shape>
</item>
<item
android:bottom="1dp"
android:top="1dp">
<shape android:shape="rectangle">
<solid android:color="#color/menu_label" />
</shape>
</item>
But I am not getting the shadow with this code. What should I change to get the shadow?
ActionBar is deprecated. Switch to a Toolbar, and you should get your shadow.

Set remaining progress bar color in android

I'm setting the color of the progress bar using the following style code
<item android:id="#android:id/progress">
<clip>
<shape>
<corners android:radius="0dip" />
<gradient
android:angle="0"
android:endColor="#36A19C"
android:startColor="#36A19C" />
</shape>
</clip>
</item>
My progressbar looks like below image
How can I set the color of the grey part shown in my progress bar???
Try looking at some tutorials here and here.
Create a file custom_progressbar.xml in your drawable
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<!-- Define the background properties like color etc -->
<item android:id="#android:id/background">
<shape>
<gradient
android:startColor="backgroundColor"
android:endColor="backgroundColor"
android:angle="0"
/>
</shape>
</item>
<!-- Define the progress properties like start color, end color etc -->
<item android:id="#android:id/progress">
<clip>
<shape>
<gradient
android:startColor="progressColor"
android:endColor="progressColor"
android:angle="0"
/>
</shape>
</clip>
</item>
</layer-list>
Set this to your progressBar in code
// Get the Drawable custom_progressbar
Drawable customDrawable= res.getDrawable(R.drawable.custom_progressbar);
// set the drawable as progress drawavle
progressBar.setProgressDrawable(customDrawable);
You can try this way:
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:id="#android:id/background">
<shape>
<solid android:color="#color/seekbar_background" />
<stroke
android:width="#dimen/seekbar_background_border"
android:color="#android:color/transparent" />
</shape>
</item>
<item android:id="#android:id/progress">
<clip>
<shape>
<solid android:color="#color/seekbar_progress" />
</shape>
</clip>
</item>
</layer-list>
The progress bar uses a layer list that holds several layers. You need background and progress. Hope this helps you :)
Add this line of code to your progress bar XML.
android:progressBackgroundTint="#ReplaceThisWithYourColor"
It works for me.

How to apply stroke to a button specific sides in android

How can I apply stroke to a button's only two sides that is LEFT border and RIGHT border? in android.
I have apply the following code but it's not working
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<!-- This is the main color -->
<item>
<shape>
<solid android:color="#EEEEEE" />
</shape>
</item>
<!-- This is the line -->
<item android:right="1dp">
<shape>
<solid android:color="#333333" />
</shape>
</item>
<item android:left="1dp">
<shape >
<solid android:color="#333333"/>
</shape>
</item>
</layer-list>
You were on the right track. This will do the trick:
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape>
<solid android:color="#EEEEEE" />
</shape>
</item>
<!-- This is the line -->
<item android:right="1dp" android:left="1dp">
<shape>
<solid android:color="#333333" />
</shape>
</item>
</layer-list>
And use this as the background for your button. Just tested it - works just fine.
I would suggest using a 9-patch drawable that looks like this:
It has the required black border, 1px on each side of the 0x333 color and the rest is filled with 0xeee. 9-patches scale with the size of the view that they are applied to. So you can use it as a background or src

Shape drawable as background, a line at the bottom

I am using a drawable as a background of a TextView just to have a divider line below the text. A achivied it with this drawable-xml:
<?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="#FFDDDDDD" />
<gradient
android:angle="0"
android:startColor="#FFAAAAAA"
android:endColor="#FFEEEEEE"
/>
</shape>
</item>
<item android:bottom="2dp">
<shape
android:shape="rectangle">
<solid android:color="#FF000000" />
</shape>
</item>
</layer-list>
But this method draws a colored rectangle above a black rectangle. I would like to have just the line at the bottom of the shape with no black rectangle because black is not transparent. How could I achieve that?
This is how I got a line at the bottom for mine. Draw a stroke but then shift the item up and to the sides to get the top and sides to not show the stroke:
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:top="-8dp" android:left="-8dp" android:right="-8dp">
<shape>
<solid android:color="#2b7996"/>
<stroke android:color="#33b5e5" android:width="6dp"/>
</shape>
</item>
</layer-list>
I think it's better solution:
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:gravity="bottom">
<shape>
<size android:height="1dp" />
<solid android:color="#000000" />
</shape>
</item>
</layer-list>
In general, I try to mess as little as possible with backgrounds unless absolutely necessary, since doing so overrides the default background colors that have states for focused, pressed, etc. I suggest just using an additional view (in a vertical LinearLayout) that is as thick as you need it to be. For example:
<View
android:background="#FF000000"
android:layout_height="2dp"
android:layout_width="fill_parent"/>
Usually for similar tasks - I created layer-list drawable like this one:
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape android:shape="rectangle">
<solid android:color="#color/underlineColor"/>
</shape>
</item>
<item android:bottom="3dp">
<shape android:shape="rectangle">
<solid android:color="#color/buttonColor"/>
</shape>
</item>
The idea is that first you draw the rectangle with underlineColor and then on top of this one you draw another rectangle with the actual buttonColor but applying bottomPadding. It always works.
But when I needed to have buttonColor to be transparent I couldn't use the above drawable. I found one more solution
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape android:shape="rectangle">
<solid android:color="#android:color/transparent"/>
</shape>
</item>
<item android:drawable="#drawable/white_box" android:gravity="bottom" android:height="2dp"/>
</layer-list>
(as you can see here the mainButtonColor is transparent and white_box is just a simple rectangle drawable with white Solid)
With this solution where ever you require different line you can. My requirement was underline only. Even you can give different colors to the layout. You can see in below picture, white line
<?xml version="1.0" encoding="UTF-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:top="-5dp" android:left="-5dp" android:right="-5dp">
<shape>
<solid android:color="#android:color/transparent"/>
<stroke android:color="#color/white" android:width="5dp"/>
</shape>
</item>
<item android:top="-5dp" android:bottom="-5dp" android:right="-5dp">
<shape>
<solid android:color="#android:color/transparent"/>
<stroke android:color="#color/colorPrimaryDark" android:width="5dp"/>
</shape>
</item>
<item android:bottom="-5dp" android:left="-5dp" android:right="-5dp">
<shape>
<solid android:color="#android:color/transparent"/>
<stroke android:color="#color/colorPrimaryDark" android:width="5dp"/>
</shape>
</item>
<item android:top="-5dp" android:left="-5dp" android:bottom="-5dp">
<shape>
<solid android:color="#android:color/transparent"/>
<stroke android:color="#color/colorPrimaryDark" android:width="5dp"/>
</shape>
</item>
<item android:bottom="2dp" android:left="5dp" android:right="5dp" android:top="5dp">
<shape>
<solid android:color="#color/colorPrimary"/>
<corners android:bottomRightRadius="0dp"
android:bottomLeftRadius="0dp"
android:topLeftRadius="0dp"
android:topRightRadius="0dp" />
</shape>
</item>
</layer-list>
This is a slightly lighter variant of the above.
/drawable/rect_highlight.xml
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
<stroke android:width="1px" android:color="#color/colorHighlight"/>
</shape>
/drawable/underline.xml
<inset xmlns:android="http://schemas.android.com/apk/res/android" android:insetLeft="-1px" android:insetRight="-1px" android:insetTop="-1px" android:drawable="#drawable/rect_highlight"/>
Usage:
<TextView ... android:background="#drawable/underline"/>
It's not mine, somebody smarter than I came up with it. If I was smarter, I would have asked who. :)
This solution worked for most of the cases that I needed something like that.
<?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="#android:color/transparent" /> <!--background color of box-->
</shape>
</item>
<item
android:height="2dp"
android:gravity="bottom">
<shape>
<solid android:color="#000000" />
</shape>
</item>
</layer-list>
A simple solution is extending a TextView, then override the onDraw.
#Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
Paint paint = new Paint();
paint.setColor(ContextCompat.getColor(getContext(),R.color.colorTextUnderLine));
paint.setStrokeWidth(10);
canvas.drawLine(0.0f,canvas.getHeight(),canvas.getWidth(),canvas.getHeight(),
paint);
}

Drawing top border on a shape on Android

I'm creating a custom progress bar (positioned under a WebView) and what I would like to draw is a 1dp-wide line between the WebView and the ProgressBar. I'm modifying existing drawable, namely progress_horizontal.xml, and tried something like this:
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
(...)
<item>
<shape android:shape="line">
<stroke android:width="1dp" android:color="#FF000000" />
</shape>
</item>
</layer-list>
This line however is vertically centered but I want it to be drawn on top of the drawable. The only idea I could come up with is to use this "hacky" gradient below:
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
(...)
<item>
<shape>
<gradient
android:startColor="#FF000000"
android:centerColor="#00000000"
android:centerY="0.01"
android:endColor="#00000000"
android:angle="270"
/>
</shape>
</item>
</layer-list>
Do you have better ideas how to draw a single line shape aligned to the top of the drawable defined with layer-list?
I spent a while trying to figure this out as well. Hopefully there is a better way to do it but here is the method I used, which is also kind of hackish, but in case it helps someone, I thought I would share.
The first item in your layer list should be a solid of whatever color you want to be your border. Following that would be the thing(s) you want to have the border, with padding on whatever side you want the border to be on.
For example:
<?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="YOUR BORDER COLOR" />
</shape>
</item>
<item android:top="YOUR TOP BORDER THICKNESS">
THE THING YOU WANT A BORDER ON
</item>
</layer-list>
The idea is that the padding on the item reveals the solid shape behind it, giving the border appearance. You could add padding to any side to add a border. I imagine you could get more complicated and have different colored borders this way as well.
Seems like a hack but it worked for me.
EDIT: I said "padding" but in layer-lists it's more of an offset.
i was going through all related topics but no one could solve my problem. But some of them were very useful to understand how the layer-list or shape parameters work.
My problem was to define a button with a linear gradient and draw a top and a bottom line in different colors. After all I hacked this solution. I saved the file unter res/drawable/blue_btn.xml
<?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">
<solid android:color="#color/blue_end" />
<padding
android:left="10dp"
android:top="10dp"
android:right="10dp"
android:bottom="10dp" />
<stroke
android:width="1dp"
android:color="#color/bottomline_btn" />
</shape>
</item>
<item>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<item>
<shape android:shape="rectangle">
<solid android:color="#color/blue" />
<gradient
android:startColor="#color/blue"
android:endColor="#color/blue_end"
android:angle="270" />
</shape>
</item>
<item android:top="0dp" android:bottom="-1dp" android:left="-1dp" android:right="-1dp">
<shape android:shape="rectangle">
<stroke
android:width="1dp"
android:color="#color/topline_btn" />
<solid android:color="#00000000" />
<corners android:radius="0dp" />
</shape>
</item>
<item android:top="-1dp" android:bottom="0dp" android:left="-1dp" android:right="-1dp">
<shape android:shape="rectangle">
<stroke
android:width="1dp"
android:color="#color/bottomline_btn" />
<solid android:color="#00000000" />
<corners android:radius="0dp" />
</shape>
</item>
</layer-list>
</item>
</selector>
<color name="topline_btn">#31ffffff</color>
<color name="bottomline_btn">#31000000</color>
<color name="blue">#449def</color>
<color name="blue_end">#2f6699</color>
Did you try offsetting it by something like this
http://android.amberfog.com/?p=9
My solution is to add 9-patch as the last layer item :
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<item>
<shape>
<solid android:color="#color/tab_button_active_layer2" />
</shape>
</item>
<item android:bottom="#dimen/tab_button_active_bottom_bar_width">
<shape>
<solid android:color="#color/tab_button_active_layer1" />
</shape>
</item>
<!-- 9-patch drawable -->
<item android:drawable="#drawable/tab_button_border_top"/>
</layer-list>

Categories

Resources