TextInputLayout is not showing programmatically

Viewed 379

So I am facing a problem where I have one spinner which has 4 options 1-4 and I have 4 TextInputLayout now what I want to achieve is to have the fragment start I want my textinputlayout to be hidden. When I click on the spinner and select the first option then only one TextInputLayout should be visible. When I select 2 options I need to 2 TextInputLayout to be visible and so on. Now the problem is my TextInputLayout is still not visible I have provided switch case in which of 1 is selected then make TextInputLayout visible but it's not working. Any help would be appreciated

XML FIle:

<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.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">
    <ScrollView
       android:layout_width="match_parent"
       android:layout_height="wrap_content"
       android:layout_marginTop="60dp">
        <LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">
            <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="16dp"
            android:text="Choose Number of vehicle and quantity"
            android:layout_marginLeft="12dp"/>
            <Spinner
            android:id="@+id/spinner"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="16dp"
            android:layout_marginLeft="12dp"
            android:layout_marginRight="12dp"
            android:background="@drawable/spinner_border"/>
            <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
                <com.google.android.material.textfield.TextInputLayout
        android:id="@+id/VehicleNoText1"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_marginTop="16dp"
        android:layout_marginLeft="12dp"
        android:layout_marginRight="12dp"
        android:layout_weight="0.5"
        android:hint="Vehicle No"
        android:visibility="invisible"
        style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox">
                    <com.google.android.material.textfield.TextInputEditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:textSize="12sp"
            android:imeOptions="actionNext"
            android:inputType="text"/>
                </com.google.android.material.textfield.TextInputLayout>
                <com.google.android.material.textfield.TextInputLayout
           android:id="@+id/QuantityText1"
           android:layout_width="0dp"
           android:layout_height="match_parent"
           android:hint="Quantity"
           android:layout_marginTop="16dp"
           android:layout_marginLeft="9dp"
           android:layout_marginRight="12dp"
           android:layout_weight="0.5"
           android:visibility="invisible"
           style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox">
                    <com.google.android.material.textfield.TextInputEditText
               android:layout_width="match_parent"
               android:layout_height="wrap_content"
               android:textSize="12sp"
               android:inputType="text"/>
                </com.google.android.material.textfield.TextInputLayout>
            </LinearLayout>
            <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal">
                <com.google.android.material.textfield.TextInputLayout
                android:id="@+id/VehicleNoText2"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:hint="Vehicle No"
                android:layout_marginTop="16dp"
                android:layout_marginLeft="12dp"
                android:layout_marginRight="12dp"
                android:layout_weight="0.5"
                android:visibility="invisible"
                
          style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox">
                    <com.google.android.material.textfield.TextInputEditText
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:textSize="12sp"
                    android:imeOptions="actionNext"
                    android:inputType="text"/>
                </com.google.android.material.textfield.TextInputLayout>
                <com.google.android.material.textfield.TextInputLayout
                android:id="@+id/QuantityText2"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:hint="Quantity"
                android:layout_marginTop="16dp"
                android:layout_marginLeft="9dp"
                android:layout_marginRight="12dp"
                android:layout_weight="0.5"
                android:visibility="invisible"
                
             
             
       style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox">
                    <com.google.android.material.textfield.TextInputEditText
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:textSize="12sp"
                    android:inputType="text"/>
                </com.google.android.material.textfield.TextInputLayout>
            </LinearLayout>
            <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal">
                <com.google.android.material.textfield.TextInputLayout
                android:id="@+id/VehicleNoText3"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:hint="Vehicle No"
                android:layout_marginTop="16dp"
                android:layout_marginLeft="12dp"
                android:layout_marginRight="12dp"
                android:layout_weight="0.5"
                android:visibility="invisible"
                
             
       style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox">
                    <com.google.android.material.textfield.TextInputEditText
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:textSize="12sp"
                    android:imeOptions="actionNext"
                    android:inputType="text"/>
                </com.google.android.material.textfield.TextInputLayout>
                <com.google.android.material.textfield.TextInputLayout
                android:id="@+id/QuantityText3"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:hint="Quantity"
                android:layout_marginTop="16dp"
                android:layout_marginLeft="9dp"
                android:layout_marginRight="12dp"
                android:layout_weight="0.5"
                android:visibility="invisible"
                
            style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox">
                    <com.google.android.material.textfield.TextInputEditText
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:textSize="12sp"
                    android:inputType="text"/>
                </com.google.android.material.textfield.TextInputLayout>
            </LinearLayout>
            <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal">
                <com.google.android.material.textfield.TextInputLayout
                android:id="@+id/VehicleNoText4"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:hint="Vehicle No"
                android:layout_marginTop="16dp"
                android:layout_marginLeft="12dp"
                android:layout_marginRight="12dp"
                android:layout_weight="0.5"
                android:visibility="invisible"
                
      style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox">
                    <com.google.android.material.textfield.TextInputEditText
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:textSize="12sp"
                    android:imeOptions="actionNext"
                    android:inputType="text"/>
                </com.google.android.material.textfield.TextInputLayout>
                <com.google.android.material.textfield.TextInputLayout
                android:id="@+id/QuantityText4"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:hint="Quantity"
                android:layout_marginTop="16dp"
                android:layout_marginLeft="9dp"
                android:layout_marginRight="12dp"
                android:layout_weight="0.5"
                android:visibility="invisible"
                
         style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox">
                    <com.google.android.material.textfield.TextInputEditText
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:textSize="12sp"
                    android:inputType="text"/>
                </com.google.android.material.textfield.TextInputLayout>
            </LinearLayout>
        </LinearLayout>
    </ScrollView>
