Friday, April 15, 2016

Dialog Fragment Part-II

Hi ... Dear All ..Today I am uploading a superb example of Dialog Displaying options to select.
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. MyDialSingle.java 

package com.exam.ravi.dialogex1;

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

public class MyDialSingle extends DialogFragment {
    final CharSequence  items[]={"Easy","Medium","Hard"};
    String select;
    @Override

    public Dialog onCreateDialog(Bundle savedInstanceState) {
        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
        builder.setTitle("This is Single Selection");
        builder.setSingleChoiceItems(items,0,new OnClickListener()
        {
            @Override

            public void onClick(DialogInterface dialog, int args) {

                switch(args)
                {
                    case 0:  select= (String) items[args];
                        break;
                    case 1:  select= (String) items[args];
                        break;
                    case 2:  select= (String) items[args];
                        break;

                }

            }
            }).setPositiveButton("OK", new OnClickListener() {
            @Override

            public void onClick(DialogInterface dialog, int which) {
         Toast.makeText(getActivity(), "You Selected" + select
                                          Toast.LENGTH_SHORT).show();
            }
        });
        builder.setTitle("Select Demo");
        AlertDialog dialog = builder.create();

        return dialog;
    }
}

b. MainActivity.java

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)
    {   MyDialSingle md = new MyDialSingle();
        md.show(getFragmentManager(), "My_dail_tag");

    }


}

No comments:

Post a Comment