Reputation: 295
I'm just a beginner with Android. I want the button to change its color on mouseover.
I don't know how to do that in android. Can it be done?
View for a button:
<Button
android:id="@+id/b8"
android:text="click me"
style="?android:attr/buttonStyleSmall"
android:textSize="20dp" />
Upvotes: 9
Views: 66590
Reputation: 1835
For anyone looking to use this in Compose, you will now use a MutableInteractionSource
, for example:
@Composable
fun MyButton() {
val interactionSource = remember { MutableInteractionSource() }
val isHovered by interactionSource.collectIsHoveredAsState()
val isPressed by interactionSource.collectIsPressedAsState()
val isFocused by interactionSource.collectIsFocusedAsState()
Button (
onClick = { },
interactionSource = interactionSource,
modifier = Modifier.background(
if (isPressed) Color.DarkGray else if (isHovered) Color.LightGray else Color.Gray
),
colors = ButtonDefaults.buttonColors(
containerColor = Color.Transparent, // Set transparent to avoid default color
contentColor = Color.White
)
) {
Text("My Button")
}
}
See also: How to change button background color on click?
Upvotes: 0
Reputation: 105
Here is xml-- hover.xml --
<item android:drawable="@drawable/image__hover" android:state_focused="false" android:state_pressed="true"/>
<item android:drawable="@drawable/normalimage"/>
and how i use it in Button for color change on hover
<Button
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/hover"
></Button>
Upvotes: 2
Reputation: 48686
You need to use what's called a selector
.
You can read about them and get a tutorial from this site.
Keep in mind that there really isn't a concept in Android as "hover" since you can't hover your finger over the display. But you can create selectors for, say, when a button has focus. Normally a button can have three states: Normal, Focused and Pressed.
Upvotes: 7
Reputation: 1
Hovering is possible on Android: all devices that have a track ball or D-pad or QWERTY keyboard arrow keys can move the "hover" or focus to anything that is pressable (clickable). Then, on my G1 for example, you press the track ball to press it down into the android:state_pressed
state.
Upvotes: 0
Reputation: 209
The particular mouse hover functionality as we know it, is supported on Android OS 4.0 and above. Views have onHoverListeners(). Isnt it finally great
Upvotes: 4