Skip to content

Commit cfbfe0b

Browse files
author
AndroidDeveloperLB
committed
-improved sample to make nice manual properties changes, instead of randomizations.
-made the library set the minimal font size to be the minimal recommended by the documentation.
1 parent a2397f6 commit cfbfe0b

File tree

6 files changed

+223
-59
lines changed

6 files changed

+223
-59
lines changed

AutoFitTextViewLibrary/.classpath

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
<classpathentry exported="true" kind="con" path="com.android.ide.eclipse.adt.ANDROID_FRAMEWORK"/>
44
<classpathentry exported="true" kind="con" path="com.android.ide.eclipse.adt.LIBRARIES"/>
55
<classpathentry exported="true" kind="con" path="com.android.ide.eclipse.adt.DEPENDENCIES"/>
6-
<classpathentry kind="src" path="gen"/>
76
<classpathentry kind="src" path="src"/>
7+
<classpathentry kind="src" path="gen"/>
88
<classpathentry kind="output" path="bin/classes"/>
99
</classpath>

AutoFitTextViewLibrary/src/com/example/autofittextviewtest/AutoResizeTextView.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ public class AutoResizeTextView extends TextView
2727
private float mMaxTextSize;
2828
private float mSpacingMult =1.0f;
2929
private float mSpacingAdd =0.0f;
30-
private float mMinTextSize =20;
30+
private float mMinTextSize;
3131
private int mWidthLimit;
3232
private int mMaxLines;
3333
private boolean mEnableSizeCache =true;
@@ -61,6 +61,8 @@ public AutoResizeTextView(final Context context,final AttributeSet attrs)
6161
public AutoResizeTextView(final Context context,final AttributeSet attrs,final int defStyle)
6262
{
6363
super(context,attrs,defStyle);
64+
// using the minimal recommended font size
65+
mMinTextSize=TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP,12,getResources().getDisplayMetrics());
6466
mMaxTextSize=getTextSize();
6567
if(mMaxLines==0)
6668
// no value was assigned during construction

AutoFitTextViewSample/.classpath

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
<classpathentry exported="true" kind="con" path="com.android.ide.eclipse.adt.ANDROID_FRAMEWORK"/>
44
<classpathentry exported="true" kind="con" path="com.android.ide.eclipse.adt.LIBRARIES"/>
55
<classpathentry exported="true" kind="con" path="com.android.ide.eclipse.adt.DEPENDENCIES"/>
6-
<classpathentry kind="src" path="gen"/>
76
<classpathentry kind="src" path="src"/>
7+
<classpathentry kind="src" path="gen"/>
88
<classpathentry kind="output" path="bin/classes"/>
99
</classpath>

AutoFitTextViewSample/AndroidManifest.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,4 +24,4 @@
2424
</activity>
2525
</application>
2626

27-
</manifest>
27+
</manifest>

AutoFitTextViewSample/res/layout/activity_main.xml

Lines changed: 97 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -4,35 +4,109 @@
44
android:layout_height="match_parent"
55
tools:context=".MainActivity" >
66

