poovi.org poovi.org - 5 months ago 22
Android Question

How to set the button background image through code

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?

Answer

for set background image for button which is in drawable folder then use below code

btn.setBackgroundResource(R.drawable.new_todo_image);
Comments