So I created a DialogFragment .java file to display my Alert Dialog. I used the tutorial here to create an AlertDialog with a list.
How can I reference this fragment in my ContactList.java file? What I understood to be the way to do this from the tutorial didn’t seem to work. When I’m trying to do the onClick event using the code below in my ContactList.java file, I get a red squiggly under getSupportFragmentManager().
Here’s the code I have for the fragment:
public class StatusDialogFragment extends DialogFragment {
public Dialog onCreateDialog(Bundle savedInstanceState) {
// Use the Builder class for convenient dialog construction
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setTitle(R.string.choose_status);
builder.setItems(R.array.status_choices,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
dismiss();
}
});
return builder.create();
}
}
Here’s my relevant ContactList.java file code:
public class ContactList extends Activity implements OnClickListener {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_contact_list);
Intent intent = getIntent();
}
public void chooseStatus(View view) {
DialogFragment newFragment = new StatusDialogFragment();
newFragment.show(getSupportFragmentManager(), "statuschoice");
}
Lastly, here’s the XML code for my button that I want to open the dialog.
<ImageButton
android:id="@+id/status"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/status_available_icon"
android:onClick="chooseStatus" />
Thanks for your time.
You need to extend the compatibility class
FragmentActivityif you’re coding for platforms below 3.0:If not, change
getSupportFragmentManager()togetFragmentManager()Also make sure that if you’re using
FragmentActivity, that yourStatusDialogFragmentextends the compatibilityDialogFragmentfrom the support package.And if not (meaning you’re only targeting 3.0+), make sure that the
DialogFragmentclass you extend from is not from the support package.