How to Customize a Progress Bar In Android
Customizing a ProgressBar
requires defining the attribute or properties for the background and progress of your progress bar.
Create an XML file named customprogressbar.xml
in your res->drawable
folder:
custom_progressbar.xml
<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="#000001"
android:centerColor="#0b131e"
android:centerY="1.0"
android:endColor="#0d1522"
android:angle="270"
/>
</shape>
</item>
<!-- Define the progress properties like start color, end color etc -->
<item android:id="@android:id/progress">
<clip>
<shape>
<gradient
android:startColor="#007A00"
android:centerColor="#007A00"
android:centerY="1.0"
android:endColor="#06101d"
android:angle="270"
/>
</shape>
</clip>
</item>
</layer-list>
Now you need to set the progressDrawable
property in customprogressbar.xml
(drawable)
You can do this in the XML file or in the Activity (at run time).
Do the following in your XML:
<ProgressBar
android:id="@+id/progressBar1"
style="?android:attr/progressBarStyleHorizontal"
android:progressDrawable="@drawable/custom_progressbar"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
At run time do the following
// Get the Drawable custom_progressbar
Drawable draw=res.getDrawable(R.drawable.custom_progressbar);
// set the drawable as progress drawable
progressBar.setProgressDrawable(draw);
Edit: corrected xml layout
Custom logo as progress bar in android studio
You would use AnimationDrawable instead of ProgressBar, copy and modify from the original vector to quadruple vectors (like drawable/phase1.xml
~ drawable/phase4.xml
) and list them in an animation list (like drawable/progress.xml
). For example:
<animation-list xmlns:android="http://schemas.android.com/apk/res/android"
android:oneshot="false">
<item
android:drawable="@drawable/phase1"
android:duration="100" />
<item
android:drawable="@drawable/phase2"
android:duration="100" />
<item
android:drawable="@drawable/phase3"
android:duration="100" />
<item
android:drawable="@drawable/phase4"
android:duration="100" />
</animation-list>
Then you can load it into an ImageView and animate it:
ImageView progress.setImageResource(R.drawable.progress);
((AnimationDrawable) progress.getDrawable()).start();
Custom progressbar how to
You can use something like this with ConstraintLayout
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:id="@+id/button3"
android:layout_width="match_parent"
android:layout_height="0dp"
android:background="#A496D8"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHeight_percent="0.1"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:padding="0dp"
android:background="#000000"
android:elevation="6dp"
app:layout_constraintBottom_toBottomOf="@+id/button3"
app:layout_constraintStart_toStartOf="@+id/button3"
app:layout_constraintTop_toTopOf="@+id/button3" />
<TextView
android:id="@+id/textView2"
android:layout_width="0dp"
android:layout_height="0dp"
android:elevation="6dp"
android:text="Some text"
android:textAlignment="center"
android:textColor="#000000"
app:layout_constraintBottom_toBottomOf="@+id/button3"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="@+id/button3"
app:layout_constraintTop_toTopOf="@+id/button3"
app:layout_constraintVertical_bias="0.0" />
</androidx.constraintlayout.widget.ConstraintLayout>
It will look like this:
But to be honest, I think you better use some library that already handling a lot of logic for you. for example:
NumberProgressBar
ProgressView
Android-RoundCornerProgressBar
Another easy option that you have is to use a horizontal progress bar (How to create a horizontal loading progress bar?) and just put some text on it.
How can I add a custom progress bar before logging in?
First of all, thats not how asynchronous calls are made, if timeout occurs, your code may show a progressbar and stops executing unless button is pressed again,
You should implement a thread and use an interface for callbacks, dismiss the dialog on connection or login completed event.Also you will not need to wait for x seconds if you are in background thread waiting for callbacks, (5 milliseconds is a very short time for a network call, even your ping might take upto 100ms)
And remember to keep reference to the dialog so you can latter dismiss it, i don't think your are doing that even in the code not shared in the question
How to communicate with ui thread
Easier example
In your class, outside oncreate
AlertDialog builder; //you can name it anything
In your progressDialog
builder = new AlertDialog.Builder(this)
instead of
final AlertDialog.Builder builder = new AlertDialog.Builder(this)
And on connection or login event (or whenever you want to dismiss the dialog
if (builder!=null) {
builder.dismiss();
builder = null; //newbies safety
}
How to create a custom ProgressBar with drawables, without programming?
Create progress.xml file in drawable folder:
<?xml version="1.0" encoding="utf-8"?>
<rotate xmlns:android="http://schemas.android.com/apk/res/android"
android:fromDegrees="0"
android:pivotX="50%"
android:pivotY="50%"
android:toDegrees="360">
<shape
android:innerRadiusRatio="3"
android:shape="ring"
android:thicknessRatio="8"
android:useLevel="false">
<size
android:width="76dip"
android:height="76dip" />
<gradient
android:angle="0"
android:endColor="#1672B6"
android:startColor="@android:color/transparent"
android:type="sweep"
android:useLevel="false" />
</shape>
</rotate>
Then use it like this:
<ProgressBar
android:id="@+id/pbarLoading"
android:layout_width="36dp"
android:layout_height="36dp"
android:indeterminateDrawable="@drawable/progress"/>
Custom Progress bar in android with Background
Here i found my answer
Firstly you need add xml drawable resource with the name of 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="18dp" />
<stroke android:color="@color/transparent" android:width="6dp" />
</shape>
Now make an other xml resource file with the name of 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="18dp" />
<solid android:color="@color/transparent" />
<!-- <stroke android:color="@color/transparent" android:width="6dp" />-->
</shape>
</item>
<item android:id="@android:id/progress">
<clip>
<shape>
<corners android:radius="18dp" />
<solid android:color="#FFFFFF" />
</shape>
</clip>
</item>
</layer-list>
and Finally in Splash activity xml file paste this code for Progress bar
<TextView
android:id="@+id/textView"
android:layout_centerInParent="true"
android:layout_centerHorizontal="true"
android:layout_width="wrap_content"
android:textColor="@color/white"
android:textSize="22dp"
android:textStyle="bold"
android:fontFamily="@font/symbioarltmedium"
android:layout_height="wrap_content"
android:text="80%"/>
<ProgressBar
android:layout_marginTop="10dp"
android:layout_centerHorizontal="true"
android:layout_below="@+id/textView"
android:id="@+id/progressBar"
style="@style/Widget.AppCompat.ProgressBar.Horizontal"
android:layout_width="300dp"
android:layout_height="50dp"
android:indeterminate="false"
android:max="100"
android:padding="5dp"
android:progress="1"
android:background="@drawable/progress_bar_background"
android:progressDrawable="@drawable/curved_progress_bar"
/>
and here in Java
private ProgressBar progressBar;
TextView textView;
private int progressStatus = 0;
private Handler handler = new Handler();
progressBar = (ProgressBar) findViewById(R.id.progressBar);
textView = (TextView) findViewById(R.id.textView);
// Start long running operation in a background thread
new Thread(new Runnable() {
public void run() {
while (progressStatus < 100) {
progressStatus += 1;
// Update the progress bar and display the
//current value in the text view
handler.post(new Runnable() {
public void run() {
progressBar.setProgress(progressStatus);
textView.setText(progressStatus+"%");
}
});
try {
// Sleep for 200 milliseconds.
Thread.sleep(25);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
Intent intent = new Intent(SplashActivity.this, HomeActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
finish();
}
}).start();
and use this color
<color name="transparent">#27EEECEC</color>
How to create Custom Horizontal progress bar
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 Topics
How to Update Xml File from Another Xml File Dynamically
Why Do We Have to Call Super in Android Sometimes
How to Instantiate an Abstract Class Directly
Binary Xml File Line #0: Error Inflating Class Imageview
How to Combine One Android Studio Project into Another Android Studio Project
How to Import All Packages in Jruby
Simple Calculator with Jsp/Servlet and Ajax
Convert from Enum Ordinal to Enum Type
Setting the Default Font of Swing Program
How to Add One Month to Current Date in Java
Click Is Not Working on the Listitem Listview Android
Reducing Android App (Apk) Size
Does Proguard Work to Obfuscate Static String Constants
Broadcastreceiver for Action_Media_Button Not Working
Android - Viewrootimpl$Calledfromwrongthreadexception
How to Escape Apostrophe or Quotes on a Jsp (Used by JavaScript)