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 alot of space around the text. I have tried the code below: LinearLayout layout = (LinearLay
Solution 1:
in eclipse write your button name. txtName then many options will come for you and choose whatever you want. or use 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);
Solution 2:
Here is a solution:
...
Button b = new Button(getContext());
b.setText("Text");
b.setPadding(dpToPx(5), 0, dpToPx(5), 0);
// crazy I have to set both of these?...
b.setMinWidth(0);
b.setMinimumWidth(10);
b.setTextSize(TypedValue.COMPLEX_UNIT_SP, 10);
LinearLayout.LayoutParams params = new
LinearLayout.LayoutParams(LinearLayout
.LayoutParams.WRAP_CONTENT, dpToPx(20));
b.setLayoutParams(params);
...
publicstaticintdpToPx(int dp) {
return (int) (dp * Resources.getSystem().getDisplayMetrics().density);
}
Solution 3:
txtName.setHeight(20);
txtName.setWidth(20);
Post a Comment for "How To Programmatically Make A Small Button Wrap Small Text"