Nora
Nora

Reputation: 1893

How to change button size programmatically in Android?

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?

Upvotes: 5

Views: 10241

Answers (6)

MdBasha
MdBasha

Reputation: 451

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);

Upvotes: 0

Shivanand Darur
Shivanand Darur

Reputation: 3224

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()));

Upvotes: 0

Roberto Manfreda
Roberto Manfreda

Reputation: 2623

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);

Upvotes: 0

Hossein Seifi
Hossein Seifi

Reputation: 1420

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

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

Upvotes: 10

Manish Mahajan
Manish Mahajan

Reputation: 2082

Try this one

deleteBtn.setLayoutParams (new LayoutParams(LayoutParams.MATCH_PARENT, 15));

Upvotes: 4

Brainnovo
Brainnovo

Reputation: 1829

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;
    }

Upvotes: 2

Related Questions