If you are looking to change the image or color of the widget when it is clicked or focused, Here is the code snippet that explains it:-
Step 1:- set the widget in an XML file for which you want to applying selector:-
<ImageView
android:id="@+id/toolbar_home"
android:layout_width="30dp"
android:layout_height="30dp"
android:src="@drawable/selector_home" >
</ImageView>
Step 2:- create an XML file under drawable folder as:-
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:state_pressed="true"
android:drawable="@drawable/greenhome" />
<item
android:state_focused="true"
android:drawable="@drawable/homwhite"/>
<item
android:drawable="@drawable/home" />
</selector>
Step 3:- set on click listner in your main activity:-
ImageView homeIcon=(ImageView) findViewById(R.id.toolbar&#95;home);
homeIcon.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View view)
{
Toast.makeText(getApplicationContext(), "Home Clicked", Toast.LENGTH&#95;SHORT).show();
}
});
0 Comment(s)