Android App Development With AndroidCource: alert dialog
Showing posts with label alert dialog. Show all posts
Showing posts with label alert dialog. Show all posts

Friday, 15 September 2017

Android Alert Dialog

Alert Dialog


AlertDialog can be used to display the dialog message with OK and Cancel buttons.

AlertDialog is composed of three regions: title, content area and action buttons.

A dialog is a small window that prompts the user to make a decision or enter additional information.

AlertDialog is the subclass of Dialog class.

AlertDialog need to make an object of AlertDialogBuilder which an inner class of AlertDialog.

Now you have to set the positive (yes) or negative (no) button using the object of the AlertDialogBuilder class.

Methods of AlertDialog


setMessage()

Sets the message to be displayed in the alert dialog

setIcon()

sets the icon in the alert dialog

setCancelable()

Sets the property that the dialog can be cancelled or not

setTitle()

Set the title to be appear in the dialog

show()

Show Display Dialog

Monday, 21 August 2017

Alert Dialog Example in Android

AlertDialogActivity.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              xmlns:tools="http://schemas.android.com/tools"
              android:id="@+id/activity_dialog"
              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"
              android:orientation="vertical"
              android:background="#79c9c2"
              tools:context="com.bhaumik.programmingstudy.AlertDialogActivity">

    <Button
            android:id="@+id/btn_simpleDialog"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Show Alert Dialog"
            android:textAllCaps="false"
            android:textSize="18dp"
            android:layout_gravity="center_horizontal"/>
</LinearLayout>
AlertDialogActivity.java
package com.bhaumik.programmingstudy;

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 DialogActivity extends AppCompatActivity {

    Button btn;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_dialog);

        btn = (Button) findViewById(R.id.btn_simpleDialog);

        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                AlertDialog.Builder dialog = new AlertDialog.Builder(DialogActivity.this);
                dialog.setTitle("Message Dialog");
                dialog.setMessage("You h'v New Message");
                dialog.setPositiveButton("View", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        ToastMsg.ShowToast(DialogActivity.this,"Thank You");
                    }
                });
                dialog.setNegativeButton("Skip", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        ToastMsg.ShowToast(DialogActivity.this,"Skip Anyway");
                    }
                });
                dialog.show();
            }
        });
    }
}

Ads