0

i want to set 2 different size in a multiline button.

what i do now is using below method but i have no control over the size and does not know how it looks like on different screen size.

  final Button btnreset = (Button) findViewById(R.id.resetQ);
    btnreset.setText(Html.fromHtml("<big>Click Here</big><br/><small>To Queue</small>"));

some of them say instead of using big or small. use html tag. html tag works fine on the color attribute but not size attribute.

  final Button btnreset = (Button) findViewById(R.id.resetQ);
    btnreset.setText(Html.fromHtml("<font size='9'>Click Here</font><br/><font size='2'>To Queue</font>"));

below is my xml coding

<Button
                   android:id="@+id/resetQ"
                   android:layout_width="wrap_content"
                   android:layout_height="wrap_content"
                   android:layout_marginTop="50dp"                     
                   android:text="New Queue"
                       android:textSize="@dimen/lblsize"    
                     android:layout_weight="1" />

Edited

so now my code becomes

  Spannable spannable = new SpannableString("Click Here To Queue");
    spannable.setSpan(new RelativeSizeSpan(1.5f), 0, 10,Spannable.SPAN_INCLUSIVE_INCLUSIVE);
   btnreset.setText(spannable);
vbCoder
  • 75
  • 3
  • 12

2 Answers2

4

You can use something like this :

Spannable spannable = new SpannableString("Click here\nto queue");
spannable.setSpan(new StyleSpan(android.graphics.Typeface.BOLD), 0, 10,Spannable.SPAN_INCLUSIVE_INCLUSIVE);
spannable.setSpan(new RelativeSizeSpan(2f), 11, 20,Spannable.SPAN_INCLUSIVE_INCLUSIVE);
btnreset.setText(spannable);

This sets "Click here" to Bold. Then "to queue" to double size font on the next line.

faizal
  • 3,497
  • 7
  • 37
  • 62
2

For Android 5.0 (Lollipop) "or newer" add setTransformationMethod(null) to your button (see this link).

Spannable span = new SpannableString(text + "\n" +  underText);
span.setSpan(new AbsoluteSizeSpan(12, true), text.length()+1, text.length()+1+underText.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);

Button button = new Button(ctx);
button.setTextSize(16);
button.setText(span);
button.setTransformationMethod(null);
Community
  • 1
  • 1
user3007
  • 21
  • 3