Problem with setText() on custom CheckBox

by mobilek...@googlemail.com » Thu, 05 Mar 2009 07:13:29 GMT


Sponsored Links
 Hi,

I've implemented a custom CheckBox view and faced a probelm with
adding text to it.

Here's my implementation:

public class MyCheckBox extends CheckBox{

private int imgResourceFocusedChecked, imgResourceFocusedNotChecked,
imgResourceNotFocusedChecked, imgResourceNotFocusedNotChecked;

        public MyCheckBox(Context context){
                super(context);
        }

        public MyCheckBox(Context context, AttributeSet attrs) {
                super(context, attrs);
                init(attrs);
        }

        public MyCheckBox(Context context, AttributeSet attrs, int defStyle)
{
                super(context, attrs, defStyle);
                init(attrs);
        }

        private void init(AttributeSet attrs){
                TypedArray a = getContext().obtainStyledAttributes(attrs,
R.styleable.MyCheckBox);
                imgResourceFocusedChecked = a.getResourceId
(R.styleable.MyCheckBox_checkboxFocusedChecked, -1);
                imgResourceFocusedNotChecked = a.getResourceId
(R.styleable.MyCheckBox_checkboxFocusedNotChecked, -1);
                imgResourceNotFocusedChecked = a.getResourceId
(R.styleable.MyCheckBox_checkboxNotFocusedChecked, -1);
                imgResourceNotFocusedNotChecked = a.getResourceId
(R.styleable.MyCheckBox_checkboxNotFocusedNotChecked, -1);
                strResourceText = a.getResourceId
(R.styleable.MyCheckBox_checkboxText, -1);
        }

        public void onDraw(Canvas canvas){
                if (this.isChecked()){
                        if (this.isFocused()){
                                
this.setBackgroundResource(imgResourceFocusedChecked);
                        } else {
                                
this.setBackgroundResource(imgResourceNotFocusedChecked);
                        }
                } else {
                        if (this.isFocused()){
                                
this.setBackgroundResource(imgResourceFocusedNotChecked);
                        } else {
                                
this.setBackgroundResource(imgResourceNotFocusedNotChecked);
                        }
                }
        }
}


Then from my layout xml I do something like:

<MyCheckBox
        android:id="@+id/checkBox"
        android:layout_width="32px"
        android:layout_height="32px"
        android:text="This is a check box."
        cb:checkboxFocusedChecked="@drawable/cb_focused_checked"
        cb:checkboxFocusedNotChecked="@drawable/cb_focused_not_checked"
        cb:checkboxNotFocusedChecked="@drawable/cb_not_focused_checked"
        cb:checkboxNotFocusedNotChecked="@drawable/
cb_not_focused_not_checked"
        style="@style/myStyle"/>

However, the text never comes up. Using the same technique with Button
and EditText views works fine , but not with check boxes. Hope to get
an advice of how to implement the proper behaviour. Thanks!




--~--~---------~--~----~------------~-------~--~----~



Other Threads

1. BitmapFactory.decodeStream() works in Emulator, not on phone?

I'm debugging an app that is loading images from Flickr's webservice
API. I have a snippet of code that is using BitmapFactory.decodeStream
() to create a Bitmap from an InputStream reading from a URL to a jpeg
hosted on Flickr.

This code works fine in the emulator, but if I debug the same app on
my MyTouch3g, decodeStream() is returning null.

Any ideas?

Here's the piece of code:

Bitmap bm = null;
                try {
                        URL url = new URL(sourceUrl);
                        URLConnection conn = url.openConnection();
                        conn.connect();
                        InputStream is = conn.getInputStream();
                        BufferedInputStream bis = new BufferedInputStream(is, 
32000);

                        bm = BitmapFactory.decodeStream(bis);
                        bis.close();
                        is.close();
                } catch (IOException ioe) {
                        ioe.printStackTrace();
                        Log.e(APP_LOG_TAG, "Error getting bitmap", ioe);
                } catch (Exception e) {
                        e.printStackTrace();
                        Log.e(APP_LOG_TAG, "Error loading bitmap from Flickr: 
", e);
                }


Thanks, Kevin Hooke
--~--~---------~--~----~------------~-------~--~----~

2. salam kenal dan WTA porting hero rom ke magic

Salam kenal rekan-rekan sekalian,
Saya newbie di sini.
Mohon sharing ilmu robot ijo nya ya.

Kalo diijinkan adakah rekan yg posisi di Bandung dan berkenan saya
temui untuk copy bahan bahan untuk port hero rom ke magic berhubung
fakir bandwith.
Syukur kalo ada step tutorialnya hehehe..

Oya, magic saya 32b keluaran airtel dan entah kenap nggak ada gms nya.

Thanks sebelumnya.

--~--~---------~--~----~------------~-------~--~----~
Google Groups "Indonesian Android Community [id-android]" group.

To post to this group, send email to id-android@googlegroups.com

To request to subscribe to this group, please visit the following page:
 

3. Verizon attacks iPhone with n ew 鈥楧roid鈥?campaign

4. Is it possible to change the app name from code after installation?

5. How to play the music in the earpiece instead of speaker?

6. Good documentation of shapes, styles and themes

7. Could you tell me how to change all dp values to px values by default in xml