LeGraa LeGraa -4 years ago 139
Android Question

Copy Paste userinput

I'm working on an exercise and I am stock right now.
I have to Copy the Userinput and then Paste the String created.
Can anyone help me?

This is my code:

public class CopyPasteActivity extends AppCompatActivity {

private Button CopyButton;
private Button PasteButton;
private EditText UserInput;
private TextView PasteText;
private final static String TAG = "CopyPasteActivity";


protected void onCreate(Bundle savedInstanceState) {

CopyButton = (Button) findViewById(R.id.copy_button);
PasteButton = (Button) findViewById(R.id.paste_button);
UserInput = (EditText) findViewById(R.id.user_input);
PasteText = (TextView) findViewById(R.id.paste_text);

final String userinput = new String();

CopyButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Log.d(TAG, "The button Copy was pressed");
String userinput = UserInput.getText().toString();

PasteButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Log.d(TAG, "The button True was pressed");

Answer Source

The problem is that you are declaring userinput inside onCreate() and making it final, which prevents you from editing it later. Also inside the CopyButton click listener you are creating a new local variable for userinput whose scope is limited to that listener only.

Move String userinput = new String(); outside of onCreate(), removing final, and change the line in the CopyButton listener to

userinput = UserInput.getText().toString();
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download