Android Question states.State_Hovered

Martin Domian

Member
Licensed User
Longtime User
Hi.

I want the buttons to behave the same as DefaultDrawable, but with my colors:

Then the hover effect disappears. I move the mouse over it and nothing happens.
State_Hovered is supported by Android, but is not available in B4A. But since it works with the default setting, there must be an easy way.

here is the way, which is not working.

Thanks for any ideas

Martin


 

drgottjr

Expert
Licensed User
Longtime User
there is no hovered state for a button. there is one for a view (which is what you were probably referring to).
to get that to work with a button, you need a listener. but not an ontouch listener. a genericmotion listener is needed.
that's a little trickier than a standard listener. i'm looking into that. if someone else beats me to it, no problem.
otherwise, i'll post as soon as i can get it running.
 
Upvote 0

Martin Domian

Member
Licensed User
Longtime User
Thanks. I do not understand why so tricky.
I set the Button View to Standard, then it works.
This app is for TV Box which is used my mouse
 
Upvote 0

drgottjr

Expert
Licensed User
Longtime User
now you mention a mouse. i have a listener but i did not know you had an external input device (an android smartphone has no way of know where your finger is unless you touch the screen). i do not have a tv box, so i cannot test my listener. i will see if an otc mouse works.
 
Upvote 0

drgottjr

Expert
Licensed User
Longtime User
yes, an otc mouse on an android smartphone will respond to onhover. add this to your project:
B4X:
#if Java

import android.view.View;
import android.graphics.Color;
import android.view.MotionEvent;
import android.view.InputDevice;


    public void makeHoverable( View v, int hoveredColor, int normalColor ) {
        v.setOnGenericMotionListener(new View.OnGenericMotionListener() {
            @Override
            public boolean onGenericMotion(View v, MotionEvent event) {
                if (event.getSource() == InputDevice.SOURCE_MOUSE) {
                    switch (event.getAction()) {
                        case MotionEvent.ACTION_HOVER_ENTER:
                            BA.Log("enter");
                            v.setBackgroundColor(hoveredColor);
                            return true;
                        case MotionEvent.ACTION_HOVER_EXIT:
                            BA.Log("exit");
                            v.setBackgroundColor(normalColor);
                            return true;
                        case MotionEvent.ACTION_HOVER_MOVE:
                            // Handle hover move
                            BA.Log("moving...");
                            return true;
                    }
                }
                return false;
            }
        });
    }


in main, add this to oncreate:
B4X:
    Dim jo As JavaObject
    jo.InitializeContext
    jo.RunMethod("makeHoverable",Array( button1, hoveredColor, normalColor))

or you could stick it in your CreateStateDrawable(normalColor As Int, pressedColor As Int, focusedColor As Int, hoveredColor As Int) As StateListDrawable sub.
it doesn't have any adverse effect on your statedrawables. it has nothing to do with them.

i would post screen captures to show it working, but the screen photo tool doesn't work with b4a bridge, and the mouse is plugged into my usb port. i can only tell you it works with a mouse on my phone.
 
Last edited:
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…