Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • How to set selector in android

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 574
    Comment on it

    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&amp;#95;home);
       homeIcon.setOnClickListener(new View.OnClickListener() 
          {
           @Override
            public void onClick(View view)
             {
               Toast.makeText(getApplicationContext(), "Home Clicked", Toast.LENGTH&amp;#95;SHORT).show();
             }
            });
    

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: