0

background

i need to show amount of money in textView with SpannableString and then fit the font of the TextView to the boundaries given to it, as follows:

enter image description here

SpannableString Code:

String s= "2,000,000.000";
SpannableString ss1=  new SpannableString(s);

ss1.setSpan(new RelativeSizeSpan(1.4f), 0,1, 0); // set size 2
ss1.setSpan(new RelativeSizeSpan(1.2f), 2,5, 0); // set size 000 (first)
ss1.setSpan(new RelativeSizeSpan(1.2f), 6,9, 0); // set size 000 (second)
ss1.setSpan(new RelativeSizeSpan(1f), 10,13, 0); // set size 000 (Third)

TextView tv= (TextView)findViewById(R.id.money);
tv.setText(ss1);

The problem

My problem is to fit the text size with screen width

I tried to use the solutions for these posts(here and here) but none of them not work well with SpannableString.

The question

Does anybody know of a solution for this problem?

سعید .م
  • 137
  • 1
  • 8

3 Answers3

1

ok, i solved this problem myself

Along with enlarging, I used miniaturize

like this:

ss1.setSpan(new RelativeSizeSpan(1.2f),  0,  1,  0);
ss1.setSpan(new RelativeSizeSpan( 1f ),  2,  5,  0);
ss1.setSpan(new RelativeSizeSpan( 1f ),  6,  9,  0);
ss1.setSpan(new RelativeSizeSpan(0.8f),  10, 13, 0);
سعید .م
  • 137
  • 1
  • 8
0

چطوری پسر؟ You can get both Width and Height of the Screen and Use it Everywhere:

DisplayMetrics displayMetrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
final int DisplayHeight = displayMetrics.heightPixels;
final int DisplayWidth = displayMetrics.widthPixels;

And in Your Case you want to set width for tv like Below:

tv.getLayoutParams().width = DisplayWidth;

This Measurement is Very powerful, Feel free to use it everywhere. The ONLY thing to Consider For HEIGHT, If you have your Activity Flag on FULL_SCREEN You Actually Need to Consider Navigation Bar Height As well Which is in most Smartphones 48dp

This means you can Use DisplayHeight+ContextCompat.getDimen(mContext,R.dimen.dimen48);

And define dimen48 in your dimen.xml as 48dp

Shahin
  • 391
  • 3
  • 9
  • jigar too xml Vaseye Toole Font 'wrap_content' ro Tanzim Kardi?? Age Nasho begoo Vasat Inja ye Code Benevisam – Shahin Feb 02 '18 at 18:20
  • Ok, Daram Vasat Minevisam – Shahin Feb 02 '18 at 18:28
  • اومد. ببین جواب میده؟ – Shahin Feb 02 '18 at 18:50
  • ببینم میخوای طول تنظیم بشه، یا سایز فونت؟ میگی سایز فونت تغییر نمیکنه؟ – Shahin Feb 02 '18 at 19:08
  • Khob spannableString az API21 Lollipop be Bala Kar mikone, va yek bar ham ba in tanzimat check kon: android:textAllCaps="false", "android:mutileLine", android:ellipsize="end", and android:singleLine="false; – Shahin Feb 02 '18 at 19:24
  • Emtehaaan Kon. Alan bokon begoooo chi shoood – Shahin Feb 02 '18 at 19:25
  • ای بابا. حالا در میارم واست اگه نتونستی. اما فعلاً باید برم سعید جان. ببخش جواب نگرفتی. دوس داشتی بهم پیام بده. ۰۹۳۸۸۵۲۸۷۲۹ – Shahin Feb 02 '18 at 19:41
  • Ghorboonet mamnunam. Nice to meet you too. Contact me whenever you want ;* – Shahin Feb 02 '18 at 19:48
0

First, This is How you can Set Width and Height for your Textview:

  1. If your TextView is Inside a RelativeLayour:

RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) tv.getLayoutParams();

params.width = getResources().getDimensionPixelSize(R.dimen.any_width_you_want);
params.height = getResources().getDimensionPixelSize(R.dimen.any_height_you_want);

tv.setLayoutParams(params);
  1. If your TextView is inside anything else:

ViewGroup.LayoutParams params = (ViewGroup.LayoutParams) tv.getLayoutParams();

params.width = getResources().getDimensionPixelSize(R.dimen.any_width_you_want);
params.height = getResources().getDimensionPixelSize(R.dimen.any_height_you_want);

tv.setLayoutParams(params);
  1. Now, Your tv TextView has a Text Size which you defined in xml source right? Let's say your TextView Font size is 14sp.

Tha means your TextView Height is 14+6 -- > 20dp

So, See what Font Size is your tv Textview

  1. I Assume your textview size is 14sp. Now you Need to Calculate the Highest Scale in SpannableString you Created.

Based on ss1.setSpan(new RelativeSizeSpan(1.4f), 0,1, 0); Highest Scale is 1.4f

This means Your highest HEIGHT for tv is (20dp)*1.4

SO The Final Height You Want is <<<28dp>>>

  1. Define this 28dp in your dimens.xml like this:

<< ? xml version = "1.0"encoding = "utf-8" ? >
  <resources >
  ...
 
  <dimen name ="dimen28">28dp< /dimen>  
 
  ... 
  
 
  </resources>
  1. Now you're Done by Doing This:

params.height = getResources().getDimensionPixelSize(R.dimen.dimen28);
tv.setLayoutParams(params);

یادت نره من فرض کردم سایز فونت شما 14 هستش. این کار رو با سایز فونت خودت انجام بده

Shahin
  • 391
  • 3
  • 9