I am using a Button
created using following code
LinearLayout ll = new LinearLayout(this);
ll.setOrientation(LinearLayout.VERTICAL);
Button btn = new Button(this);
btn.setOnClickListener(newtodobtn);
btn.setText("New Todo");
btn.setBackgroundDrawable(new Button(this).getBackground());
ll.addView(btn);
I have an image in path @drawable/new_todo_image
to set as background for the button. How to set it to the Button
programmatically?
The default button in HTML can be changed to an image using CSS. The required button is selected using the respective CSS selector. The background property can then be set to include a background image and change the image type as required. The border of the button can also be removed to show only the image itself.
for set background image for button which is in drawable folder then use below code
btn.setBackgroundResource(R.drawable.new_todo_image);
Try this:
btn.setBackgroundDrawable(getResources().getDrawable(R.drawable.new_todo_image));
try this:
btn.setBackgroundDrawable(getResources().getDrawable(R.drawable.new_todo_image));
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With