Skip to content

Commit

Permalink
Allow long-click of card to copy id to clipboard
Browse files Browse the repository at this point in the history
  • Loading branch information
Branden Archer authored and Branden Archer committed Jun 21, 2016
1 parent f2f56b0 commit 94c79b4
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 0 deletions.
40 changes: 40 additions & 0 deletions app/src/main/java/protect/card_locker/MainActivity.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package protect.card_locker;

import android.content.ClipData;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.ClipboardManager;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.database.Cursor;
Expand All @@ -10,13 +12,16 @@
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.ContextMenu;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.webkit.WebView;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import java.util.Calendar;

Expand Down Expand Up @@ -65,6 +70,8 @@ private void updateLoyaltyCardList()
final LoyaltyCardCursorAdapter adapter = new LoyaltyCardCursorAdapter(this, cardCursor);
cardList.setAdapter(adapter);

registerForContextMenu(cardList);

cardList.setOnItemClickListener(new AdapterView.OnItemClickListener()
{
@Override
Expand All @@ -83,6 +90,39 @@ public void onItemClick(AdapterView<?> parent, View view, int position, long id)
});
}

@Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo)
{
super.onCreateContextMenu(menu, v, menuInfo);
if (v.getId()==R.id.list)
{
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.card_longclick_menu, menu);
}
}

@Override
public boolean onContextItemSelected(MenuItem item)
{
AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) item.getMenuInfo();
ListView listView = (ListView) findViewById(R.id.list);

Cursor cardCursor = (Cursor)listView.getItemAtPosition(info.position);
LoyaltyCard card = LoyaltyCard.toLoyaltyCard(cardCursor);

if(card != null && item.getItemId() == R.id.action_clipboard)
{
ClipboardManager clipboard = (ClipboardManager) getSystemService(CLIPBOARD_SERVICE);
ClipData clip = ClipData.newPlainText(card.store, card.cardId);
clipboard.setPrimaryClip(clip);

Toast.makeText(this, R.string.copy_to_clipboard_toast, Toast.LENGTH_LONG).show();
return true;
}

return super.onContextItemSelected(item);
}

@Override
public boolean onCreateOptionsMenu(Menu menu)
{
Expand Down
9 changes: 9 additions & 0 deletions app/src/main/res/menu/card_longclick_menu.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">

<item
android:id="@+id/action_clipboard"
android:title="@string/copy_to_clipboard"
app:showAsAction="always"/>
</menu>
4 changes: 4 additions & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
<string name="edit">Edit</string>
<string name="delete">Delete</string>
<string name="ok">OK</string>
<string name="copy_to_clipboard">Copy To clipboard</string>

<string name="editCardTitle">Edit Loyalty Card</string>
<string name="addCardTitle">Add Loyalty Card</string>
Expand Down Expand Up @@ -55,4 +56,7 @@

<string name="selectBarcodeTitle">Select Barcode</string>
<string name="enterBarcodeInstructions">Enter the barcode value then select the image which represents the barcode you want to use</string>

<string name="copy_to_clipboard_toast">Card ID copied to clipboard</string>

</resources>

0 comments on commit 94c79b4

Please sign in to comment.