android ProgressBar without clipping? - android

Any default ProgressBar has a ClipDrawable level, for example a green Drawable is filling the grey background one on my device, the yellow on some others.
My question: is that possible to create a ProgressBar without this (partly) empty background when progress is less than max or should I use an ImageView instead? I need only the ClipDrawable to change its width so it looks like a diagram while the grey background never appears.

Create layer-list drawable like this.
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="#android:id/background">
<shape android:shape="rectangle" >
<solid android:color="#android:color/transparent" />
</shape>
</item>
<item android:id="#android:id/secondaryProgress">
<clip>
<shape>
<gradient
android:startColor="#234"
android:centerColor="#234"
android:centerY="0.75"
android:endColor="#a24"
android:angle="270"
/>
</shape>
</clip>
</item>
<item android:id="#android:id/progress">
<clip>
<shape>
<gradient
android:startColor="#144281"
android:centerColor="#0b1f3c"
android:centerY="0.75"
android:endColor="#06101d"
android:angle="270"
/>
</shape>
</clip>
</item>
The main point is to make item with android:id="#android:id/background use #android:color/transparent
And then set it to android:progressDrawable property of ProgressBar
<ProgressBar
android:id="#+id/progressBar"
android:progressDrawable="#drawable/your_layer_list_created_on_previous_step"
android:layout_width="fill_parent"
android:layout_height="8dp"
style="?android:attr/progressBarStyleHorizontal"
android:indeterminateOnly="false"
android:max="100" />

Related

How to create horizontal indeterminate progress drawable with round corners for progressbar in android?

I want to create horizontal indeterminate progress drawable with round corners for progress bar in android. How to create that using xml?
Edit 1: I am using following 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>
<solid android:color="?attr/dividerAndProgressColor" />
<corners android:radius="20dp" />
</shape>
</item>
<item android:id="#android:id/secondaryProgress">
<clip>
<shape>
<solid android:color="?attr/secondaryText" />
<corners android:radius="20dp" />
</shape>
</clip>
</item>
<item
android:id="#android:id/progress"
>
<clip>
<shape>
<solid android:color="?attr/secondaryText" />
<corners android:radius="20dp" />
</shape>
</clip>
</item>
</layer-list>
android:indeterminateDrawable="#drawable/progress_drawable_start_end_indeterminate"
But there is one problem, animation starts from 0 reaches 100 and then restarts. This is not desired in case of indeterminate progressbar.
round corner
lookout this, https://stackoverflow.com/a/42646939/12709358
<ProgressBar
android:id="#+id/progressbar_horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:indeterminateOnly="true"
android:indeterminateDrawable="#drawable/progressbar_indeterminate_horizontal"
android:progressDrawable="#drawable/progressbar_horizontal"
android:minHeight="24dip"
android:maxHeight="24dip"
/>
also see this, https://stackoverflow.com/a/63463786/12709358

how to set progress within progress-bar in android

this is my horizontal progress-bar->
<ProgressBar
android:id="#+id/pb"
style="#android:style/Widget.Holo.Light.ProgressBar.Horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/tv"
android:hapticFeedbackEnabled="true"
android:progressDrawable="#drawable/point_one_progress"
android:scaleY="0.80" />
this is my point_one_progress.xml
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="#android:id/background">
<shape>
<corners android:radius="12dp"/>
<solid android:color="#cccccc"/>
</shape>
</item>
<item android:id="#android:id/progress"
android:top="4dp"
android:bottom="4dp"
android:left="-1dp"
>
<scale android:scaleWidth="100%">
<shape>
<corners android:radius="12dp"/>
<solid android:color="#color/colorPrimaryDark"/>
</shape>
</scale>
</item>
</layer-list>
i am using horizontal progress-bar display progress based one point for more than 3 value progress coming perfect and within progress-bar but for 1,2,3 progress is coming outside the progress-bar please suggest me how to fix it how to keep progress within progress-bar for 1,2,3 value .
you can see in below image for value 1->
for 10 value you can see screen
Its the issue with ur drawable. remove top , bottom fields from the item tag. Your drawable file should look like this.Also remove scaleY from the progress bar view
<?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="12dp"/>
<gradient
android:startColor="#color/white"
android:endColor= "#color/white"
/>
</shape>
</item>
<item android:id="#android:id/progress">
<clip>
<shape>
<corners android:radius="12dp"/>
<solid android:color="#color/primaryDarkColor"/>
</shape>
</clip>
</item>
</layer-list>
It's issue with drawable padding. You need to make it 0dp for all four sides.
Here is xml code.
<ProgressBar
android:id="#+id/pb"
style="#android:style/Widget.Holo.Light.ProgressBar.Horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:hapticFeedbackEnabled="true"
android:progress="2"
android:progressDrawable="#drawable/point_one_progress" />
Here is corrected 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="12dp" />
<solid android:color="#cccccc" />
</shape>
</item>
<item android:id="#android:id/progress">
<clip>
<shape>
<corners android:radius="12dp" />
<solid android:color="#color/colorPrimaryDark" />
</shape>
</clip>
</item>
</layer-list>

