Android button with transparent png image and background color

I need a button with a transparent foreground image and background color. So use this code. The background color goes beyond the image. I need a button with the same image size.

Depending on the user experience, I need to change the foreground color and background color. I want to add an image and background color separately so that I can change one of them at minimal cost. I have to use many buttons in this user interface, so this will be done in Java code.

layout = new TableLayout(this);
layout.setLayoutParams(new TableLayout.LayoutParams(8,7));
TableRow row2 = new TableRow(this);
buttonPlayer1 = new ImageButton(this);
buttonPlayer1.setImageDrawable(getResources().getDrawable(R.drawable.blankc4));
buttonPlayer1.setBackgroundColor(Color.GREEN);
row2.addView(buttonPlayer1);
layout.addView(row2);
+3
source share
1 answer

, , , , getHeight() getWidth() , Button , , setHeight() setWidth() .

LinearLayout:


 public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        LinearLayout btnLO = new LinearLayout(this);    
        btnLO.setOrientation(LinearLayout.VERTICAL);
        ImageButton i1 = new ImageButton(this);
        i1.setBackgroundColor(color.background_dark);
        i1.setImageDrawable(getResources().getDrawable(R.drawable.rana));   
        btnLO.addView(i1, new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));                      
  //    btnLO.setGravity(Gravity.LEFT | Gravity.CENTER_HORIZONTAL);     
        this.addContentView(btnLO, new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT)); 
    }
+1

All Articles