Implement Progress Bar Using Android Studio

Steps To Design Progress Bar Using Android Studio

1.Open Android Studio

2.Drag a Button and Provide appropriate name and id for the Button
Xml file for Button Design...

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/linearLayout">
<Button
android:id="@+id/btnstart"
android:text="Start"
android:layout_width="200dp"
android:layout_height="70dp"
android:layout_below="@+id/linearLayout"
android:layout_centerHorizontal="true"
android:layout_marginTop="73dp"
android:textSize="30dp"
android:textColor="#6505b9" />
</LinearLayout>
view raw gistfile1.txt hosted with ❤ by GitHub
3.Now,Lets work with MainActivity.java file....refer the following code...
package com.example.siddhi.progressbar;
import android.app.ProgressDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ProgressBar;
import static java.lang.Thread.sleep;
public class MainActivity extends AppCompatActivity {
//create 3 private variables
private ProgressDialog Pbar; //ProgressDailog variable
private int Percentage = 0; //initialize percentage to 0
Button Bstart; //Button variablr
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Bstart = (Button) findViewById(R.id.btnstart); //locate the button
Bstart.setOnClickListener(new View.OnClickListener() //provide Onclick listener
{
@Override
public void onClick(View v)
{
Pbar = new ProgressDialog(v.getContext()); //initialize the progress dailog
Pbar.setMessage("Progressing Please Wait..."); // set a message for processing
Pbar.setProgressStyle(Pbar.STYLE_HORIZONTAL); //Progress Dailog style
Pbar.setProgress(0);
Pbar.setMax(0);
Pbar.show(); //Enables Progress Dailog
new Thread(new Runnable() //create a thread for running
{
@Override
public void run() {
while(Percentage < 100)
{
try {
sleep(100); //make the thread sleep for 100 ms
Percentage++; //eventally percentage will increment
Pbar.setProgress(Percentage);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
//close dailog
Pbar.dismiss();
}
}).start(); // start of the thread...
}
});
}
}
view raw gistfile1.txt hosted with ❤ by GitHub


4.That's It....You Are Done!!.....





If You are New or Don't Know How To Run App On Emulator Go to My Following Link...

Run App On Emulator..


Thank You...!!!

Comments

Popular posts from this blog

Design Analog and Digital Clock Using Android Studio

Design a Simple Calculator Application Using Android Studio....(Part 1-Design Calcualator Layout)

Create Simple "Hello World" Android Application Using Android Studio