Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Measure height of TextInputLayout error container

Hi I have to make layout as below, edit text has to be in TextInputLayout to have error and floating hint functionality, and spinner on the right must have underline. My question is how to do it, because when I'm adding EditText into TextInputLayout there is a padding bellow and both underlines aren't in the same line. Is it possible to measure somehow this error container height? enter image description here

like image 459
falsetto Avatar asked Feb 25 '16 15:02

falsetto


2 Answers

So there are two possibility to do this:

  1. Set errorEnabled as false and create custom TextView with error text.
  2. Add extra invisible TextView to the bottom of second view with style and font size which can be found in TextInputLayout constructor. I'm not using this method but in my case it is 12sp.
like image 53
falsetto Avatar answered Oct 21 '22 23:10

falsetto


This is a common problem and one that I had to deal with myself just the other day. You can always take a look at the measurements on the guidelines here rather than needing to look into the code.

With regards to making a spinner match you can start with adding a background like this drawable

<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item
        android:top="-1dp"
        android:left="-1dp"
        android:right="-1dp"
        android:bottom="1dp" >
        <shape android:shape="rectangle">
            <padding android:top="4dp" android:bottom="4dp" />
            <stroke android:width="1dp" android:color="@color/my_grey"/>
        </shape>
    </item>
</layer-list>

You'll probably want to add android:padding="0dp" to the Spinner and rather than using one of th simple_... layouts which add padding which does not match EditText, return a plain TextView as your view returned from the adapter but continue to use the simple_... layout for the dropdown view, like the adapter example below

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    if (convertView == null) {
        //convertView = mInflater.inflate(R.layout.support_simple_spinner_dropdown_item, parent, false);
        convertView = new TextView(parent.getContext());
    }
    //if you want to mimick the floating hint behaviour from the edit text,      
    //you can animate the label visibility here
    return populateView(position, convertView, parent);
}

private View populateView(int position, View convertView, ViewGroup parent) {
    // in here set your values as you usually would
    return convertView;
}

@Override
public View getDropDownView(int position, View convertView, ViewGroup parent) {
    if (convertView == null) {
        convertView = mInflater.inflate(R.layout.support_simple_spinner_dropdown_item, parent, false);
    }
    return populateView(position, convertView, parent);
}

Now provided your margins on the EditText and Spinner match, they will line up. Although in some layouts I have to set android:layout_marginTop="0dp" on the Spinner

like image 25
Nick Cardoso Avatar answered Oct 22 '22 00:10

Nick Cardoso