Android getting touched button's text with onTouch()?

The onTouch method has a View parameter which is a reference to the touched view. Cast this to Button to get its caption: String caption=((Button)v).getText() By the way, why are you using onTouchListener? If you only want to detect clicks, use onClickListener it's simpler (and has the same argument) Using tags instead of the caption could be another enhancement: you can add tags to views with setTag (it accepts an optional key too) and retrieve them by getTag.

You could tag your buttons with your sound sample numbers for example.

The onTouch method has a View parameter which is a reference to the touched view. Cast this to Button to get its caption: String caption=((Button)v).getText(); By the way, why are you using onTouchListener? If you only want to detect clicks, use onClickListener, it's simpler (and has the same argument).

Using tags instead of the caption could be another enhancement: you can add tags to views with setTag (it accepts an optional key too) and retrieve them by getTag. You could tag your buttons with your sound sample numbers for example.

I'm using onTouch because when you're tapping for beats and such, you don't time it with your finger being lifted. There's a slight delay sometimes, and it just feels off. Also, can I set the tags in the XML files, so I don't have to worry about making the java code messy?

I'm assuming it'd be android:tag="1" etc? – Henzl0l Feb 12 at 20:26.

I'm trying to make something like a drum machine that plays sounds when you press the buttons. However, I'm having trouble making it change sounds for each button, since one onTouch method handles all the events. I can't seem to get the button's text (Which is right now, '1', '2', etc.) using v, so I'm pretty much stumped.

I've only been playing with android for two days now. Sorry if nothing makes sense hehe.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions