Showing posts with label Android ProgressBar. Show all posts
Showing posts with label Android ProgressBar. Show all posts

Android ProgressBar


1.Download Event using ProgressBar in Android. 
2.Different Styles of ProgressBar in Android.

1.Download Event using ProgressBar in Android. 

Step 1 : Select File -> New -> Project -> Android Application Project (or) Android Project. Fill the forms and click "Finish" button. If you have any doubt regarding create a new project Click Here. 

Step 2 : Open res -> layout -> activity_main.xml (or) main.xml and add following code : 

 <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" >   
   <Button   
     android:id="@+id/button1"   
     android:layout_width="wrap_content"   
     android:layout_height="wrap_content"   
     android:layout_alignParentTop="true"   
     android:layout_centerHorizontal="true"   
     android:layout_marginTop="60dp"   
     android:text="Download File" />   
 </RelativeLayout>   

Step : Open src -> package -> MainActivity.java and add following code : 

 package com.gudivada.hemanthsomaraju;   
 import android.os.Bundle;   
 import android.os.Handler;   
 import android.os.Message;   
 import android.app.Activity;   
 import android.app.Dialog;   
 import android.app.ProgressDialog;   
 import android.content.DialogInterface;   
 import android.view.View;   
 import android.view.View.OnClickListener;   
 import android.widget.Button;   
 import android.widget.Toast;   
 public class MainActivity extends Activity {   
   Button btnStartProgress;   
   private ProgressDialog _progressDialog;   
   private int _progress = 0;   
   private Handler _progressHandler;   
 @Override   
 public void onCreate(Bundle savedInstanceState) {   
 super.onCreate(savedInstanceState);   
 setContentView(R.layout.activity_main);   
 btnStartProgress = (Button) findViewById(R.id.button1);   
 btnStartProgress.setOnClickListener(new OnClickListener()   
 {   
   @Override   
   public void onClick(View v)   
   {   
 showDialog(1);   
     _progress = 0;   
     _progressDialog.setProgress(0);   
     _progressHandler.sendEmptyMessage(0);   
   }   
 });   
 _progressHandler = new Handler()   
 {   
 public void handleMessage(Message msg)   
 {   
 super.handleMessage(msg);   
 if (_progress >= 100)   
 {   
   _progressDialog.dismiss();   
 } else   
 {   
   _progress++;   
   _progressDialog.incrementProgressBy(1);   
   _progressHandler.sendEmptyMessageDelayed(0, 100);   
 }   
 }   
      };   
 }   
 @Override   
 protected Dialog onCreateDialog(int i)   
    {   
       _progressDialog = new ProgressDialog(this);   
       _progressDialog.setIcon(R.drawable.ic_launcher);   
       _progressDialog.setTitle("Downloading files...");   
       _progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);   
       _progressDialog.setButton(DialogInterface.BUTTON_POSITIVE, "Hide", new   
         DialogInterface.OnClickListener()   
       {   
         public void onClick(DialogInterface dialog,   
           int whichButton)   
         {   
           Toast.makeText(getBaseContext(),   
               "Hide clicked!", Toast.LENGTH_SHORT).show();   
         }   
       });   
       _progressDialog.setButton(DialogInterface.BUTTON_NEGATIVE, "Cancel", new   
         DialogInterface.OnClickListener()   
       {   
         public void onClick(DialogInterface dialog,   
            int whichButton)   
         {   
           Toast.makeText(getBaseContext(),   
               "Cancel clicked!", Toast.LENGTH_SHORT).show();   
         }   
       });   
       return _progressDialog;   
 }   
 }   

Step : Open AndroidManifest.xml and add following code : 

 <?xml version="1.0" encoding="utf-8"?>   
 <manifest xmlns:android="http://schemas.android.com/apk/res/android"   
   package="com.gudivada.hemanthsomaraju"   
   android:versionCode="1"   
   android:versionName="1.0" >   
   <uses-sdk   
     android:minSdkVersion="8"   
     android:targetSdkVersion="19" />   
   <application   
     android:allowBackup="true"   
     android:icon="@drawable/ic_launcher"   
     android:label="@string/app_name"   
     android:theme="@style/AppTheme" >   
     <activity   
       android:name="com.gudivada.hemanthsomaraju.MainActivity"   
       android:label="@string/app_name" >   
       <intent-filter>   
         <action android:name="android.intent.action.MAIN" />   
         <category android:name="android.intent.category.LAUNCHER" />   
       </intent-filter>   
     </activity>   
   </application>   
 </manifest>   

