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...
3.Now,Lets work with MainActivity.java file....refer the following code...
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..
1.Open Android Studio
2.Drag a Button and Provide appropriate name and id for the Button
Xml file for Button Design...
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
<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> |
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.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... | |
} | |
}); | |
} | |
} |
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
Post a Comment