Skip to content Skip to sidebar Skip to footer

How To Pass Style Attr From Custom View Over?

I'm creating a custom TextSwitcher as below public class CustomTextSwitcher extends TextSwitcher { private static final long SHOW_TEXT_ANIMATION_TIME = 100; public CustomT

Solution 1:

The AttributeSet you get from the Constructor is generated from the style attribute in XML along with the other attributes provided. So you would just save it then pass it along in the constructor to your TextView.. The setStyle method can actually be used with the TextView#setTextAppearance method which accepts style IDs. It will only look at style attributes that are associated with the TextView. I would say this is easier than parsing through an AttributeSet and creating your own styles.

Solution 2:

I found the way to do so. It's as simple as attrs.getStyleAttribute(). Shown the code below

publicclassCustomTextSwitcherextendsTextSwitcher {
    privatestaticfinallongSHOW_TEXT_ANIMATION_TIME=100;

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

    privatevoidinit(AttributeSet attrs) {

        this.setFactory(newViewFactory() {
            @Overridepublic View makeView() {
                returnnewTextView(newContextThemeWrapper(context, 
                        attrs.getStyleAttribute()), null, 0);
            }
        });
        Animationin= AnimationUtils.loadAnimation(context, android.R.anim.fade_in);
        Animationout= AnimationUtils.loadAnimation(context, android.R.anim.fade_out);

        in.setDuration(SHOW_TEXT_ANIMATION_TIME);
        out.setDuration(SHOW_TEXT_ANIMATION_TIME);

        this.setInAnimation(in);
        this.setOutAnimation(out);
    }
}

Post a Comment for "How To Pass Style Attr From Custom View Over?"