lundi 11 janvier 2021

How to set text to corresponding images in Android Studio

On working on an android application in which I want to set an image in image view and by clicking the button I got the corresponding text from the string array. I already put my images in drawable folder enter image description here Array of string is :

private String[] captions =new String[] {
        /*0*/"man in white shirt and glasses is sitting at table",
        /*1*/"man in black shirt is playing the guitar",
        /*2*/"man in blue shirt is standing in front of water",
        /*3*/"two dogs are running through the snow",
        /*4*/"group of people are standing in front of crowd",
        /*5*/"two dogs are playing in the snow",
        /*6*/"group of people are sitting at the table",
        /*7*/"man in blue shirt is standing in front of building",
        /*8*/"man in blue shirt is standing in the water of the water",
        /*9*/"man in black shirt playing the guitar",
        /*10*/"man in black shirt and blue is sitting on the beach",
        /*11*/"group of people are standing in front of building",
        /*12*/"group of people are standing in the street",
        /*13*/"man in white shirt is holding his face",
        /*14*/"man in white shirt is cooking",
        /*15*/"two men are playing in the grass",
        /*16*/"man in black shirt and white is playing the guitar",
        
};

I am enable to use the if condition here. I am using view binding in this project that's why I access the button like binding.buttonid.

 binding.detectCaption.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            //binding.displayView.setText("two dogs are running through the snow");
            if (binding.imageView==R.drawable.one)//This line shows error{
                binding.displayView.setText(captions[0]);
            }
           

        }
    });

Please tell me how to do this.

Aucun commentaire:

Enregistrer un commentaire