Make the text appear when clicking the button and the image specifies it?

Asked

Viewed 1,155 times

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?

  • 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,

  • Instead of texto.equals("O CACHORRO E BOBO") use texto.getText().toString().equals("O CACHORRO E BOBO")

  • opa, thank you very much, also worked!

1 answer

2

The most direct solution for what you want is to create an id array with the ids of each image and that correspond to the texts in the array frases:

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", 
};

private int[] drawables = {
        R.drawable.cao,
        R.drawable.gato,
        R.drawable.baleia,
        R.drawable.rato,
        R.drawable.mamae_sopa,
        R.drawable.buzina,
        R.drawable.papai,
        R.drawable.mamae_linda,
};

Then just click on define the phrase and image based on the same random number drawn:

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]);

        //aplicar a imagem correspondente
        imagem.setImageResource(drawables[numeroAleatorio]);
    }
});

If you want to give the possibility of not having images for all texts, you can start the ones you don’t have with -1 and change the click code to:

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]);

        //agora testa se tem a imagem correspondente antes de aplicar    
        if (drawables[numeroAleatorio] != -1){
            imagem.setImageResource(drawables[numeroAleatorio]);
        }
        else {
            Toast.makeText(MainActivity.this, "imagem não encotrada",Toast.LENGTH_SHORT).show();
        }

    }
});

Organizing with a class

If you start to have several separate pieces of information for each element it is best to create a class that groups this information:

public class Imagem {
    private String texto;
    private int drawable;

    public Imagem(String texto, int drawable){
        this.texto = texto;
        this.drawable = drawable;
    }

    public String getTexto(){
        return texto;
    }

    public int getDrawable(){
        return drawable;
    }
}

Then instead of building the 2 arrays that were exemplified above, I would build an array of these objects:

private Imagem[] imagens = {
    new Imagem("O CACHORRO E BOBO", R.drawable.cao),
    new Imagem("O GATO E MIMADO", R.drawable.gato),
    new Imagem("A BALEIA É ROSA", R.drawable.baleia),
    new Imagem("A MAMÃE FEZ UMA SOPA", -1),
    new Imagem("A BUZINA É DO CAMINHÃO", R.drawable.buzina),
    new Imagem("O PAPAI É MUITO BONITO", R.drawable.papai),
    new Imagem("A BUZINA É DO CAMINHÃO", -1),
    new Imagem("A MAMÃE É MUITO LINDA", R.drawable.mamae)
};

Now the click obtains the element drawn and from this element extracts all the information:

botaoIniciar.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
        Random numeroRandomico = new Random();
        int numeroAleatorio = numeroRandomico.nextInt(frases.length);
        Imagem escolhida = imagens[numeroAleatorio];

        texto.setText(escolhida.getTexto());

        //agora testa se tem a imagem correspondente antes de aplicar    
        if (escolhida.getDrawable() != -1){
            imagem.setImageResource(escolhida.getDrawable());
        }
        else {
            Toast.makeText(MainActivity.this, "imagem não encotrada",Toast.LENGTH_SHORT).show();
        }

    }
});
  • THANK YOU FOR YOUR INFORMATION! YOU HAVE HELPED ME SO MUCH!!!

  • @senderrodriguesdossantos No problem. I’m glad I helped

  • Isac, ask me one more question, in case you said to work with a class to group this information and then create an array of objects. could exemplify something like?

  • @senderrodriguesdossantos I already edited the answer to contemplate this example.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.