How to create Custom Horizontal progress bar - android

Please help to create horizontal progress bar like this

Set style for horizontal progress bar
style="?android:attr/progressBarStyleHorizontal"
Create custom progress drawable: green_progress_drawable.xml
<?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>
<corners android:radius="5dip" />
<gradient
android:startColor="#779d9e9d"
android:centerColor="#995a5d5a"
android:centerY="0.75"
android:endColor="#ff747674"
android:angle="270"
/>
</shape>
</item>
<item android:id="#android:id/secondaryProgress">
<clip>
<shape>
<corners android:radius="5dip" />
<gradient
android:startColor="#80ffd300"
android:centerColor="#80ffb600"
android:centerY="0.75"
android:endColor="#a0ffcb00"
android:angle="270"
/>
</shape>
</clip>
</item>
<item
android:id="#android:id/progress"
>
<clip>
<shape>
<corners
android:radius="5dip" />
<gradient
android:startColor="#33FF33"
android:endColor="#008000"
android:angle="270" />
</shape>
</clip>
</item>
</layer-list>
My progressbar code:
<ProgressBar
android:progressDrawable="#drawable/green_progress_drawable"
style="?android:attr/progressBarStyleHorizontal"
android:id="#+id/mf_progress_bar"
app:layout_widthPercent="80%"
app:layout_heightPercent="8%"
app:layout_marginTopPercent="5%"
android:layout_gravity="center_horizontal"
/>
Code credit to #Ryan https://stackoverflow.com/a/5745923/3879847
My output:
I hope this post maybe helpful for someone..

You do not need a custom progress bar. Use style="#android:style/Widget.ProgressBar.Horizontal" in your layout xml file. You will also need to use
ProgressBar.incrementProgressBy(int progress);
See Android Developers|ProgressBar

Related

Seekbar Thumb clipping

I'm creating a custom Seekbar with thumb.
Currently it looks like this:
http://imgur.com/ofVc7eg
The problem:
The drawable i'm using for the thumb (an oval shape) clips over the progressbar.
My code look like this:
The Seekbar in the layout:
<SeekBar
android:background="#android:color/holo_red_light"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:thumb="#xml/thumb_image"
android:progressDrawable="#xml/progress"
android:max="100"/>
progress.xml:
<?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>
<corners android:radius="5dip" />
<gradient
android:startColor="#ff9d9e9d"
android:centerColor="#ff5a5d5a"
android:centerY="0.75"
android:endColor="#ff747674"
android:angle="270"
/>
</shape>
</item>
<item android:id="#android:id/secondaryProgress">
<clip>
<shape>
<corners android:radius="5dip" />
<gradient
android:startColor="#80ffd300"
android:centerColor="#80ffb600"
android:centerY="0.75"
android:endColor="#a0ffcb00"
android:angle="270"
/>
</shape>
</clip>
</item>
<item android:id="#android:id/progress">
<clip>
<shape>
<corners android:radius="5dip" />
<gradient
android:startColor="#FFD700"
android:centerColor="#FFB90F"
android:centerY="0.75"
android:endColor="#FFA500"
android:angle="270"
/>
</shape>
</clip>
</item>
</layer-list>
thumb_image.xml:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval"
android:color="#android:color/transparent">
<solid android:color="#android:color/black" />
<size
android:width="30dp"
android:height="30dp" />
</shape>
try
android:splitTrack="false"
in the seek bar
For reference see this thread
Custom seekbar thumb not transparent on Lollipop API21
What worked for me was setting android:clipChildren="false" on the parent view.
I have used android:thumbOffset="-0dp" and assigned custom thumb and its working fine. Implemented many solutions but they are not working perfectly because of custom thumb drawable but this one is straightforward with minimal changes.

How to set progressbar background

I have set the progressbar style using the below code.
I have declared the progressbar widget:
<ProgressBar
android:id="#+id/loadProgress"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="match_parent"
android:layout_height="20dip"
android:max="100"
android:progressDrawable="#drawable/load_progress"
android:progress="0" />
defined the "#drawable/load_progress"
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="#android:id/background"
android:drawable="#drawable/background_bk">
</item>
<item
android:id="#android:id/progress"
android:drawable="#drawable/progress_bk">
</item>
</layer-list>
defined the "#drawable/background_bk" (it is red)
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >
<gradient
android:angle="0"
android:endColor="#ff0000"
android:startColor="#ff0990" />
<stroke
android:width="1dp"
android:color="#ff0000" />
</shape>
defined the #drawable/progress_bk (it is green)
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >
<gradient
android:angle="0"
android:endColor="#00ff00"
android:startColor="#00ff00" />
<stroke
android:width="1dp"
android:color="#00ff00" />
</shape>
The progressbar always shows totally green (which is how it should appear when the progress value is 100), even if I set the progress value to less than 100. Any input would be appreciated.
Here is what I did to have a progress bar that was green with clear background:
android:background="#color/white"
android:progressBackgroundTint="#color/white"
android:progressTint="#color/green_light"
Finally, I find a easy solution.
1
you can find the default drawable xml file in your folder
"SDK_path\platforms\android19\data\res\drawable"
Overthere you can find many default style of many widgets.
2
and get the file "progress_horizontal.xml".
the codes in that file is:
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="#android:id/background">
<shape>
<corners android:radius="5dip" />
<gradient
android:startColor="#ff9d9e9d"
android:centerColor="#ff5a5d5a"
android:centerY="0.75"
android:endColor="#ff747674"
android:angle="270"
/>
</shape>
</item>
<item android:id="#android:id/secondaryProgress">
<clip>
<shape>
<corners android:radius="5dip" />
<gradient
android:startColor="#80ffd300"
android:centerColor="#80ffb600"
android:centerY="0.75"
android:endColor="#a0ffcb00"
android:angle="270"
/>
</shape>
</clip>
</item>
<item android:id="#android:id/progress">
<clip>
<shape>
<corners android:radius="5dip" />
<gradient
android:startColor="#ffffd300"
android:centerColor="#ffffb600"
android:centerY="0.75"
android:endColor="#ffffcb00"
android:angle="270"
/>
</shape>
</clip>
</item>
</layer-list>
3
you can easily change the color of the "progress color" and "background color".

