Search code examples
androidandroid-fragmentstabsandroid-alertdialog

Android using AlertDialog with custom layout in fragment


Good evening out there,

i am trying to use an alert dialog in an Fragment (Cause of the TabNavigation). It is nessesary that i use the layout "privacy".

But eclipse gave me an error at the "AlertDialog.Builder": (The constructor AlertDialog.Builder(AboutActivity2) is undefined)

and at the ".from" after the inflate: (The method from(Context) in the type LayoutInflater is not applicable for the arguments (AboutActivity2))

Thanks for help, greetings

    View rootView = inflater.inflate(R.layout.activity_about2, container, false);  
    rootView.findViewById(R.id.privacybutton).setOnClickListener(this);
    return rootView;
}

final OnClickListener mGlobal_OnClickListener = new OnClickListener() {
    public void onClick(View v) {
        switch(v.getId()) {
        case R.id.privacybutton:
            AlertDialog.Builder alertDialog = new AlertDialog.Builder(getActivity());
            LayoutInflater factory = LayoutInflater.from(getActivity());
            final View view = factory.inflate(R.layout.privacy, null);
            alertDialog.setView(view);
            alertDialog.setNegativeButton("Schließen", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog,int which) {
                dialog.dismiss();
                        }
                    });
            alertDialog.show();
            break;
    }
}

Solution

  • AlertDialog.Builder receives a context as parameter. And not a fragment.

    http://developer.android.com/reference/android/app/AlertDialog.Builder.html#AlertDialog.Builder(android.content.Context)

    Use getActivity() instead :

        AlertDialog.Builder alertDialog3 = new AlertDialog.Builder(getActivity());
        LayoutInflater factory3 = LayoutInflater.from(getActivity());
    

    You also need to add the listener to your button. You can do it like this:

    View rootView = inflater.inflate(R.layout.activity_about2, container, false);  
    rootView.findViewByID(R.id.privacybutton).setOnClickListener(this);
    return rootView;
    

    FINAL CODE

        View rootView = inflater.inflate(R.layout.activity_about2, container, false);
        rootView.findViewById(R.id.privacybutton).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                AlertDialog.Builder alertDialog = new AlertDialog.Builder(v.getContext());
                LayoutInflater factory = LayoutInflater.from(v.getContext());
                final View view = factory.inflate(R.layout.privacy, null);
                alertDialog.setView(view);
                alertDialog.setNegativeButton("Schließen", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                    }
                });
                alertDialog.show();
            }
        });
        return rootView;