Dan
Dan

Reputation: 63

Match sound files to imageview or image click

I'm new to android development and i was wondering if there is a way to retrieve the matching sound files depending on an image click. I have 6 images that are in random order so i want to play the appropriate sound depending on what image i clicked. Thanks!

Upvotes: 0

Views: 683

Answers (2)

Matt Clark
Matt Clark

Reputation: 28599

As Mentioned by @LeartS, you could set the tag property of the image:

img1.setTag("\path\to\audio.mp3");

or

android:tag="@string/some_file"

And retrieve it when needed

public void onClick(View v){
    play(img1.getTag().toString());
}

Another another way that you could do it would by using the IDs of the image views

Create a final ID

final int ID_IMG_APPLE = 0x0001;

Set the ID to the view

img1.setId(ID_IMG_APPLE);

Check the ID when a view is clicked:

public void onClick(View v){
    switch(v.getId()){
    case ID_IMG_APPLE:
        play("apple.mp3");
        break;
    }
}

Upvotes: 0

LeartS
LeartS

Reputation: 2896

You can set a tag for a view, both in xml and Java, containing "additional information". In this case you may want to set the related audio resource / file name.

Upvotes: 1

Related Questions