0

I want to use on hover functionality in Android. But I am new to Android so I am not able to use it.

Below is my main.xml file.

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:padding="5dip" >

    <!-- Top Company Logo Starts -->

    <ImageView
        android:id="@+id/imgLogo"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_marginRight="5dip"
        android:padding="3dip"
        android:src="@drawable/logo_demo" />

    <!-- Top Company Logo Starts -->

    <!-- Flight Search Box Starts-->
    <RelativeLayout
        android:id="@+id/flight_relative"
        android:layout_width="match_parent"
        android:layout_height="70dp"
        android:layout_below="@+id/imgLogo"
        android:layout_marginTop="5dp"
        android:background="@drawable/gray_bg"
        android:gravity="center_vertical" >

        <ImageView
            android:id="@+id/flight_list_image"
            android:layout_width="50dip"
            android:layout_height="50dip"
            android:layout_alignParentLeft="true"
            android:layout_marginRight="5dip"
            android:padding="3dip"
            android:src="@drawable/ic_launcher" />

        <TextView
            android:id="@+id/flight_content"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignTop="@+id/flight_list_image"
            android:layout_marginRight="5dp"
            android:layout_marginTop="10dp"
            android:layout_toLeftOf="@+id/flight_arrow"
            android:layout_toRightOf="@+id/flight_list_image"
            android:text="@string/flight_tittle"
            android:textColor="#152b72"
            android:textStyle="bold"
            android:textSize="15dp"
             />

        <TextView
            android:id="@+id/content_flight"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/flight_content"
            android:layout_marginRight="5dp"
            android:layout_toLeftOf="@+id/flight_arrow"
            android:layout_toRightOf="@+id/flight_list_image"
            android:text="@string/flight_content"
            android:textColor="#2f2f2f"
            android:textSize="10dp" />

        <ImageView
            android:id="@+id/flight_arrow"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignBottom="@+id/flight_content"
            android:layout_alignParentRight="true"
            android:src="@drawable/arrow" />


    <!-- Flight Search Box Ends -->
     </RelativeLayout>
    <!-- Approval Box Ends -->
</RelativeLayout>

Now what I want to do when the user hovers on the inner relative layout is to change the color of the RelativeLayout and the TextView. So can please suggest me how could I implement this?

gunar
  • 14,660
  • 7
  • 56
  • 87
  • 2
    I'm not sure about the onHover method in android, because you can't really hover a finger over a UI element, however, I guess you could use a stylus on tablets... That said, Selectors may be the way to go. Take a look at: http://www.mkyong.com/android/android-imagebutton-selector-example/ - May lead you in the right direction :) HTH – laminatefish Jul 11 '13 at 12:59
  • please take a look at my answer http://stackoverflow.com/questions/14023886/android-button-selector/14024007#14024007 you can use that code to relative layout or any other view instead of button. – Chintan Rathod Jul 11 '13 at 13:14
  • @ChintanRathod so how i change the color of my text..the layout background will get change but how we can change the text color –  Jul 12 '13 at 06:01
  • @ChintanRathod please help me on this issue also how to change the textcolor on hover of text view –  Jul 12 '13 at 06:32
  • @Rahul, you need to create another drawable file in which there are other then selector of list view. You need to also assign style to text view also when selected, focused or normal state. – Chintan Rathod Jul 12 '13 at 06:38
  • It's not working for me i am posting a new question Please comment on that http://stackoverflow.com/questions/17609316/on-hover-not-working-properly-in-android –  Jul 12 '13 at 07:06

3 Answers3

0

you can do this by using selector

<item android:drawable="@drawable/bg_selected" android:state_selected="true"></item>
<item android:drawable="@drawable/bg_hover" android:state_hovered="true"></item>
<item android:drawable="@drawable/bg_normal"></item>

use android:state_hovered property

Shree
  • 787
  • 8
  • 23
0

You already have an accepted answer to your question , still I would like to share that onhover means the mouse is over something and which is one of these states :

  • pressed
  • selected
  • focused

And if you want to simulate a focused or hover state then you should use a phone with touch pad or mouse pointer like some phones have. Touch as well as keypad.

Prateek
  • 3,923
  • 6
  • 41
  • 79
0

You can use a ColorStateList to (for example) change the color of the text of a button on hover.

Android documentation has a pretty clear example: https://developer.android.com/guide/topics/resources/color-list-resource

While many Android devices are touchscreen, so a hover state may not make a lot of sense, something like this may be very handy with Android TV.

gcdev
  • 1,406
  • 3
  • 17
  • 30