in application i can simply define attr.xml
into values
and use that into design layout. but i can not set this attributes programical into widgets, for example i have this attribute:
<declare-styleable name="ButtonTextStyle">
<attr name="font_button">
<enum name="consolas" value="0" />
<enum name="times" value="1" />
</attr>
</declare-styleable>
and i can use this attribute into xml by:
<com.sample.app.Widgets.ButtonTextStyle
android:id="@+id/btn_way_bill_click"
android:layout_width="fill_parent"
app:font_button="consolas">
now i'm define some Button programical into project and i want to use consolas
font defined into font_button
by:
button.setTextAppearance(context, R.attr.font_button);
but i get error for this code and i can not resolve problem, i'm define custom class extends from Button like with this:
public class ButtonTextStyle extends Button {
private Context mContext;
public ButtonTextStyle(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
mContext = context;
init(attrs, defStyle);
}
public ButtonTextStyle(Context context, AttributeSet attrs) {
super(context, attrs);
mContext = context;
init(attrs,-1);
}
public ButtonTextStyle(Context context) {
super(context);
mContext = context;
init(null,-1);
}
private void init(AttributeSet attrs,int defStyle) {
if (!isInEditMode()) {
TypedArray a = mContext.obtainStyledAttributes(attrs,R.styleable.ButtonTextStyle, defStyle, 0);
String str = a.getString(R.styleable.ButtonTextStyle_font_button);
switch (Integer.parseInt(str)) {
case 0:
str = "fonts/consolas.ttf";
break;
case 1:
str = "fonts/times.ttf";
break;
}
setTypeface(FontManager.getInstance(getContext()).loadFont(str));
}
}
}
PROBLEM:
Expected resource of type style
and i can not set for example enum to widgets like with set times
font
HOW to set programical this custom class with attribute defined into attr.xml
UPDATED: my class to add button is below method. i want to set font for it programically:
private void addButton(final CDialog owner, final Dialog dialog, final DialogButton dlgBtn) {
LinearLayout linearLayout = (LinearLayout) dialog.findViewById(R.id.tsw__layerButton);
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, 70);
//layoutParams.setMargins(11, 0, 8, 0);
Button button = new Button(context);
if (buttonTheme != -1) {
button.setBackgroundResource(buttonTheme);
}
button.setPadding(0,2,0,0);
button.setGravity(Gravity.CENTER);
button.setText(buttonMsg[dlgBtn.ordinal()]);
button.setTextSize(14);
button.setTextColor(G.context.getResources().getColor(R.color.white_text));
button.setWidth(dpToPx(buttonWidth));
//button.setHeight(dpToPx(32));
button.setLayoutParams(layoutParams);
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
dialog.dismiss();
if (dialogListener != null) {
dialogListener.onCloseDialog(owner, dlgBtn);
}
}
});
linearLayout.addView(button);
}