I try to open an AlertDialog with a Image button on 1 of 3 Fragments I have in my MainActivity, but im Always having 1 of 2 Problems.
I tried several different approaches but all of them had a Problem like this.
My Goal is to have the ImageButton on my timetableFragment.java open a Dialog. Currently I have created an extra class for the AlertDialog.
Im a beginner please try to keep it simple.
My main acticity:
public class MainActivity extends AppCompatActivity {
private FrameLayout bar_todo;
private FrameLayout bar_timetable;
private FrameLayout bar_notes;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
todoFragment todoFragment = new todoFragment();
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container, todoFragment).commit();
bar_todo = findViewById(R.id.image_todo);
bar_timetable = findViewById(R.id.image_timetable);
bar_notes = findViewById(R.id.image_notes);
bar_todo.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
todoFragment todoFragment = new todoFragment();
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container, todoFragment).commit();
}
});
bar_timetable.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
timetableFragment timetableFragment = new timetableFragment();
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container, timetableFragment).commit();
}
});
bar_notes.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
notesFragment notesFragment = new notesFragment();
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container, notesFragment).commit();
}
});
}
public static void openDialog() {
SchulfachDialog schulfachDialog = new SchulfachDialog();
schulfachDialog.show(getSupportFragmentManager(), "ecameo");
}
}
AlertDialog Class:
public class SchulfachDialog extends AppCompatDialogFragment {
private EditText editTextName;
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
LayoutInflater inflater = getActivity().getLayoutInflater();
View view = inflater.inflate(R.layout.popup_schulfach, null);
builder.setView(view)
.setTitle("Add new subject")
.setMessage("Message")
.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
})
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
});
return builder.create();
}
}
timetableFragment (Fragment the button is in)
public class timetableFragment extends Fragment {
private static final String TAG = "todoFragment";
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
getActivity().setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
setRetainInstance(true);
View rootview = inflater.inflate(R.layout.timetable_fragment, container, false);
ImageButton btn_fach = getView().findViewById(R.id.btnSchulfach);
btn_fach.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
MainActivity.openDialog();
}
});
return rootview;
}
}
Hey the SchulfachDialog you have created is a DialogFragment, and a DialogFragment can be opened by a fragment by itself, so you can directly open the dialog from your timetableFragment using childFragmentManager in the onClick like,
btn_fach.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
SchulfachDialog schulfachDialog = new SchulfachDialog();
schulfachDialog.show(getChildFragmentManager(), "ecameo");
}
});
Also,
View rootview = inflater.inflate(R.layout.timetable_fragment, container, false);
ImageButton btn_fach = getView().findViewById(R.id.btnSchulfach);
Here instead of "getView()" use "rootview" otherwise it will give NullPointerException
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.