How to use Context and AlertDialog with fragments

1.5k views Asked by At

so i was trying to implement an alart dialog with a EditText in a fragment heres the code: 1.prompt_dialog.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:orientation="vertical"
    android:layout_height="match_parent">
    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Type your reminder title : "
        android:textAppearance="?android:attr/textAppearanceLarge" />

    <EditText
        android:id="@+id/editTextDialogUserInput"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />
</LinearLayout>

2.MainFragment.java

public class ReminderFragment extends Fragment implements View.OnClickListener {

        final Context context = this;

        public ReminderFragment() {
                    // Required empty public constructor
            }

            @Override
            public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

                View layout = inflater.inflate(R.layout.main_fragment_layout,container,false);

                Button openDialog = (Button) layout.findViewById(R.id.openDialogButton);
                openDialog .setOnClickListener(this);


                return layout;
            }
        public void onClick(View view){

                LayoutInflater li = LayoutInflater.from(context);
                View promptsView = li.inflate(R.layout.prompt_dialog, null);

                AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
                    context);

                // set prompts.xml to alertdialog builder
                alertDialogBuilder.setView(promptsView);
                // set dialog message
                alertDialogBuilder
                        .setCancelable(false)
                        .setPositiveButton("Ok",
                            new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog,int id) {

                            }
                        })
                        .setNegativeButton("Cancel",
                            new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog,int id) {
                                dialog.cancel();
                            }
                        });
                AlertDialog alertDialog = alertDialogBuilder.create();
                alertDialog.show();
            }
    }

but on the Context variable final Context context = this; there an error saying

Incompatible Types:
        Required: android.content.Context
        Found: com.example.user.packgename.MainFragment

i tried using the context on another class that extends Activity class and used this code final Context context = this; it wokrs just fine, with my case maybe its because its extending from the Fragment class.

so how can i solve the problem?is there any other way to do this?

Thanks.

1

There are 1 answers

2
Kamran Ahmed On BEST ANSWER

Replace all uses of context and use getActivity() instead of it.

All basic four components of an Android application extend Context which comprise of - Activity, Service, BroadcastReceiver and ContentProvider. The application itself too extend Context.

To perform operations related to UI, you need to use Context of an Activity like displaying an AlertDialog.

final Context context = this;

This line is valid as long as it is written within the scope of any of the components listed above. It won't work in a Fragment because it does not extend Context of its own, it uses parent Activity's context.