Breaking News

Blogger Tips and TricksLatest Tips And TricksBlogger Tricks

How to set Image Button pressed state background in Android Studio



How to set ImageButton pressed state background in Android Studio


activity_main.xml

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/rl"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="10dp"
    tools:context=".MainActivity"
    android:background="#f6f5f9"
    >
    <!-- ImageButton with default click effect/pressed state -->
    <ImageButton
        android:id="@+id/ib"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/transparent_image_button"
        />
    <!--
        ImageButton with custom pressed state.
        ImageButton selector.
        ImageButton click effect.
        A transparent png image used for ImageButton.
    -->
    <ImageButton
        android:id="@+id/ib2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/transparent_image_button"
        android:layout_toRightOf="@id/ib"
        android:background="@drawable/imageview_selector"
        android:padding="5dp"
        />
</RelativeLayout>

res/drawable/imageview_selector.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <!-- disabled state-->
    <item
        android:state_enabled="false"
        android:drawable="@android:color/holo_red_light"
        />
    <!-- pressed state / clicked state -->
    <item
        android:state_pressed="true"
        android:state_enabled="true"
        android:drawable="@android:color/holo_red_dark"
        />
    <!-- focused state -->
    <item
        android:state_focused="true"
        android:state_enabled="true"
        android:drawable="@android:color/holo_orange_dark"
        />
    <!-- enabled state / default state-->
    <item
        android:state_enabled="true"
        android:drawable="@android:color/holo_blue_light"
        />
</selector>







No comments