Add a remove option to the popup menu

This commit is contained in:
Jakob Nixdorf 2017-07-03 22:52:42 +02:00
parent ffc649c613
commit 260a7d522d
No known key found for this signature in database
GPG key ID: BE99BF86574A7DBC
3 changed files with 67 additions and 68 deletions

View file

@ -32,6 +32,8 @@ import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
@ -132,11 +134,69 @@ public class EntriesCardAdapter extends RecyclerView.Adapter<EntriesCardAdapter.
return true;
}
public void editEntryLabel(final int pos) {
AlertDialog.Builder builder = new AlertDialog.Builder(context);
final EditText input = new EditText(context);
input.setText(getItem(pos).getLabel());
input.setSingleLine();
FrameLayout container = new FrameLayout(context);
FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
params.leftMargin = context.getResources().getDimensionPixelSize(R.dimen.activity_horizontal_margin);
params.rightMargin = context.getResources().getDimensionPixelSize(R.dimen.activity_horizontal_margin);
input.setLayoutParams(params);
container.addView(input);
builder.setTitle(R.string.alert_rename)
.setView(container)
.setPositiveButton(R.string.button_save, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
getItem(pos).setLabel(input.getEditableText().toString());
notifyItemChanged(pos);
SettingsHelper.store(context, entries);
}
})
.setNegativeButton(android.R.string.cancel, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {}
})
.create()
.show();
}
private void showPopupMenu(View view, final int pos) {
View menuItemView = view.findViewById(R.id.menuButton);
PopupMenu popup = new PopupMenu(view.getContext(), menuItemView);
MenuInflater inflate = popup.getMenuInflater();
inflate.inflate(R.menu.menu_popup, popup.getMenu());
popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
@Override
public boolean onMenuItemClick(MenuItem item) {
int id = item.getItemId();
if (id == R.id.menu_popup_editLabel) {
editEntryLabel(pos);
return true;
} else if (id == R.id.menu_popup_remove) {
onItemDismiss(pos);
return true;
} else {
return false;
}
}
});
popup.show();
}
public void setMoveEventCallback(ViewHolderEventCallback cb) {
this.viewHolderEventCallback = cb;
}
public static class EntryViewHolder extends RecyclerView.ViewHolder
public class EntryViewHolder extends RecyclerView.ViewHolder
implements ItemTouchHelperViewHolder {
private ViewHolderEventCallback eventCallback;
@ -159,33 +219,11 @@ public class EntriesCardAdapter extends RecyclerView.Adapter<EntriesCardAdapter.
menuButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
showPopupMenu(view);
showPopupMenu(view, getAdapterPosition());
}
});
}
private void showPopupMenu(View view) {
View menuItemView = view.findViewById(R.id.menuButton);
PopupMenu popup = new PopupMenu(view.getContext(), menuItemView);
MenuInflater inflate = popup.getMenuInflater();
inflate.inflate(R.menu.menu_popup, popup.getMenu());
popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
@Override
public boolean onMenuItemClick(MenuItem item) {
int id = item.getItemId();
if (id == R.id.editLabel) {
eventCallback.onEditButtonClicked(getAdapterPosition());
return true;
} else {
return false;
}
}
});
popup.show();
}
@Override
public void onItemSelected() {
if (eventCallback != null)
@ -202,7 +240,5 @@ public class EntriesCardAdapter extends RecyclerView.Adapter<EntriesCardAdapter.
public interface ViewHolderEventCallback {
void onMoveEventStart();
void onMoveEventStop();
void onEditButtonClicked(final int pos);
}
}

View file

@ -53,8 +53,6 @@ import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.view.animation.LinearInterpolator;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.ProgressBar;
import android.widget.TextView;
@ -330,11 +328,6 @@ public class MainActivity extends AppCompatActivity {
public void onMoveEventStop() {
startUpdater();
}
@Override
public void onEditButtonClicked(final int pos) {
editEntryLabel(pos);
}
});
handler = new Handler();
@ -416,40 +409,6 @@ public class MainActivity extends AppCompatActivity {
}
}
// Edit entry label
public void editEntryLabel(final int pos) {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
final EditText input = new EditText(this);
input.setText(adapter.getItem(pos).getLabel());
input.setSingleLine();
FrameLayout container = new FrameLayout(this);
FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
params.leftMargin = getResources().getDimensionPixelSize(R.dimen.activity_horizontal_margin);
params.rightMargin = getResources().getDimensionPixelSize(R.dimen.activity_horizontal_margin);
input.setLayoutParams(params);
container.addView(input);
builder.setTitle(R.string.alert_rename)
.setView(container)
.setPositiveButton(R.string.button_save, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
adapter.getItem(pos).setLabel(input.getEditableText().toString());
adapter.notifyItemChanged(pos);
SettingsHelper.store(getBaseContext(), entries);
}
})
.setNegativeButton(android.R.string.cancel, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {}
})
.create()
.show();
}
// Options menu
@Override
public boolean onCreateOptionsMenu(Menu menu) {

View file

@ -2,7 +2,11 @@
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="@+id/editLabel"
android:id="@+id/menu_popup_editLabel"
android:title="@string/menu_edit_label" />
<item
android:id="@+id/menu_popup_remove"
android:title="@string/alert_remove" />
</menu>