Search code examples
androidandroid-custom-viewandroid-scrollviewdrawerlayoutnavigationview

Items unresponsive after Inflating Custom View in NavigationDrawer Android


I am trying to create a custom navigation drawer. I created a layout of what I want the navigation drawer to look like and I'm inflating it inside the NavigationView dynamically. The problem that I'm facing now is that after the view inflates, if I try to change the text of any textview, it does not update. Also some of the items are not responding to their onClickListeners.

Here is my custom menu layout:

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout
       android:id="@+id/menu_layout"
       xmlns:android="http://schemas.android.com/apk/res/android"
       android:layout_width="fill_parent"
       android:layout_height="fill_parent">

    <LinearLayout
        android:id="@+id/top_blue_layout"
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/fp_blue"
        android:layout_alignParentTop="true"
        android:elevation="5dp">

        <LinearLayout
            android:id="@+id/refersh_account_layout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            android:paddingBottom="10dp">

            <ImageView
                android:id="@+id/refresh_account_image"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:src="@drawable/refresh_icon"
                android:padding="5dp"
                android:tint="#80ffffff"/>

            <TextView
                android:id="@+id/refresh_account_text"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/sync_now"
                android:textColor="#90ffffff"
                android:textSize="10sp"
                android:layout_gravity="center"
                android:textAllCaps="true"/>

        </LinearLayout>

        <LinearLayout
            android:id="@+id/account_info_layout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            android:paddingLeft="35dp"
            android:paddingRight="35dp">

            <TextView
                android:id="@+id/email_tv"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="PEDRO.SANCHEZ@EMAIL.COM"
                android:ellipsize="end"
                android:lines="1"
                android:textAllCaps="true"
                android:textColor="@color/white"
                />

            <RelativeLayout
                android:id="@+id/phone_number_layout"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:paddingBottom="20dp">

                <TextView
                    android:id="@+id/phone_number_tv"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:textStyle="bold"
                    android:textColor="@color/white"
                    android:text="(555) 123-4567"
                    android:layout_gravity="center"
                    android:layout_alignParentStart="true"
                    android:layout_alignParentLeft="true"/>

            </RelativeLayout>

            <LinearLayout
                android:id="@+id/days_layout"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="vertical"
                android:layout_marginTop="10dp">

                <RelativeLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content">

                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="Days Left"
                        android:textColor="#95ffffff"
                        android:textSize="12sp"
                        android:layout_alignParentStart="true"
                        android:layout_alignParentLeft="true"
                        />

                    <TextView
                        android:id="@+id/days_left_tv"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="18 Days left"
                        android:textColor="@color/white"
                        android:textSize="12sp"
                        android:layout_alignParentEnd="true"
                        android:layout_alignParentRight="true"
                        />
                </RelativeLayout>

                <ProgressBar
                    android:id="@+id/days_left_progress_bar"
                    android:indeterminate="false"
                    android:layout_marginTop="5dp"
                   style="@android:style/Widget.ProgressBar.Horizontal"
                    android:layout_width="match_parent"
                    android:layout_height="5dp"
               android:progressDrawable="@drawable/progress_bar_colors"
                    android:max="100"
                    android:progress="50"
                    android:rotation="180"
                    />
            </LinearLayout>
        </LinearLayout>
    </LinearLayout>

    <ScrollView
        android:id="@+id/middle_scroll_view"
        android:layout_below="@+id/top_blue_layout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_above="@+id/bottom_layout">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:background="@color/white">

        <RelativeLayout
            android:id="@+id/global_plans_main_layout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:padding="5dp">

            <ImageView
                android:id="@+id/global_plans_image_view"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:src="@drawable/ic_public"
                android:layout_alignParentLeft="true"
                android:layout_alignParentStart="true"
                android:layout_marginRight="7dp"
                android:tint="@color/fp_blue"/>

            <TextView
                android:id="@+id/global_plans_tv"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Global Plans"
                android:textColor="@color/fp_blue"
                android:textStyle="bold"
                android:layout_centerVertical="true"
                android:layout_toRightOf="@+id/global_plans_image_view"
                android:layout_toEndOf="@+id/global_plans_image_view"/>

            <ImageView
                android:id="@+id/global_plans_dropdown_arrow"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:src="@drawable/quickaction_arrow_down"
                android:tint="@color/fp_blue"
                android:layout_alignParentEnd="true"
                android:layout_alignParentRight="true"
                android:layout_centerVertical="true"
                android:layout_marginRight="5dp"/>

        </RelativeLayout>

        <View
            android:layout_width="wrap_content"
            android:layout_height="1dip"
            android:background="#E2E2E2" />

        <RelativeLayout
            android:id="@+id/services_main_layout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:padding="5dp">

            <ImageView
                android:id="@+id/services_image_view"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:src="@drawable/ic_download"
                android:layout_alignParentLeft="true"
                android:layout_alignParentStart="true"
                android:layout_marginRight="7dp"
                android:tint="@color/fp_blue"/>

            <TextView
                android:id="@+id/services_plans_tv"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Services"
                android:textColor="@color/fp_blue"
                android:textStyle="bold"
                android:layout_centerVertical="true"
                android:layout_toRightOf="@+id/services_image_view"
                android:layout_toEndOf="@+id/services_image_view" />

            <ImageView
                android:id="@+id/services_dropdown_arrow"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:src="@drawable/quickaction_arrow_down"
                android:tint="@color/fp_blue"
                android:layout_alignParentEnd="true"
                android:layout_alignParentRight="true"
                android:layout_centerVertical="true"
                android:layout_marginRight="5dp"
                android:visibility="gone"/>

        </RelativeLayout>
    </LinearLayout>
    </ScrollView>

