androiduser-interfaceandroid-tablelayouttablerowviewflipper

setBackground to buttons in a table


I have a series of buttons, in tables, in a ViewFlipper, so I can paginate the tables. If I set them no background, they keep their margins between each other. But I need to "make them pretty", so I added a gradient to the buttons. But now they are not keeping their margins any longer.

I am adding them programatically, looping an ArrayList of the texts the button have to have, adding them in the proper row of the table, and so on. Like this:

final Button btn = new Button(this);
btn.setText(array.get(i));
btn.setTextSize(10);

btn.setOnClickListener(new View.OnClickListener() 
{
    @Override
    public void onClick(View v) 
    {
        Log.i("TAG", "Botón pulsado: " + texto);
        goToModule(texto);
    }
});
btn.setHeight(height);
btn.setWidth(width);
btn.setOnTouchListener(parentListener);
btn.post(new Runnable() 
{
    @Override
    public void run() 
    {
        btn.setCompoundDrawablesWithIntrinsicBounds(null,getDrawableForText(texto),null, null);
        btn.setPadding(0,35,0,0);
        int[] colors = {Color.parseColor("#008000"),Color.parseColor("#ADFF2F")};
        GradientDrawable gd = new GradientDrawable(
        GradientDrawable.Orientation.TL_BR, colors);
        gd.setCornerRadius(30f);
        btn.setBackground(gd);
    }
});
row1.addView(btn);

The result in the screen is like this:

enter image description here

Is there anyway to make the buttons look like the ones without the gradient, I mean, respecting the margins between each other, to make them look more like a proper table?

Thank you.


Solution

  • Try adding margin to button

     TableRow.LayoutParams params = new TableRow.LayoutParams(
                TableRow.LayoutParams.WRAP_CONTENT,
                TableRow.LayoutParams.WRAP_CONTENT
        );
        params.setMargins(10, 10, 10, 10);
        btn.setLayoutParams(params);