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

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