Progress bar with rounded corners?

I am trying to achieve this progress bar design:
The current code that I have produces this:
This is the code:
<item android:id="#android:id/background">
<shape>
<corners android:radius="8dp"/>
<solid android:color="#color/dirtyWhite"/>
</shape>
</item>
<item android:id="#android:id/progress">
<clip>
<shape>
<corners android:radius="8dp"/>
<solid android:color="#color/colorPrimaryDark"/>
</shape>
</clip>
</item>
My progress bar:
<ProgressBar
android:id="#+id/activeProgress"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="300dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:progress="10"
android:progressDrawable="#drawable/rounded_corners_progress_bar"/>
I tried adding <size> attribute on the <shape> in <clip to make the progress shape a bit smaller but it did not work. Also, the progress bar is flat and I want to be curved as per design. What I need to change in order to achieve the design?
How to make the progress shape a bit smaller?
You need to give the progress item a little padding, like so:
<item android:id="#android:id/progress"
android:top="2dp"
android:bottom="2dp"
android:left="2dp"
android:right="2dp">
How to make the progress bar to be curved as per design?
Replace the <clip></clip> element, with <scale android:scaleWidth="100%"></scale>. That will make the shape keep its form as it grows - and not cut off.
Unfortunately, it will have a little unwanted visual effect at the beginning - as the shape corners don't have enough space to draw. But it might be good enough for most cases.
Full code:
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="#android:id/background">
<shape>
<corners android:radius="8dp"/>
<solid android:color="#color/dirtyWhite"/>
</shape>
</item>
<item android:id="#android:id/progress"
android:top="1dp"
android:bottom="1dp"
android:left="1dp"
android:right="1dp">
<scale android:scaleWidth="100%">
<shape>
<corners android:radius="8dp"/>
<solid android:color="#color/colorPrimaryDark"/>
</shape>
</scale>
</item>
</layer-list>
With the Material Components Library you can use the LinearProgressIndicator and the app:trackCornerRadius attribute.
Something like:
<com.google.android.material.progressindicator.LinearProgressIndicator
android:indeterminate="true"
app:trackThickness="xxdp"
app:trackCornerRadius="xdp"/>
It works also for the determinate progress indicator removing the android:indeterminate="true" or using android:indeterminate="false"
Note: it requires at least the version 1.3.0.
Thanks to Georgi Koemdzhiev for a nice question and images. For those who want to make similar to his, do the following.
1) Create a background for a ProgressBar.
progress_bar_background.xml:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle"
>
<corners android:radius="3dp" />
<stroke android:color="#color/white" android:width="1dp" />
</shape>
2) Create a scale for the ProgressBar.
curved_progress_bar.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="2dp" />
<solid android:color="#color/transparent" />
</shape>
</item>
<item android:id="#android:id/progress">
<clip>
<shape>
<corners android:radius="2dp" />
<solid android:color="#ffff00" />
</shape>
</clip>
</item>
</layer-list>
3) In layout file add the ProgressBar.
<FrameLayout
android:layout_width="match_parent"
android:layout_height="6dp"
android:layout_marginStart="20dp"
android:layout_marginTop="10dp"
android:layout_marginEnd="20dp"
android:background="#drawable/progress_bar_background"
>
<ProgressBar
android:id="#+id/progress_bar"
style="#style/Widget.AppCompat.ProgressBar.Horizontal"
android:layout_width="match_parent"
android:layout_height="4dp"
android:layout_margin="1dp"
android:indeterminate="false"
android:progressDrawable="#drawable/curved_progress_bar"
/>
</FrameLayout>
You can achieve progress bar with both inner progress color ,border
color,empty progress with transparent color.
<?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="15dip" />
<stroke android:width="1dp" android:color="#color/black" />
</shape>
</item>
<item android:id="#android:id/secondaryProgress">
<clip>
<shape>
<corners android:radius="5dip" />
<gradient
android:startColor="#color/black"
android:centerColor="#color/trans"
android:centerY="0.75"
android:endColor="#color/black"
android:angle="270"
/>
</shape>
</clip>
</item>
<item
android:id="#android:id/progress"
>
<clip>
<shape>
<corners
android:radius="5dip" />
<gradient
android:startColor="#color/black"
android:endColor="#color/black"
android:angle="270" />
</shape>
</clip>
</item>
</layer-list>
<com.google.android.material.progressindicator.LinearProgressIndicator
android:id="#+id/pb_team2"
android:layout_width="0dp"
android:layout_height="8dp"
android:layout_marginStart="55dp"
android:layout_marginEnd="15dp"
app:trackColor="#E0E0E0"
app:trackCornerRadius="6dp"
app:trackThickness="8dp"
android:progress="2"
app:indicatorColor="#color/red"
android:scaleX="-1"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="#id/tv_versus"
app:layout_constraintTop_toBottomOf="#id/tv_team2_score" />
add material dependencies in build.gradle(Module)
implementation 'com.google.android.material:material:1.6.0'
come back at activity_main.xml. Add
<com.google.android.material.progressindicator.LinearProgressIndicator>
...
app:trackCornerRadius="3dp"
</com.google.android.material.progressindicator.LinearProgressIndicator>
Hope it will do your work.
You can create a custom progressbar with Drawable.
1) create first drawable as a custom_progress_bar_horizontal.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>
<padding
android:bottom="2dip"
android:left="2dip"
android:right="2dip"
android:top="2dip" />
<corners android:radius="5dip" />
<gradient
android:angle="270"
android:centerColor="#80385682"
android:centerY="0.50"
android:endColor="#80577AAF"
android:startColor="#80222F44" />
</shape>
</item>
<item android:id="#android:id/progress">
<clip>
<shape>
<corners android:radius="5dip" />
<gradient
android:angle="90"
android:endColor="#FF1997E1"
android:startColor="#FF0E75AF" />
</shape>
</clip>
</item>
</layer-list>
2) Create style in style.xml file.
<style name="CustomProgressBar" parent="android:Widget.ProgressBar.Horizontal">
<item name="android:indeterminateOnly">false</item>
<item name="android:progressDrawable">#drawable/custom_progress_bar_horizontal</item>
<item name="android:minHeight">10dip</item>
<item name="android:maxHeight">20dip</item>
</style>
3) Use anywhere in your project.
<ProgressBar
android:id="#+id/mProgressBar"
style="#style/CustomProgressBar"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
Output:-

