Program on Alert Dialog and Progress Dialog in Android Studio.
activity_main.xml
activity_main.xml
xml version="1.0" encoding="utf-8"?><LinearLayout
android:id="@+id/activity_main" android:layout_width="match_parent" android:orientation="vertical" android:layout_height="match_parent" tools:context=".MainActivity">
<Button android:text="Normal Dialog Box" android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/button" android:onClick="onClick"/>
<Button android:text="Progressive Dialog Box" android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/button2" android:onClick="onClick2"/>
<Button android:text="Latest Dialog Box" android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/button3" android:onClick="onClick3"/>
</LinearLayout>
MainActivity.java
public class MainActivity extends AppCompatActivity { CharSequence[] items={"Google","Yahoo","Microsoft"}; boolean[] itemsChecked=new boolean[items.length]; ProgressDialog dialog; AlertDialog dilog; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } public void onClick(final View view){ AlertDialog.Builder alert=new AlertDialog.Builder(this); alert.setTitle("Normal Alert Dialog Box"); //SET MULTI CHOICE ITEMS DOESN'T WORK IF WE USE "SET MESSAGE". //alert.setMessage("This is a normal Dialog box,select your options accordingly"); alert.setIcon(R.mipmap.ic_launcher); alert.setPositiveButton("OK", new DialogInterface.OnClickListener() {//sets positive button like OK @Override public void onClick(DialogInterface dialogInterface, int i) { Toast.makeText(getApplicationContext(),"OK Clicked",Toast.LENGTH_LONG).show(); } }); alert.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {//sets negative button like Cancel @Override public void onClick(DialogInterface dialogInterface, int i) { Toast.makeText(getApplicationContext(),"Cancelling",Toast.LENGTH_LONG).show(); dilog.dismiss(); } }); alert.setMultiChoiceItems(items, itemsChecked, new DialogInterface.OnMultiChoiceClickListener() { @Override public void onClick(DialogInterface dialogInterface, int which, boolean isChecked) { Toast.makeText(getApplicationContext(),items[which] + (isChecked ? " Checked!":" UnChecked!"),Toast.LENGTH_SHORT).show(); } }); dilog=alert.create(); dilog.show(); } public void onClick2(final View v){ dialog=new ProgressDialog(MainActivity.this);//creating an instance of progress dialog dialog.show();//Displaying progress layout dialog.setTitle("Loading");//setting title dialog.setMessage("Please Wait");//setting message dialog.setCancelable(true);//settting cancel option new Thread(new Runnable() { @Override public void run() { try{ Thread.sleep(5000); dialog.dismiss();//dialog dismisses after 5 seconds }catch (InterruptedException e){ e.printStackTrace(); } } }).start(); } public void onClick3(View v){ final ProgressDialog dialog1=new ProgressDialog(MainActivity.this); dialog1.setIcon(R.mipmap.ic_launcher); dialog1.setTitle("Downloading"); dialog1.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);//shows progress bar in horizontal view with % left to download dialog1.setButton(DialogInterface.BUTTON_POSITIVE, "OK", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialogInterface, int i) { } }); dialog1.setButton(DialogInterface.BUTTON_NEGATIVE, "CANCEL", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialogInterface, int i) { dialog1.dismiss();//on selecting cancel,aborts downloading } }); new Thread(new Runnable() { @Override public void run() { for(int i=1;i<=15;i++){ try{ Thread.sleep(1000); dialog1.incrementProgressBy((int)100/15);//updates progress for every one second }catch(InterruptedException e){ e.printStackTrace(); } } dialog1.dismiss(); } }).start(); dialog1.show(); } }
View in emulator:
Comments
Post a Comment