From 2799a89386dceb5613203c0f6fff781712641527 Mon Sep 17 00:00:00 2001 From: Nikolaos Bampaliaris Date: Sun, 6 Oct 2019 15:06:42 +0300 Subject: [PATCH] Implemented long click copy topic url --- .../activities/topic/TopicActivity.java | 59 +++++++++++++++++++ app/src/main/res/values/strings.xml | 2 + 2 files changed, 61 insertions(+) diff --git a/app/src/main/java/gr/thmmy/mthmmy/activities/topic/TopicActivity.java b/app/src/main/java/gr/thmmy/mthmmy/activities/topic/TopicActivity.java index 2bb084e3..71afc53f 100644 --- a/app/src/main/java/gr/thmmy/mthmmy/activities/topic/TopicActivity.java +++ b/app/src/main/java/gr/thmmy/mthmmy/activities/topic/TopicActivity.java @@ -2,7 +2,10 @@ package gr.thmmy.mthmmy.activities.topic; import android.annotation.SuppressLint; import android.app.NotificationManager; +import android.content.ClipData; +import android.content.ClipboardManager; import android.content.Context; +import android.content.DialogInterface; import android.content.Intent; import android.content.SharedPreferences; import android.graphics.Rect; @@ -162,6 +165,7 @@ public class TopicActivity extends BaseActivity implements TopicAdapter.OnPostFo toolbarTitle.setMarqueeRepeatLimit(-1); toolbarTitle.setText(topicTitle); toolbarTitle.setSelected(true); + this.setToolbarOnLongClickListener(topicPageUrl); setSupportActionBar(toolbar); if (getSupportActionBar() != null) { getSupportActionBar().setDisplayHomeAsUpEnabled(true); @@ -793,4 +797,59 @@ public class TopicActivity extends BaseActivity implements TopicAdapter.OnPostFo } }); } + + + + /**This method sets a long click listener on the title of the topic. Once the + * listener gets triggered, it copies the link url of the topic in the clipboard. + * This method is getting called on the onCreate() of the TopicActivity*/ + void setToolbarOnLongClickListener(String url) + { + + toolbar.setOnLongClickListener(new View.OnLongClickListener() { + @Override + public boolean onLongClick(View view) { + + + //TRy to set the clipboard text. + try + { + //Create a ClipboardManager. + ClipboardManager clipboard = (ClipboardManager) getSystemService(CLIPBOARD_SERVICE); + + clipboard.setPrimaryClip(ClipData.newPlainText(BUNDLE_TOPIC_URL, url)); + + //Make a toast to inform the user that the url was copied. + Toast.makeText( + TopicActivity.this, + TopicActivity.this.getString(R.string.url_copied_msg), + Toast.LENGTH_SHORT).show(); + } + + + //Something happened. Probably the device does not support this. + catch (NullPointerException e) + { + //Create a new dialog builder. + AlertDialog.Builder builder = new AlertDialog.Builder(TopicActivity.this); + + //Set UP the alert dialog. + builder.setMessage(getString(R.string.url_copy_does_not_work_msg)); + builder.setTitle("ERROR"); + builder.setPositiveButton("OK", new DialogInterface.OnClickListener() { + @Override + public void onClick(DialogInterface dialogInterface, int i) { + dialogInterface.dismiss(); + } + }); + + //Create and show the alert dialog. + AlertDialog dialog = builder.create(); + dialog.show(); + } + + return true; + } + }); + } } \ No newline at end of file diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 0199788b..42cce649 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -231,4 +231,6 @@ New topic Create topic + Url copied + There was a problem copying the url. Probably your device does not support this functionality