-
Notifications
You must be signed in to change notification settings - Fork 154
Expand file tree
/
Copy pathMainActivity.kt
More file actions
136 lines (123 loc) · 5.79 KB
/
MainActivity.kt
File metadata and controls
136 lines (123 loc) · 5.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
package com.example.autofittextviewsample
import android.content.Intent
import android.net.Uri
import android.os.Bundle
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.LayoutParams
import android.view.ViewTreeObserver.OnPreDrawListener
import android.widget.SeekBar
import android.widget.SeekBar.OnSeekBarChangeListener
import androidx.appcompat.app.AppCompatActivity
import com.lb.auto_fit_textview.AutoResizeTextView
import kotlinx.android.synthetic.main.activity_main.*
class MainActivity : AppCompatActivity() {
// private final Random _random =new Random();
// private static final String ALLOWED_CHARACTERS ="qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM1234567890";
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
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()
}
})
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)
findViewById<View>(R.id.plusLineCountButton).setOnClickListener {
var maxLinesCount = Integer.parseInt(linesCountTextView!!.text.toString())
linesCountTextView!!.text = Integer.toString(++maxLinesCount)
recreateTextView()
}
findViewById<View>(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<textLength;++i)
// builder.append(ALLOWED_CHARACTERS.charAt(_random.nextInt(ALLOWED_CHARACTERS.length())));
// return builder.toString();
// }
override fun onCreateOptionsMenu(menu: Menu): Boolean {
menuInflater.inflate(R.menu.main, menu)
return true
}
override fun onOptionsItemSelected(item: MenuItem): Boolean {
var url: String? = null
when (item.itemId) {
R.id.menuItem_all_my_apps -> 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
}
R.id.menuItem_show_dynamicResizeSample -> {
startActivity(Intent(this, DynamicResizeActivity::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)
}
}
}