Do not delete registration around button text

I wrote code to change the minWidth of the 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 have a letter in each button and finally dynamically add it to the layout. Due to filling, it takes up a lot of space, due to which I set the minimum width to 0, but still it does not work. It shows the same button by default. Please, help.

+6
source share
4 answers

as you want the interactive view to look like a button with text

you can create an xml layout using a button or card and text content inside it

then add it as follows:

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

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

also you can add to your button in alphabet_layout.xml

 android:background="@null" 
+2
source

You must apply LayoutParams to your Button alphabet .

+1
source

Try installing a button addition, for example

 Button alphabet = new Button(getContext()); alphabet.setText(""+(char)('A'+i)); alphabet.setPadding(0,0,0,0); allAlphabets.addView(alphabet); 
+1
source

You can set the code below:

 LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT); button.setLayoutParams(params); button.setGravity(Gravity.CENTER_HORIZONTAL); button.setTextSize(32); 
0
source

Source: https://habr.com/ru/post/1013778/


All Articles