7-
<Button
8-
android:id="@+id/randomizButton"
9-
android:layout_width="wrap_content"
7+
<RelativeLayout
8+
android:id="@+id/parametersContainer"
9+
android:layout_width="match_parent"
1010
android:layout_height="wrap_content"
11-
android:layout_alignParentBottom="true"
12-
android:layout_centerHorizontal="true"
13-
android:text="@string/randomize" />
11+
android:columnCount="2"
12+
android:orientation="horizontal"
13+
android:padding="4dp" >
14+
15+
<EditText
16+
android:id="@+id/contentEditText"
17+
android:layout_width="wrap_content"
18+
android:layout_height="wrap_content"
19+
android:layout_alignParentLeft="true"
20+
android:layout_alignParentTop="true"
21+
android:layout_alignRight="@+id/widthSeekBar"
22+
android:layout_gravity="fill_horizontal"
23+
android:ems="10"
24+
android:hint="content"
25+
android:text="content text sample" />
26+
27+
<Button
28+
android:id="@+id/minusLineCountButton"
29+
android:layout_width="wrap_content"
30+
android:layout_height="wrap_content"
31+
android:layout_below="@+id/contentEditText"
32+
android:layout_toRightOf="@+id/textView2"
33+
android:text="-" />
34+
35+
<TextView
36+
android:id="@+id/linesCountTextView"
37+
android:layout_width="wrap_content"
38+
android:layout_height="wrap_content"
39+
android:layout_alignBaseline="@+id/minusLineCountButton"
40+
android:layout_alignBottom="@+id/minusLineCountButton"
41+
android:layout_toRightOf="@+id/minusLineCountButton"
42+
android:paddingLeft="10dp"
43+
android:paddingRight="10dp"
44+
android:text="2" />
45+
46+
<Button
47+
android:id="@+id/plusLineCountButton"
48+
android:layout_width="wrap_content"
49+
android:layout_height="wrap_content"
50+
android:layout_below="@+id/contentEditText"
51+
android:layout_toRightOf="@+id/linesCountTextView"
52+
android:text="+" />
53+
54+
<TextView
55+
android:id="@+id/textView1"
56+
android:layout_width="wrap_content"
57+
android:layout_height="wrap_content"
58+
android:layout_alignParentLeft="true"
59+
android:layout_alignRight="@+id/minusLineCountButton"
60+
android:layout_alignTop="@+id/widthSeekBar"
61+
android:text="width:"
62+
android:textSize="20dp" />
63+
64+
<SeekBar
65+
android:id="@+id/widthSeekBar"
66+
android:layout_width="match_parent"
67+
android:layout_height="wrap_content"
68+
android:layout_below="@+id/minusLineCountButton"
69+
android:layout_toRightOf="@+id/TextView01"
70+
android:max="100"
71+
android:progress="50" />
72+
73+
<TextView
74+
android:id="@+id/TextView01"
75+
android:layout_width="wrap_content"
76+
android:layout_height="wrap_content"
77+
android:layout_alignParentLeft="true"
78+
android:layout_alignTop="@+id/heightSeekBar"
79+
android:text="height:"
80+
android:textSize="20dp" />
81+
82+
<SeekBar
83+
android:id="@+id/heightSeekBar"
84+
android:layout_width="wrap_content"
85+
android:layout_height="wrap_content"
86+
android:layout_alignParentRight="true"
87+
android:layout_below="@+id/widthSeekBar"
88+
android:layout_toRightOf="@+id/TextView01"
89+
android:max="100"
90+
android:progress="50" />
91+
92+
<TextView
93+
android:id="@+id/textView2"
94+
android:layout_width="wrap_content"
95+
android:layout_height="wrap_content"
96+
android:layout_alignBottom="@+id/minusLineCountButton"
97+
android:layout_alignParentLeft="true"
98+
android:layout_alignTop="@+id/minusLineCountButton"
99+
android:gravity="center_vertical"
100+
android:text="max lines count:" />
101+
</RelativeLayout>
14102

15103
<FrameLayout
16104
android:id="@+id/container"
17105
android:layout_width="match_parent"
18-
android:layout_height="wrap_content"
19-
android:layout_above="@+id/descTextView"
20-
android:layout_alignParentLeft="true"
21-
android:layout_alignParentRight="true"
22-
android:layout_alignParentTop="true"
106+
android:layout_height="match_parent"
107+
android:layout_alignParentBottom="true"
108+
android:layout_below="@+id/parametersContainer"
23109
android:background="#ffff0000" >
24110
</FrameLayout>
25111

26-
<TextView
27-
android:id="@+id/descTextView"
28-
android:layout_width="wrap_content"
29-
android:layout_height="wrap_content"
30-
android:layout_above="@+id/randomizButton"
31-
android:layout_centerHorizontal="true"
32-
android:ellipsize="marquee"
33-
android:focusable="true"
34-
android:focusableInTouchMode="true"
35-
android:singleLine="true"
36-
android:text="Click the randomize button to randomize textView properties" />
37-
38112
</RelativeLayout>
Lines changed: 120 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,70 +1,143 @@
11
package com.example.autofittextviewsample;
2-
import java.util.Random;
32
import android.app.Activity;
43
import android.content.Intent;
54
import android.net.Uri;
65
import android.os.Bundle;
6+
import android.text.Editable;
77
import android.text.TextUtils.TruncateAt;
8+
import android.text.TextWatcher;
9+
import android.util.TypedValue;
810
import android.view.Gravity;
911
import android.view.Menu;
1012
import android.view.MenuItem;
1113
import android.view.View;
1214
import android.view.View.OnClickListener;
1315
import android.view.ViewGroup;
1416
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;
1521
import android.widget.TextView;
1622
import com.example.autofittextviewtest.AutoResizeTextView;
1723

1824
public class MainActivity extends Activity
1925
{
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;
2233

2334
@Override
2435
protected void onCreate(final Bundle savedInstanceState)
2536
{
2637
super.onCreate(savedInstanceState);
2738
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()
3179
{
3280
@Override
3381
public void onClick(final View v)
3482
{
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();
55106
}
56107
});
57108
}
58109

59-
private String getRandomText()
110+
protected void recreateTextView()
60111
{
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);
66131
}
67132

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+
// }
68141
@Override
69142
public boolean onCreateOptionsMenu(final Menu menu)
70143
{
@@ -96,4 +169,19 @@ public boolean onOptionsItemSelected(final MenuItem item)
96169
startActivity(intent);
97170
return true;
98171
}
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+
}
99187
}

0 commit comments

Comments
 (0)