So im trying to build chat and the textEdit where you write your message. I want to change height depending on amount of text.
a good thing to know also are that this is in a fragment.
Like in messenger or normal messaging apps.
Kotlin:
fun initTextWatcher(){ var tw: TextWatcher = object : TextWatcher { override fun onTextChanged(s: CharSequence, start: Int, before: Int, count: Int) { val textview = viewOfLayout.chat_input_text if (activity != null) { activity?.runOnUiThread { textview.height = 100 } } } override fun afterTextChanged(s: Editable) {} override fun beforeTextChanged(s: CharSequence, start: Int, count: Int, after: Int) {} } viewOfLayout.chat_input_text.addTextChangedListener(tw) }
XML:
<android.support.constraint.ConstraintLayout android:layout_width="0dp" android:layout_height="wrap_content" android:background="@color/White" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintBottom_toBottomOf="parent" android:id="@+id/chat_input_text_Send_container"> <Button android:text="@string/Send" android:layout_width="wrap_content" android:layout_height="35dp" android:id="@+id/chat_send_button" app:layout_constraintEnd_toEndOf="parent" android:layout_marginEnd="10dp" android:layout_marginBottom="10dp" app:layout_constraintBottom_toBottomOf="parent" android:background="@drawable/chat_round_corners" android:textColor="@color/White" android:textSize="15sp"/> <EditText android:layout_width="0dp" android:layout_height="wrap_content" android:maxHeight="70dp" android:minHeight="35dp" android:ems="10" android:inputType="textCapSentences|textMultiLine" android:id="@+id/chat_input_text" android:layout_marginTop="10dp" app:layout_constraintTop_toTopOf="parent" android:layout_marginBottom="10dp" app:layout_constraintBottom_toBottomOf="parent" android:layout_marginEnd="8dp" app:layout_constraintEnd_toStartOf="@+id/chat_send_button" android:layout_marginStart="8dp" app:layout_constraintStart_toStartOf="parent" android:textColor="@color/colorPrimary" android:gravity="top|left" android:textSize="15sp" android:background="@drawable/chat_text_round_corners" android:padding="4dp"/> <ProgressBar style="?android:attr/progressBarStyle" android:layout_width="25dp" android:layout_height="25dp" android:indeterminateTint="@color/White" android:id="@+id/chat_progressbar_send" app:layout_constraintTop_toTopOf="@+id/chat_send_button" app:layout_constraintEnd_toEndOf="@+id/chat_send_button" app:layout_constraintStart_toStartOf="@+id/chat_send_button" app:layout_constraintBottom_toBottomOf="@+id/chat_send_button" android:visibility="invisible"/> </android.support.constraint.ConstraintLayout>
Advertisement
Answer
I fixed it by setting layout params every time the text changed to the params of the editText
.
fun initTextWatcher(){ var tw: TextWatcher = object : TextWatcher { override fun onTextChanged(s: CharSequence, start: Int, before: Int, count: Int) { val chat_textedit = viewOfLayout.chat_input_text if (activity != null) { activity?.runOnUiThread { val lparams = chat_textedit.layoutParams as ConstraintLayout.LayoutParams chat_textedit.layoutParams = lparams } } } override fun afterTextChanged(s: Editable) {} override fun beforeTextChanged(s: CharSequence, start: Int, count: Int, after: Int) {} } viewOfLayout.chat_input_text.addTextChangedListener(tw) }