diff --git a/catroid/src/main/java/org/catrobat/catroid/ui/UiUtils.java b/catroid/src/main/java/org/catrobat/catroid/ui/UiUtils.java index be08d7786c6..cef0c6fb69b 100644 --- a/catroid/src/main/java/org/catrobat/catroid/ui/UiUtils.java +++ b/catroid/src/main/java/org/catrobat/catroid/ui/UiUtils.java @@ -27,6 +27,7 @@ import android.content.Context; import android.content.ContextWrapper; import android.os.Build; +import android.util.Pair; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; @@ -164,4 +165,34 @@ public View getView(int position, @Nullable View convertView, @NonNull ViewGroup } }; } + + public static ArrayAdapter getAlertDialogAdapterForTextWithIcons(Activity activity, Context context, Pair[] items) { + return new ArrayAdapter>(context, android.R.layout.simple_list_item_1, items) { + TextView item; + + @NonNull + @Override + public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) { + final LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); + + if (convertView == null) { + convertView = inflater.inflate(R.layout.dialog_item_with_icon, parent, false); + item = (TextView) convertView.findViewById(R.id.item_text); + convertView.setTag(item); + } else { + item = (TextView) convertView.getTag(); + } + + item.setText(items[position].first); + + if (activity.getWindow().getDecorView().getLayoutDirection() == View.LAYOUT_DIRECTION_LTR) { + item.setCompoundDrawablesWithIntrinsicBounds(items[position].second, 0, 0, 0); + } else { + item.setCompoundDrawablesWithIntrinsicBounds(0, 0, items[position].second, 0); + } + + return convertView; + } + }; + } } diff --git a/catroid/src/main/java/org/catrobat/catroid/ui/recyclerview/backpack/BackpackRecyclerViewFragment.java b/catroid/src/main/java/org/catrobat/catroid/ui/recyclerview/backpack/BackpackRecyclerViewFragment.java index 8dd3d3553f7..57992dad5e6 100644 --- a/catroid/src/main/java/org/catrobat/catroid/ui/recyclerview/backpack/BackpackRecyclerViewFragment.java +++ b/catroid/src/main/java/org/catrobat/catroid/ui/recyclerview/backpack/BackpackRecyclerViewFragment.java @@ -26,6 +26,7 @@ import android.content.DialogInterface; import android.os.Bundle; import android.preference.PreferenceManager; +import android.util.Pair; import android.view.ActionMode; import android.view.LayoutInflater; import android.view.Menu; @@ -33,7 +34,9 @@ import android.view.View; import android.view.ViewGroup; import android.widget.ListAdapter; +import android.widget.ListView; import android.widget.PopupMenu; +import android.widget.TextView; import org.catrobat.catroid.R; import org.catrobat.catroid.ui.UiUtils; @@ -49,6 +52,7 @@ import java.util.ArrayList; import java.util.Collections; import java.util.List; +import java.util.Objects; import androidx.annotation.IntDef; import androidx.annotation.PluralsRes; @@ -57,6 +61,8 @@ import androidx.recyclerview.widget.ItemTouchHelper; import androidx.recyclerview.widget.RecyclerView; +import static org.catrobat.catroid.ui.UiUtils.getAlertDialogAdapterForTextWithIcons; + public abstract class BackpackRecyclerViewFragment extends Fragment implements ActionMode.Callback, RVAdapter.SelectionListener, @@ -292,28 +298,40 @@ public void onItemClick(final T item, MultiSelectionManager selectionManager) { return; } - List options = new ArrayList<>(); - options.add(R.string.unpack); - options.add(R.string.delete); - List names = new ArrayList<>(); - for (Integer option: options) { - names.add(getString(option)); - } - ListAdapter arrayAdapter = UiUtils.getAlertDialogAdapterForMenuIcons(options, - names, requireContext(), requireActivity()); - - new AlertDialog.Builder(requireContext()) - .setTitle(getItemName(item)) - .setAdapter(arrayAdapter, (dialog, which) -> { - switch (which) { - case 0: - unpackItems(new ArrayList<>(Collections.singletonList(item))); - break; - case 1: - showDeleteAlert(new ArrayList<>(Collections.singletonList(item))); - } - }) - .show(); + Pair[] items = new Pair[] { + new Pair(getString(R.string.unpack), R.drawable.ic_logout), + new Pair(getString(R.string.delete), R.drawable.ic_delete)}; + + LayoutInflater inflater = LayoutInflater.from(requireContext()); + View customDialogView = inflater.inflate(R.layout.dialog_backpack_custom_alert, null); + ListAdapter listAdapter = getAlertDialogAdapterForTextWithIcons(requireActivity(), + requireContext(), items); + + AlertDialog dialog = new AlertDialog.Builder(requireContext()) + .setView(customDialogView) + .create(); + Objects.requireNonNull(dialog.getWindow()) + .setBackgroundDrawableResource(R.drawable.backpack_background_round); + + TextView textView = customDialogView.findViewById(R.id.backpack_dialog_title); + textView.setText(getItemName(item)); + + ListView listView = customDialogView.findViewById(R.id.backpack_item_list); + listView.setAdapter(listAdapter); + listView.setOnItemClickListener((parent, view, position, id) -> { + switch (position) { + case 0: + unpackItems(new ArrayList<>(Collections.singletonList(item))); + break; + case 1: + showDeleteAlert(new ArrayList<>(Collections.singletonList(item))); + break; + } + + dialog.dismiss(); + }); + + dialog.show(); } public void setShowProgressBar(boolean show) { diff --git a/catroid/src/main/java/org/catrobat/catroid/ui/recyclerview/fragment/RecyclerViewFragment.java b/catroid/src/main/java/org/catrobat/catroid/ui/recyclerview/fragment/RecyclerViewFragment.java index 8b4a895de33..c2435d9f8b1 100644 --- a/catroid/src/main/java/org/catrobat/catroid/ui/recyclerview/fragment/RecyclerViewFragment.java +++ b/catroid/src/main/java/org/catrobat/catroid/ui/recyclerview/fragment/RecyclerViewFragment.java @@ -27,12 +27,15 @@ import android.os.Bundle; import android.preference.PreferenceManager; import android.util.Log; +import android.util.Pair; import android.view.ActionMode; import android.view.LayoutInflater; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.view.ViewGroup; +import android.widget.ListAdapter; +import android.widget.ListView; import android.widget.TextView; import org.catrobat.catroid.R; @@ -55,6 +58,7 @@ import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.util.List; +import java.util.Objects; import androidx.annotation.IntDef; import androidx.annotation.NonNull; @@ -66,6 +70,7 @@ import androidx.recyclerview.widget.RecyclerView; import static org.catrobat.catroid.common.SharedPreferenceKeys.SORT_PROJECTS_PREFERENCE_KEY; +import static org.catrobat.catroid.ui.UiUtils.getAlertDialogAdapterForTextWithIcons; public abstract class RecyclerViewFragment extends Fragment implements ActionMode.Callback, @@ -477,19 +482,37 @@ public void notifyDataSetChanged() { } protected void showBackpackModeChooser() { - CharSequence[] items = new CharSequence[] {getString(R.string.pack), getString(R.string.unpack)}; - new AlertDialog.Builder(requireContext()) - .setTitle(R.string.backpack_title) - .setItems(items, (dialog, which) -> { - switch (which) { - case 0: - startActionMode(BACKPACK); - break; - case 1: - switchToBackpack(); - } - }) - .show(); + Pair[] items = new Pair[] { + new Pair(getString(R.string.pack), R.drawable.ic_login), + new Pair(getString(R.string.unpack), R.drawable.ic_logout)}; + + LayoutInflater inflater = LayoutInflater.from(requireContext()); + View customDialogView = inflater.inflate(R.layout.dialog_backpack_custom_alert, null); + ListAdapter listAdapter = getAlertDialogAdapterForTextWithIcons(requireActivity(), + requireContext(), items); + + AlertDialog dialog = new AlertDialog.Builder(requireContext()) + .setView(customDialogView) + .create(); + Objects.requireNonNull(dialog.getWindow()) + .setBackgroundDrawableResource(R.drawable.backpack_background_round); + + ListView listView = customDialogView.findViewById(R.id.backpack_item_list); + listView.setAdapter(listAdapter); + listView.setOnItemClickListener((parent, view, position, id) -> { + switch (position) { + case 0: + startActionMode(BACKPACK); + break; + case 1: + switchToBackpack(); + break; + } + + dialog.dismiss(); + }); + + dialog.show(); } protected abstract void packItems(List selectedItems); diff --git a/catroid/src/main/java/org/catrobat/catroid/ui/recyclerview/fragment/ScriptFragment.java b/catroid/src/main/java/org/catrobat/catroid/ui/recyclerview/fragment/ScriptFragment.java index 80223f287a5..6327350efd9 100644 --- a/catroid/src/main/java/org/catrobat/catroid/ui/recyclerview/fragment/ScriptFragment.java +++ b/catroid/src/main/java/org/catrobat/catroid/ui/recyclerview/fragment/ScriptFragment.java @@ -29,6 +29,7 @@ import android.os.Bundle; import android.os.Parcelable; import android.util.Log; +import android.util.Pair; import android.view.ActionMode; import android.view.LayoutInflater; import android.view.Menu; @@ -37,7 +38,10 @@ import android.view.View; import android.view.ViewGroup; import android.view.inputmethod.InputMethodManager; +import android.widget.ArrayAdapter; import android.widget.ListAdapter; +import android.widget.ListView; +import android.widget.TextView; import org.catrobat.catroid.BuildConfig; import org.catrobat.catroid.ProjectManager; @@ -93,6 +97,7 @@ import java.lang.annotation.RetentionPolicy; import java.util.ArrayList; import java.util.List; +import java.util.Objects; import java.util.UUID; import androidx.annotation.IntDef; @@ -107,6 +112,7 @@ import static org.catrobat.catroid.common.Constants.CODE_XML_FILE_NAME; import static org.catrobat.catroid.common.Constants.UNDO_CODE_XML_FILE_NAME; +import static org.catrobat.catroid.ui.UiUtils.getAlertDialogAdapterForTextWithIcons; public class ScriptFragment extends ListFragment implements ActionMode.Callback, @@ -847,19 +853,37 @@ public boolean onBrickLongClick(Brick brick, int position) { } private void showBackpackModeChooser() { - CharSequence[] items = new CharSequence[] {getString(R.string.pack), getString(R.string.unpack)}; - new AlertDialog.Builder(getContext()) - .setTitle(R.string.backpack_title) - .setItems(items, (dialog, which) -> { - switch (which) { - case 0: - startActionMode(BACKPACK); - break; - case 1: - switchToBackpack(); - } - }) - .show(); + Pair[] items = new Pair[] { + new Pair(getString(R.string.pack), R.drawable.ic_login), + new Pair(getString(R.string.unpack), R.drawable.ic_logout)}; + + LayoutInflater inflater = LayoutInflater.from(requireContext()); + View customDialogView = inflater.inflate(R.layout.dialog_backpack_custom_alert, null); + ListAdapter listAdapter = getAlertDialogAdapterForTextWithIcons(requireActivity(), + requireContext(), items); + + AlertDialog dialog = new AlertDialog.Builder(requireContext()) + .setView(customDialogView) + .create(); + Objects.requireNonNull(dialog.getWindow()) + .setBackgroundDrawableResource(R.drawable.backpack_background_round); + + ListView listView = customDialogView.findViewById(R.id.backpack_item_list); + listView.setAdapter(listAdapter); + listView.setOnItemClickListener((parent, view, position, id) -> { + switch (position) { + case 0: + startActionMode(BACKPACK); + break; + case 1: + switchToBackpack(); + break; + } + + dialog.dismiss(); + }); + + dialog.show(); } public void showNewScriptGroupAlert(List selectedBricks) { diff --git a/catroid/src/main/res/drawable/backpack_background_round.xml b/catroid/src/main/res/drawable/backpack_background_round.xml new file mode 100644 index 00000000000..1ba16228456 --- /dev/null +++ b/catroid/src/main/res/drawable/backpack_background_round.xml @@ -0,0 +1,27 @@ + + + + + + \ No newline at end of file diff --git a/catroid/src/main/res/layout/dialog_backpack_custom_alert.xml b/catroid/src/main/res/layout/dialog_backpack_custom_alert.xml new file mode 100644 index 00000000000..59d4cf11bce --- /dev/null +++ b/catroid/src/main/res/layout/dialog_backpack_custom_alert.xml @@ -0,0 +1,52 @@ + + + + + + + + + + \ No newline at end of file diff --git a/catroid/src/main/res/layout/dialog_item_with_icon.xml b/catroid/src/main/res/layout/dialog_item_with_icon.xml new file mode 100644 index 00000000000..e37d24ae34b --- /dev/null +++ b/catroid/src/main/res/layout/dialog_item_with_icon.xml @@ -0,0 +1,35 @@ + + + \ No newline at end of file diff --git a/catroid/src/main/res/values/colors.xml b/catroid/src/main/res/values/colors.xml index 76aeee9ddbc..d8729245c7f 100644 --- a/catroid/src/main/res/values/colors.xml +++ b/catroid/src/main/res/values/colors.xml @@ -42,6 +42,7 @@ #00ACC1 #00475E #4D7F8F + #424242 #A8DFF4