Custom Progress bar or Seekbar or animation?

I need to create some progress bar without dialog like this:
I could not find how to create custom progress bar and use it in main activity (not in dialog). This red bar consists of 2 small images and I need to add them continuously while my thread is working.
Now What is the way to do it ?
Maybe it is better to use animations ?
Any idea ?
Thanks
There is actually a ProgressBar widget.
<ProgressBar
android:id="#+id/pb"
style="#android:style/Widget.ProgressBar.Horizontal"
android:layout_width="match_parent"
android:layout_height="10dp"
android:max="100"
android:progressDrawable="#drawable/progressbar" />
and as progressbar drawable e.g.
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:id="#android:id/background">
<shape>
<gradient
android:angle="270"
android:centerColor="#AA444444"
android:centerY="0.5"
android:endColor="#88000000"
android:startColor="#88000000" />
</shape>
</item>
<item android:id="#android:id/progress">
<clip>
<shape>
<gradient
android:angle="270"
android:centerColor="#FFFFFF44"
android:centerY="0.5"
android:endColor="#FFAAAA00"
android:startColor="#FFAAAA00" />
</shape>
</clip>
</item>
<item android:id="#android:id/secondaryProgress">
<clip>
<shape>
<gradient
android:angle="270"
android:centerColor="#FF88FF88"
android:centerY="0.5"
android:endColor="#FF44AA44"
android:startColor="#FF44AA44" />
</shape>
</clip>
</item>
</layer-list>
and for updates from your thread, post a runnable with progressbar.setProgress() or progressBar.setSecondaryProgress(), or even better, use an AsyncTask and call publishProgress and receive the calls in onProgressUpdate()

How to implement this type of Progressbar in android using xml file

How to apply glow effect?
I am trying to create this type of horizontal ProgressBar but no idea about the glowing effect for as starting point of progress.
Here is my code for gradient color
<?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="0dip" />
<solid android:color="#ff9d9e9d"/>
</shape>
</item>
<item android:id="#android:id/secondaryProgress">
<clip>
<shape>
<corners android:radius="5dip" />
<gradient
android:angle="180"
android:startColor="#8872f8fc"
android:endColor="#886dc0e3" />
</shape>
</clip>
</item>
<item android:id="#android:id/progress">
<clip>
<shape>
<corners android:radius="5dip" />
<gradient
android:angle="180"
android:startColor="#31c6f7"
android:centerColor="#2ea6d1"
android:endColor="#2884a6" />
</shape>
</clip>
</item>
</layer-list>
You can use a png image for glowing head. Use < bitmap> tag inside the layer-list and align it to right. I did a similar thing for a completely different application
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<item>
<shape>
<gradient
android:angle="270"
android:endColor="#4C4C4C"
android:startColor="#303030" />
<stroke
android:width="0dp"
android:color="#000000" />
<corners android:radius="0dp" />
<padding
android:bottom="0dp"
android:left="5dp"
android:right="0dp"
android:top="0dp" />
</shape>
</item>
<item>
<bitmap
android:antialias="true"
android:gravity="right|bottom|fill_vertical"
android:src="#drawable/spinner_down_arrow" />
</item>
</layer-list>
The glow effects are png drawables with transparency. Layed on top of the right side of current process.
Create a custom view progress bar.
Alternatively you can add an ImageView to your layout. Create the drawables for progress bars. Then draw inside the ImageView with 2 layers by code. First layer would be background and the second layer would be the knob. Write redrawing code to the onTouch Listener of imageview to update view as touched.

Categories

Resources