Open top menu

How to Implement selector ImageButton In android?


In this blog explain how to implemet selector ImageButton in android. you can change easily image background color onclick imageButton via selector xml class. try this code

Stepe 1. first ,you can create Button selector xml class inside res/drawable folder...

button_selector.xml
...................................................................................................................................................................

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@drawable/button_pressed_yellow"
          android:state_pressed="true" />
    <item android:drawable="@drawable/button_focused_orange"
          android:state_focused="true" />
    <item android:drawable="@drawable/button_normal_green" />
</selector>




Stepe 2 . create activity_main.xml class inside the res/layout folder...

activity_main.xml .
.................................................................................................................................................................
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Androidbeginner point" 
        android:textSize="25dp"
        android:layout_gravity="center"
        android:layout_marginTop="20dp"/>

    <Button
        android:id="@+id/ButtonSelector"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/button_selector"
        android:layout_marginTop="30dp" 
        android:layout_gravity="center"/>
    
</LinearLayout>


Stepe 3.create main java class inside src folder...

MainActivity.java.
................................................................................................................................................................

public class MainActivity extends Activity {

Button imageBtn;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

setContentView(R.layout.main);

imageBtn = (Button) findViewById(R.id.ButtonSelector);
imageBtn.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View arg0) {
Toast.makeText(MainActivity.this,"good Looking",Toast.LENGTH_SHORT).show();
}

});
}

}




1. Normal buuton ...

2. when ,you click on ImageButton change the button state...
try this code...
button_focused_orange image
 
button_pressed_yellow

button_normal_green

0 comments