1

Say i have textview size is 18sp, if textview goes to second line, how to reduce textview size? any idea?

TextView text= (TextView)findViewById(R.id.text);
text.setTextSize(18);
//how to reduce text size if text goes to second line when it's too large?

Thanks in advance

Himanshu Agarwal
  • 4,515
  • 5
  • 35
  • 49
Shadow
  • 6,754
  • 5
  • 39
  • 88

3 Answers3

2

Here is the complete solution for Your problem. Check out this link

It will automatically reduce the font size if size of your text value is greater than your view.

HapPy coading..!!

Rhn Bhadani
  • 2,208
  • 1
  • 17
  • 25
  • 1
    While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. – nbrooks Jun 17 '14 at 10:50
  • @nbrookd Yes I know many time some link got expiry,but I don`t think that Github link gonna dead..!! – Rhn Bhadani Jun 17 '14 at 11:29
  • 1
    It can if the project is removed, if the implementation changes, if github is down...accepted best practice here is to include at least high level detail in your answer here of what your solution does – nbrooks Jun 17 '14 at 16:48
1

You can bind TextWatcher to you textView as done in following code snippet and listen for text changes in your textView.

...
txtView = (TextView)findViewById(R.id.txt);
txtView.addTextChangedListener(new TextWatcher() {

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
        }

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count,
                int after) {
        }

        @Override
        public void afterTextChanged(Editable s) {
            if(txtView.getLineCount() > 1){
                txtView.setTextSize(TypedValue.COMPLEX_UNIT_SP, 14);
            }
        }
});
...
KunalK
  • 1,844
  • 4
  • 22
  • 40
0

Use spannable as

String s= "Hello Everyone next line";
 SpannableString ss1=  new SpannableString(s);
 ss1.setSpan(new RelativeSizeSpan(2f), 0,13, 0); // set size of Hello Everyone
 ss1.setSpan(new RelativeSizeSpan(f), 14,23, 0);// set size of Next line
 TextView tv= (TextView) findViewById(R.id.textview);
 tv.setText(ss1); 
Android Priya
  • 686
  • 1
  • 5
  • 23