-
Notifications
You must be signed in to change notification settings - Fork 154
Expand file tree
/
Copy pathDynamicResizeActivity.kt
More file actions
92 lines (80 loc) · 4.49 KB
/
DynamicResizeActivity.kt
File metadata and controls
92 lines (80 loc) · 4.49 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
package com.example.autofittextviewsample
import android.os.Bundle
import android.text.Editable
import android.text.TextWatcher
import android.view.View
import android.view.View.OnClickListener
import android.widget.SeekBar
import android.widget.SeekBar.OnSeekBarChangeListener
import androidx.appcompat.app.AppCompatActivity
import androidx.constraintlayout.widget.ConstraintLayout
import androidx.core.view.updateLayoutParams
import kotlinx.android.synthetic.main.activity_resize.*
class DynamicResizeActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_resize)
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) {
contentTextView!!.text = contentEditText!!.text.toString()
}
})
startSeekBar!!.setOnSeekBarChangeListener(object : OnSeekBarChangeListener {
override fun onStopTrackingTouch(seekBar: SeekBar) {}
override fun onStartTrackingTouch(seekBar: SeekBar) {}
override fun onProgressChanged(p0: SeekBar?, p1: Int, p2: Boolean) {
topStartMargin!!.updateLayoutParams<ConstraintLayout.LayoutParams> {
// textViewContainer!!.width / 2 so each margin only goes to the center
width = 1.coerceAtLeast(startSeekBar!!.progress * textViewContainer!!.width / 2 / startSeekBar!!.max)
}
}
})
topSeekBar!!.setOnSeekBarChangeListener(object : OnSeekBarChangeListener {
override fun onStopTrackingTouch(seekBar: SeekBar) {}
override fun onStartTrackingTouch(seekBar: SeekBar) {}
override fun onProgressChanged(p0: SeekBar?, p1: Int, p2: Boolean) {
topStartMargin!!.updateLayoutParams<ConstraintLayout.LayoutParams> {
// textViewContainer!!.height / 2 so each margin only goes to the center
height = 1.coerceAtLeast(topSeekBar!!.progress * textViewContainer!!.height / 2 / topSeekBar!!.max)
}
}
})
endSeekBar!!.setOnSeekBarChangeListener(object : OnSeekBarChangeListener {
override fun onStopTrackingTouch(seekBar: SeekBar) {}
override fun onStartTrackingTouch(seekBar: SeekBar) {}
override fun onProgressChanged(p0: SeekBar?, p1: Int, p2: Boolean) {
bottomEndMargin!!.updateLayoutParams<ConstraintLayout.LayoutParams> {
// textViewContainer!!.width / 2 so each margin only goes to the center
width = 1.coerceAtLeast(endSeekBar!!.progress * textViewContainer!!.width / 2 / endSeekBar!!.max)
}
}
})
bottomSeekBar!!.setOnSeekBarChangeListener(object : OnSeekBarChangeListener {
override fun onStopTrackingTouch(seekBar: SeekBar) {}
override fun onStartTrackingTouch(seekBar: SeekBar) {}
override fun onProgressChanged(p0: SeekBar?, p1: Int, p2: Boolean) {
bottomEndMargin!!.updateLayoutParams<ConstraintLayout.LayoutParams> {
// textViewContainer!!.height / 2 so each margin only goes to the center
height = 1.coerceAtLeast(bottomSeekBar!!.progress * textViewContainer!!.height / 2 / bottomSeekBar!!.max)
}
}
})
findViewById<View>(R.id.plusLineCountButton).setOnClickListener {
var maxLinesCount = Integer.parseInt(linesCountTextView!!.text.toString())
linesCountTextView!!.text = Integer.toString(++maxLinesCount)
contentTextView!!.maxLines = maxLinesCount
}
findViewById<View>(R.id.minusLineCountButton).setOnClickListener(OnClickListener {
var maxLinesCount = Integer.parseInt(linesCountTextView!!.text.toString())
if (maxLinesCount == 1)
return@OnClickListener
linesCountTextView!!.text = Integer.toString(--maxLinesCount)
contentTextView!!.maxLines = maxLinesCount
})
// synchronize contentTextView to the controls
contentTextView!!.text = contentEditText!!.text.toString()
contentTextView!!.maxLines = Integer.parseInt(linesCountTextView!!.text.toString())
}
}