Step : Our output will be like this : 

 

 




2.Different Styles of ProgressBar in Android. 

Step : Select File -> New -> Project -> Android Application Project (or) Android Project. Fill the forms and click "Finish" button. If you have any doubt regarding create a new project Click Here. 

Step : Open res -> layout -> activity_main.xml (or) main.xml and add following code : 

 <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" >   
   <TextView   
     android:id="@+id/textView1"   
     android:textSize="20dp"   
     android:textColor="#FF0000"   
     android:layout_width="wrap_content"   
     android:layout_height="wrap_content"   
     android:layout_alignParentTop="true"   
     android:layout_centerHorizontal="true"   
     android:layout_marginTop="60dp"   
     android:text="ProgressBar Different Styles" />   
   <ProgressBar   
     android:id="@+id/progressBar1"   
     style="?android:attr/progressBarStyleLarge"   
     android:layout_width="wrap_content"   
     android:layout_height="wrap_content"   
     android:layout_below="@+id/textView1"   
     android:layout_centerHorizontal="true"   
     android:layout_marginTop="40dp" />   
   <ProgressBar   
     android:id="@+id/progressBar2"   
     android:layout_width="wrap_content"   
     android:layout_height="wrap_content"   
     android:layout_below="@+id/progressBar1"   
     android:layout_centerHorizontal="true"   
     android:layout_marginTop="30dp" />   
   <ProgressBar   
     android:id="@+id/progressBar3"   
     style="?android:attr/progressBarStyleSmall"   
     android:layout_width="wrap_content"   
     android:layout_height="wrap_content"   
     android:layout_below="@+id/progressBar2"   
     android:layout_centerHorizontal="true"   
     android:layout_marginTop="30dp" />   
   <ProgressBar   
     android:id="@+id/progressBar4"   
     style="?android:attr/progressBarStyleHorizontal"   
     android:layout_width="wrap_content"   
     android:layout_height="wrap_content"   
     android:layout_below="@+id/progressBar3"   
     android:layout_centerHorizontal="true"   
     android:layout_marginTop="30dp" />   
 </RelativeLayout>   

Step : Open src -> package -> MainActivity.java and add following code : 

 package com.gudivada.hemanthsomaraju;   
 import android.os.Bundle;   
 import android.app.Activity;   
 import android.view.Menu;   
 public class MainActivity extends Activity {   
   @Override   
   public void onCreate(Bundle savedInstanceState) {   
     super.onCreate(savedInstanceState);   
     setContentView(R.layout.activity_main);   
   }   
   @Override   
   public boolean onCreateOptionsMenu(Menu menu) {   
     getMenuInflater().inflate(R.menu.main, menu);   
     return true;   
   }   
 }   

Step : Open AndroidManifest.xml and add following code :

 <?xml version="1.0" encoding="utf-8"?>   
 <manifest xmlns:android="http://schemas.android.com/apk/res/android"   
   package="com.gudivada.hemanthsomaraju"   
   android:versionCode="1"   
   android:versionName="1.0" >   
   <uses-sdk   
     android:minSdkVersion="8"   
     android:targetSdkVersion="19" />   
   <application   
     android:allowBackup="true"   
     android:icon="@drawable/ic_launcher"   
     android:label="@string/app_name"   
     android:theme="@style/AppTheme" >   
     <activity   
       android:name="com.gudivada.hemanthsomaraju.MainActivity"   
       android:label="@string/app_name" >   
       <intent-filter>   
         <action android:name="android.intent.action.MAIN" />   
         <category android:name="android.intent.category.LAUNCHER" />   
       </intent-filter>   
     </activity>   
   </application>   
 </manifest>   

Step : Our output will be like this :