<RelativeLayout
    android:id="@+id/bottom_layout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@color/fp_blue"
    android:layout_alignParentBottom="true"
    android:padding="5dp">

    <ImageView
        android:id="@+id/self_help_image_view"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/faq"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_marginRight="7dp"
        android:layout_marginEnd="7dp"
        android:layout_centerVertical="true"
        />

    <TextView
        android:id="@+id/self_help_tv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="SELF HELP"
        android:layout_toRightOf="@+id/self_help_image_view"
        android:layout_toEndOf="@+id/self_help_image_view"
        android:textColor="#90ffffff"
        android:textSize="10sp"
        android:layout_gravity="center"
        android:layout_centerVertical="true"
        />
</RelativeLayout>
    </RelativeLayout>

Here is xml file for the navigation drawer

  <android.support.v4.widget.DrawerLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/white"
    tools:context=".ui.SipHome"
    android:theme="@style/Theme.AppCompat.Light">


    <LinearLayout
       android:layout_width="match_parent"
       android:layout_height="match_parent"
       android:orientation="vertical">
       .
       .
       .
       . //page content
       .
    </LinearLayout>

    <android.support.design.widget.NavigationView
       android:id="@+id/left_drawer"
       android:layout_width="wrap_content"
       android:layout_height="match_parent"
       android:layout_gravity="start"/>

   </android.support.v4.widget.DrawerLayout>

I am inflating the custom menu layout into the navigation view using this line of code where mDrawerLeft is the navigation view:

    View rootView =LayoutInflater.from(mParentActivity).inflate(R.layout.custom_menu_layout, mDrawerLeft, true);

I then use rootView to find all of the TextViews and ImageViews I created in the custom menu layout and when I try to change their text according to logic I have, it doesn't work. I debugged it and when I try to get the text from a text view using textview.getText(), it returns the updated text that is supposed to show up, but its not showing up on the UI which makes me think that its a problem with UI refresh. I tried invalidate() but it didn't work either. Can someone tell me what I'm doing wrong?

Parts of the menu are responsive to the onClickListeners I set on them, except for anything inside the ScrollView. I think thats a separate issue but any help there would be appreciated as well. Thank you.


Solution

  • I was able to solve the issue by using the tag inside the NavigationView to include my custom layout, instead of inflating it dynamically. Here is the updated XML file for the navigation drawer.

        <android.support.v4.widget.DrawerLayout 
         xmlns:android="http://schemas.android.com/apk/res/android"
         xmlns:tools="http://schemas.android.com/tools"
         xmlns:app="http://schemas.android.com/apk/res-auto"
         android:id="@+id/drawer_layout"
         android:layout_width="match_parent"
         android:layout_height="match_parent"
         android:background="@color/white"
         tools:context=".ui.SipHome"
         android:theme="@style/Theme.AppCompat.Light">
    
    
         <LinearLayout
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          android:orientation="vertical">
          .
          .
          .
          . //page content
          .
         </LinearLayout>
    
         <android.support.design.widget.NavigationView
          android:id="@+id/left_drawer"
          android:layout_width="wrap_content"
          android:layout_height="match_parent"
          android:layout_gravity="start">
    
             <include layout="@layout/custom_menu_layout"/>
    
         </android.support.design.widget.NavigationView>
    
        </android.support.v4.widget.DrawerLayout>