2

Trato de colocar el EditText y el ImageButton dentro de un textInputLayout de forma horizontal, en el Preview de android aparece bien, pero al momento de ejecutar en el emulador no aparece nada. Lo que quiero hacer es un campo donde el usuario pueda ingresar su fecha de nacimiento y quiero agregarle el ImageButton para que al presionar ahí se abra un calendario, y no quiero cambiar de contenedor ya que con el textInputLayout puedo lograr que el textHint se vaya a la parte superior quedando como un titulo (Fecha de Nacimiento).

<android.support.design.widget.TextInputLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dp"
        android:weightSum="1"
        android:orientation="horizontal"
        style="@style/textInputLayoutOutlinedBoxDense">

        <android.support.design.widget.TextInputEditText
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight=".9"
            android:hint="Fecha de Nacimiento"/>

        <ImageButton
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight=".1"/>

    </android.support.design.widget.TextInputLayout>
4
  • Donde tenes este layout o dónde lo usas?
    – Juan
    el 13 nov. 2018 a las 14:18
  • lo tengo en un android.support.design.widget.CoordinatorLayout
    – Brynner
    el 13 nov. 2018 a las 14:21
  • Me parece que necesitas un Layout en el medio que haga de GroupParent. ConstraintLayout o RelativeLayout como para posicionar los elementos. (Adentro del Coordinator, y contenientdo al TextInputLayout)
    – Juan
    el 13 nov. 2018 a las 14:22
  • Tambien hice eso, coloque un linearLayout para almacenar el editText y el ImageView de manera horizontal, pero surge un problema y es que en el textInputLayout se puede habilitar el contador de caracteres(countEnable), y ese contador queda arriba del editText cuando deberia quedar abajo.
    – Brynner
    el 13 nov. 2018 a las 14:29

1 respuesta 1

0

Incluí el LinearLayout entre el Coordinator y el TextInputLayout, movíe el ImageButton al LinearLayout y acomodé el layout. Al botón por ahí te conviene wrapearlo en un FrameLayout y darle un tamaño específico.

<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto">
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:weightSum="100">
        <android.support.design.widget.TextInputLayout
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_marginTop="20dp"
            android:layout_weight="90"
            app:counterEnabled="true">

            <android.support.design.widget.TextInputEditText
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:hint="Fecha de Nacimiento"/>

        </android.support.design.widget.TextInputLayout>
        <ImageButton
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="10"
            android:layout_gravity="center"/>
    </LinearLayout>
</android.support.design.widget.CoordinatorLayout>

Y queda de esta forma:

Layout

II) Otra opción es usar el drawableRight del TextInputEditText.

<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto">
        <android.support.design.widget.TextInputLayout
            android:id="@+id/ti"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="20dp"
            app:counterEnabled="true">

            <android.support.design.widget.TextInputEditText
                android:id="@+id/tied"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:drawableRight="@drawable/calendario"
                android:drawablePadding="5dp"
                android:hint="Fecha de Nacimiento"/>

        </android.support.design.widget.TextInputLayout>
</android.support.design.widget.CoordinatorLayout>

Y en el onCreate() del Activity

tied = findViewById(R.id.tied);

tied.setOnTouchListener(new View.OnTouchListener() {
       @Override
       public boolean onTouch(View v, MotionEvent event) {

           final int DRAWABLE_RIGHT = 2;

           if(event.getAction() == MotionEvent.ACTION_UP || event.getAction() == MotionEvent.ACTION_POINTER_UP) {
               if(event.getRawX() >= (tied.getRight() - tied.getCompoundDrawables()[DRAWABLE_RIGHT].getBounds().width())) {

                   Log.d(TAG, "Click en imagen"); // Lo que acciona el click

                   return true;
               }
           }
           return false;
       }
});

TextInputLayout con drawableRight

Nota: La imagen es de 30 x 30 px. Para cambiar el tamaño hay que cambiar el tamaño del png.

0

Tu Respuesta

By clicking “Publica tu respuesta”, you agree to our terms of service and acknowledge you have read our privacy policy.

¿No es la respuesta que buscas? Examina otras preguntas con la etiqueta o formula tu propia pregunta.