setText on button from another activity android setText on button from another activity android android android

setText on button from another activity android


You can pass the button caption to CadastroTimes with intent as

Intent intent = new Intent(EditarTimes.this, CadastroTimes.class);intent.putExtra("buttontxt","Changed Text");startActivity(intent);

Then in CadastroTimes.java set the text of the button to the new value that you passed. The code will look like:

button = (Button)findViewById(R.id.button); // This is your reference from the xml. button is my name, you might have your own id given already.Bundle extras = getIntent().getExtras();String value = ""; // You can do it in better and cleaner wayif (extras != null) {    value = extras.getString("buttontxt");}button.setText(value);

Do remember to do it in onCreate after setContentView


//From ActivityIntent intent = new Intent(EditarTimes.this, CadastroTimes.class);intent.putExtra("change_tag", "text to change");startActivity(intent);//To Activitypublic void onCreate(..){    Button changeButton = (Button)findViewById(R.id.your_button);    // Button to set received text    Intent intent = getIntent();    if(null != intent &&              !TextUtils.isEmpty(intent.getStringExtra("change_tag"))) {        String changeText = intent.getStringExtra("change_tag");        // Extracting sent text from intent                changeButton.setText(changeText);        // Setting received text on Button     }}


1: Use intent.putExtra() to share a value from one activity another activity, as:

In ActivityOne.class :

startActivity(    Intent(        applicationContext,         ActivityTwo::class.java    ).putExtra(        "key",         "value"    ))

In ActivityTwo.class :

var value = ""if (intent.hasExtra("key")    value = intent.getStringExtra("key")

2: Modify button text programatically as:

btn_object.text = value

Hope this will help you