Applying style to views dynamically in java code

sadaf picture sadaf · Jun 22, 2012 · Viewed 10k times · Source

I have following custom button view.

public class PrayerTimeLabel extends Button {

int hours;
int minutes;
String dayHalf; //am or pm
Context parentActivity;
PrayerControl parentControl;

public PrayerTimeLabel(Context context,PrayerControl parent) {
    super(context);                 
    init(context,parent,0);
}

public PrayerTimeLabel(Context context, int defStyle, PrayerControl parent) {
    //super(context, null, R.style.Button_PrayerTimeButton);
    super(context, null, defStyle);             
    init(context,parent,defStyle);
}


private void init(final Context context, PrayerControl parent, int defStyle)
{        
    parentActivity = context;
    parentControl = parent;
    Typeface tf = Typeface.createFromAsset(context.getAssets(),"fonts/digital.ttf");
    this.setTypeface(tf);       
    this.setText(false);

    this.setOnClickListener(new OnClickListener() {         
        public void onClick(View v) {
            TimeDialog dialogBox = parentControl.getDialogBox();
            dialogBox.setTime(hours, minutes, dayHalf);
            dialogBox.show();
        }
    });
}


public void setTime(int hrs, int min, String half,boolean signalParent)
{
    hours = hrs;
    minutes = min;
    dayHalf = half;
    this.setText(signalParent);
}

public void setText(boolean signalParent)
{
    super.setText(String.format("%02d", hours)+":"+String.format("%02d", minutes)+" "+dayHalf);
    if(signalParent){
        parentControl.setPrayerTime(hours, minutes, dayHalf);
    }
}

}

and I have the following style defined in my style.xml

    <style name="Button.PrayerTimeButton" parent="@android:style/TextAppearance.Widget.Button">
    <item name="android:background">#000</item>
    <item name="android:textSize">18dp</item>
    <item name="android:textColor">#FFFF00</item>
</style>

The extended button is not getting this style. Can some on point our what I am doing wrong? I searched for the solution and found this. Can some one suggest some thing

Note: I cannot use XML to apply styles. It has to be constructor.

Edit:

Following is the class where this custom button is created and used. I have deleted many irrelevant lines of code

public class PrayerControl extends LinearLayout {


protected PrayerTimeLabel prayerTimeButton;

protected String prayerName;
protected static int counter=0;


public PrayerControl(Context context) {
    super(context);
    init(context);
}

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

    protected void getXMLAttributes(Context context, AttributeSet attrs)
{
    TypedArray a = context.obtainStyledAttributes(attrs,R.styleable.PrayerControl);

    prayerName = a.getString(R.styleable.PrayerControl_name);
    dayHalf = a.getString(R.styleable.PrayerControl_dayHalf);
    hours = a.getInteger(R.styleable.PrayerControl_hours, 4);
    minutes = a.getInteger(R.styleable.PrayerControl_minutes, 30);

    ltrProgress = a.getInteger(R.styleable.PrayerControl_postNamazInterval, 0);
    rtlProgress = a.getInteger(R.styleable.PrayerControl_preNamazInterval, 0);
    intervalMax = a.getInteger(R.styleable.PrayerControl_intervalMax, 30);


    a.recycle();

}

protected void init(Context context)
{
    counter++;
    parentActivity = context;
    this.setOrientation(LinearLayout.HORIZONTAL);
    this.setId(counter);    

    prayerTimeButtonStyle = R.style.Button_PrayerTimeButton;
    initializePrayerTimeButton();

}


protected void initializePrayerTimeButton()
{
    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
            LinearLayout.LayoutParams.WRAP_CONTENT,
            40);        
    params.gravity = Gravity.CENTER;
    //params.weight  = 1.0f;

    prayerTimeButton = new PrayerTimeLabel(parentActivity,prayerTimeButtonStyle,this);
    prayerTimeButton.setTime(hours, minutes, dayHalf,false);

    prayerTimeButton.setLayoutParams(params);
    this.addView(prayerTimeButton);
}

}

Answer

Sherif elKhatib picture Sherif elKhatib · Jun 25, 2012

This is an old answer: I got a -1 a couple of minutes ago and here is the

NEW SOLUTION:

The basic idea is to pass an attribute to the constructor. Check this link for the complete solution: Applying style to views dynamically in java code

OLD SOLUTION (NOT WORKING):

Add these constructors to your class:

public StyledButton(Context context) {
    super(context, null, R.style.Button_PrayerTimeButton);
//... whatever
}

public StyledButton(Context context, AttributeSet attrs) {
    super(context, attrs, R.style.Button_PrayerTimeButton);
//... whatever
}

public StyledButton(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, R.style.Button_PrayerTimeButton);
//... whatever
}