How to programmatically make a small button wrap small text

I want to make small buttons,

So far I have made the text small, but there is still a lot of space around the text. I tried the code below:

LinearLayout layout = (LinearLayout) view.findViewById(R.id.fragment_dds_tag_linearLayout); Button txtName = new Button(getActivity(), null, android.R.attr.buttonStyleSmall); txtName.setClickable(false); txtName.setTextSize(5); txtName.setMaxLines(1); txtName.setMaxHeight(40); txtName.setMinHeight(0); txtName.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT)); txtName.setText(tagsList.get(i)); layout.addView(txtName); 

But I want it to look just like this layout XML file:

  <Button android:id="@+id/button1" style="?android:attr/buttonStyleSmall" android:layout_width="wrap_content" android:layout_height="wrap_content" android:minHeight="5dp" android:textSize="10sp" android:text="Button" /> 

It looks like setMinHieght doesn't work when I do this programmatically. What can i do wrong?

Here is the image:

Buttons

One labeled buttons were created using xml and all other programmatically.

here is the layout code for the whole page:

 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <TextView android:id="@+id/fragment_dds_rating_ratingBar_textView_heading" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Tags" android:textAppearance="?android:attr/textAppearanceMedium" /> <Button android:id="@+id/button1" style="?android:attr/buttonStyleSmall" android:layout_width="wrap_content" android:layout_height="wrap_content" android:minHeight="5dp" android:textSize="10sp" android:text="Button" /> <LinearLayout android:id="@+id/fragment_dds_tag_linearLayout" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="10dp" android:orientation="horizontal" > </LinearLayout> 

EDIT: Possible Solution:

Is there no way that I can programmatically get the xml layout button (which displays correctly) and then fill in another text and duplicate it as many times as I need?

+4
source share
3 answers

in eclipse write the name of your button. txtName, then you will come with many options and choose whatever you want. or use the developer's website.

 Button txtName = new Button(getActivity(), null, android.R.attr.buttonStyleSmall); txtName.setClickable(false); txtName.setHeight(17); txtName.setWidth(25); txtName.setTextSize(10); txtName.setMaxLines(1); txtName.setMaxHeight(5); txtName.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT)); txtName.setText(tagsList.get(i)); layout.addView(txtName); 
+2
source
 txtName.setHeight(20); txtName.setWidth(20); 
0
source

You must use a fixed size (equal to the minimum and maximum values ​​for width or height)

the code:

 btn.setMaxHeight(40); btn.setMinHeight(40); btn.setMaxWidth(40); btn.setMinimumWidth(40); 

XML:

 android:maxHeight="40dp" android:minHeight="40dp" android:maxWidth="40dp" android:minWidth="40dp" 
0
source

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


All Articles