diff --git a/.idea/caches/build_file_checksums.ser b/.idea/caches/build_file_checksums.ser index bf01341..7e1385d 100644 Binary files a/.idea/caches/build_file_checksums.ser and b/.idea/caches/build_file_checksums.ser differ diff --git a/.idea/caches/gradle_models.ser b/.idea/caches/gradle_models.ser index 2fc2323..7675afe 100644 Binary files a/.idea/caches/gradle_models.ser and b/.idea/caches/gradle_models.ser differ diff --git a/.idea/misc.xml b/.idea/misc.xml index 6ab3d89..b8ca955 100644 --- a/.idea/misc.xml +++ b/.idea/misc.xml @@ -12,7 +12,7 @@ - + diff --git a/AutoFitTextViewLibrary/build.gradle b/AutoFitTextViewLibrary/build.gradle index 0eb80c2..7937b5f 100644 --- a/AutoFitTextViewLibrary/build.gradle +++ b/AutoFitTextViewLibrary/build.gradle @@ -1,4 +1,6 @@ apply plugin: 'com.android.library' +apply plugin: 'kotlin-android-extensions' +apply plugin: 'kotlin-android' android { compileSdkVersion 28 @@ -27,8 +29,17 @@ android { minifyEnabled false } } + compileOptions { + sourceCompatibility = '1.8' + targetCompatibility = '1.8' + } } dependencies { implementation 'androidx.appcompat:appcompat:1.0.2' + compile "androidx.core:core-ktx:+" + implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version" +} +repositories { + mavenCentral() } diff --git a/AutoFitTextViewLibrary/src/com/lb/auto_fit_textview/AutoResizeTextView.java b/AutoFitTextViewLibrary/src/com/lb/auto_fit_textview/AutoResizeTextView.java deleted file mode 100644 index f6b60bd..0000000 --- a/AutoFitTextViewLibrary/src/com/lb/auto_fit_textview/AutoResizeTextView.java +++ /dev/null @@ -1,254 +0,0 @@ -package com.lb.auto_fit_textview; - -import android.annotation.TargetApi; -import android.content.Context; -import android.content.res.Resources; -import android.graphics.RectF; -import android.graphics.Typeface; -import android.os.Build; -import android.text.Layout.Alignment; -import android.text.StaticLayout; -import android.text.TextPaint; -import android.text.method.TransformationMethod; -import android.util.AttributeSet; -import android.util.TypedValue; - -import androidx.appcompat.widget.AppCompatTextView; - -/** - * a textView that is able to self-adjust its font size depending on the min and max size of the font, and its own size.
- * code is heavily based on this StackOverflow thread: - * http://stackoverflow.com/questions/16017165/auto-fit-textview-for-android/21851239#21851239
- * It should work fine with most Android versions, but might have some issues on Android 3.1 - 4.04, as setTextSize will only work for the first time.
- * More info here: https://code.google.com/p/android/issues/detail?id=22493 and here in case you wish to fix it: http://stackoverflow.com/a/21851239/878126 - */ -public class AutoResizeTextView extends AppCompatTextView { - private static final int NO_LINE_LIMIT = -1; - private final RectF _availableSpaceRect = new RectF(); - private final SizeTester _sizeTester; - private float _maxTextSize, _spacingMult = 1.0f, _spacingAdd = 0.0f, _minTextSize; - private int _widthLimit, _maxLines; - private boolean _initialized = false; - private TextPaint _paint; - - private interface SizeTester { - /** - * @param suggestedSize Size of text to be tested - * @param availableSpace available space in which text must fit - * @return an integer < 0 if after applying {@code suggestedSize} to - * text, it takes less space than {@code availableSpace}, > 0 - * otherwise - */ - int onTestSize(int suggestedSize, RectF availableSpace); - } - - public AutoResizeTextView(final Context context) { - this(context, null, android.R.attr.textViewStyle); - } - - public AutoResizeTextView(final Context context, final AttributeSet attrs) { - this(context, attrs, android.R.attr.textViewStyle); - } - - public AutoResizeTextView(final Context context, final AttributeSet attrs, final int defStyle) { - super(context, attrs, defStyle); - // using the minimal recommended font size - _minTextSize = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, 12, getResources().getDisplayMetrics()); - _maxTextSize = getTextSize(); - _paint = new TextPaint(getPaint()); - if (_maxLines == 0) - // no value was assigned during construction - _maxLines = NO_LINE_LIMIT; - // prepare size tester: - _sizeTester = new SizeTester() { - final RectF textRect = new RectF(); - - @TargetApi(Build.VERSION_CODES.JELLY_BEAN) - @Override - public int onTestSize(final int suggestedSize, final RectF availableSpace) { - _paint.setTextSize(suggestedSize); - final TransformationMethod transformationMethod = getTransformationMethod(); - final String text; - if (transformationMethod != null) - text = transformationMethod.getTransformation(getText(), AutoResizeTextView.this).toString(); - else - text = getText().toString(); - final boolean singleLine = getMaxLines() == 1; - if (singleLine) { - textRect.bottom = _paint.getFontSpacing(); - textRect.right = _paint.measureText(text); - } else { - final StaticLayout layout = new StaticLayout(text, _paint, _widthLimit, Alignment.ALIGN_NORMAL, _spacingMult, _spacingAdd, true); - // return early if we have more lines - if (getMaxLines() != NO_LINE_LIMIT && layout.getLineCount() > getMaxLines()) - return 1; - textRect.bottom = layout.getHeight(); - int maxWidth = -1; - int lineCount = layout.getLineCount(); - for (int i = 0; i < lineCount; i++) { - int end = layout.getLineEnd(i); - if (i < lineCount - 1 && end > 0 && !isValidWordWrap(text.charAt(end - 1), text.charAt(end))) - return 1; - if (maxWidth < layout.getLineRight(i) - layout.getLineLeft(i)) - maxWidth = (int) layout.getLineRight(i) - (int) layout.getLineLeft(i); - } - //for (int i = 0; i < layout.getLineCount(); i++) - // if (maxWidth < layout.getLineRight(i) - layout.getLineLeft(i)) - // maxWidth = (int) layout.getLineRight(i) - (int) layout.getLineLeft(i); - textRect.right = maxWidth; - } - textRect.offsetTo(0, 0); - if (availableSpace.contains(textRect)) - // may be too small, don't worry we will find the best match - return -1; - // else, too big - return 1; - } - }; - _initialized = true; - } - - public boolean isValidWordWrap(char before, char after) { - return before == ' ' || before == '-'; - } - - @Override - public void setAllCaps(boolean allCaps) { - super.setAllCaps(allCaps); - adjustTextSize(); - } - - @Override - public void setTypeface(final Typeface tf) { - super.setTypeface(tf); - adjustTextSize(); - } - - @Override - public void setTextSize(final float size) { - _maxTextSize = size; - adjustTextSize(); - } - - @Override - public void setMaxLines(final int maxLines) { - super.setMaxLines(maxLines); - _maxLines = maxLines; - adjustTextSize(); - } - - @Override - public int getMaxLines() { - return _maxLines; - } - - @Override - public void setSingleLine() { - super.setSingleLine(); - _maxLines = 1; - adjustTextSize(); - } - - @Override - public void setSingleLine(final boolean singleLine) { - super.setSingleLine(singleLine); - if (singleLine) - _maxLines = 1; - else _maxLines = NO_LINE_LIMIT; - adjustTextSize(); - } - - @Override - public void setLines(final int lines) { - super.setLines(lines); - _maxLines = lines; - adjustTextSize(); - } - - @Override - public void setTextSize(final int unit, final float size) { - final Context c = getContext(); - Resources r; - if (c == null) - r = Resources.getSystem(); - else r = c.getResources(); - _maxTextSize = TypedValue.applyDimension(unit, size, r.getDisplayMetrics()); - adjustTextSize(); - } - - @Override - public void setLineSpacing(final float add, final float mult) { - super.setLineSpacing(add, mult); - _spacingMult = mult; - _spacingAdd = add; - } - - /** - * Set the lower text size limit and invalidate the view - * - * @param minTextSize - */ - public void setMinTextSize(final float minTextSize) { - _minTextSize = minTextSize; - adjustTextSize(); - } - - private void adjustTextSize() { - // This is a workaround for truncated text issue on ListView, as shown here: https://github.com/AndroidDeveloperLB/AutoFitTextView/pull/14 - // TODO think of a nicer, elegant solution. -// post(new Runnable() -// { -// @Override -// public void run() -// { - if (!_initialized) - return; - final int startSize = (int) _minTextSize; - final int heightLimit = getMeasuredHeight() - getCompoundPaddingBottom() - getCompoundPaddingTop(); - _widthLimit = getMeasuredWidth() - getCompoundPaddingLeft() - getCompoundPaddingRight(); - if (_widthLimit <= 0) - return; - _paint = new TextPaint(getPaint()); - _availableSpaceRect.right = _widthLimit; - _availableSpaceRect.bottom = heightLimit; - superSetTextSize(startSize); -// } -// }); - } - - private void superSetTextSize(int startSize) { - int textSize = binarySearch(startSize, (int) _maxTextSize, _sizeTester, _availableSpaceRect); - super.setTextSize(TypedValue.COMPLEX_UNIT_PX, textSize); - } - - private int binarySearch(final int start, final int end, final SizeTester sizeTester, final RectF availableSpace) { - int lastBest = start, lo = start, hi = end - 1, mid; - while (lo <= hi) { - mid = lo + hi >>> 1; - final int midValCmp = sizeTester.onTestSize(mid, availableSpace); - if (midValCmp < 0) { - lastBest = lo; - lo = mid + 1; - } else if (midValCmp > 0) { - hi = mid - 1; - lastBest = hi; - } else return mid; - } - // make sure to return last best - // this is what should always be returned - return lastBest; - } - - @Override - protected void onTextChanged(final CharSequence text, final int start, final int before, final int after) { - super.onTextChanged(text, start, before, after); - adjustTextSize(); - } - - @Override - protected void onSizeChanged(final int width, final int height, final int oldwidth, final int oldheight) { - super.onSizeChanged(width, height, oldwidth, oldheight); - if (width != oldwidth || height != oldheight) - adjustTextSize(); - } -} diff --git a/AutoFitTextViewLibrary/src/com/lb/auto_fit_textview/AutoResizeTextView.kt b/AutoFitTextViewLibrary/src/com/lb/auto_fit_textview/AutoResizeTextView.kt new file mode 100644 index 0000000..e25a84b --- /dev/null +++ b/AutoFitTextViewLibrary/src/com/lb/auto_fit_textview/AutoResizeTextView.kt @@ -0,0 +1,240 @@ +package com.lb.auto_fit_textview + +import android.annotation.TargetApi +import android.content.Context +import android.content.res.Resources +import android.graphics.RectF +import android.graphics.Typeface +import android.os.Build +import android.text.Layout.Alignment +import android.text.StaticLayout +import android.text.TextPaint +import android.util.AttributeSet +import android.util.TypedValue +import androidx.appcompat.widget.AppCompatTextView + +/** + * a textView that is able to self-adjust its font size depending on the min and max size of the font, and its own size.

+ * code is heavily based on this StackOverflow thread: + * http://stackoverflow.com/questions/16017165/auto-fit-textview-for-android/21851239#21851239

+ * It should work fine with most Android versions, but might have some issues on Android 3.1 - 4.04, as setTextSize will only work for the first time.

+ * More info here: https://code.google.com/p/android/issues/detail?id=22493 and here in case you wish to fix it: http://stackoverflow.com/a/21851239/878126 + */ +class AutoResizeTextView @JvmOverloads constructor(context: Context, attrs: AttributeSet? = null, defStyle: Int = android.R.attr.textViewStyle) : AppCompatTextView(context, attrs, defStyle) { + private val _availableSpaceRect = RectF() + private val _sizeTester: SizeTester + private var _maxTextSize: Float = 0.toFloat() + private var _spacingMult = 1.0f + private var _spacingAdd = 0.0f + private var _minTextSize: Float = 0.toFloat() + private var _widthLimit: Int = 0 + private var _maxLines: Int = 0 + private var _initialized = false + private var _paint: TextPaint? = null + + private interface SizeTester { + /** + * @param suggestedSize Size of text to be tested + * @param availableSpace available space in which text must fit + * @return an integer < 0 if after applying `suggestedSize` to + * text, it takes less space than `availableSpace`, > 0 + * otherwise + */ + fun onTestSize(suggestedSize: Int, availableSpace: RectF): Int + } + + init { + // using the minimal recommended font size + _minTextSize = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, 12f, resources.displayMetrics) + _maxTextSize = textSize + _paint = TextPaint(paint) + if (_maxLines == 0) + // no value was assigned during construction + _maxLines = NO_LINE_LIMIT + // prepare size tester: + _sizeTester = object : SizeTester { + internal val textRect = RectF() + + @TargetApi(Build.VERSION_CODES.JELLY_BEAN) + override fun onTestSize(suggestedSize: Int, availableSpace: RectF): Int { + _paint!!.textSize = suggestedSize.toFloat() + val transformationMethod = transformationMethod + val text: String + if (transformationMethod != null) + text = transformationMethod.getTransformation(getText(), this@AutoResizeTextView).toString() + else + text = getText().toString() + val singleLine = maxLines == 1 + if (singleLine) { + textRect.bottom = _paint!!.fontSpacing + textRect.right = _paint!!.measureText(text) + } else { + val layout = StaticLayout(text, _paint, _widthLimit, Alignment.ALIGN_NORMAL, _spacingMult, _spacingAdd, true) + // return early if we have more lines + if (maxLines != NO_LINE_LIMIT && layout.lineCount > maxLines) + return 1 + textRect.bottom = layout.height.toFloat() + var maxWidth = -1 + val lineCount = layout.lineCount + for (i in 0 until lineCount) { + val end = layout.getLineEnd(i) + if (i < lineCount - 1 && end > 0 && !isValidWordWrap(text[end - 1], text[end])) + return 1 + if (maxWidth < layout.getLineRight(i) - layout.getLineLeft(i)) + maxWidth = layout.getLineRight(i).toInt() - layout.getLineLeft(i).toInt() + } + //for (int i = 0; i < layout.getLineCount(); i++) + // if (maxWidth < layout.getLineRight(i) - layout.getLineLeft(i)) + // maxWidth = (int) layout.getLineRight(i) - (int) layout.getLineLeft(i); + textRect.right = maxWidth.toFloat() + } + textRect.offsetTo(0f, 0f) + return if (availableSpace.contains(textRect)) -1 else 1 + // else, too big + } + } + _initialized = true + } + + fun isValidWordWrap(before: Char, after: Char): Boolean { + return before == ' ' || before == '-' + } + + override fun setAllCaps(allCaps: Boolean) { + super.setAllCaps(allCaps) + adjustTextSize() + } + + override fun setTypeface(tf: Typeface?) { + super.setTypeface(tf) + adjustTextSize() + } + + override fun setTextSize(size: Float) { + _maxTextSize = size + adjustTextSize() + } + + override fun setMaxLines(maxLines: Int) { + super.setMaxLines(maxLines) + _maxLines = maxLines + adjustTextSize() + } + + override fun getMaxLines(): Int { + return _maxLines + } + + override fun setSingleLine() { + super.setSingleLine() + _maxLines = 1 + adjustTextSize() + } + + override fun setSingleLine(singleLine: Boolean) { + super.setSingleLine(singleLine) + if (singleLine) + _maxLines = 1 + else + _maxLines = NO_LINE_LIMIT + adjustTextSize() + } + + override fun setLines(lines: Int) { + super.setLines(lines) + _maxLines = lines + adjustTextSize() + } + + override fun setTextSize(unit: Int, size: Float) { + val c = context + val r: Resources + r = if (c == null) + Resources.getSystem() + else + c.resources + _maxTextSize = TypedValue.applyDimension(unit, size, r.displayMetrics) + adjustTextSize() + } + + override fun setLineSpacing(add: Float, mult: Float) { + super.setLineSpacing(add, mult) + _spacingMult = mult + _spacingAdd = add + } + + /** + * Set the lower text size limit and invalidate the view + * + * @param minTextSize + */ + fun setMinTextSize(minTextSize: Float) { + _minTextSize = minTextSize + adjustTextSize() + } + + private fun adjustTextSize() { + // This is a workaround for truncated text issue on ListView, as shown here: https://github.com/AndroidDeveloperLB/AutoFitTextView/pull/14 + // TODO think of a nicer, elegant solution. + // post(new Runnable() + // { + // @Override + // public void run() + // { + if (!_initialized) + return + val startSize = _minTextSize.toInt() + val heightLimit = measuredHeight - compoundPaddingBottom - compoundPaddingTop + _widthLimit = measuredWidth - compoundPaddingLeft - compoundPaddingRight + if (_widthLimit <= 0) + return + _paint = TextPaint(paint) + _availableSpaceRect.right = _widthLimit.toFloat() + _availableSpaceRect.bottom = heightLimit.toFloat() + superSetTextSize(startSize) + // } + // }); + } + + private fun superSetTextSize(startSize: Int) { + val textSize = binarySearch(startSize, _maxTextSize.toInt(), _sizeTester, _availableSpaceRect) + super.setTextSize(TypedValue.COMPLEX_UNIT_PX, textSize.toFloat()) + } + + private fun binarySearch(start: Int, end: Int, sizeTester: SizeTester, availableSpace: RectF): Int { + var lastBest = start + var lo = start + var hi = end - 1 + var mid: Int + while (lo <= hi) { + mid = (lo + hi).ushr(1) + val midValCmp = sizeTester.onTestSize(mid, availableSpace) + if (midValCmp < 0) { + lastBest = lo + lo = mid + 1 + } else if (midValCmp > 0) { + hi = mid - 1 + lastBest = hi + } else + return mid + } + // make sure to return last best + // this is what should always be returned + return lastBest + } + + override fun onTextChanged(text: CharSequence, start: Int, before: Int, after: Int) { + super.onTextChanged(text, start, before, after) + adjustTextSize() + } + + override fun onSizeChanged(width: Int, height: Int, oldwidth: Int, oldheight: Int) { + super.onSizeChanged(width, height, oldwidth, oldheight) + if (width != oldwidth || height != oldheight) + adjustTextSize() + } + + companion object { + private val NO_LINE_LIMIT = -1 + } +} diff --git a/AutoFitTextViewSample/build.gradle b/AutoFitTextViewSample/build.gradle index 6640526..3c10f6e 100644 --- a/AutoFitTextViewSample/build.gradle +++ b/AutoFitTextViewSample/build.gradle @@ -1,4 +1,6 @@ apply plugin: 'com.android.application' +apply plugin: 'kotlin-android-extensions' +apply plugin: 'kotlin-android' android { compileSdkVersion 28 @@ -27,6 +29,10 @@ android { assets.srcDirs = ['assets'] } } + compileOptions { + sourceCompatibility = '1.8' + targetCompatibility = '1.8' + } } @@ -35,4 +41,9 @@ dependencies { implementation 'androidx.appcompat:appcompat:1.0.2' implementation 'androidx.recyclerview:recyclerview:1.0.0' implementation project(':AutoFitTextViewLibrary') + compile "androidx.core:core-ktx:+" + implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version" +} +repositories { + mavenCentral() } diff --git a/AutoFitTextViewSample/src/com/example/autofittextviewsample/Main2Activity.java b/AutoFitTextViewSample/src/com/example/autofittextviewsample/Main2Activity.java deleted file mode 100644 index 2b825cc..0000000 --- a/AutoFitTextViewSample/src/com/example/autofittextviewsample/Main2Activity.java +++ /dev/null @@ -1,51 +0,0 @@ -package com.example.autofittextviewsample; - -import android.os.Bundle; -import androidx.appcompat.app.AppCompatActivity; -import androidx.recyclerview.widget.LinearLayoutManager; -import androidx.recyclerview.widget.RecyclerView; -import androidx.recyclerview.widget.RecyclerView.Adapter; -import android.view.LayoutInflater; -import android.view.View; -import android.view.ViewGroup; -import android.widget.TextView; - -public class Main2Activity extends AppCompatActivity { - - @Override - protected void onCreate(Bundle savedInstanceState) { - super.onCreate(savedInstanceState); - setContentView(R.layout.activity_main2); - RecyclerView recyclerView = (RecyclerView) findViewById(android.R.id.list); - recyclerView.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false)); - recyclerView.setAdapter(new Adapter() { - @Override - public ViewHolder onCreateViewHolder(final ViewGroup parent, final int viewType) { - return new ViewHolder(LayoutInflater.from(Main2Activity.this).inflate(R.layout.item, parent, false)) { - }; - } - - @Override - public void onBindViewHolder(final ViewHolder holder, final int position) { - StringBuilder sb = new StringBuilder("item:"); - for (int i = 0; i <= position; ++i) - sb.append(Integer.toString(position)); - holder.textView.setText(sb); - } - - @Override - public int getItemCount() { - return 50; - } - }); - } - - private static class ViewHolder extends RecyclerView.ViewHolder { - final TextView textView; - - public ViewHolder(final View itemView) { - super(itemView); - textView = (TextView) itemView.findViewById(android.R.id.text1); - } - } -} diff --git a/AutoFitTextViewSample/src/com/example/autofittextviewsample/Main2Activity.kt b/AutoFitTextViewSample/src/com/example/autofittextviewsample/Main2Activity.kt new file mode 100644 index 0000000..7cecc61 --- /dev/null +++ b/AutoFitTextViewSample/src/com/example/autofittextviewsample/Main2Activity.kt @@ -0,0 +1,47 @@ +package com.example.autofittextviewsample + +import android.os.Bundle +import android.view.LayoutInflater +import android.view.View +import android.view.ViewGroup +import android.widget.TextView +import androidx.appcompat.app.AppCompatActivity +import androidx.recyclerview.widget.LinearLayoutManager +import androidx.recyclerview.widget.RecyclerView +import androidx.recyclerview.widget.RecyclerView.Adapter + +class Main2Activity : AppCompatActivity() { + + override fun onCreate(savedInstanceState: Bundle?) { + super.onCreate(savedInstanceState) + setContentView(R.layout.activity_main2) + val recyclerView = findViewById(android.R.id.list) as RecyclerView + recyclerView.layoutManager = LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false) + recyclerView.adapter = object : Adapter() { + override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder { + return object : ViewHolder(LayoutInflater.from(this@Main2Activity).inflate(R.layout.item, parent, false)) { + + } + } + + override fun onBindViewHolder(holder: ViewHolder, position: Int) { + val sb = StringBuilder("item:") + for (i in 0..position) + sb.append(Integer.toString(position)) + holder.textView.text = sb + } + + override fun getItemCount(): Int { + return 50 + } + } + } + + private open class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) { + internal val textView: TextView + + init { + textView = itemView.findViewById(android.R.id.text1) as TextView + } + } +} diff --git a/AutoFitTextViewSample/src/com/example/autofittextviewsample/MainActivity.java b/AutoFitTextViewSample/src/com/example/autofittextviewsample/MainActivity.java deleted file mode 100644 index b110f1a..0000000 --- a/AutoFitTextViewSample/src/com/example/autofittextviewsample/MainActivity.java +++ /dev/null @@ -1,173 +0,0 @@ -package com.example.autofittextviewsample; - -import android.content.Intent; -import android.net.Uri; -import android.os.Bundle; -import androidx.appcompat.app.AppCompatActivity; -import android.text.Editable; -import android.text.TextUtils.TruncateAt; -import android.text.TextWatcher; -import android.util.TypedValue; -import android.view.Gravity; -import android.view.Menu; -import android.view.MenuItem; -import android.view.View; -import android.view.View.OnClickListener; -import android.view.ViewGroup; -import android.view.ViewGroup.LayoutParams; -import android.view.ViewTreeObserver.OnPreDrawListener; -import android.widget.EditText; -import android.widget.SeekBar; -import android.widget.SeekBar.OnSeekBarChangeListener; -import android.widget.TextView; - -import com.lb.auto_fit_textview.AutoResizeTextView; - -public class MainActivity extends AppCompatActivity { - // private final Random _random =new Random(); - // private static final String ALLOWED_CHARACTERS ="qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM1234567890"; - private EditText _contentEditText; - private ViewGroup _textViewcontainer; - private SeekBar _widthSeekBar; - private SeekBar _heightSeekBar; - private TextView _linesCountTextView; - - @Override - protected void onCreate(final Bundle savedInstanceState) { - super.onCreate(savedInstanceState); - setContentView(R.layout.activity_main); - _textViewcontainer = (ViewGroup) findViewById(R.id.container); - _contentEditText = (EditText) findViewById(R.id.contentEditText); - _contentEditText.addTextChangedListener(new TextWatcher() { - @Override - public void onTextChanged(final CharSequence s, final int start, final int before, final int count) { - } - - @Override - public void beforeTextChanged(final CharSequence s, final int start, final int count, final int after) { - } - - @Override - public void afterTextChanged(final Editable s) { - recreateTextView(); - } - }); - _widthSeekBar = (SeekBar) findViewById(R.id.widthSeekBar); - _heightSeekBar = (SeekBar) findViewById(R.id.heightSeekBar); - final OnSeekBarChangeListener seekBarChangeListener = new OnSeekBarChangeListener() { - @Override - public void onStopTrackingTouch(final SeekBar seekBar) { - } - - @Override - public void onStartTrackingTouch(final SeekBar seekBar) { - } - - @Override - public void onProgressChanged(final SeekBar seekBar, final int progress, final boolean fromUser) { - recreateTextView(); - } - }; - _heightSeekBar.setOnSeekBarChangeListener(seekBarChangeListener); - _widthSeekBar.setOnSeekBarChangeListener(seekBarChangeListener); - _linesCountTextView = (TextView) findViewById(R.id.linesCountTextView); - findViewById(R.id.plusLineCountButton).setOnClickListener(new OnClickListener() { - @Override - public void onClick(final View v) { - int maxLinesCount = Integer.parseInt(_linesCountTextView.getText().toString()); - _linesCountTextView.setText(Integer.toString(++maxLinesCount)); - recreateTextView(); - } - }); - findViewById(R.id.minusLineCountButton).setOnClickListener(new OnClickListener() { - @Override - public void onClick(final View v) { - int maxLinesCount = Integer.parseInt(_linesCountTextView.getText().toString()); - if (maxLinesCount == 1) - return; - _linesCountTextView.setText(Integer.toString(--maxLinesCount)); - recreateTextView(); - } - }); - runJustBeforeBeingDrawn(_textViewcontainer, new Runnable() { - @Override - public void run() { - recreateTextView(); - } - }); - - - } - - protected void recreateTextView() { - _textViewcontainer.removeAllViews(); - final int maxWidth = _textViewcontainer.getWidth(); - final int maxHeight = _textViewcontainer.getHeight(); - final AutoResizeTextView textView = new AutoResizeTextView(MainActivity.this); - textView.setGravity(Gravity.CENTER); - final int width = _widthSeekBar.getProgress() * maxWidth / _widthSeekBar.getMax(); - final int height = _heightSeekBar.getProgress() * maxHeight / _heightSeekBar.getMax(); - final int maxLinesCount = Integer.parseInt(_linesCountTextView.getText().toString()); - textView.setMaxLines(maxLinesCount); - textView.setTextSize(TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, maxHeight, getResources().getDisplayMetrics())); - textView.setEllipsize(TruncateAt.END); - // since we use it only once per each click, we don't need to cache the results, ever - textView.setLayoutParams(new LayoutParams(width, height)); - textView.setBackgroundColor(0xff00ff00); - final String text = _contentEditText.getText().toString(); - textView.setText(text); - _textViewcontainer.addView(textView); - } - - // private String getRandomText() - // { - // final int textLength=_random.nextInt(20)+1; - // final StringBuilder builder=new StringBuilder(); - // for(int i=0;i(R.id.container) as ViewGroup + _contentEditText = findViewById(R.id.contentEditText) as EditText + _contentEditText!!.addTextChangedListener(object : TextWatcher { + override fun onTextChanged(s: CharSequence, start: Int, before: Int, count: Int) {} + + override fun beforeTextChanged(s: CharSequence, start: Int, count: Int, after: Int) {} + + override fun afterTextChanged(s: Editable) { + recreateTextView() + } + }) + _widthSeekBar = findViewById(R.id.widthSeekBar) as SeekBar + _heightSeekBar = findViewById(R.id.heightSeekBar) as SeekBar + val seekBarChangeListener = object : OnSeekBarChangeListener { + override fun onStopTrackingTouch(seekBar: SeekBar) {} + + override fun onStartTrackingTouch(seekBar: SeekBar) {} + + override fun onProgressChanged(seekBar: SeekBar, progress: Int, fromUser: Boolean) { + recreateTextView() + } + } + _heightSeekBar!!.setOnSeekBarChangeListener(seekBarChangeListener) + _widthSeekBar!!.setOnSeekBarChangeListener(seekBarChangeListener) + _linesCountTextView = findViewById(R.id.linesCountTextView) as TextView + findViewById(R.id.plusLineCountButton).setOnClickListener { + var maxLinesCount = Integer.parseInt(_linesCountTextView!!.text.toString()) + _linesCountTextView!!.text = Integer.toString(++maxLinesCount) + recreateTextView() + } + findViewById(R.id.minusLineCountButton).setOnClickListener(OnClickListener { + var maxLinesCount = Integer.parseInt(_linesCountTextView!!.text.toString()) + if (maxLinesCount == 1) + return@OnClickListener + _linesCountTextView!!.text = Integer.toString(--maxLinesCount) + recreateTextView() + }) + runJustBeforeBeingDrawn(_textViewcontainer!!, Runnable { recreateTextView() }) + + + } + + protected fun recreateTextView() { + _textViewcontainer!!.removeAllViews() + val maxWidth = _textViewcontainer!!.width + val maxHeight = _textViewcontainer!!.height + val textView = AutoResizeTextView(this@MainActivity) + textView.gravity = Gravity.CENTER + val width = _widthSeekBar!!.progress * maxWidth / _widthSeekBar!!.max + val height = _heightSeekBar!!.progress * maxHeight / _heightSeekBar!!.max + val maxLinesCount = Integer.parseInt(_linesCountTextView!!.text.toString()) + textView.maxLines = maxLinesCount + textView.textSize = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, maxHeight.toFloat(), resources.displayMetrics) + textView.ellipsize = TruncateAt.END + // since we use it only once per each click, we don't need to cache the results, ever + textView.layoutParams = LayoutParams(width, height) + textView.setBackgroundColor(-0xff0100) + val text = _contentEditText!!.text.toString() + textView.text = text + _textViewcontainer!!.addView(textView) + } + + // private String getRandomText() + // { + // final int textLength=_random.nextInt(20)+1; + // final StringBuilder builder=new StringBuilder(); + // for(int i=0;i url = "https://play.google.com/store/apps/developer?id=AndroidDeveloperLB" + R.id.menuItem_all_my_repositories -> url = "https://github.com/AndroidDeveloperLB" + R.id.menuItem_current_repository_website -> url = "https://github.com/AndroidDeveloperLB/AutoFitTextView" + R.id.menuItem_show_recyclerViewSample -> { + startActivity(Intent(this, Main2Activity::class.java)) + return true + } + } + if (url == null) + return true + val intent = Intent(Intent.ACTION_VIEW, Uri.parse(url)) + intent.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY or Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET) + intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_MULTIPLE_TASK) + startActivity(intent) + return true + } + + companion object { + + fun runJustBeforeBeingDrawn(view: View, runnable: Runnable) { + val preDrawListener = object : OnPreDrawListener { + override fun onPreDraw(): Boolean { + runnable.run() + view.viewTreeObserver.removeOnPreDrawListener(this) + return true + } + } + view.viewTreeObserver.addOnPreDrawListener(preDrawListener) + } + } +} diff --git a/build.gradle b/build.gradle index d8ad1d0..c073375 100644 --- a/build.gradle +++ b/build.gradle @@ -1,12 +1,14 @@ // Top-level build file where you can add configuration options common to all sub-projects/modules. buildscript { + ext.kotlin_version = '1.3.21' repositories { jcenter() google() } dependencies { classpath 'com.android.tools.build:gradle:3.3.0' + classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version" // NOTE: Do not place your application dependencies here; they belong // in the individual module build.gradle files