Anandhu
Anandhu

Reputation: 312

Add AlertDialogue Box in fragments?

I am trying to add the Alert Dialogue box in android fragments.. It's like when the fragment is loading it should check for the value of a string and according to that it need to display one dialogue box.. But it's coming in to the if condition, but alert box is not working..

this is my code

    public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.fragment_planet, container, false);



    if(heart.equals("No connection"))
    {
        Toast.makeText(getActivity(), "in alert", Toast.LENGTH_SHORT).show();
        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity().getApplicationContext());
        builder.setCancelable(true);
        builder.setTitle("Connection Problem");
        builder.setMessage("No device detected Do you want to restart the application?");
        builder.setPositiveButton("OK", new DialogInterface.OnClickListener()
        {
            @Override
            public void onClick(DialogInterface arg0, int arg1) 
            {

                Intent restrat=new Intent(getActivity(),LandingPage.class);
                startActivity(restrat);


            }
        });

        builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener()
        {
            @Override
            public void onClick(DialogInterface arg0, int arg1) 
            {




            }
        });

    }
}

How to solve this problem?

Upvotes: 1

Views: 238

Answers (1)

SathishKumar
SathishKumar

Reputation: 1644

Try this,

@Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
            super.onCreateView(inflater, container, savedInstanceState);
        View rootView = inflater.inflate(R.layout.fragment_planet, container,
                false);

        if (heart.equals("No connection")) {
            Toast.makeText(getActivity(), "in alert", Toast.LENGTH_SHORT)
                    .show();
            AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()
                    .getApplicationContext());
            builder.setCancelable(true);
            builder.setTitle("Connection Problem");
            builder.setMessage("No device detected Do you want to restart the application?");
            builder.setPositiveButton("OK",
                    new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface arg0, int arg1) {

                            Intent restrat = new Intent(getActivity(),
                                    LandingPage.class);
                            startActivity(restrat);

                        }
                    });

            builder.setNegativeButton("Cancel",
                    new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface arg0, int arg1) {

                        }
                    });
            AlertDialog dialog=builder.create();
            dialog.show();
        }
        return rootView;
    }

Upvotes: 1

Related Questions