Home setText on button from another activity android
Reply: 6

setText on button from another activity android

Alan Santos
1#
Alan Santos Published in 2015-11-08 18:13:33Z

I have a problem, I want to click on the list, calling a new activity and rename the button to another name.

I tried several things, nothing worked, can someone please help me?

My class EditarTimes

private AdapterView.OnItemClickListener selecionarTime = new AdapterView.OnItemClickListener() {

        public void onItemClick(AdapterView arg0, View arg1, int pos, long id) {
            t = times.get(pos);

            CadastroTimes cad = new CadastroTimes();
            CadastroTimes.salvar.setText("Alterar");
            Intent intent = new Intent(EditarTimes.this, CadastroTimes.class);
            startActivity(intent);


        }

    };
public class CadastroTimes extends AppCompatActivity {

    private Time t;
    private timeDatabase db;
    private EditText edID;
    private EditText edNome;
    public Button salvar;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_cadastro_times);

        edID = (EditText) findViewById(R.id.edID);
        edNome = (EditText) findViewById(R.id.edNome);
        db = new timeDatabase(getApplicationContext());
        salvar = (Button) findViewById(R.id.btnCadastrar);
        salvar.setText("Cadastrar");
        String newString;
        if (savedInstanceState == null) {
            Bundle extras = getIntent().getExtras();
            if(extras == null) {
                newString= null;
            } else {
                newString= extras.getString("Alterar");
            }
        } else {
            newString= (String) savedInstanceState.getSerializable("Alterar");
        }

        //button in CadastroTimes activity to have that String as text
        System.out.println(newString + " AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA");
        salvar.setText(newString);
    }

    public void salvarTime(View v) {
        t = new Time();
        t.setNome(edNome.getText().toString());

        if (salvar.getText().equals("Alterar")) {
            db.atualizar(t);
            exibirMensagem("Time atualizado com sucesso!");
        } else {
            db.salvar(t);
            exibirMensagem("Time cadastrado com sucesso!");
        }

        Intent intent = new Intent(this, EditarTimes.class);
        startActivity(intent);

    }


    private void limparDados() {
        edID.setText("");
        edNome.setText("");
        edNome.requestFocus();
    }

    private void exibirMensagem(String msg) {
        Toast.makeText(getApplicationContext(), msg, Toast.LENGTH_LONG).show();
    }

}
public class EditarTimes extends AppCompatActivity {

    private Time t;
    private List times;
    private timeDatabase db;
    private ListView lvTimes;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_editar_times);

        lvTimes = (ListView) findViewById(R.id.lvTimes);
        lvTimes.setOnItemClickListener(selecionarTime);
        lvTimes.setOnItemLongClickListener(excluirTime);
        times = new ArrayList();
        db = new timeDatabase(getApplicationContext());
        atualizarLista();
    }

    private void excluirTime(final int idTime) {


        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle("Excluir time?")
                .setIcon(android.R.drawable.ic_dialog_alert)
                .setMessage("Deseja excluir esse time?")
                .setCancelable(false)
                .setPositiveButton(getString(R.string.sim),
                        new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog, int id) {
                                if (db.deletar(idTime)) {
                                    atualizarLista();
                                    exibirMensagem(getString(R.string.msgExclusao));
                                } else {
                                    exibirMensagem(getString(R.string.msgFalhaExclusao));
                                }

                            }
                        })
                .setNegativeButton(getString(R.string.nao),
                        new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog, int id) {
                                dialog.cancel();
                            }
                        });
        builder.create();
        builder.show();

        atualizarLista();

    }

    private void atualizarLista() {

        times = db.listAll();
        if (times != null) {
            if (times.size() > 0) {
                TimeListAdapter tla = new TimeListAdapter(
                        getApplicationContext(), times);
                lvTimes.setAdapter(tla);
            }

        }

    }

    private AdapterView.OnItemClickListener selecionarTime = new AdapterView.OnItemClickListener() {

        public void onItemClick(AdapterView arg0, View arg1, int pos, long id) {
            t = times.get(pos);

            Intent intent = new Intent(EditarTimes.this, CadastroTimes.class);
            String strName = "Alterar";
            intent.putExtra("Alterar", strName);
            startActivity(intent);




            //preecherDados(t);

        }

    };

    private AdapterView.OnItemLongClickListener excluirTime = new AdapterView.OnItemLongClickListener() {

        public boolean onItemLongClick(AdapterView arg0, View arg1,
                                       int pos, long arg3) {
            excluirTime(times.get(pos).getId());
            return true;
        }


    };

    /*private void preecherDados(Time time) {
        edID.setText(String.valueOf(time.getId()));
        edNome.setText(time.getNome());
    }*/

    private void exibirMensagem(String msg) {
        Toast.makeText(getApplicationContext(), msg, Toast.LENGTH_LONG).show();
    }

    public void telaCadastrar(View view) {
        Intent intent = new Intent(this, CadastroTimes.class);
        startActivity(intent);
    }

    public void botaoSair(View view) {
        Intent intent = new Intent(this, TelaInicial.class);
        startActivity(intent);
    }
}
Ajay Yadav
2#
Ajay Yadav Reply to 2017-10-10 12:09:05Z

