First of all forgive my ignorance, I've been learning to program for Android for 2 days and I'm missing something.
As the title says try to pass the text of a button from one activity to another
The code of the first activity:
private Button btnBorrachos;
private Button btnAnimales;
private String sendBundle;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnBorrachos = (Button) findViewById(R.id.buttonBorrachos);
btnAnimales = (Button) findViewById(R.id.buttonAnimales);
btnBorrachos.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
sendBundle = btnBorrachos.getText().toString();
Intent intent = new Intent(MainActivity.this, PruebaActivity.class);
intent.putExtra("txtbundle", sendBundle);
startActivity(intent);
}
});
btnAnimales.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
sendBundle = btnAnimales.getText().toString();
Intent intent = new Intent(MainActivity.this, AnimalesActivity.class);
intent.putExtra("txtbundle", sendBundle);
startActivity(intent);
}
});
}
What I'm trying to do is that depending on the button that is given, I will capture the text of that button in the sendBundle variable and with Intent send it to the second activity.
Code of the second activity:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_prueba);
Bundle bundle = getIntent().getExtras();
switch (bundle.getString("txtbundle")) {
case "Borrachos":
Toast.makeText(PruebaActivity.this, "Ha llegado 'Borrachos'", Toast.LENGTH_LONG);
break;
case "Animales":
Toast.makeText(PruebaActivity.this, "Ha llegado 'Animales'", Toast.LENGTH_LONG);
break;
default:
Toast.makeText(PruebaActivity.this, "Ha llegado DESCONOCIDO", Toast.LENGTH_LONG);
}
}
Supposedly what I should do is that depending on the text that comes from each button I show a Toast or another. But it does not show any in any case.
the comparison seems to be fine, these are the texts of the buttons:
android:text="Borrachos"
android:text="Animales"