DynamicDialogFragment can now have 1 to 3 buttons - to be tested

This commit is contained in:
2017-12-12 23:05:37 +01:00
parent 5020c169d6
commit cb80cc45b1
6 changed files with 31 additions and 17 deletions

View File

@@ -90,9 +90,8 @@ public class MainActivity extends AppCompatActivity {
// Set the arguments
Bundle args = new Bundle();
args.putBoolean("neutral", false);
args.putString("button_positive", getString(R.string.new_task_save));
args.putString("button_negative", getString(R.string.new_task_cancel));
args.putInt("button_count", 1);
args.putString("button_negative", getString(R.string.task_list_ok));
taskListFragment.setArguments(args);
if (getResources().getBoolean(R.bool.large_layout))
@@ -143,7 +142,7 @@ public class MainActivity extends AppCompatActivity {
Bundle args = new Bundle();
args.putInt("list", currentTabPosition);
args.putBoolean("today", sharedPref.getBoolean("pref_conf_today_enable", false));
args.putBoolean("neutral", false);
args.putInt("button_count", 2);
args.putString("button_positive", getString(R.string.new_task_save));
args.putString("button_negative", getString(R.string.new_task_cancel));
args.putString("button_neutral", getString(R.string.new_task_delete));

View File

@@ -30,16 +30,17 @@ import com.wismna.geoffroy.donext.R;
*/
public abstract class DynamicDialogFragment extends DialogFragment {
boolean mHasNeutralButton = false;
int mButtonCount = 2;
int mContentLayoutId = 0;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
// This part is only needed on small layouts (large layouts use onCreateDialog)
if (!getShowsDialog()) {
View view = inflater.inflate(R.layout.fragment_dynamic_dialog, container, false);
AppCompatActivity activity = (AppCompatActivity) getActivity();
assert activity != null;
activity.setSupportActionBar(setToolbarTitle(view));
ActionBar actionBar = activity.getSupportActionBar();
@@ -66,13 +67,9 @@ public abstract class DynamicDialogFragment extends DialogFragment {
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
Bundle args = getArguments();
// Set the dialog buttons
assert args != null;
// Add action buttons
builder.setView(view)
// Add action buttons
.setPositiveButton(args.getString("button_positive"), new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
onPositiveButtonClick(view);
}
})
.setNegativeButton(args.getString("button_negative"), new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// Send the negative button event back to the host activity
@@ -81,7 +78,14 @@ public abstract class DynamicDialogFragment extends DialogFragment {
onNegativeButtonClick();
}
});
if (mHasNeutralButton) {
if (mButtonCount == 2) {
builder.setPositiveButton(args.getString("button_positive"), new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
onPositiveButtonClick(view);
}
});
}
if (mButtonCount == 3) {
builder.setNeutralButton(args.getString("button_neutral"), new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
@@ -105,13 +109,20 @@ public abstract class DynamicDialogFragment extends DialogFragment {
public void onPrepareOptionsMenu(Menu menu) {
Bundle args = getArguments();
// Show the neutral button if needed
if (!mHasNeutralButton) {
if (mButtonCount < 3) {
menu.removeItem(R.id.menu_neutral_button);
}
else {
menu.findItem(R.id.menu_neutral_button).setTitle(args.getString("button_neutral"));
}
menu.findItem(R.id.menu_positive_button).setTitle(args.getString("button_positive"));
// Show the positive button if needed
if (mButtonCount < 2) {
menu.removeItem(R.id.menu_positive_button);
}
else {
menu.findItem(R.id.menu_positive_button).setTitle(args.getString("button_positive"));
}
super.onPrepareOptionsMenu(menu);
}
@@ -124,6 +135,7 @@ public abstract class DynamicDialogFragment extends DialogFragment {
// Hide the keyboard if present
if (view != null) {
InputMethodManager imm = (InputMethodManager) getActivity().getSystemService(Activity.INPUT_METHOD_SERVICE);
assert imm != null;
imm.hideSoftInputFromWindow(getView().getWindowToken(), 0);
}
if (id == R.id.menu_positive_button) {

View File

@@ -58,7 +58,7 @@ public class TaskFormDialogFragment extends DynamicDialogFragment {
mContentLayoutId = R.layout.content_task_form;
Bundle args = getArguments();
if (args != null) {
mHasNeutralButton = args.getBoolean("neutral");
mButtonCount = args.getInt("button_count");
}
}
@@ -106,6 +106,7 @@ public class TaskFormDialogFragment extends DynamicDialogFragment {
// Auto set list value to current tab
Bundle args = getArguments();
assert args != null;
int id = args.getInt("list");
spinner.setSelection(id);

View File

@@ -48,7 +48,7 @@ public class TaskListsFragment extends DynamicDialogFragment implements
Bundle args = getArguments();
if (args != null) {
mHasNeutralButton = args.getBoolean("neutral");
mButtonCount = args.getInt("button_count");
}
mContentLayoutId = R.layout.fragment_tasklists;

View File

@@ -59,4 +59,5 @@
<string name="task_list_edit_list_hint">Nom de la liste</string>
<string name="incompatible_sdk_version">Sorry, your Android version is not supported.</string>
<string name="action_history">Historique</string>
<string name="task_list_ok">OK</string>
</resources>

View File

@@ -82,4 +82,5 @@
<!-- TODO: Remove or change this placeholder text -->
<string name="hello_blank_fragment">Hello blank fragment</string>
<string name="title_activity_main2">Main2Activity</string>
<string name="task_list_ok">OK</string>
</resources>