murattttttttt
murattttttttt

Reputation: 1

How to create custom button class and add default features

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

Upvotes: 0

Views: 3998

Answers (3)

VIVEK CHOUDHARY
VIVEK CHOUDHARY

Reputation: 546

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")

Upvotes: 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))

    }
}

Upvotes: 1

alokHarman
alokHarman

Reputation: 289

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);

Upvotes: 0

Related Questions