I solved this with a rahter ugly hack for lack of better idea:

In a derived class (which extends Switch), I overrode the following method:

    @Override
    public void requestLayout() {
        IslLog.i(TAG, "requestLayout");
        try {
            java.lang.reflect.Field mOnLayout = 
Switch.class.getDeclaredField("mOnLayout");
            mOnLayout.setAccessible(true);
            mOnLayout.set(this, null);
            java.lang.reflect.Field mOffLayout = 
Switch.class.getDeclaredField("mOffLayout");
            mOffLayout.setAccessible(true);
            mOffLayout.set(this, null);
        } catch (Exception x) {
            Log.e(TAG, x.getMessage(), x);
        }
        super.requestLayout();
    }

This now works. After I use `setTextOn` or `setTextOff`, I just call 
`requestLayout`, which uses reflection to set `mOnLayout` and `mOffLayout` 
to null; `requestLayout` in turn triggers `onMeasure`, which re-initializes 
those variables. It is ugly, but it works and is, IMHO better than copying 
the complete source of Switch to application.

-- Miha

On Monday, November 4, 2013 3:50:37 PM UTC+1, Miha wrote:
>
> Hi!
>
> I have an issue where I can't change Switch textOn/Off at runtime. This 
> means, that the following code, bound to a simple Button (for reproduction 
> purposes) does not work:
>
>
>  @Override
>
>  public void onClick(View arg0) {
>
>   _sw.setTextOn("On " + _counter);
>
>   _sw.setTextOff("Off " + _counter);
>
>   _sw.setText("Text" + _counter);
>
>   _sw.setVisibility(_sw.getVisibility() == View.GONE ? View.VISIBLE : 
> View.GONE);
>
>   _counter  ++;
>
>   _sw.invalidate();
>
>   _sw.setFocusable(true);
>
> }
>
>
> This code changes the text associated with Switch, but not On or Off 
> labels on the button. Interestingly enough, if I call getTextOn or 
> getTextOff, I get back the correct value, that was set on this Switch. Any 
> ideas why this does not work as expected?
>
> Regards,
>
>  Miha.
>

-- 
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en
--- 
You received this message because you are subscribed to the Google Groups 
"Android Developers" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
For more options, visit https://groups.google.com/groups/opt_out.

Reply via email to