3

Here is my button:

public class ChimmerButton extends Button {

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

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

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

/*
 * This method is used to apply the external font
 */
public void setTypeface(Typeface tf, int style) {
    if (!isInEditMode()) {
        super.setTypeface(Typeface.createFromAsset(
                getContext().getAssets(), "calibre-regular.ttf"));
    }
}
}

How can i apply Theme.Light.NoTitleBar.Fullscreen on all chimmerButtons using above code?? Is there any solution for this?

mawalker
  • 2,072
  • 2
  • 22
  • 34
Vatish Sharma
  • 1,536
  • 3
  • 16
  • 35
  • Notice the second argument: `ChimmerButton(Context context, AttributeSet attrs)` more on this [here](http://stackoverflow.com/questions/4493947/how-to-define-theme-style-item-for-custom-widget) – Skynet Jan 01 '16 at 04:22

3 Answers3

1

Note that this should be called before any views are instantiated in the Context (for example before calling setContentView(View) or inflate(int, ViewGroup)).

from:

http://developer.android.com/reference/android/view/ContextThemeWrapper.html#setTheme%28int%29

Unfortunately, You must set the theme before displaying the Activity at all.

Therefore you can't have 'dynamic' themes driven by runtime code (though the comment above shows how to make a custom theme for your buttons)

mawalker
  • 2,072
  • 2
  • 22
  • 34
0
ContextThemeWrapper themedContext;

public ChimmerButton(ContextThemeWrapper themedContext) {
    This.themedContext = themedContext;
}  

if ( Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB ) {
    themedContext = new ContextThemeWrapper( Activity.this, android.R.style.Theme_Holo_Light_Dialog_NoActionBar );
}
else {
    themedContext = new ContextThemeWrapper( Activity.this, android.R.style.Theme_Light_NoTitleBar );
Pitty
  • 1,907
  • 5
  • 16
  • 34
0

Button's theme is context dependent by default. So instead of setting theme for buttons, set theme for the activity that contains the button. It is a much easier solution.

Apply a theme to an activity in Android?

Community
  • 1
  • 1
T D Nguyen
  • 7,054
  • 4
  • 51
  • 71