user9300302
user9300302

Reputation:

EditText setText() not working

I am programming an Android Application that Downloads a file by URL. For that I need to send a link from one Activity to another with EXTRA_MESSAGE. Then an EditText should be changed with setText(), but the EditText is still empty. My code:

EditText torul = findViewById(R.id.url);
Intent intent = getIntent();
String message = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);
torul.setText(message);

Before you answer to do it with:

torul.setText(message, TextView.BufferType.EDITABLE);

Tried it, didn't work..

How I add my EXTRA_MESSAGE:

hl.setOnItemClickListener(new AdapterView.OnItemClickListener(){
    @Override
    public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
        String url = String.valueOf(((TextView) view).getText());
        start(url);
    }
});
private void start(String url) {
    Intent copy = new Intent(this, MainActivity.class);
    copy.putExtra(EXTRA_MESSAGE, url);
    startActivity(copy);
}

Upvotes: 0

Views: 2586

Answers (3)

deluxe1
deluxe1

Reputation: 857

The problem is that setText() is called in the onCreate() method, which is the first method called when the activity is created. Setting the text should be done in the onResume() method. This fixes the problem.

Upvotes: 2

Muhammad Saad Rafique
Muhammad Saad Rafique

Reputation: 3189

I think here is problem:

Intent copy = new Intent(this, MainActivity.class);

You have to give context of second activity so it should be:

Intent copy = new Intent(MainActivity.this, YourSecondActivity.class);

Upvotes: 0

Sanket Sangani
Sanket Sangani

Reputation: 261

Instead of this:

String message = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);

use this method in second activity:

String message = getIntent().getExtras().getString(MainActivity.EXTRA_MESSAGE);

Upvotes: 0

Related Questions