1

I want to have button size according to the text content. For example, if size of text is about 4 letters then width of button be should big enough to adjust 4 letters only.Here is My Code:

layout = (LinearLayout) dialog.getCustomView().findViewById(R.id.layoutTags);
layout.setOrientation(LinearLayout.VERTICAL);  //Can also be done in xml by android:orientation="vertical"
layout.setWeightSum(1);
layout.removeAllViews();
layout.invalidate();
float rowneed = ((float) count1 / 5);
k = 0;
for (int i = 0; i < ceil(rowneed); i++) {
    LinearLayout row = new LinearLayout(getContext());
    row.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT));
    row.removeAllViews();
    for (int j = 0; j < 5; j++) {
        btnTag = new Button(getContext());
        btnTag.setHeight(15);
        btnTag.setWidth(1);
        btnTag.setMinimumWidth(155);
        btnTag.setMinimumHeight(135);

        if (k < count1) {
            btnTag.setText(filteredTags.get(k).gettagName());
            btnTag.setId(k);
            k++;
            btnTag.setVisibility(View.VISIBLE);

        } else {
            btnTag.setVisibility(View.INVISIBLE);
        }
        Log.e("count", " " + k + " " + count1 + " " + ceil(rowneed) + " " + edtTag.getText().toString());
        btnTag.setTextSize(7);
        btnTag.setGravity(0);

        row.addView(btnTag);
    }
    layout.addView(row);
}
Priya singh
  • 87
  • 1
  • 9

2 Answers2

0

Set LayoutParams in java

ViewGroup.LayoutParams p = button.getLayoutParams();
p.width= ViewGroup.LayoutParams.WRAP_CONTENT;
button.setLayoutParams(p);
Shabbir Dhangot
  • 8,954
  • 10
  • 58
  • 80
KKSINGLA
  • 1,284
  • 2
  • 10
  • 22
0

Use this :

button.setLayoutParams(new LinearLayout.LayoutParams(
LayoutParams.MATCH_PARENT,
LayoutParams.MATCH_PARENT,
1.0f));
Vaibhavi
  • 61
  • 8