0

I am attempting to have a chat window that has the chat messages at the top (TextView) with a chat selector/input box (Spinner/EditText) below. My goal is for the spinner/editText to be 30dp tall. I want the TextView to fill the remaining space.

This is as close as I am getting. When I try to hardcode the 30dp height on the EditText it breaks. Any assistance would be greatly appreciated.

enter image description here

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:id="@+id/textView"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_marginStart="2dp"
        android:layout_marginTop="2dp"
        android:layout_marginEnd="2dp"
        android:text="This should fill height"
        app:layout_constraintBottom_toTopOf="@id/editText"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_chainStyle="spread" />

    <Spinner
        android:id="@+id/spinner"
        android:layout_width="wrap_content"
        android:layout_height="0dp"
        android:layout_marginStart="2dp"
        android:layout_marginTop="2dp"
        android:layout_marginBottom="2dp"
        app:layout_constraintBottom_toBottomOf="@id/editText"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="@id/editText" />

    <EditText
        android:id="@+id/editText"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginStart="2dp"
        android:layout_marginBottom="2dp"
        android:text="This should be 30dp tall"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toEndOf="@+id/spinner"
        app:layout_constraintTop_toBottomOf="@+id/textView" />
</android.support.constraint.ConstraintLayout>
KisnardOnline
  • 653
  • 4
  • 16
  • 42

2 Answers2

0

Please don't use The support library anymore and shift over to AndroidX.

I would suggest not using preset heights for EditTexts as it could lead to clipping of text like this: 1
I would suggest using Constraint Guidelines. For example,Something Like this:

<androidx.constraintlayout.widget.Guideline
    android:id="@+id/guideline1"
    android:layout_width="0dp"
    android:layout_height="0dp"
    android:orientation="horizontal"
    app:layout_constraintGuide_percent="0.7" />

This takes the position of 70% of the screen from the top, now you can constraint to the guideline as you wish.

CyberShark
  • 410
  • 5
  • 16
-1

I think you can use this.
First add this in gradle

 dependencies {

implementation 'androidx.percentlayout:percentlayout:1.0.0'

}

and your layout change to this

    <?xml version="1.0" encoding="utf-8"?>
<androidx.percentlayout.widget.PercentRelativeLayout  xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    android:paddingRight="10dp"
    android:paddingLeft="10dp"
    tools:context=".Cal"
    tools:showIn="@layout/cal">

    <TextView
        android:id="@+id/Q1"
        android:layout_width="0dp"
        android:layout_height="0dp"

        android:layout_marginTop="10dp"
        android:text="test"
        android:textSize="28sp"
        android:gravity="center"
        app:layout_heightPercent="80%"
        app:layout_widthPercent="100%" />


         <Spinner
        android:id="@+id/spinner"
        android:layout_width="0dp"
        android:layout_height="30dp"
         android:layout_below="@id/Q1"

        app:layout_widthPercent="30%" />

    <EditText
        android:id="@+id/editText"
        android:layout_width="0dp"
        android:layout_height="30dp"
         android:layout_below="@id/Q1"
         android:layout_toRightOf="@id/spinner"

        app:layout_widthPercent="70%"  />



</androidx.percentlayout.widget.PercentRelativeLayout >

try it

samurai
  • 101
  • 1
  • 13