15 Ağustos 2011 Pazartesi

StateListDrawable kullanarak kendi checkbox'ınızı yaratın. Create your own checkboxes with using StateListDrawable.

Merhaba,

Üzerinde çalıştığım bir projede dinamik oluşturduğum checkboxların theme üzerinde gözükmemesi ancak checked edildikten sonra fark edilmesi problemiyle karşılaştım. Bu sorunu gidermek için theme'i değiştirebilirdim ancak bu pek işime gelmedi, bende statelere göre checkboxların render edildiği imageleri değiştirme yoluna gittim. Umarım işinize yarar.
(Last day I came across a problem while using checkbox object. After that i decided to create my own custom checkbox then this morning i did it and wanted to share with you guys. With the code as can be seen below you can create your own custom checkbox too. What you need is paying some attention to StateListDrawable concept. I hope it will be remarkable detail for you.)

CheckBox myCheckbox = new CheckBox(this);
StateListDrawable stateList = new StateListDrawable();
int statePressed = android.R.attr.state_pressed;
int stateChecked = android.R.attr.state_checked;
stateList.addState(new int[] {-stateChecked}, new BitmapDrawable(BitmapFactory.decodeResource(getResources(), R.drawable.unchecked)));
stateList.addState(new int[] {stateChecked}, new BitmapDrawable(BitmapFactory.decodeResource(getResources(), R.drawable.checked)));
stateList.addState(new int[] {statePressed}, new BitmapDrawable(BitmapFactory.decodeResource(getResources(), R.drawable.pressed)));
myCheckbox.setButtonDrawable(stateList);

İyi çalışmalar.

3 yorum:

  1. the pressed state is not working.

    does it respond to pressed at all or what is the problem?

    thank you very much for any answer :)

    YanıtlaSil
  2. Hi, could you share the codes with me? It runs correctly on mines.

    YanıtlaSil
  3. Hey,

    well, the same as yours, probably. But it never goes into "pressed" state ever, only "checked" and "unchecked". Can you tell me when it appears "pressed" for you? On touch?

    Thank you very much again... :)

    YanıtlaSil