Browse Source

Tidy up long click copy topic url

widgets
Ezerous 5 years ago
parent
commit
5737b69931
  1. 68
      app/src/main/java/gr/thmmy/mthmmy/activities/topic/TopicActivity.java
  2. 3
      app/src/main/res/values/strings.xml

68
app/src/main/java/gr/thmmy/mthmmy/activities/topic/TopicActivity.java

@ -798,58 +798,30 @@ public class TopicActivity extends BaseActivity implements TopicAdapter.OnPostFo
}); });
} }
/**This method sets a long click listener on the title of the topic. Once the /**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. * 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*/ * This method is getting called on the onCreate() of the TopicActivity*/
void setToolbarOnLongClickListener(String url) void setToolbarOnLongClickListener(String url) {
{ toolbar.setOnLongClickListener(view -> {
//Try to set the clipboard text
toolbar.setOnLongClickListener(new View.OnLongClickListener() { try {
@Override //Create a ClipboardManager
public boolean onLongClick(View view) { ClipboardManager clipboard = (ClipboardManager) getSystemService(CLIPBOARD_SERVICE);
clipboard.setPrimaryClip(ClipData.newPlainText(BUNDLE_TOPIC_URL, url));
//TRy to set the clipboard text.
try //Make a toast to inform the user that the url was copied
{ Toast.makeText(
//Create a ClipboardManager. TopicActivity.this,
ClipboardManager clipboard = (ClipboardManager) getSystemService(CLIPBOARD_SERVICE); TopicActivity.this.getString(R.string.url_copied_msg),
Toast.LENGTH_SHORT).show();
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;
} }
//Something happened. Probably the device does not support this (report to Firebase)
catch (NullPointerException e) {
Timber.e(e, "Error while trying to copy topic's url.");
}
return true;
}); });
} }
} }

3
app/src/main/res/values/strings.xml

@ -231,6 +231,5 @@
<!-- New topic activity --> <!-- New topic activity -->
<string name="new_topic_toolbar">New topic</string> <string name="new_topic_toolbar">New topic</string>
<string name="create_topic">Create topic</string> <string name="create_topic">Create topic</string>
<string name="url_copied_msg">Url copied</string> <string name="url_copied_msg">URL copied</string>
<string name="url_copy_does_not_work_msg">There was a problem copying the url. Probably your device does not support this functionality</string>
</resources> </resources>

Loading…
Cancel
Save