Change of button look on touch, not just on click - android

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....

Related

How do I set a button as always pressed? (Android)

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);

Android Button performClick

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...

how to stay the button pressed

I want to stay the button pressed when clicked on that button. Button's background is done using StateList
You should consider using a ToggleButton for this kind of behaviour: http://developer.android.com/reference/android/widget/ToggleButton.html
Maybe have a look at/extend the CheckBox source code and use that as a starting point?

How to dynamically change a pressed button background color?

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);

Android: How to do create custom EditText with a clickable arrow on the right

I would like to have an EditText with one modification: on the right but still inside the EditText there should an arrow pointing downwards that I can set OnClickListener to so that when the user clicks on the arrow it displays a menu.
What is the best way to do this?
Do you mean something like this ?
see image
Add the arrow by setting the drawable right attribute
android:drawableRight="#drawable/right"
to your EditText. Then you would need to set an OnTouchListener to get the events.
I did this by putting EditText and a Button into RelativeLayout, the Button (which has custom background drawable) is overlapping the EditBox.
When user clicks on it, the EditBox doesn't receive the click event.
Sounds like a combo box. If you look at the "Building Custom Components" section of the Dev Guide, they mention combo box briefly, but give details on how to build any custom component.

Categories

Resources