diff --git a/app/src/main/java/com/simplemobiletools/calculator/MainActivity.java b/app/src/main/java/com/simplemobiletools/calculator/MainActivity.java index 8343795..72486b2 100644 --- a/app/src/main/java/com/simplemobiletools/calculator/MainActivity.java +++ b/app/src/main/java/com/simplemobiletools/calculator/MainActivity.java @@ -1,5 +1,7 @@ package com.simplemobiletools.calculator; +import android.content.ClipData; +import android.content.ClipboardManager; import android.content.Intent; import android.content.res.Resources; import android.os.Bundle; @@ -122,6 +124,32 @@ public class MainActivity extends AppCompatActivity implements Calculator { calc.numpadClicked(id); } + @OnLongClick(R.id.formula) + public boolean formulaLongPressed() { + copyToClipboard(false); + return true; + } + + @OnLongClick(R.id.result) + public boolean resultLongPressed() { + copyToClipboard(true); + return true; + } + + private void copyToClipboard(boolean copyResult) { + String value; + if (copyResult) { + value = result.getText().toString(); + } else { + value = formula.getText().toString(); + } + + final ClipboardManager clipboard = (ClipboardManager) getSystemService(CLIPBOARD_SERVICE); + final ClipData clip = ClipData.newPlainText(getResources().getString(R.string.app_name), value); + clipboard.setPrimaryClip(clip); + Utils.showToast(getApplicationContext(), R.string.copied_to_clipboard); + } + @Override public void setValue(String value) { result.setText(value); diff --git a/app/src/main/java/com/simplemobiletools/calculator/Utils.java b/app/src/main/java/com/simplemobiletools/calculator/Utils.java new file mode 100644 index 0000000..bf344fc --- /dev/null +++ b/app/src/main/java/com/simplemobiletools/calculator/Utils.java @@ -0,0 +1,10 @@ +package com.simplemobiletools.calculator; + +import android.content.Context; +import android.widget.Toast; + +public class Utils { + public static void showToast(Context context, int resId) { + Toast.makeText(context, context.getResources().getString(resId), Toast.LENGTH_SHORT).show(); + } +} diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 9d8fa60..85bf9f2 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -1,5 +1,6 @@ Simple Calculator + Value copied to clipboard About