5

I am creating buttons and adding them to a LinearLayout programmatically. However, I am struggling with changing the button sizes. In the code below, no matter what num I enter in deleteBtn.setWidth(num), and deleteBtn.setHeight(num), nothing changes.

private void populateHorizontalLayouts(CustomMessageEvent event) {
    // Need to remove all views each time an user adds a number
    // so that the same number is not rendered multiple times.

    nameAndNumbersLayout.removeAllViews();

    //displayedNamesAndNumbers.add(event.getCustomMessage());
    for(int i =0; i < displayedNamesAndNumbers.size(); i++){
        String displayedNumberAndName = displayedNamesAndNumbers.get(i);

        LinearLayout horizontalLayout = new LinearLayout(view.getContext());
        horizontalLayout.setOrientation(LinearLayout.HORIZONTAL);
        horizontalLayout.setId(i);

        Button deleteBtn = new Button(view.getContext());
        deleteBtn.setId(i);
        deleteBtn.setText("Delete");
        deleteBtn.setWidth(5);
        deleteBtn.setHeight(5);

        TextView nameAndNumView = new TextView(view.getContext());
        nameAndNumView.setId(i);
        nameAndNumView.setText(displayedNumberAndName);

        horizontalLayout.addView(deleteBtn);
        horizontalLayout.addView(nameAndNumView);

        nameAndNumbersLayout.addView(horizontalLayout);
    }
}

What should I write so that the button size changes?

Nora
  • 1,825
  • 8
  • 31
  • 47
  • 2
    Possible duplicate of [Programmatically change the width of the Button in android](https://stackoverflow.com/questions/11293932/programmatically-change-the-width-of-the-button-in-android) – tatocaster Jun 28 '18 at 07:43

6 Answers6

9

You should apply LayoutParams of the LinearLayout which contains your Button.

deleteBtn.setLayoutParams(new LinearLayout.LayoutParams(5, 5));
Hossein Seifi
  • 1,380
  • 11
  • 29
4

Try this one

deleteBtn.setLayoutParams (new LayoutParams(LayoutParams.MATCH_PARENT, 15));
Manish Mahajan
  • 2,062
  • 1
  • 13
  • 19
2

Try the following:

int pixels = Math.round(convertDpToPixel(50, getApplicationContext()));

deleteBtn.setLayoutParams(new LinearLayout.LayoutParams(pixels, pixels));

public static float convertDpToPixel(float dp, Context context){
    Resources resources = context.getResources();
    DisplayMetrics metrics = resources.getDisplayMetrics();
    float px = dp * ((float)metrics.densityDpi / DisplayMetrics.DENSITY_DEFAULT);
    return px;
    }
Brainnovo
  • 1,749
  • 2
  • 12
  • 17
0

You can set width and height using LayoutParams, generally when i manage with dimensions i use as reference screen's dimension. Often i do something like this:

Display display = getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);
int width = size.x;
int height = size.y;

float targetWidth = width / 3;
float targetHeight = targetWidth * 3;
deleteBtn.setLayoutParams(new LinearLayout.LayoutParams(targetWidth, targetHeight);
Roberto Manfreda
  • 2,345
  • 3
  • 25
  • 39
0

If you just want to change the height and keep the width as it is,

// This sets the height to 5 *pixels*, with keeping the width as it is.
deleteBtn.setLayoutParams (new LayoutParams(deleteBtn.getLayoutParams().getWidth(), 5));

and to change only width,

deleteBtn.setLayoutParams (new LayoutParams(5, deleteBtn.getLayoutParams().getHeight()));
Shivanand Darur
  • 3,158
  • 1
  • 26
  • 32
0

Search for answer to a button [del] to be displayed in a Tablerow [tbrow], resulted only in setting the parameters for the Tablerow where the button appears.

            del = new Button(getApplicationContext());
            del.setText("DELETE");
            del.setTextColor(Color.BLACK);
            del.setGravity(Gravity.END);
            del.setBackgroundColor(Color.parseColor("#FFFFC107"));       
            del.setLayoutParams (new TableRow.LayoutParams(80, 50));
            tbrow.addView(del);
MdBasha
  • 423
  • 4
  • 16