Created
April 8, 2021 15:54
-
-
Save codinginflow/477606b85ed11c537a81e80224361878 to your computer and use it in GitHub Desktop.
Circular Determinate ProgressBar with Background and Text Tutorial
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="utf-8"?> | |
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" | |
xmlns:app="http://schemas.android.com/apk/res-auto" | |
xmlns:tools="http://schemas.android.com/tools" | |
android:layout_width="match_parent" | |
android:layout_height="match_parent" | |
tools:context=".MainActivity"> | |
<ProgressBar | |
android:id="@+id/progress_bar" | |
style="@style/CircularDeterminateProgressBar" | |
android:layout_width="200dp" | |
android:layout_height="200dp" | |
app:layout_constraintBottom_toBottomOf="parent" | |
app:layout_constraintLeft_toLeftOf="parent" | |
app:layout_constraintRight_toRightOf="parent" | |
app:layout_constraintTop_toTopOf="parent" | |
tools:progress="60" /> | |
<TextView | |
android:id="@+id/text_view_progress" | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:textAppearance="@style/TextAppearance.AppCompat.Large" | |
app:layout_constraintBottom_toBottomOf="@+id/progress_bar" | |
app:layout_constraintEnd_toEndOf="@+id/progress_bar" | |
app:layout_constraintStart_toStartOf="@+id/progress_bar" | |
app:layout_constraintTop_toTopOf="@+id/progress_bar" | |
tools:text="60%" /> | |
<Button | |
android:id="@+id/button_decr" | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:text="- 10%" | |
app:layout_constraintStart_toStartOf="@+id/progress_bar" | |
app:layout_constraintTop_toBottomOf="@+id/progress_bar" /> | |
<Button | |
android:id="@+id/button_incr" | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:text="+ 10%" | |
app:layout_constraintEnd_toEndOf="@+id/progress_bar" | |
app:layout_constraintTop_toBottomOf="@+id/progress_bar" /> | |
</androidx.constraintlayout.widget.ConstraintLayout> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="utf-8"?> | |
<layer-list xmlns:android="http://schemas.android.com/apk/res/android"> | |
<item> | |
<shape | |
android:shape="ring" | |
android:thicknessRatio="16" | |
android:useLevel="false"> | |
<solid android:color="#DDD" /> | |
</shape> | |
</item> | |
<item> | |
<rotate | |
android:fromDegrees="270" | |
android:toDegrees="270"> | |
<shape | |
android:shape="ring" | |
android:thicknessRatio="16" | |
android:useLevel="true"> | |
<gradient | |
android:endColor="@color/colorPrimary" | |
android:startColor="@color/colorAccent" | |
android:type="sweep" /> | |
</shape> | |
</rotate> | |
</item> | |
</layer-list> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.codinginflow.circulardeterminateprogressbar | |
import androidx.appcompat.app.AppCompatActivity | |
import android.os.Bundle | |
import kotlinx.android.synthetic.main.activity_main.* | |
class MainActivity : AppCompatActivity() { | |
private var progr = 0 | |
override fun onCreate(savedInstanceState: Bundle?) { | |
super.onCreate(savedInstanceState) | |
setContentView(R.layout.activity_main) | |
updateProgressBar() | |
button_incr.setOnClickListener { | |
if (progr <= 90) { | |
progr += 10 | |
updateProgressBar() | |
} | |
} | |
button_decr.setOnClickListener { | |
if (progr >= 10) { | |
progr -= 10 | |
updateProgressBar() | |
} | |
} | |
} | |
private fun updateProgressBar() { | |
progress_bar.progress = progr | |
text_view_progress.text = "$progr%" | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<resources> | |
<!-- Base application theme. --> | |
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar"> | |
<!-- Customize your theme here. --> | |
<item name="colorPrimary">@color/colorPrimary</item> | |
<item name="colorPrimaryDark">@color/colorPrimaryDark</item> | |
<item name="colorAccent">@color/colorAccent</item> | |
</style> | |
<style name="CircularDeterminateProgressBar"> | |
<item name="android:indeterminateOnly">false</item> | |
<item name="android:progressDrawable">@drawable/circle</item> | |
</style> | |
</resources> |
maybe a hint that the used colors are not Standart and must created manualy.
eg:
@color/colorPrimary
but result is really nice, thanks a lot.
how to make it in java
I just did it, look: https://github.com/dgopadakak/CircleProgressBar
how to make it in java
package com.codinginflow.circulardeterminateprogressbar;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
private int progress = 0;
private ProgressBar progressBar;
private TextView textViewProgress;
private Button buttonIncrement;
private Button buttonDecrement;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
progressBar = findViewById(R.id.progress_bar);
textViewProgress = findViewById(R.id.text_view_progress);
buttonIncrement = findViewById(R.id.button_increment);
buttonDecrement = findViewById(R.id.button_decrement);
updateProgressBar();
buttonIncrement.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (progress <= 90) {
progress += 10;
updateProgressBar();
}
}
});
buttonDecrement.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (progress >= 10) {
progress -= 10;
updateProgressBar();
}
}
});
}
private void updateProgressBar() {
progressBar.setProgress(progress);
textViewProgress.setText(progress + "%");
}
}
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
thanks bro