Search code examples
androidtextviewstring.xml

display string and integer in a bracket in textview


I have tried

String name = mContext.getString(R.string.product_and_quantity, banner.getProduct_name().trim(), banner.getProduct_quantity());

and in String.xml file

 <string name="product_and_quantity">%1$s x(%2$d)</string>

but still I am not getting proper view in some items as shown in image.

I tried this too

 String name=banner.getProduct_name().trim()+"x("+ banner.getProduct_quantity()+")";
       SpannableStringBuilder spannableStringBuilder=new SpannableStringBuilder();
       spannableStringBuilder.append(banner.getProduct_name().trim());
       spannableStringBuilder.append(" X(");

       spannableStringBuilder.append(Integer.toString(banner.getProduct_quantity()));
       spannableStringBuilder.append(mContext.getString(R.string.closing_bracket));

and string.xml file has

 <string name="closing_bracket">)</string>
but still i am getting the same error. 
I have invested 3-4 hours on stackoverflow about this error but no solution. Please help.

enter image description here


Solution

  • It could be that the text is too long and breaks into 3 lines and only the first two are shown. Like this:

    Tandori
    Sauce Pasta
    (1)
    

    And the textview is limited in height and only shows the first two lines.

    EDIT

    Based on the provided layout xml and discussion in comments, the solution was, to change the textview's height to match_parent.