0

I'm trying to create a custom button class but I can't set the default text size and background.

ButtonGray.kt

class ButtonGray(context: Context?, attrs: AttributeSet?) : androidx.appcompat.widget.AppCompatButton(context, attrs) {

    init {
        textSize = 30.0F
        background = R.drawable.bg_btn_gray
    }
}

ButtonGray.kt

text size cannot be assigned by default.

EDIT:

I try this code:

 background = context?.getDrawable(R.drawable.bg_btn_gray)

Does not give an error. But it didn't work by default.

enter image description here

3
  • Why not do this with styles? The class should define the behavior. The sytle should define the appearance. Commented Jul 27, 2020 at 14:44
  • Can you show an example? Commented Jul 27, 2020 at 15:41
  • It’s all in the official documentation. Commented Jul 27, 2020 at 16:25

3 Answers 3

1
import android.content.Context
import android.util.AttributeSet
import androidx.appcompat.widget.AppCompatButton
import androidx.core.content.ContextCompat
   
class ButtonGray : AppCompatButton {
    constructor(context: Context) : super(context) {
        init()
    }

    constructor(
        context: Context,
        attrs: AttributeSet?
    ) : super(context, attrs) {
        init()
    }

    constructor(
        context: Context,
        attrs: AttributeSet?,
        defStyleAttr: Int
    ) : super(context, attrs, defStyleAttr) {
        init()
    }

    private fun init() {

        width= resources.getDimension(R.dimen.your_dimen).toInt()
        height= resources.getDimension(R.dimen.your_dimen).toInt()

        setBackgroundColor(ContextCompat.getColor(context,R.color.yourColor))

    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

Welcome to Stackoverflow, Please consider writing a sentence or two describing your solution.
1

Here is how I have a created a custom button in Kotlin.

import android.content.Context
import android.util.AttributeSet
import androidx.appcompat.widget.AppCompatButton
import com.urfeed.R


class PrimaryButton : AppCompatButton {

    constructor(context: Context) : super(context){
        init()
    }

    constructor(
        context: Context,
        attrs: AttributeSet?
    ) : super(context, attrs) {
        init()
    }

    constructor(
        context: Context,
        attrs: AttributeSet?,
        defStyleAttr: Int
    ) : super(context, attrs, defStyleAttr) {
        init()
    }


    private fun init() {
        this.background = context.getDrawable(R.drawable.gradient)
        this.textSize = 10f
        this.setTextColor(context.getColor(R.color.white))
    }

    fun turnIntoPrimary(text: String){
        this.text = text
        this.background = context.getDrawable(R.drawable.primary_bkg_drawable)
        postInvalidate()
    }

     fun turnIntoSecondary(text: String){
        this.text = text
        this.background = context.getDrawable(R.drawable.secondary_bkg_drawable)
        postInvalidate()
    }

}

Usage:

button.turnIntoPrimary("Primary")

button.turnIntoSecondary("Secondary")

Comments

0

Use this class and customize in init section

import android.content.Context
import android.util.AttributeSet
import androidx.appcompat.widget.AppCompatButton

class ButtonGray(context: Context, attrs: AttributeSet?, defStyleAttr: Int) :        AppCompatButton(context, attrs, defStyleAttr){

constructor(context: Context, attrs: AttributeSet?) : this(context, attrs, 0) {
    init()
}
constructor(context: Context) : this(context, null, 0) {
    init()
}

init {
    setBackgroundColor(R.drawable.bg_btn_gray)
    textSize = 30.0F
    text = "Hello"
}}

I have Java Way of implementation. which is working You have to read attributes for background, size etc. Put some default value in case not set and set these to button like below code

Read Attributes

TypedArray attributes = context.getTheme().obtainStyledAttributes(attributeSet, R.styleable.IGButton,0,0);
    try {
        setType(attributes.getInt(R.styleable.IGButton_buttonType,0));
        setiConPos(attributes.getInt(R.styleable.IGButton_iconPos,0));
        borderWidth = attributes.getInt(R.styleable.IGButton_borderWidth, Constants.BUTTON_BORDER_WIDTH);
        radius = attributes.getInt(R.styleable.IGButton_borderRadius, Constants.BUTTON_BORDER_RADIUS);
        fontSize = attributes.getInt(R.styleable.IGButton_fontSizeSP, Constants.BUTTON_TEXT_FONT_SIZE);
        int drawableId = attributes.getResourceId(R.styleable.IGButton_smallIcon, 0);
        if(drawableId > 0) {
            icon = getResources().getDrawable(drawableId, context.getTheme());
        }
    }
    finally {
        attributes.recycle();
    }

Set it like

this.setTextColor(textColor);
this.setAllCaps(false);
    this.setTypeface(Typeface.defaultFromStyle(Typeface.BOLD));
    ((Button)this).setTextSize(TypedValue.COMPLEX_UNIT_SP, fontSize);

Set Custom Background

GradientDrawable drawable = new GradientDrawable();
    drawable.setColor(backgroundColor);
    drawable.setCornerRadius(radius);
    drawable.setStroke(borderWidth, borderColor);
    this.setBackground(drawable);
    StateListAnimator animator =  AnimatorInflater.loadStateListAnimator(context,
            R.animator.button_animation_set);
    ((Button)this).setStateListAnimator(animator);

1 Comment

I updated answer. Some code in java you may have to write in kotlin.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.