Wednesday, April 14, 2010

How-to: Android Favorite Button (the right way, this time)

My last blog post is about how to make a favorite button in an Android application. It is wrong, those drawables should not be used independently. Instead, the @android:drawable/btn_star resource should be used, as it is a state-drawable and contains all the possible states (checked, unchecked, checked and focused, checked and clicked, ...). The proper way to use this is:

<CheckBox android:layout_width="wrap_content" android:layout_height="wrap_content" android:button="@android:drawable/btn_star"/>

The result looks exactly like the button used in Android's Contacts application when viewing a contact (upper-right corner).

Man, that took some digging.

12 comments:

  1. Thank you. Exactly what I've been looking for - I've figured out the whole state thing but I've been setting android:background instead of android:button and ended up with 2 buttons instead. Now it all works well.

    ReplyDelete
  2. haha me too artem, thanks felix

    ReplyDelete
  3. Amazing! Just what I need! :)

    ReplyDelete
  4. This worked perfectly to change the checkbox to a different android image. But if you want to change the default checkbox to a custom image:

    Copy the btn_check.xml from android-sdk/platforms/android-#/data/res/drawable to your project's drawable folder and change the 'on' and 'off' images to your custom ones.

    The last change is then android:button="@drawable/btn_check" rather than android:button="@android:drawable/btn_check"

    ReplyDelete
  5. Thank you

    Regards,
    Vivek Kumar

    ReplyDelete
  6. thank you felix. this is awesome. i just removed 20 lines of messy code thanks to you.

    ReplyDelete
  7. As claimed by Stanford Medical, It is indeed the SINGLE reason women in this country get to live 10 years longer and weigh an average of 42 pounds lighter than we do.

    (And actually, it really has NOTHING to do with genetics or some hard exercise and really, EVERYTHING about "HOW" they are eating.)

    P.S, What I said is "HOW", not "what"...

    TAP on this link to discover if this easy test can help you decipher your true weight loss potential

    ReplyDelete
  8. Your Affiliate Profit Machine is ready -

    And making money online using it is as easy as 1...2...3!

    Here is how it works...

    STEP 1. Choose which affiliate products the system will advertise
    STEP 2. Add some push button traffic (it ONLY takes 2 minutes)
    STEP 3. See how the system explode your list and sell your affiliate products all for you!

    Are you ready to make money ONLINE?

    Get the full details here

    ReplyDelete
  9. Compre documentos en línea, documentos originales y registrados.
    Acerca de Permisodeespana, algunos dicen que somos los solucionadores de problemas, mientras que otros se refieren a nosotros como vendedores de soluciones. Contamos con cientos de clientes satisfechos a nivel mundial. Hacemos documentos falsos autorizados y aprobados como Permiso de Residencia Español, DNI, Pasaporte Español y Licencia de Conducir Española. Somos los fabricantes y proveedores de primer nivel de estos documentos, reconocidos a nivel mundial.

    Comprar permiso de residencia,
    permiso de residenciareal y falso en línea,
    Compre licencia de conducir en línea,
    Compre una licencia de conducir española falsa en línea,
    Comprar tarjeta de identificación,
    Licencia de conducir real y falsa,
    Compre pasaporte real en línea,

    Visit Here fpr more information. :- https://permisodeespana.com/licencia-de-conducir-espanola/
    Address: 56 Guild Street, London, EC4A 3WU (UK)
    Email: contact@permisodeespana.com
    WhatsApp: +443455280186

    ReplyDelete