1
I want to do a project where clicking the button will appear a text and when the text appears I want an image to appear together. ex: random text chosen "the dog is silly" appears the photo of the dog.
follows my current code
public class MainActivity extends AppCompatActivity {
private Button botaoIniciar;
private TextView texto;
private ImageView imagem;
private String[] frases = {"O CACHORRO E BOBO",
"O GATO E MIMADO", "A BALEIA É ROSA",
"O RATO ROEU O QUEIJO", "A MAMÃE FEZ UMA SOPA",
"A BUZINA É DO CAMINHÃO","O PAPAI É MUITO BONITO",
"A MAMÃE É MUITO LINDA", };
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
botaoIniciar = (Button) findViewById(R.id.botaoIniciarId);
texto = (TextView) findViewById(R.id.textoId);
imagem = (ImageView) findViewById(R.id.imagemId);
botaoIniciar.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Random numeroRandomico = new Random();
int numeroAleatorio = numeroRandomico.nextInt(frases.length);
texto.setText(frases[numeroAleatorio]);
getImagem();
}
});
}
public void getImagem() {
if (texto.equals("O CACHORRO E BOBO")) {
imagem.setImageResource(R.drawable.cao);
}else{
Toast.makeText(MainActivity.this, "imagem não encotrada",Toast.LENGTH_SHORT).show();
}
}
}
What’s the matter with your code?
– ramaral
the problem is that I can’t find the error, because it doesn’t display the image along with the text. if the text is the dog is silly was to appear the image of the dog, but does not appear,
– sender rodrigues dos santos
Instead of
texto.equals("O CACHORRO E BOBO")
usetexto.getText().toString().equals("O CACHORRO E BOBO")
– ramaral
opa, thank you very much, also worked!
– sender rodrigues dos santos