Ok, so the first step would be to take the button you want and make it a public static object (and put it at the top of the class).

public static Button button;

Then you can manipulate that using this in another class:

 ClassName.button.setText("My Button");

In your case it is

CadastroTimes.salvar.setText("Alterar");
V-rund Puro-hit
3#
V-rund Puro-hit Reply to 2016-10-08 05:23:25Z

Now, i got you:

Your EditarTimes activity with listview:

//set setOnItemClickListener

 youtListView.setOnItemClickListener(new android.widget.AdapterView.OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView<?> parent, View view,int position, long id) {

    Intent i = new Intent(EditarTimes.this, CadastroTimes.class);  
 //text which you want to display on the button to CadastroTimes activity
   String strName = "hello button"; 
    i.putExtra("STRING_I_NEED", strName);             
                }
            });

In CadastroTimes activity,

under onCreate() method, get the text string as:-

String newString;
if (savedInstanceState == null) {
    Bundle extras = getIntent().getExtras();
    if(extras == null) {
        newString= null;
    } else {
        newString= extras.getString("STRING_I_NEED");
    }
} else {
    newString= (String) savedInstanceState.getSerializable("STRING_I_NEED");
}

//button in CadastroTimes activity to have that String as text yourButton.setText(newString);

V-rund Puro-hit
4#
V-rund Puro-hit Reply to 2016-10-08 05:24:07Z

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 way
if (extras != null) {
    value = extras.getString("buttontxt");
}
button.setText(value);

Do remember to do it in onCreate after setContentView

t0mm13b
5#
t0mm13b Reply to 2016-06-12 12:57:48Z

For changing the button text:

  1. Use a static method to call from the other activity to directly modify the button caption.
  2. Use an intent functionality, which is preferable.
  3. Use an Interface and implement it, which is used for communicating between activities or fragment in a manner of fire and forget principle.
Ameer Sabith
6#
Ameer Sabith Reply to 2017-11-17 09:20:34Z

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

In ActivityOne.class :

Intent intent = new Intent(getApplicationContext(), ActivityTwo.class);
intent.putExtra("key", "value");
intent.startActivity();

In ActivityTwo.class :

Intent intent = getIntent();
String var = intent.getStringExtra("key");

2: Modify button text programatically as:

btn_object.setText(var);

Hope this will help you

Shibin Francis
7#
Shibin Francis Reply to 2018-01-23 06:27:43Z
//From Activity
Intent intent = new Intent(EditarTimes.this, CadastroTimes.class);
intent.pueExtra("chang_tag", "text to change");
startActivity(intent)

//To Activity

public void onCreate(..){

    Button changeButton = (Button)findViewById(R.id.your_button);
    // Button to set received text
    Intent intent = getIntent();
    if(null != intent) {
        String changeText = intent.getStringExtra("chang_tag", "");
        // Extracting sent text from intent

        (!TextUtils.isEmpty(changeText )){
            changeButton.setText(changeText);
            // Setting received text on Button

        }
     }
}

Sorry I am bit late. However, I hope will help you.

You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.310512 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO