Make input edit text grow dynamically

This commit is contained in:
Niels Andriesse 2021-06-15 16:14:35 +10:00
parent e06a3271df
commit 196fc25902
5 changed files with 60 additions and 8 deletions

View File

@ -194,7 +194,7 @@ android {
versionCode canonicalVersionCode * postFixSize
versionName canonicalVersionName
minSdkVersion 21
minSdkVersion 23
targetSdkVersion 30
multiDexEnabled = true

View File

@ -64,7 +64,6 @@ class ConversationActivityV2 : PassphraseRequiredActionBarActivity() {
threadID = intent.getLongExtra(THREAD_ID, -1)
setUpRecyclerView()
setUpToolBar()
setUpInputBar()
}
private fun setUpRecyclerView() {
@ -97,10 +96,6 @@ class ConversationActivityV2 : PassphraseRequiredActionBarActivity() {
profilePictureView.update(thread, threadID)
}
private fun setUpInputBar() {
inputBar.inputBarEditText.imeOptions = inputBar.inputBarEditText.imeOptions or 16777216 // Always use incognito keyboard
}
override fun onPrepareOptionsMenu(menu: Menu): Boolean {
ConversationMenuHelper.onPrepareOptionsMenu(menu, menuInflater, thread, this) { onOptionsItemSelected(it) }
super.onPrepareOptionsMenu(menu)

View File

@ -5,10 +5,13 @@ import android.util.AttributeSet
import android.view.LayoutInflater
import android.widget.LinearLayout
import android.widget.RelativeLayout
import kotlinx.android.synthetic.main.activity_conversation_v2.*
import kotlinx.android.synthetic.main.view_input_bar.view.*
import network.loki.messenger.R
import org.thoughtcrime.securesms.loki.utilities.toDp
import kotlin.math.roundToInt
class InputBar : LinearLayout {
class InputBar : LinearLayout, InputBarEditTextDelegate {
private val attachmentsButton by lazy { InputBarButton(context, R.drawable.ic_plus_24) }
private val microphoneButton by lazy { InputBarButton(context, R.drawable.ic_microphone) }
@ -34,6 +37,16 @@ class InputBar : LinearLayout {
microphoneButtonContainer.addView(microphoneButton)
microphoneButton.layoutParams = RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.MATCH_PARENT)
microphoneButton.setOnClickListener { }
inputBarEditText.imeOptions = inputBarEditText.imeOptions or 16777216 // Always use incognito keyboard
inputBarEditText.delegate = this
}
// endregion
override fun inputBarEditTextHeightChanged(newValue: Int) {
val vMargin = resources.getDimension(R.dimen.small_spacing).roundToInt()
val layoutParams = this.layoutParams as RelativeLayout.LayoutParams
layoutParams.height = newValue + 2 * vMargin
this.layoutParams = layoutParams
}
// endregion
}

View File

@ -0,0 +1,44 @@
package org.thoughtcrime.securesms.conversation.v2.input_bar
import android.content.Context
import android.text.Layout
import android.text.StaticLayout
import android.util.AttributeSet
import android.widget.RelativeLayout
import androidx.appcompat.widget.AppCompatEditText
import org.thoughtcrime.securesms.loki.utilities.toPx
import kotlin.math.max
import kotlin.math.min
import kotlin.math.roundToInt
class InputBarEditText : AppCompatEditText {
var delegate: InputBarEditTextDelegate? = null
private val snMinHeight = toPx(40.0f, resources)
private val snMaxHeight = toPx(80.0f, resources)
constructor(context: Context) : super(context)
constructor(context: Context, attrs: AttributeSet) : super(context, attrs)
constructor(context: Context, attrs: AttributeSet, defStyleAttr: Int) : super(context, attrs, defStyleAttr)
override fun onTextChanged(text: CharSequence, start: Int, lengthBefore: Int, lengthAfter: Int) {
super.onTextChanged(text, start, lengthBefore, lengthAfter)
val builder = StaticLayout.Builder.obtain(text, 0, text.length, paint, width)
.setAlignment(Layout.Alignment.ALIGN_NORMAL)
.setLineSpacing(0.0f, 1.0f)
.setIncludePad(false)
val layout = builder.build()
val height = layout.height.toFloat()
val constrainedHeight = min(max(height, snMinHeight), snMaxHeight)
if (constrainedHeight.roundToInt() == this.height) { return }
val layoutParams = this.layoutParams as? RelativeLayout.LayoutParams ?: return
layoutParams.height = constrainedHeight.roundToInt()
this.layoutParams = layoutParams
delegate?.inputBarEditTextHeightChanged(constrainedHeight.roundToInt())
}
}
interface InputBarEditTextDelegate {
fun inputBarEditTextHeightChanged(newValue: Int)
}

View File

@ -22,7 +22,7 @@
android:layout_centerVertical="true"
android:layout_marginStart="@dimen/small_spacing" />
<EditText
<org.thoughtcrime.securesms.conversation.v2.input_bar.InputBarEditText
android:id="@+id/inputBarEditText"
android:layout_width="match_parent"
android:layout_height="40dp"