CustomButton provides several public methods to control its appearance and behavior at runtime.
setPressStatus
Set the button’s appearance to selected or unselected state programmatically.
public void setPressStatus(boolean isPress)
true to show the selected/pressed state, false to show the normal state
Example:
CustomButton button = findViewById(R.id.my_button);
button.setPressStatus(true); // Show selected state
setDrawableLeft
Add a drawable icon to the left side of the button text.
Basic usage
public void setDrawableLeft(int imgResId)
The drawable will be sized to match the line height of the text.
Advanced usage
public void setDrawableLeft(int imgResId, int width, int height)
The drawable width in pixels
The drawable height in pixels
Example:
CustomButton button = findViewById(R.id.my_button);
button.setDrawableLeft(R.drawable.ic_star); // Auto-sized
button.setDrawableLeft(R.drawable.ic_star, 48, 48); // Custom size
setDrawableRight
Add a drawable icon to the right side of the button text.
Basic usage
public void setDrawableRight(int imgResId)
The drawable will be sized to match the line height of the text.
Advanced usage
public void setDrawableRight(int imgResId, int width, int height)
The drawable width in pixels
The drawable height in pixels
Example:
CustomButton button = findViewById(R.id.my_button);
button.setDrawableRight(R.drawable.ic_arrow); // Auto-sized
button.setDrawableRight(R.drawable.ic_arrow, 32, 32); // Custom size
setColor
Set all color properties of the button at once.
public void setColor(
int textnormal,
int textselected,
int buttonnormal,
int buttonselected,
int strokenormal,
int strokeselected
)
Text color in normal state
Background color in normal state
Background color when pressed
Outline color in normal state
Outline color when pressed
Example:
CustomButton button = findViewById(R.id.my_button);
button.setColor(
Color.WHITE, // text normal
Color.LTGRAY, // text selected
Color.parseColor("#FF6200EE"), // button normal
Color.parseColor("#FF3700B3"), // button selected
Color.parseColor("#FF3700B3"), // stroke normal
Color.parseColor("#FF6200EE") // stroke selected
);
This method provides a convenient way to theme the button programmatically without setting each color attribute individually.