skip to Main Content

In styles.xml I got:

<style name="MyButtonStyle">
    <item name="android:buttonStyle">@style/Base.Widget.AppCompat.Button.Borderless</item>
    <item name="android:textSize">24px</item>
    <item name="android:drawablePadding">4dp</item>
    <item name="android:textColor">@color/menuButtonForecolor</item>
    <item name="android:textStyle">bold</item>
</style>

I wanna use this style inside CMyButton.java like this

public class CMyButton extends AppCompatButton {

    private static int getDefStyleId()
    {
        return R.style.MyButtonStyle;
    }

    protected void initThis() {

    }

    public CMyButton(Context context) {

        this(context,null, getDefStyleId());
    }

    public CMyButton(Context context, AttributeSet attrs) {
        this(context, attrs, getDefStyleId());
    }

    public CMyButton(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        this.initThis();
    }
}

I use this button in panel_layout.xml:

<merge xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    style="@style/LinearLayoutTheme">

    <college.test.progs.CMyButton
        android:id="@+id/showFacultiesButton"
        android:text="@string/faculties_button_title"
        android:gravity="center"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

</merge>

And this panel_layout.xml is used inside CPanel.java

public class CPanel extends LinearLayout {

    private CMyButton facultiesButton;

    protected void initThis(Context context) {
        inflate(getContext(),R.layout.panel_layout,this);
        this.facultiesButton = (CMyButton) findViewById(R.id.showFacultiesButton);
    }

    public CPanel(Context context) {
        this(context,null);
    }

    public CPanel(Context context, AttributeSet attrs) {
        this(context, attrs,0);
    }

    public CPanel(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        this.initThis(context);
    }

    @TargetApi(Build.VERSION_CODES.LOLLIPOP)
    public CPanel(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        this.initThis(context);
    }
}

Then I add this panel layout to main activity’s layout

But when I add this button to any activity the style is not applied. What is wrong with this class? I know that I can apply the style in layout file but I need to do it in the code

2

Answers


  1. Did you change MyButtonStyle as your theme’s android:buttonStyle attribute value?

    Hope this helps!

    Login or Signup to reply.
  2. There are 2 ways to apply a style to a view:

    Apply it directly to the view:

    <college.test.progs.CMyButton
       android:id="@+id/showFacultiesButton"
       android:text="@string/faculties_button_title"
       android:gravity="center"
       android:style="@style/MyButtonStyle"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content" />
    

    Apply it to the Application through the style.xml file:

    <item name="buttonStyle">@style/MyButtonStyle</item>
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search