</androidx.coordinatorlayout.widget.CoordinatorLayout>

My Fragment class

List < String > categories = new ArrayList < > ();
categories.add("1");
categories.add("2");
categories.add("3");
categories.add("4");

ArrayAdapter < String > dataAdapter = new ArrayAdapter < > (this.getActivity(), android.R.layout.simple_spinner_item, categories);

// Drop down layout style - list view with radio button
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

// attaching data adapter to spinner
vehicleSelector.setAdapter(dataAdapter);


vehicleSelector.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
    @Override
    public void onItemSelected(AdapterView << ? > adapterView, View view, int i, long l) {

        String selectedItem = adapterView.getItemAtPosition(i).toString();
        Log.i("Values", selectedItem);
        switch (selectedItem) {

            case "1":
                c13.getEditText().setVisibility(View.VISIBLE);

                Objects.requireNonNull(c14.getEditText()).setVisibility(View.VISIBLE);
                break;

            case "2":
                Objects.requireNonNull(c13.getEditText()).setVisibility(View.VISIBLE);
                Objects.requireNonNull(c14.getEditText()).setVisibility(View.VISIBLE);
                Objects.requireNonNull(c15.getEditText()).setVisibility(View.VISIBLE);
                Objects.requireNonNull(c16.getEditText()).setVisibility(View.VISIBLE);
                break;

            case "3":
                Objects.requireNonNull(c13.getEditText()).setVisibility(View.VISIBLE);
                Objects.requireNonNull(c14.getEditText()).setVisibility(View.VISIBLE);
                Objects.requireNonNull(c15.getEditText()).setVisibility(View.VISIBLE);
                Objects.requireNonNull(c16.getEditText()).setVisibility(View.VISIBLE);
                Objects.requireNonNull(c17.getEditText()).setVisibility(View.VISIBLE);
                Objects.requireNonNull(c18.getEditText()).setVisibility(View.VISIBLE);
                break;

            case "4":
                Objects.requireNonNull(c13.getEditText()).setVisibility(View.VISIBLE);
                Objects.requireNonNull(c14.getEditText()).setVisibility(View.VISIBLE);
                Objects.requireNonNull(c15.getEditText()).setVisibility(View.VISIBLE);
                Objects.requireNonNull(c16.getEditText()).setVisibility(View.VISIBLE);
                Objects.requireNonNull(c17.getEditText()).setVisibility(View.VISIBLE);
                Objects.requireNonNull(c18.getEditText()).setVisibility(View.VISIBLE);
                Objects.requireNonNull(c19.getEditText()).setVisibility(View.VISIBLE);
                Objects.requireNonNull(c20.getEditText()).setVisibility(View.VISIBLE);
                break;

        }
    }
    @Override
    public void onNothingSelected(AdapterView << ? > adapterView) {

    }
});
1 Answers

Initialize your TextInputEditText variable like this:

TextInputEditText myEditText = findViewById(R.id.my_edit_text);

and then perform

myEditText.setVisibility(View.VISIBLE)

instead of

myTextInputLayout.getEditText().setVisibility(View.VISIBLE). ( This is what you are doing, calling getEditText() on a textInputLayout variable ).

Note that you need to specify the id to the TextInputEditText that lives inside the textInputLayout.

Related