Skip to main content

Post 83: Android: change progress bar color at runtime programmatically

Change progress bar color at run-time programmatically:

Each time we run this application it calculate the commitment percentage and display a red progress bar if it is less than 50% or green one if it equal or greater than 50%.

      

How to create the full layout of this simple application is beyond this post, we will concentrate on the progress bar. The following code snippet is the definition of a progress bar and setting its properties in the layout xml file of the main activity

<progressbar 
   android:id="@+id/myprogressbar" 
   android:layout_alignparenttop="true" 
   android:layout_centerhorizontal="true" 
   android:layout_height="wrap_content" 
   android:layout_margin="20dp" 
   android:layout_width="fill_parent" 
   android:max="100" 
   style="android: attr/progressBarStyleHorizontal;
"/>

android:max
is the max value of the progress bar and we set its style with the built-in android style of horizontal progress bar.

To change the progress bar with colors you want you need to create a layer-list xml file and save it into res/drawable folder of your project. This xml file will contains the new style of the progress bar that will be attached to it at run time. 

<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
    <item android:id="@android:id/background">
        <shape>
            <gradient
                    android:startColor="#000001"
                    android:centerColor="#0b131e"
                    android:centerY="0.75"
                    android:endColor="#0d1522"
                    android:angle="270"
            />
        </shape>
    </item>
    <item android:id="@android:id/progress">
        <clip>
            <shape>
                <gradient
                    android:startColor="#00ff00"
                    android:centerColor="#00dd00"
                    android:centerY="0.75" 
                    android:endColor="#00bb00"
                    android:angle="270"
                />
            </shape>
        </clip>
    </item>
</layer-list>

This is the new drawable of the red progress bar, you can replace values with red font with the color you want. In our example you need to create another one for green progress bar.(In the end of this post there is a link for downloading the full project of this example).

Now we prepared the required files that we need to change the progress bar color and it is time to apply this new style programmatically when the user click the calculate button.

public void Calculate(View view)
{
  if(view.getId()==R.id.btncalc)
  {
     EditText etcomusers = (EditText)findViewById(R.id.comusersnumber);
     double commiteduser =Double.parseDouble(etcomusers.getText().toString());
      
     EditText ettotcomusers = (EditText)findViewById(R.id.totalcomusersnumber);
     double totalcommiteduser =Double.parseDouble(ettotcomusers.getText().toString());
      
    int percent = (int) ((commiteduser/totalcommiteduser)*100);
      
    ProgressBar myprogressbar = (ProgressBar)findViewById(R.id.myprogressbar);
      
    Resources res = getResources();
    Rect bounds = myprogressbar.getProgressDrawable().getBounds();
      
    if(percent >= 50)
    {
     myprogressbar.setProgressDrawable(res.getDrawable(R.drawable.greenprogressbar));
    }
    else
    {
     myprogressbar.setProgressDrawable(res.getDrawable(R.drawable.redprogressbar));
    }
    myprogressbar.getProgressDrawable().setBounds(bounds);
    myprogressbar.setProgress(percent);
  }
}

first section of code for getting the entered values and calculate the committed users percentage. Before changing the progress bar color we need to get the Bounds of the current progress drawable to be applied after setting the new ProgressDrawable with new color. If comitted users percentage is greater than 50% we set the ProgressDrawable with the green one else we set it with the red then we set the Bounds and Progress of the progress bar(the length of colored bar).


Download Code



Comments

Popular posts from this blog

How to get VTU Provisional Degree Certificate (PDC) ?

Here is the procedure to get the Provisional Degree Certificate from the VTU by passed out candidates. You can get the certificate through post by sending the required documents to VTU. Documents and things required: 1. Provisional Degree Certificate (PDC) application form 2. DD of specified fees amount or print copy of receipt if you paid the fees online 3. A letter to the VTU registrar 4. A4 size envelope Step 1: Fill the PDC application You can download the PDC application form from the VTU website. Click below link for application download. PDC application form download     After downloading the application take a print of it and properly fill the form. Step 2: Make a DD of specified amount as the fees of PDC: Go to the bank and make the DD of prescribed fee amount for the PDC. The fee amount is specified in the above link from where you downloaded the application. Make the DD in favor of " Finance Officer, VTU Belgaum ".   ...

Fresher's Walkins Bangalore: 16th March 2017

Read Walkin details properly , Try to call company before attending if possible !!! ____________________________________ 1. ACT Fibernet : Software Developers Interview Dates: 16th & 17th March 2017 Job Description: Candidate must have a bachelors degree( BE, B.Tech) . Must have scored above 60% in their graduation, XII,X grade exams. Experience level: 0 to 9 months. Good analytical & excellent communication skills. Good programming skills, and must be logical and analytical in thinking. Understanding of development best-practices and concepts, including reusability, extensibility and documentation of implemented code. Conceptual understanding of the structure and benefits of object-oriented programming language Conceptual understanding of SQL and RDBMS such as MySQL and Oracle. Good understanding on Operating system concepts Linux , Windows Understanding and experience (academic or on-the-job) in using C/C++/.NET/CSS/Java/JS/HTML languages to implement cod...