In order to fudge multitouch buttons, I set a massive invisible imageView over top of everything. The I just poll for where it was touched and call performClick on the button in that area under it. Now my problem is that performClick only calls the onClick method, and doesn't actually perform a legitimate button press, so there's no animation (color change etc). I have a custom xml for the buttons, and it worked fine without the imageView. I try using setPressed and setEnabled, but the png never changes, and the button looks static. What am I missing?
How about tying a Boolean to each button. All the button logic does is flip flop the variable and maybe change an indicator (button color or text) to pushed/not pushed. Shouldn't be more than 3 or 4 lines of code executed per button press. Fire the heavy code when a 'do it' button is pressed. That would have to be faster than computing which button was pressed under the overlay...
Related
I have a bar in my app with 2 buttons side by side. One button represents the page the user is currently on and the other redirects the user to another page with the same button bar. What I want is for the button that represents the page the user is currently on to look like it is pressed (i.e. the color it would be if it is pressed). I already have it set so it can't be clicked. Is there a setting or attribute or do I HAVE to create my own selector (because I don't really understand how to implement that)?
Have never tried this but
button.setPressed(true)
should work.
Edit: You can also add
button.setClickable(false)
Edit: This does work but the order is important. Use this
btn.setClickable(false);
btn.setPressed(true);
I have an onclick listener for a button. Is there any way when I click the button, make the text change color until my new activity is brought up?
You can use setTextColor() on a button, too, as it extends a TextView (you can Ctrl+F on that page for "setTextColor()" to find the reference, it's under "Inherited XML Attributes"). So when the button is pressed you would use button.setTextColor(newColorInt); and when the loading is done button.setTextColor(oldColorInt);
If you register an OnClickListener and "click" a button, it gets blue and the font white (at least in my case). But if I register an OnTouchListener that's not the case. How do I change that? I need the OnTouch because I want to send signals as long as the Button is pressed.
I want every button to look like this: pressed button as it should look like.
But the ones for which I work with an OnTouchListener instead of an OnClickListener it looks like this at the moment (done programmaticaly, because otherwise it would look like an unpressed button): ugly look of pressed button.
But this is ugly.
How can I apply exactly the standard look of a pressed button programmaticaly?
The code to make an ugly button:
view.getBackground().setColorFilter(Color.BLUE, android.graphics.PorterDuff.Mode.MULTIPLY);
view.invalidate();
I want every button to look like this:
pressed button as it should look like.
But the ones for which I work with an OnTouchListener instead of an OnClickListener it looks like this at the moment (done programmaticaly, because otherwise it would look like an unpressed button):
ugly look of pressed button.
But this is ugly.
How can I apply exactly the standard look of a pressed button programmaticaly?
The code to make an ugly button:
view.getBackground().setColorFilter(Color.BLUE, android.graphics.PorterDuff.Mode.MULTIPLY);
view.invalidate();
you should think of using selectors for the button...i hope i understood your question....
I'm changing the background drawable for my buttons when they're clicked (as they trigger functions such as play/record). While playing/recording, the button glows (a different 9-patch is set as the background) and turns into a stop button. My problem is that currently I'm using:
b.setBackgroundResource(R.drawable.btn_default_normal);
to set the background back again afterwards. This works, but the normal behaviour when I use setEnabled(false) is lost. After a button has been used, and reset to normal, it retains the normal background (rather than the dimmed one) when disabled. The text still changes colour though. Is there a way of 'resetting' the background of the button to default, so that it retains its normal behaviour?
Ok, I got the answer from this blog post in the end. What you have to do is create an xml document with all the different button states, and assign that rather than just an image to the background.
I'm developing a question game and I want to change the answer button pressed background color to green if the answer is correct or to red if the answer is wrong just in the moment the user press the button.
Actually I have a custom_button.xml which I assign to the buttons in the layout:
<Button
android:id="#+id/la"
android:width="63dp"
android:height="65dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/la"
android:tag="#string/la"
android:layout_toRightOf="#+id/fa"
**android:background="#drawable/custom_button"**
android:layout_margin="3dp"
/>
Is there a way to change the pressed background of a button just in the moment the user is pressing the button?
I tried using setBackgroundDrawable() inside the button OnClickListener but this change the button behaviour for the next time the user click the button, not the actual.
bt.setBackgroundDrawable(getResources().getDrawable(R.drawable.custom_button_fail));
thanks in advance!
I tried using setBackgroundDrawable() inside the button OnClickListener but this change the button behaviour for the next time the user click the button, not the actual.
That's because the onClick method is called after the button is pressed. Your best choice here is:
Create two different drawables for your buttons. 1st for a normal button with normal background when it's not pressed and green background when pressed. 2nd for a normal button with normal background when it's not pressed and red background when pressed.
On onCreate assign the correct background to the buttons depending on whether the answer would be correct or not.
By the way, there is a shorter way to do so:
bt.setBackgroundResource(R.drawable.custom_button);