Skip to content
Advertisement

How to hide and unhide a view on a navigation item click?

I have a navigation drawer with 3 items. When I click on Item 1 I need it to show me an edit text for example and when I click on item 2 or 3 I need it to hide what’s been showing and display something else, something belongs to item 2 and item 3.

I tried with Toast messages at first on every item and it worked well but when I made an edit text and made it visible once you click on item 1, it shows me that edit text but when I go back and click on item 2 or item 3 to hide item 1’s edit text nothing happens at all.. It seems like the drawer stopped responding, I don’t know .

Here is my xml code :

 <com.google.android.material.navigation.NavigationView
            android:id="@+id/drawer_nav"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:background="@color/black"
            app:menu="@menu/navigation"
            app:itemTextColor="@color/white"
            app:itemIconTint="@color/white"
            android:layout_gravity="start"
            />

    <com.google.android.material.textfield.TextInputLayout
        android:id="@+id/txtSummary"
        android:layout_width="200dp"
       android:visibility="gone"
        android:layout_height="200dp"
        android:gravity="top">

        <com.google.android.material.textfield.TextInputEditText
            android:id="@+id/inputET"
            android:layout_width="match_parent"
            android:layout_height="150dp"
            android:layout_gravity="center_horizontal"
            android:layout_margin="10dp"
            android:gravity="right"
            android:scrollIndicators="left"
            android:textColor="@color/black"
            android:background="@color/white"
            android:inputType="textMultiLine"/>
    </com.google.android.material.textfield.TextInputLayout>

And this is my Java code :

txtSummary = findViewById(R.id.txtSummary);
textInputEditText = findViewById(R.id.inputET);
navigationView = findViewById(R.id.drawer_nav);
@Override
            public boolean onNavigationItemSelected(@NonNull MenuItem item) {
                

                    switch (item.getItemId()){
                        case R.id.item1:
                            Toast.makeText(getApplication()," Item 1 icon has been clicked  ",Toast.LENGTH_LONG).show();
                            txtSummary.setVisibility(View.VISIBLE);
                            textInputEditText.setVisibility(View.VISIBLE);
                            return true;


                        case R.id.item2:
                            txtSummary.setVisibility(View.GONE);
                            textInputEditText.setVisibility(View.GONE);
                            Toast.makeText(getApplication()," Item 2 icon has been clicked  ",Toast.LENGTH_LONG).show();
                            return true;

                        case R.id.item3:
                            txtSummary.setVisibility(View.GONE);
                            textInputEditText.setVisibility(View.GONE);
                            Toast.makeText(getApplication()," Item 3 icon has been clicked  ",Toast.LENGTH_LONG).show();
                  
                            return true;
                    }
                
                return true;
            }
        });```

Advertisement

Answer

Try to change your layout code. Put your edit text inside any other layout. Take reference from below code

<androidx.drawerlayout.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/my_drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">

<FrameLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content">
    <com.google.android.material.textfield.TextInputLayout
        android:id="@+id/txtSummary"
        android:layout_width="200dp"
        android:layout_height="200dp"
        android:gravity="top"
        android:visibility="gone">

        <com.google.android.material.textfield.TextInputEditText
            android:id="@+id/inputET"
            android:layout_width="match_parent"
            android:layout_height="150dp"
            android:layout_gravity="center_horizontal"
            android:layout_margin="10dp"
            android:background="@color/white"
            android:gravity="right"
            android:inputType="textMultiLine"
            android:scrollIndicators="left"
            android:textColor="@color/black" />
    </com.google.android.material.textfield.TextInputLayout>
</FrameLayout>

<com.google.android.material.navigation.NavigationView
    android:id="@+id/drawer_nav"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    android:fitsSystemWindows="true"
    app:menu="@menu/navigation" />

</androidx.drawerlayout.widget.DrawerLayout>
User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement