Monday, 24 October 2016

Android custom circle progress bar

MyActivity.java

public class MyActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        animations();
    }
    
public void animations(){
runOnUiThread(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
for(int loop=0;loop<35;loop++){
ProgressBar progressbar = (ProgressBar) findViewById(R.id.progressBar);
    progressbar.setProgress(loop);
    final long timeinterval = 100;
    try {
Thread.sleep(timeinterval);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}
}
});

}
}

activity_main.xml

<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="400dp"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.myApp.jiohome.MainActivity" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="hello_world" />

    <ProgressBar
        android:id="@+id/progressBar"
        android:layout_width="200dp"
        android:layout_height="200dp"
        android:indeterminate="false"
        android:layout_centerInParent="true"
        android:progressDrawable="@layout/circle_progress_bar"
        android:background="@layout/circle_shape"
        style="?android:attr/progressBarStyleHorizontal"
        android:max="100"
         
        />
    <LinearLayout 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:orientation="vertical">
   <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"  
        android:text="hello_world"
        android:id="@+id/balanceText"></TextView>
   <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="hello_world"></TextView>
     <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="hello_world"></TextView>
       <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="hello_world"></TextView>
   
   </LinearLayout>
   
</RelativeLayout>

circle_progress_bar.xml

<?xml version="1.0" encoding="utf-8"?>
<rotate xmlns:android="http://schemas.android.com/apk/res/android"
    android:fromDegrees="270"
    android:toDegrees="270">
    <shape
        android:innerRadiusRatio="2.5"
        android:shape="ring"
        android:thickness="8dp"
        android:useLevel="true"
        android:background="#ff00ff"><!-- this line fixes the issue for lollipop api 21 -->

        <gradient
            android:angle="0"
            android:endColor="#ff00ff"
            android:startColor="#ff00ff"
            android:type="sweep"
            android:useLevel="false" />
    </shape>
</rotate>

circle_shape.xml

<?xml version="1.0" encoding="utf-8"?>
<shape
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="oval"
 >
<corners android:radius="20dip"/>
<stroke android:color="@android:color/transparent" android:width="50dip"/>
<solid android:color="#efefef"/>
</shape>

circular.xml

<?xml version="1.0" encoding="utf-8"?>

<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:id="@android:id/secondaryProgress">
        <shape
            android:innerRadiusRatio="6"
            android:shape="ring"
            android:thicknessRatio="20.0"
            android:useLevel="true">


            <gradient
                android:centerColor="#999999"
                android:endColor="#999999"
                android:startColor="#999999"
                android:type="sweep" />
        </shape>
    </item>

    <item android:id="@android:id/progress">
        <rotate
            android:fromDegrees="270"
            android:pivotX="50%"
            android:pivotY="50%"
            android:toDegrees="270">

            <shape
                android:innerRadiusRatio="6"
                android:shape="ring"
                android:thicknessRatio="20.0"
                android:useLevel="true">


                <rotate
                    android:fromDegrees="0"
                    android:pivotX="50%"
                    android:pivotY="50%"
                    android:toDegrees="360" />

                <gradient
                    android:centerColor="#00FF00"
                    android:endColor="#00FF00"
                    android:startColor="#00FF00"
                    android:type="sweep" />

            </shape>
        </rotate>
    </item>
</layer-list>

Output

No comments:

Post a Comment