Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add an Icon to a Button in Android from Java?

I want to add an icon to a Button that I created from Java, after hours I didn't find a solution yet...

I have something like this:

Button button = new Button(context);

And I'd like to have something like this:

button.setIcon(Icon myicon);

Is this possible? ;)

like image 985
Bat BRT Avatar asked Oct 28 '25 10:10

Bat BRT


1 Answers

You can use the MaterialButton:

MaterialButton button = new MaterialButton(this);
button.setIcon(ContextCompat.getDrawable(this,R.drawable.ic_add_24px));
button.setText("Button");

enter image description here

like image 169
Gabriele Mariotti Avatar answered Oct 29 '25 22:10

Gabriele Mariotti