Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/colorPrimary"
android:gravity="center"
tools:context=".MainActivity">
<Button
android:id="@+id/start_btn"
android:background="@color/colorPrimaryDark"
android:text="Start Custom Progress Bar"
android:textAllCaps="false"
android:padding="15dp"
android:textColor="@android:color/white"
android:textStyle="bold"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</RelativeLayout>
dependencies {
implementation 'androidx.cardview:cardview:1.0.0'
}
class CustomProgressBar {
lateinit var dialog: Dialog
fun show(context: Context): Dialog {
return show(context, null)
}
fun show(context: Context, title:CharSequence?): Dialog {
val inflator = context.getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater
val view = inflator.inflate(R.layout.progress_bar, null)
if (title != null) {
view.cp_title.text = title
}
view.cp_bg_view.setBackgroundColor(Color.parseColor("#60000000")) //Background Color
view.cp_cardview.setCardBackgroundColor(Color.parseColor("#70000000")) //Box Color
setColorFilter(view.cp_pbar.indeterminateDrawable,
ResourcesCompat.getColor(context.resources, R.color.colorPrimary, null)) //Progress Bar Color
view.cp_title.setTextColor(Color.WHITE) //Text Color
dialog = Dialog(context, R.style.CustomProgressBarTheme)
dialog.setContentView(view)
dialog.show()
return dialog
}
fun setColorFilter(@NonNull drawable: Drawable, color:Int) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
drawable.colorFilter = BlendModeColorFilter(color, BlendMode.SRC_ATOP)
} else {
@Suppress("DEPRECATION")
drawable.setColorFilter(color, PorterDuff.Mode.SRC_ATOP)
}
}
}
class MainActivity : AppCompatActivity() {
val progressBar = CustomProgressBar()
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
start_btn.setOnClickListener {
// Show progress bar
progressBar.show(this,"Please Wait...")
Handler().postDelayed({
//Dismiss progress bar after 4 seconds
progressBar.dialog.dismiss()
}, 4000)
}
}
}
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:background="#00000000"
android:id="@+id/cp_bg_view"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
xmlns:android="http://schemas.android.com/apk/res/android">
<androidx.cardview.widget.CardView
android:layout_width="150dp"
android:layout_height="150dp"
app:cardCornerRadius="16dp"
app:cardElevation="0dp"
app:cardMaxElevation="0dp"
app:cardBackgroundColor="#00000000"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintStart_toStartOf="parent"
android:id="@+id/cp_cardview"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
>
<androidx.constraintlayout.widget.ConstraintLayout
android:id="@+id/cp_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:layout_gravity="center">
<ProgressBar
android:id="@+id/cp_pbar"
android:layout_gravity="center"
android:layout_width="match_parent"
android:layout_height="45dp"
android:layout_marginTop="8dp"
app:layout_constraintTop_toTopOf="parent"
android:layout_marginBottom="8dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"/>
<TextView
android:id="@+id/cp_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="@android:color/white"
android:text=""
android:textStyle="bold"
app:layout_constraintStart_toStartOf="parent"
android:layout_marginStart="8dp"
app:layout_constraintEnd_toEndOf="parent"
android:layout_marginEnd="8dp"
app:layout_constraintBottom_toBottomOf="parent" android:layout_marginBottom="15dp"/>
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.cardview.widget.CardView>
</androidx.constraintlayout.widget.ConstraintLayout>
//Progress Bar with Text
progressBar.show(this,"Please Wait...")
//Progress Bar without Text
progressBar.show(this)
<resources xmlns:tools="http://schemas.android.com/tools">
<style name="CustomProgressBarTheme">
<item name="android:statusBarColor">#631128</item>
<item name="android:windowLightStatusBar" tools:targetApi="m">false</item>
<item name="android:windowBackground">@android:color/transparent</item>
</style>
</resources>
<resources xmlns:tools="http://schemas.android.com/tools">
<style name="CustomProgressBarTheme">
<item name="android:windowLightStatusBar" tools:targetApi="m">false</item>
<item name="android:windowBackground">@android:color/transparent</item>
</style>
</resources>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.