Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • How to make AlertDialog in android ?

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 281
    Comment on it

    Here I have created AlertDialog function in android. We can use alertdialog for choice to continue or discontinue. AlertDialog can be used to display the dialog message with OK and Cancel buttons. Android AlertDialog is the subclass of Dialog class. In the below code example, I have described how to make AlertDialog in android.

    Step(1)-activity_main.xml

    <RelativeLayout xmlns:androclass="http://schemas.android.com/apk/res/android"  
        xmlns:tools="http://schemas.android.com/tools"  
        android:layout_width="match_parent"  
        android:layout_height="match_parent"  
        tools:context=".MainActivity" >  
    
        <TextView  
            android:layout_width="wrap_content"  
            android:layout_height="wrap_content"  
            android:layout_centerHorizontal="true"  
            android:layout_centerVertical="true"  
            android:text="Welcome_evon" />  
    
    </RelativeLayout>  
    

    Step(2)-strings.xml

    <?xml version="1.0" encoding="utf-8"?>  
    <resources>  
    
        <string name="app_name">alertdialog</string>  
        <string name="Welcome_evon">Hello world!</string>  
        <string name="menu_settings">Settings</string>  
        <string name="dialog_message">Welcome to Evon</string>  
    
       <string name="dialog_title">AlertDialog</string>  
    </resources> 
    

    Step(3)- MainActivity.java

     public class MainActivity extends Activity {  
    
        @Override  
        protected void onCreate(Bundle savedInstanceState) {  
            super.onCreate(savedInstanceState);  
    
            AlertDialog.Builder builder = new AlertDialog.Builder(this);  
            //Uncomment the below code to Set the message and title from the strings.xml file  
            //builder.setMessage(R.string.dialog_message) .setTitle(R.string.dialog_title);  
    
            //Setting message manually and performing action on button click  
            builder.setMessage("Do you want to close this app?")  
                .setCancelable(false)  
                .setPositiveButton("Yes", new DialogInterface.OnClickListener() {  
                    public void onClick(DialogInterface dialog, int id) {  
                    finish();  
                    }  
                })  
                .setNegativeButton("No", new DialogInterface.OnClickListener() {  
                    public void onClick(DialogInterface dialog, int id) {  
                    //  Action for 'NO' Button  
                    dialog.cancel();  
                 }  
                });  
            AlertDialog alert = builder.create();  
            alert.setTitle("Are u sure");  
            alert.show();  
            setContentView(R.layout.activity_main);  
        }  
    
        @Override  
        public boolean onCreateOptionsMenu(Menu menu) {  
            getMenuInflater().inflate(R.menu.activity_main, menu);  
            return true;  
        }  
    
    }
    

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: