|
1 | 1 | package com.example.autofittextviewsample; |
2 | | -import java.util.Random; |
3 | 2 | import android.app.Activity; |
4 | 3 | import android.content.Intent; |
5 | 4 | import android.net.Uri; |
6 | 5 | import android.os.Bundle; |
| 6 | +import android.text.Editable; |
7 | 7 | import android.text.TextUtils.TruncateAt; |
| 8 | +import android.text.TextWatcher; |
| 9 | +import android.util.TypedValue; |
8 | 10 | import android.view.Gravity; |
9 | 11 | import android.view.Menu; |
10 | 12 | import android.view.MenuItem; |
11 | 13 | import android.view.View; |
12 | 14 | import android.view.View.OnClickListener; |
13 | 15 | import android.view.ViewGroup; |
14 | 16 | import android.view.ViewGroup.LayoutParams; |
| 17 | +import android.view.ViewTreeObserver.OnPreDrawListener; |
| 18 | +import android.widget.EditText; |
| 19 | +import android.widget.SeekBar; |
| 20 | +import android.widget.SeekBar.OnSeekBarChangeListener; |
15 | 21 | import android.widget.TextView; |
16 | 22 | import com.example.autofittextviewtest.AutoResizeTextView; |
17 | 23 |
|
18 | 24 | public class MainActivity extends Activity |
19 | 25 | { |
20 | | - private final Random _random =new Random(); |
21 | | - private static final String ALLOWED_CHARACTERS ="qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM1234567890"; |
| 26 | + // private final Random _random =new Random(); |
| 27 | + // private static final String ALLOWED_CHARACTERS ="qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM1234567890"; |
| 28 | + private EditText _contentEditText; |
| 29 | + private ViewGroup _textViewcontainer; |
| 30 | + private SeekBar _widthSeekBar; |
| 31 | + private SeekBar _heightSeekBar; |
| 32 | + private TextView _linesCountTextView; |
22 | 33 |
|
23 | 34 | @Override |
24 | 35 | protected void onCreate(final Bundle savedInstanceState) |
25 | 36 | { |
26 | 37 | super.onCreate(savedInstanceState); |
27 | 38 | setContentView(R.layout.activity_main); |
28 | | - final ViewGroup container=(ViewGroup)findViewById(R.id.container); |
29 | | - final TextView descriptionTextView=(TextView)findViewById(R.id.descTextView); |
30 | | - findViewById(R.id.randomizButton).setOnClickListener(new OnClickListener() |
| 39 | + _textViewcontainer=(ViewGroup)findViewById(R.id.container); |
| 40 | + _contentEditText=(EditText)findViewById(R.id.contentEditText); |
| 41 | + _contentEditText.addTextChangedListener(new TextWatcher() |
| 42 | + { |
| 43 | + @Override |
| 44 | + public void onTextChanged(final CharSequence s,final int start,final int before,final int count) |
| 45 | + {} |
| 46 | + |
| 47 | + @Override |
| 48 | + public void beforeTextChanged(final CharSequence s,final int start,final int count,final int after) |
| 49 | + {} |
| 50 | + |
| 51 | + @Override |
| 52 | + public void afterTextChanged(final Editable s) |
| 53 | + { |
| 54 | + recreateTextView(); |
| 55 | + } |
| 56 | + }); |
| 57 | + _widthSeekBar=(SeekBar)findViewById(R.id.widthSeekBar); |
| 58 | + _heightSeekBar=(SeekBar)findViewById(R.id.heightSeekBar); |
| 59 | + final OnSeekBarChangeListener seekBarChangeListener=new OnSeekBarChangeListener() |
| 60 | + { |
| 61 | + @Override |
| 62 | + public void onStopTrackingTouch(final SeekBar seekBar) |
| 63 | + {} |
| 64 | + |
| 65 | + @Override |
| 66 | + public void onStartTrackingTouch(final SeekBar seekBar) |
| 67 | + {} |
| 68 | + |
| 69 | + @Override |
| 70 | + public void onProgressChanged(final SeekBar seekBar,final int progress,final boolean fromUser) |
| 71 | + { |
| 72 | + recreateTextView(); |
| 73 | + } |
| 74 | + }; |
| 75 | + _heightSeekBar.setOnSeekBarChangeListener(seekBarChangeListener); |
| 76 | + _widthSeekBar.setOnSeekBarChangeListener(seekBarChangeListener); |
| 77 | + _linesCountTextView=(TextView)findViewById(R.id.linesCountTextView); |
| 78 | + findViewById(R.id.plusLineCountButton).setOnClickListener(new OnClickListener() |
31 | 79 | { |
32 | 80 | @Override |
33 | 81 | public void onClick(final View v) |
34 | 82 | { |
35 | | - container.removeAllViews(); |
36 | | - final int maxWidth=container.getWidth(); |
37 | | - final int maxHeight=container.getHeight(); |
38 | | - final AutoResizeTextView textView=new AutoResizeTextView(MainActivity.this); |
39 | | - textView.setGravity(Gravity.CENTER); |
40 | | - final int width=_random.nextInt(maxWidth)+1; |
41 | | - final int height=_random.nextInt(maxHeight)+1; |
42 | | - final int maxLines=_random.nextInt(4)+1; |
43 | | - textView.setMaxLines(maxLines); |
44 | | - textView.setTextSize(500); |
45 | | - textView.setEllipsize(TruncateAt.END); |
46 | | - // since we use it only once per each click, we don't need to cache the results, ever |
47 | | - textView.setEnableSizeCache(false); |
48 | | - textView.setEnableSizeCache(false); |
49 | | - textView.setLayoutParams(new LayoutParams(width,height)); |
50 | | - textView.setBackgroundColor(0xff00ff00); |
51 | | - final String text=getRandomText(); |
52 | | - descriptionTextView.setText("length:"+text.length()+" maxLines:"+maxLines+" width:"+width+" height:"+height); |
53 | | - textView.setText(text); |
54 | | - container.addView(textView); |
| 83 | + int maxLinesCount=Integer.parseInt(_linesCountTextView.getText().toString()); |
| 84 | + _linesCountTextView.setText(Integer.toString(++maxLinesCount)); |
| 85 | + recreateTextView(); |
| 86 | + } |
| 87 | + }); |
| 88 | + findViewById(R.id.minusLineCountButton).setOnClickListener(new OnClickListener() |
| 89 | + { |
| 90 | + @Override |
| 91 | + public void onClick(final View v) |
| 92 | + { |
| 93 | + int maxLinesCount=Integer.parseInt(_linesCountTextView.getText().toString()); |
| 94 | + if(maxLinesCount==1) |
| 95 | + return; |
| 96 | + _linesCountTextView.setText(Integer.toString(--maxLinesCount)); |
| 97 | + recreateTextView(); |
| 98 | + } |
| 99 | + }); |
| 100 | + runJustBeforeBeingDrawn(_textViewcontainer,new Runnable() |
| 101 | + { |
| 102 | + @Override |
| 103 | + public void run() |
| 104 | + { |
| 105 | + recreateTextView(); |
55 | 106 | } |
56 | 107 | }); |
57 | 108 | } |
58 | 109 |
|
59 | | - private String getRandomText() |
| 110 | + protected void recreateTextView() |
60 | 111 | { |
61 | | - final int textLength=_random.nextInt(20)+1; |
62 | | - final StringBuilder builder=new StringBuilder(); |
63 | | - for(int i=0;i<textLength;++i) |
64 | | - builder.append(ALLOWED_CHARACTERS.charAt(_random.nextInt(ALLOWED_CHARACTERS.length()))); |
65 | | - return builder.toString(); |
| 112 | + _textViewcontainer.removeAllViews(); |
| 113 | + final int maxWidth=_textViewcontainer.getWidth(); |
| 114 | + final int maxHeight=_textViewcontainer.getHeight(); |
| 115 | + final AutoResizeTextView textView=new AutoResizeTextView(MainActivity.this); |
| 116 | + textView.setGravity(Gravity.CENTER); |
| 117 | + final int width=_widthSeekBar.getProgress()*maxWidth/_widthSeekBar.getMax(); |
| 118 | + final int height=_heightSeekBar.getProgress()*maxHeight/_heightSeekBar.getMax(); |
| 119 | + final int maxLinesCount=Integer.parseInt(_linesCountTextView.getText().toString()); |
| 120 | + textView.setMaxLines(maxLinesCount); |
| 121 | + textView.setTextSize(TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP,maxHeight,getResources().getDisplayMetrics())); |
| 122 | + textView.setEllipsize(TruncateAt.END); |
| 123 | + // since we use it only once per each click, we don't need to cache the results, ever |
| 124 | + textView.setEnableSizeCache(false); |
| 125 | + textView.setEnableSizeCache(false); |
| 126 | + textView.setLayoutParams(new LayoutParams(width,height)); |
| 127 | + textView.setBackgroundColor(0xff00ff00); |
| 128 | + final String text=_contentEditText.getText().toString(); |
| 129 | + textView.setText(text); |
| 130 | + _textViewcontainer.addView(textView); |
66 | 131 | } |
67 | 132 |
|
| 133 | + // private String getRandomText() |
| 134 | + // { |
| 135 | + // final int textLength=_random.nextInt(20)+1; |
| 136 | + // final StringBuilder builder=new StringBuilder(); |
| 137 | + // for(int i=0;i<textLength;++i) |
| 138 | + // builder.append(ALLOWED_CHARACTERS.charAt(_random.nextInt(ALLOWED_CHARACTERS.length()))); |
| 139 | + // return builder.toString(); |
| 140 | + // } |
68 | 141 | @Override |
69 | 142 | public boolean onCreateOptionsMenu(final Menu menu) |
70 | 143 | { |
@@ -96,4 +169,19 @@ public boolean onOptionsItemSelected(final MenuItem item) |
96 | 169 | startActivity(intent); |
97 | 170 | return true; |
98 | 171 | } |
| 172 | + |
| 173 | + public static void runJustBeforeBeingDrawn(final View view,final Runnable runnable) |
| 174 | + { |
| 175 | + final OnPreDrawListener preDrawListener=new OnPreDrawListener() |
| 176 | + { |
| 177 | + @Override |
| 178 | + public boolean onPreDraw() |
| 179 | + { |
| 180 | + runnable.run(); |
| 181 | + view.getViewTreeObserver().removeOnPreDrawListener(this); |
| 182 | + return true; |
| 183 | + } |
| 184 | + }; |
| 185 | + view.getViewTreeObserver().addOnPreDrawListener(preDrawListener); |
| 186 | + } |
99 | 187 | } |
0 commit comments