I’m trying to add a “copy” button to a settextview.
Here is my full code:
public class activity_tasks extends AppCompatActivity {
private Button button_refresh;
TextView textView;
TextView textView4;
private AdView mAdView;
@RequiresApi(api = Build.VERSION_CODES.M)
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_tasks);
MobileAds.initialize(this, new OnInitializationCompleteListener() {
@Override
public void onInitializationComplete(InitializationStatus initializationStatus) {
}
});
mAdView = findViewById(R.id.adView);
AdRequest adRequest = new AdRequest.Builder().build();
mAdView.loadAd(adRequest);
button_refresh = findViewById(R.id.button_refresh);
button_refresh.setOnClickListener(v -> {
finish();
startActivity(getIntent());
overridePendingTransition(0, 0);
});
Button button = findViewById(R.id.button1);
Button button_copy = findViewById(R.id.button_copy);
button.setOnClickListener(v -> showInfo());
textView = findViewById(R.id.textView4);
downloadJSON();
}
private void downloadJSON() {
@SuppressLint("StaticFieldLeak")
class DownloadJSON extends AsyncTask<Void, Void, String> {
@Override
protected void onPreExecute() {
super.onPreExecute();
}
@Override
protected void onPostExecute(String s) {
try {
settextView(s);
} catch (JSONException e) {
e.printStackTrace();
}
}
@Override
protected String doInBackground(Void... voids) {
try {
URL url = new URL("https://www.URL.app/api/tasks.php");
HttpURLConnection con = (HttpURLConnection) url.openConnection();
StringBuilder sb = new StringBuilder();
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(con.getInputStream()));
String json;
while ((json = bufferedReader.readLine()) != null) {
sb.append(json).append("n");
}
return sb.toString().trim();
} catch (Exception e) {
return null;
}
}
}
DownloadJSON getJSON = new DownloadJSON();
getJSON.execute();
}
private void settextView(String json) throws JSONException {
JSONArray jsonArray = new JSONArray(json);
String() tasks = new String(jsonArray.length());
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject obj = jsonArray.getJSONObject(i);
tasks(i) = obj.getString("Task_title") + ".nn" + obj.getString("task_description");
textView.setText(tasks(0));
}
My problem is that I’m not sure where to place the following code:
button_copy.setOnClickListener(v -> {
ClipboardManager clipboard = (ClipboardManager)getApplicationContext().getSystemService(getApplicationContext().CLIPBOARD_SERVICE);
ClipData clip = ClipData.newPlainText("", obj.getString("Task_title") + ".nn" + obj.getString("task_description"));
clipboard.setPrimaryClip(clip);
Toast.makeText(getApplicationContext(), "Copied to clipboard!", Toast.LENGTH_LONG).show();
});
I have tried placing it in the settextView, however when I land on the page and the text generates, it automatically copies – but I want this attributed to a button, rather than automatically copying.
For reference, the button in my activity_list.xml is button_copy.