Progressbar with three sections in Android

I have figured out that we can set a secondary progress for a progress bar in android by defining a custom style like following xml code:
res/drawable/custom_profressbar_horizontal.xml:
<?xml version="1.0" encoding="UTF-8"?>![enter image description here][1]
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="#android:id/background">
<shape>
<corners android:radius="15dip" />
<gradient
android:startColor="#ffffffff"
android:centerColor="#ffdddddd"
android:centerY="0.50"
android:endColor="#ffffffff"
android:angle="270" />
</shape>
</item>
<item android:id="#android:id/secondaryProgress">
<clip>
<shape>
<corners android:radius="15dip" />
<solid android:color="#ec3910"/>
</shape>
</clip>
</item>
<item android:id="#android:id/progress">
<clip>
<shape>
<corners android:radius="15dip" />
<gradient android:startColor="#ff0e75af"
android:endColor="#ff1997e1"
android:angle="90" />
</shape>
</clip>
</item>
</layer-list>
and in the layout of my Activity i have this:
activity_maib.xml:
<ProgressBar
style="#style/CustomProgressBar"
android:layout_width="fill_parent"
android:layout_height="30dp"
android:layout_margin="7dp"
android:max="100"
android:progress="50"
android:secondaryProgress="80" />
and i have this progress bar:
But i need to have a third progress. how can i do this?
Thanx in advance.

Thin blue progress bar

I'm trying to create a thin, blue progress bar, similar to this:
I'm not that good with Drawables, but what I have changes the ProgressBar to green, but I'm not sure how to make it thin and blue:
<?xml version="1.0" encoding="UTF-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
>
<ProgressBar
android:id="#+id/ProgressBar"
android:layout_centerInParent="true"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:progressDrawable="#drawable/progress"
style="#android:style/Widget.ProgressBar.Horizontal"
android:visibility="gone"
/>
</LinearLayout>
Drawable
<?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>
<corners android:radius="5dip" />
<gradient
android:startColor="#ff9d9e9d"
android:centerColor="#ff5a5d5a"
android:centerY="0.75"
android:endColor="#ff747674"
android:angle="270"
/>
</shape>
</item>
<item android:id="#android:id/secondaryProgress">
<clip>
<shape>
<corners android:radius="5dip" />
<gradient
android:startColor="#80ffd300"
android:centerColor="#80ffb600"
android:centerY="0.75"
android:endColor="#a0ffcb00"
android:angle="270"
/>
</shape>
</clip>
</item>
<item
android:id="#android:id/progress"
>
<clip>
<shape>
<corners
android:radius="5dip" />
<gradient
android:startColor="#33FF33"
android:endColor="#008000"
android:angle="270" />
</shape>
</clip>
</item>
</layer-list>
The height is defined by the android:layout_height="wrap_content attribute. You could set it to 2dp for example, to create a thinner progress bar.
Changing your color will be a bit more difficult, since you are using gradients. So you'll have to change your start & end color to some kind of blue.
What I am using
<ProgressBar
android:id="#+id/progressbar"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="match_parent"
android:layout_height="4dp"
android:layout_marginTop="0.01dp"
android:max="100"
android:progress="50"
android:progressDrawable="#drawable/myprogressbar"
android:secondaryProgress="0" />
MyProgressBar.xml in the drawable folder
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="#android:id/background">
<shape>
<corners android:radius="0dip" />
<gradient android:startColor="#C0C0C0" android:centerColor="#F8F8FF"
android:centerY="0.75" android:endColor="#ffffff" android:angle="90" />
<stroke android:width="0.01dp" android:color="#6B8E23" />
</shape>
</item>
<item android:id="#android:id/progress">
<clip>
<shape>
<corners android:radius="0dip" />
<gradient android:startColor="#9ACD32" android:endColor="#FFFF00"
android:angle="90" />
<stroke android:width="1dp" android:color="#6B8E23" />
</shape>
</clip>
</item>
</layer-list>
Replace the color codes with your own blue color codes:
android:startColor="#C0C0C0" android:centerColor="#F8F8FF"
android:endColor="#ffffff"

how does setProgress() know which is the color of the progress that is to be grown in this code?

and when it calls setProgress(20) , how does it know that it is the green color will increase instead of the gray color?
pb.setProgress(0);
pb.setProgressDrawable(getResources().getDrawable(R.drawable.green_progress));
pb.setProgress(20);
Here is the green_progress.xml
<?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>
<corners android:radius="5dip" />
<gradient
android:startColor="#ff9d9e9d"
android:centerColor="#ff5a5d5a"
android:centerY="0.75"
android:endColor="#ff747674"
android:angle="270"
/>
</shape>
</item>
<item
android:id="#android:id/progress">
<clip>
<shape>
<corners
android:radius="5dip" />
<gradient
android:startColor="#color/greenStart"
android:centerColor="#color/greenMid"
android:centerY="0.75"
android:endColor="#color/greenEnd"
android:angle="270"
/>
</shape>
</clip>
</item>
</layer-list>
It's thanks to the #android:id/progress, a special id which is known by ProgressDialog class. So, in ProgressDialog, the code know which shape it has to expand and which one it has to reduce, during the progression.

Categories

Resources