How to Put Press Back Again to Exit Message in Android Studioi
Alarm Dialog Tutorial With Case In Android Studio
Alert Dialog in an android UI prompts a small window to make conclusion on mobile screen. Sometimes earlier making a decision it is required to give an warning to the user without moving to next activity. To solve this problem warning dialog came into practise. For instance you accept seen this type of alert when you try to exit the App and App ask you lot to confirm exiting.
AlertDialog.Builder Components Used In Alert Dialog
AlertDialog.Builder is used to create an interface for Alert Dialog in Android for setting like alert title, message, image, push button, push button onclick functionality etc.
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(this);
Below are the components of Alert Dialog:
one. setTitle(CharSequence title) – This component is used to set the title of the alert dialog. It is optional component.
// Setting Alert Dialog Title alertDialogBuilder.setTitle("Confirm Exit..!!!");
2. setIcon(Drawable icon) – This component add together icon before the championship. You will need to salve prototype in drawable icon.
// Icon Of Alert Dialog alertDialogBuilder.setIcon(R.drawable.question);
3. setMessage(CharSequence message) – This component displays the required bulletin in the alert dialog.
// Setting Alarm Dialog Bulletin alertDialogBuilder.setMessage("Are you certain,You want to exit");
iv. setCancelable(boolean cancelable) – This component has boolean value i.eastward true/false. If fix to false information technology allows to cancel the dialog box by clicking on expanse outside the dialog else it allows.
alertDialogBuilder.setCancelable(imitation);
5. setPositiveButton(CharSequence text, DialogInterface.OnClickListener listener) – This component add positive button and further with this user ostend he wants the alert dialog question to happen.
alertDialogBuilder.setPositiveButton("yep", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface arg0, int arg1) { finish(); } });
6. setNegativeButton(CharSequence text, DialogInterface.OnClickListener listener) – This component add negative button and further with this user confirm he doesn't want the alert dialog question to happen.
alertDialogBuilder.setNegativeButton("No", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { Toast.makeText(MainActivity.this,"You clicked over No",Toast.LENGTH_SHORT).evidence(); } });
7. setNeutralButton(CharSequence text, DialogInterface.OnClickListener listener) – This component simply add a new button and on this button developer can set any other onclick functionality like cancel push button on alert dialog.
alertDialogBuilder.setNeutralButton("Cancel", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { Toast.makeText(getApplicationContext(),"You clicked on Abolish",Toast.LENGTH_SHORT).show(); } });
Alarm Dialog Example In Android Studio
Beneath is the example of Alarm Dialog in which the functionality of Alert Dialog is defined over button click. In this example we take used a simple button and on that button click the alert dialog window will appear.
Below you lot tin can download lawmaking, see final output and pace by footstep caption of Warning Dialog example in Android Studio.
Download Code
Pace i: Create a new projection and name it AlertDialogExample.
Step two: Open res -> layout -> activity_main.xml (or) main.xml and add following lawmaking:
Here nosotros add together the push UI on click of which alarm dialog will announced. We likewise used textview for request user to click on push.
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/activity_main" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context="com.case.alertdialogexample.MainActivity"> <Button android:text="@cord/exit" android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/push button" android:onClick="exit" android:textStyle="normal|assuming" style="@style/Widget.AppCompat.Button.Colored" android:layout_alignParentBottom="true" android:layout_centerHorizontal="true" android:layout_marginBottom="131dp"/> <TextView android:text="@cord/click_over_button_to_exit" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginBottom="68dp" android:id="@+id/textView2" android:layout_above="@+id/button" android:layout_centerHorizontal="true" android:textSize="18sp" android:textStyle="normal|assuming" android:gravity="center" /> </RelativeLayout>
Step 3 : Now open app -> java -> bundle -> MainActivity.java and add the below lawmaking.
In this stride firstly AlertDialog.Builder is used to create a interface like setting alert championship, message, image, button, push onclick functionality etc.
Of import Note: Add together a epitome to the drawable folder before setting the alert dialog icon.
package com.example.alertdialogexample; import android.content.DialogInterface; import android.support.v7.app.AlertDialog; import android.back up.v7.app.AppCompatActivity; import android.os.Packet; import android.view.View; import android.widget.Toast; public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Packet savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } public void exit(View view){ AlertDialog.Builder alertDialogBuilder = new AlertDialog.Architect(this); // Setting Alert Dialog Title alertDialogBuilder.setTitle("Confirm Exit..!!!"); // Icon Of Alarm Dialog alertDialogBuilder.setIcon(R.drawable.question); // Setting Alert Dialog Message alertDialogBuilder.setMessage("Are you lot sure,You desire to get out"); alertDialogBuilder.setCancelable(faux); alertDialogBuilder.setPositiveButton("Yes", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface arg0, int arg1) { finish(); } }); alertDialogBuilder.setNegativeButton("No", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { Toast.makeText(MainActivity.this,"You lot clicked over No",Toast.LENGTH_SHORT).show(); } }); alertDialogBuilder.setNeutralButton("Cancel", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { Toast.makeText(getApplicationContext(),"Yous clicked on Cancel",Toast.LENGTH_SHORT).show(); } }); AlertDialog alertDialog = alertDialogBuilder.create(); alertDialog.show(); } }
Output:
Now run the App and click on the button. The alert dialog will appear asking user to confirm if he wants to go out App. If user click on 'No', he remains in the App and if he click on 'Yes' the user will get exit from the App.
Custom Alarm Dialog:
The custom dialog uses DIALOG to create custom alert in android studio. Dialog display a small window i.e a popup which draws the user attention over the activeness earlier they go on moving forrard. For more than details read custom dialog tutorial
Source: https://abhiandroid.com/ui/alertdialog
Postar um comentário for "How to Put Press Back Again to Exit Message in Android Studioi"