From a68ad87b4bf20a5caadeb78df2a16ebf0942ca1d Mon Sep 17 00:00:00 2001 From: tibbi Date: Sun, 28 Feb 2016 21:24:27 +0100 Subject: [PATCH] add a contextual actionbar for deleting images --- .../gallery/activities/PhotosActivity.java | 47 ++++++++++++++++++- app/src/main/res/layout/activity_photos.xml | 1 + app/src/main/res/menu/cab.xml | 9 ++++ app/src/main/res/values/strings.xml | 1 + 4 files changed, 57 insertions(+), 1 deletion(-) create mode 100644 app/src/main/res/menu/cab.xml diff --git a/app/src/main/java/com/simplemobiletools/gallery/activities/PhotosActivity.java b/app/src/main/java/com/simplemobiletools/gallery/activities/PhotosActivity.java index 8db9f6f79..99185e79b 100644 --- a/app/src/main/java/com/simplemobiletools/gallery/activities/PhotosActivity.java +++ b/app/src/main/java/com/simplemobiletools/gallery/activities/PhotosActivity.java @@ -6,6 +6,10 @@ import android.net.Uri; import android.os.Bundle; import android.provider.MediaStore; import android.support.v7.app.AppCompatActivity; +import android.view.ActionMode; +import android.view.Menu; +import android.view.MenuInflater; +import android.view.MenuItem; import android.view.View; import android.widget.AdapterView; import android.widget.GridView; @@ -19,8 +23,9 @@ import java.util.ArrayList; import java.util.List; import java.util.regex.Pattern; -public class PhotosActivity extends AppCompatActivity implements AdapterView.OnItemClickListener { +public class PhotosActivity extends AppCompatActivity implements AdapterView.OnItemClickListener, GridView.MultiChoiceModeListener { private List photos; + private int selectedItemsCnt; @Override protected void onCreate(Bundle savedInstanceState) { @@ -33,6 +38,7 @@ public class PhotosActivity extends AppCompatActivity implements AdapterView.OnI final PhotosAdapter adapter = new PhotosAdapter(this, getPhotos(path)); gridView.setAdapter(adapter); gridView.setOnItemClickListener(this); + gridView.setMultiChoiceModeListener(this); final String dirName = Helpers.getFilename(path); setTitle(dirName); @@ -66,4 +72,43 @@ public class PhotosActivity extends AppCompatActivity implements AdapterView.OnI intent.putExtra(Constants.PHOTO, photos.get(position)); startActivity(intent); } + + @Override + public void onItemCheckedStateChanged(ActionMode mode, int position, long id, boolean checked) { + if (checked) + selectedItemsCnt++; + else + selectedItemsCnt--; + + mode.setTitle(String.valueOf(selectedItemsCnt)); + mode.invalidate(); + } + + @Override + public boolean onCreateActionMode(ActionMode mode, Menu menu) { + final MenuInflater inflater = mode.getMenuInflater(); + inflater.inflate(R.menu.cab, menu); + return true; + } + + @Override + public boolean onPrepareActionMode(ActionMode mode, Menu menu) { + return false; + } + + @Override + public boolean onActionItemClicked(ActionMode mode, MenuItem item) { + switch (item.getItemId()) { + case R.id.cab_remove: + mode.finish(); + return true; + default: + return false; + } + } + + @Override + public void onDestroyActionMode(ActionMode mode) { + selectedItemsCnt = 0; + } } diff --git a/app/src/main/res/layout/activity_photos.xml b/app/src/main/res/layout/activity_photos.xml index 7b47eb155..d6a47b7a7 100644 --- a/app/src/main/res/layout/activity_photos.xml +++ b/app/src/main/res/layout/activity_photos.xml @@ -9,4 +9,5 @@ android:horizontalSpacing="1dp" android:numColumns="auto_fit" android:stretchMode="columnWidth" + android:choiceMode="multipleChoiceModal" android:verticalSpacing="1dp"/> diff --git a/app/src/main/res/menu/cab.xml b/app/src/main/res/menu/cab.xml new file mode 100644 index 000000000..3122e5205 --- /dev/null +++ b/app/src/main/res/menu/cab.xml @@ -0,0 +1,9 @@ + + + + diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index eb7526ece..91f6b62c1 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -2,4 +2,5 @@ Simple Gallery Share via Not much to do in a gallery without accessing your photos + Remove