I have a clickable TextView with a drawable as its background. I'm looking for the right way to implement a down-state drawable so the background changes when the user presses down. I have been able to almost perfectly replicate this behavior by listening for MotionEvents and changing the background when the user is pressing down.. The only thing that doesn't match up with system-wide down state behavior here is that if the user keeps holding down and moves their finger off the button, the state remains down until they release their finger from the screen... whereas in Maps overlays or Buttons or ListViews or anything else the state immediately goes back to off when the finger moves from the object.
I know there must be some proper way to go about doing this... A nudge in the right direction would be great!
Thanks,
Nick
You need to use a Selector for the background. I'm not sure if there is an official example of how to use Selectors, but I found this site that seems to have a pretty good example:
http://www.craiget.com/2009/03/restyling-android-imagebuttons/
That should get you on the right track.
Edit: Apparently that first link is down, until/unless it comes back here is another example: http://www.mkyong.com/android/android-imagebutton-selector-example/
Related
Good day to all!
I'm trying to implement a voice over for Android (like iPhone), but a specific application (not the entire operating system)
Imagine a screen with six buttons, so they occupy the entire activity, distributed equally in size.
When I "walk" with my finger on the screen, I want to give focus to the button and capture the event when the button has focus and let the focus as well.
Conclusion: As I flick on the screen and if it is over a button, the focus button. if I continue to drag the finger, give the focus to another button without taking your finger off the screen.
Can anyone help me? Sorry for bad English.
I don't think you can use the Android Button class for this, but instead do a custom view, draw six rectangles, and write an onTouchEvent method that determines what sound to play based on where the user's finger is. See the Sudokuv4 example at http://pragprog.com/book/eband3/hello-android for some code you can use.
Well you have to know positions of buttons. You can use basic view functions to get positions (getLeft(), and so on...)
After that you have to implment onTouchListner for Activity. Within you have to check where Event.x and Event.y pointers are and set foucs to specified view. After pointers move from specified view you set focus to false.
I'm new on game dev for Android.
I have a game where I need to click on moving Buttons or TextViews(not important).
I extended FrameLayout class and added some Buttons(through addView method). Then I tried to use TranslateAnimation, but it seems it doesn't updates coordinates for click event (i.e. when I click on the moving button on new position, the event is not handling, but when I click on the origin place(where it has started moving), the event catches even if the button left this place).
Question: How to create a moveable label(or button) that handles click events? Do I need to use tricks like hit testing? Or, may be I use completely wrong approach for games(e.g. I need to draw text instead of adding the views in layout)? I will be happy if you can suggest another solution.
This is limitation of the Animation in Android. They fixed that in Android 3.0. Read here for more information http://android-developers.blogspot.com/2011/02/animation-in-honeycomb.html
An excerpt:
"Finally, the previous animations changed the visual appearance of the target objects... but they didn't actually change the objects themselves. You may have run into this problem. Let's say you want to move a Button from one side of the screen to the other. You can use a TranslateAnimation to do so, and the button will happily glide along to the other side of the screen. And when the animation is done, it will gladly snap back into its original location. So you find the setFillAfter(true) method on Animation and try it again. This time the button stays in place at the location to which it was animated. And you can verify that by clicking on it - Hey! How come the button isn't clicking? The problem is that the animation changes where the button is drawn, but not where the button physically exists within the container. If you want to click on the button, you'll have to click the location that it used to live in. Or, as a more effective solution (and one just a tad more useful to your users), you'll have to write your code to actually change the location of the button in the layout when the animation finishes."
following link has some button at the top like :: "DISEASES" , "FOOD & NUTRITION" , "PHYSICAL ACTIVITY"
http://www.bam.gov/sub_yourbody/yourbody_smilestyle.html
when you focus it it will grow and remove from it it will small
so question is how can i do it?
What you have to do is set selector for your button as shown in my previous answer.
And depending on your requirement set drawable larger than the normal ones for that particular state , that will create the same effect
You have to understand that there's a huge difference between a mouse as a pointing device and your finger. A mouse can easily hover across these buttons to give you the effect, but it doesn't work as well with a finger, as you'd need to slide your finger across the buttons to get the effect - and who slides their finger across buttons instead of just pressing them?
Anyway, I suppose you could create an OnTouchListener for your Buttons, and perhaps throw in some animations to get the effects.
But - like I said - I don't see a reason to do this. It seems utterly unnecessary.
I have a Gallery that I've set up with this type of thing. Works great, except on initial load. The initial gallery item isn't "selected" according to the drawable. Sliding the gallery back and forth makes it work fine and going back to the first has it selected correctly. Any ideas?
What do you mean by "selected"? Gallery only draws a "selector" indicator if the view is focused, which usually only happens when you're using non-touch input (e.g., arrow keys, jogball, etc). If you touch/swipe to move around in it, you enter "touch mode", which usually does not draw a "selector". The same is true of ListView. Read this for more information on what Touch Mode is, and why it's important to understand the difference.
I ended up using this as a guide. I didn't get exactly what I wanted, but this was useful.
I am trying to make an application for android that is based on IPAD app. I want to keep the same look to the app, but I am not a very good designer. So I was wondering if it was possible to for me to crop say the entire header and then have an invisible button or something on top of portions of it. so the design would become a little bit easier. Also if that is possible how would I get the what ever the event item is going to be to overlap with a portion of the image if it has stranger orientation. (I think I could fake this by just moving it closest orientation and extending the size untill it fits, but it would be nice to find a better way.)
How would I go about that?
I tried making the button invisible, but that did not seem to work.
Thanks in advance
I got lost on your description. But if you want to make a button invisible, calling setVisibility(View.INVISIBLE); will not work, because it will not receive click events. You may want to set its color to transparent instead. try using:
button.setBackgroundColor(Color.TRANSPARENT);