Friday, April 15, 2016

Dialog Fragment Part-I

Hi ... Dear All ..Today I am uploading a superb example of Dialog Displaying with two buttons.(Positive & Negative)
Please check.
I am using Android Studio 1.5.1
Minimun SDK API 19
Target SDK API 23
Please Like us & put your valuable suggestions in comment box.

A. Output will be like   




B. Layout file **** Please add one button with onClick attribute in your layout file
Like 

<Button    android:layout_width="wrap_content"

    android:layout_height="wrap_content"

    android:text="Show Dialog"

    android:id="@+id/button"

    android:layout_alignParentStart="true"

    android:layout_marginTop="58dp"

    android:onClick="showDial"/>

C. Java Code 

a. MyDialog.java file as 

package com.exam.ravi.dialogex1;

import android.app.AlertDialog;
import android.app.Dialog;
import android.app.DialogFragment;
import android.content.DialogInterface;
import android.os.Bundle;
import android.widget.Toast;

public class MyDialog extends DialogFragment {
    @Override

    public Dialog onCreateDialog(Bundle savedInstanceState) {
        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
            builder.setMessage("This is my Dialog");
            builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
            @Override

            public void onClick(DialogInterface dialog, int which) {
         Toast.makeText(getActivity(),"Positve Clicked",Toast.LENGTH_SHORT).show();
            }
        });
        builder.setNegativeButton("CANCEL", new DialogInterface.OnClickListener() {
            @Override

            public void onClick(DialogInterface dialog, int which) {
         Toast.makeText(getActivity(),"Negative Clicked",Toast.LENGTH_SHORT).show();
            }
        });
        builder.setTitle("Dialog Demo");
        AlertDialog dialog = builder.create();
        return dialog;
    }
}

b. MainActivity.java is as 

package com.exam.ravi.dialogex1;

import android.os.Bundle;

import android.support.v4.app.FragmentActivity;
import android.view.View;

public class MainActivity extends FragmentActivity {

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


    }
    public void showDial(View v)
    {   MyDialog md=new MyDialog();
       md.show(getFragmentManager(), "My_dail_tag");

    }


}

No comments:

Post a Comment