Twitter Facebook Delicious Digg Stumbleupon Favorites More

Saturday 17 June 2017

Alert Dialog

STRING FILE

<resources>
    <string name="app_name">Blog_9_alertdialog</string>
    <string name="dialog_title">dialog_title</string>
    <string name="title">TITLE</string>
    <string name="msg">MSG</string>
    <string name="positive">YES</string>
    <string name="negative">NO</string>

</resources>


JAVA FILE

package com.example.viraj.blog_9_alertdialog;

import android.content.DialogInterface;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.Button;

public class MainActivity extends AppCompatActivity {
private Button b1;
    private AlertDialog.Builder di_b1;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        di_b1=new AlertDialog.Builder(MainActivity.this);

        //set title to dialog  // or me he ye   di_b1.setTitle("ALERT DIALOG EXAMPLE");
//di_b1.setTitle(R.string.dialog_title);       
 di_b1.setTitle(getResources().getString(R.string.dialog_title));

        //set message or me he ye   di_b1.setMessage("message work"));
        di_b1.setMessage(getResources().getString(R.string.msg));

        //set cancalable outside the dialogbox
        di_b1.setCancelable(false);

        //set positive yes
        di_b1.setPositiveButton(getResources().getString(R.string.positive), new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                //exit our application
                MainActivity.this.finish();
            }
        });
        //set negative
        di_b1.setNegativeButton(getResources().getString(R.string.negative), new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.cancel();
            }
        });
        //create  dialog di_b1

        AlertDialog alertDialog=di_b1.create();
        //show dialog
        alertDialog.show();
    }
}

XML FILE
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.viraj.blog_9_alertdialog.MainActivity">

    <Button
        android:id="@+id/id_button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="8dp"
        android:layout_marginLeft="8dp"
        android:layout_marginRight="8dp"
        android:layout_marginTop="8dp"
        android:text="Button"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.501" />
</android.support.constraint.ConstraintLayout>

/////////////=================================OR me he ye ok 

package com.example.viraj.alert_dialog;

import android.content.DialogInterface;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class MainActivity extends AppCompatActivity {
private Button showdialogbutton;
private AlertDialog.Builder dialog_12345_name;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        showdialogbutton=(Button) findViewById (R.id.id_button);
        showdialogbutton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                
                //show our alert dialog
                dialog_12345_name=new AlertDialog.Builder(MainActivity.this);

                //set tile
                dialog_12345_name.setTitle("ALERT DIALOG EXAMPLE");

                //SET MESSAGE
                dialog_12345_name.setMessage("MESSAGE IT WORK");

                //SET CANCALABLE outside the dialogbox
                dialog_12345_name.setCancelable(false);

                //dialog hase two button posative yes button and negative no button

                //positive yes button exit our application
                dialog_12345_name.setPositiveButton("YES", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        MainActivity.this.finish();
                    }
                });

//negative no button our application
                dialog_12345_name.setNegativeButton("NO", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface__112233, int i) {
                        dialogInterface__112233.cancel();
                    }
                });

                //create  dialog di_b1
                AlertDialog alertDialog=dialog_12345_name.create();

                //show dialog
                alertDialog.show();
            }
        });

    }
}

Share:

0 comments:

Post a Comment

Search This Blog

Popular Posts

Pages

how to make crores from 1 lakh in stock markets in 1 year

how to make crores from 1 lakh in stock markets in 1 year

Blogger Tutorials

Blogger Templates

Sample Text

Copyright © ANDROID TUTORIAL CODE | Powered by Blogger
Design by SimpleWpThemes | Blogger Theme by NewBloggerThemes.com & Distributed By Protemplateslab