I have a button that plays a sound clip when it is pressed. I would like the button to appear pressed during the duration of the sound clip. By that, I mean that I would like the button to take on the default pressed-button appearance while the sound is playing. How can I implement this? I have tried using a number of things in the onClickListener (such as setSelected, requestFocus, etc), but none of those do the trick. I have also tried changing the onClickListener to an onTouchListener, again with no dice. Am I wrong in assuming that there must be a way to simply set the button image to appear pressed? (BTW, the button object is of type Button, not ImageButton).
Thanks for any advice!
Please see this question. It details a couple of different ways this can be done.
Lookup 'selector' drawables and let android take care of this for you.
Related
So I'm following an Android tutorial and came across an issue. The video maker uses setEnabled(false) to hide a TextView until the user clicks a certain button. However, when I tried the same code, the TextView was on the screen before the user clicked the button. I've been trying to work out why for an hour, but to no avail. Below is a link to the video and a picture of my relevant code, XML code, and screen display.
Video: https://www.youtube.com/watch?v=NGRV2qY9ZiU Talks about setEnabled at 16:35
However, when I tried the same code, the TextView was on the screen before the user clicked the button.
setEnabled(false) won't actually hide the TextView. To do that you need to do
result.setVisibility(View.GONE);
When you are ready to make it visible (instead of setEnabled(true)):
result.setVisibility(View.VISIBLE);
try
result.setVisibility(View.GONE);
instead.
Edit:
note that:
result.setVisibility(View.INVISIBLE);
would also hide the view but it would still be clickable.
Use the below method. It will hide the element from the view.
result.setVisibility(View.GONE);
I want to add custom audible feedback to a button press (various click sounds encoded as *.ogg) I've done this by using the RingtoneManager to create Ringtones for each of the clicks and then .play() them in the onClick() method. This works but seems a little sluggish. This leaves me wondering if there is a better way to attach a custom sound effect to a button press. I've scanned the Button reference page and all I found was playSoundEffect() which seems to handle only system defined sounds.
thanks,
hank
Use a SoundPool- they work it preloads the data into memory (so no file reads which cause sluggishness). Ringtones are definitely not what you want to be using here- those are typically much longer and not time critical (delaying a ringtone by a second or so isn't a problem, whereas delaying a button sound effect by that long is).
I don't know how exactly this type of buttons is called that's why I can't even google about it.
You use that button to unlock phone - tap on it and move from left to right.
I'd like to add same button in my app. How to do it?
You should use SeekBar to achieve what you want. http://developer.android.com/reference/android/widget/SeekBar.html
Here is a good example
http://www.techrepublic.com/blog/app-builder/androids-seekbar-your-way/943
Try this for a library
https://github.com/sitepoint-editors/SwipeButtonExample
and this for a DIY
https://rightclicksolutions.wordpress.com/2014/04/09/android-slide-to-unlock-like-ios-mb-slider-slider/
You can overide onTouch() and change the location of the button acording to the x position. and when you lift the finger you can overide onUp() (im not sure thats the name of the method) and create an animation that will lead the button back to its place.
From your question I get that you might think its a button you can simply add from a list of buttons, well, its not. you need to manualy create it.
I want to set a button to be invisible because there will be a picture in front of it to act like the button. Do you guys know how to set this up in the xml?
thanks in advance.
Maybe the best thing you can do is to use directly an ImageView instead of a Button, setting always a OnClickListener on it in order to catch click action. Maybe you can also add a proper selector in order to get a visual feedback on the pressed/unpressed state.
I have an app widget which runs neatly. However, I am unable to highlight a click on a linked item. I've seen it in the standard app widgets like 'Music' and 'Power Control', for instance. Moreover, I've also been studying the Music app widget's source at album_appwidget.xml. The only thing I could think of is the LinearLayout defined at lines 23-35 which states
android:clickable="true"
Unfortunately, this does not work for me. So does anyone have a hint on how to highlight a click on an app widget? I've tried the LinearLayout, TextView and Button. None of them displayed a border as a highlight.
Thanks in advance,
Steff
you need to create images for those states like focussed state, pressed etc like in a button and define them in your background.
Try looking at the custom buttons where its explained how to accomplish the task thats similar to your needs.
http://www.gersic.com/blog.php?id=56.
if you want to look more and add more states you may ge better idea if you look at the android source code for buttons where they have images for each state of the button and every other widget.