Question: Not removing the padding around button text

Question

Not removing the padding around button text

Answers 4
Added at 2017-01-05 07:01
Tags
Question

I have written a code to change the minWidth of a button so that I can get rid of the padding around the button text.

for(int i=0;i<26;i++){
    Button alphabet = new Button(getContext());
    alphabet.setText(""+(char)('A'+i));
    alphabet.setMinWidth(0);
    allAlphabets.addView(alphabet);
}

I am having a letter in each button and finally adding it to a layout dynamically. Due to padding, it is taking up much space, because of which I have set the minimum width to 0, but still it doesn't work. It shows the same default button. Please help.

Answers to

Not removing the padding around button text

nr: #1 dodano: 2017-01-05 08:01

You have to apply LayoutParams with your Button alphabet.

nr: #2 dodano: 2017-01-05 08:01

pls try set padding of button like

Button alphabet = new Button(getContext());
                alphabet.setText(""+(char)('A'+i));
                 alphabet.setPadding(0,0,0,0);
                allAlphabets.addView(alphabet);
nr: #3 dodano: 2017-01-05 08:01

as you want a clickable view look like button with text

you can make a xml layout with your button or a cardview and textview inside it

then add it like this:

Button alphabetButton = LayoutInflater.from(context).inflate(R.layout.alphabet_layout);

so it's better than code design it's now a layout so you can modify the layout as you want

also you can add to your button in alphabet_layout.xml

android:background="@null"
nr: #4 dodano: 2017-01-05 17:01

You can set using below code :

LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT,
            LayoutParams.WRAP_CONTENT);
button.setLayoutParams(params);
button.setGravity(Gravity.CENTER_HORIZONTAL);
button.setTextSize(32);
Source Show
◀ Wstecz