How to get view from drawer header layout with binding in activity?

RogerParis

So this is my activity_main.xml:

<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        tools:context="MainActivity"
    >
    <!-- A DrawerLayout is intended to be used as the top-level content view using match_parent for both width and height to consume the full space available. -->
    <android.support.v4.widget.DrawerLayout
        android:id="@+id/drawer_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        >

        <!-- As the main content view, the view below consumes the entire
             space available using match_parent in both dimensions. -->

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

            <LinearLayout
                android:id="@+id/ll_container"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:orientation="vertical">

                <android.support.v7.widget.Toolbar
                    android:id="@+id/my_awesome_toolbar"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:background="@android:color/black"
                    android:fitsSystemWindows="true"
                    >

                    <TextView
                        android:id="@+id/toolbar_title"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_marginLeft="10dp"
                        android:layout_marginStart="10dp"
                        android:textColor="@android:color/white"
                        android:textSize="@dimen/abc_text_size_title_material_toolbar"
                        tools:text="@string/default_toolbar_title"/>

                </android.support.v7.widget.Toolbar>


                <FrameLayout
                    android:id="@+id/container"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent">

                </FrameLayout>
            </LinearLayout>

            <android.support.design.widget.FloatingActionButton
                android:id="@+id/fab_fuf"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentBottom="true"
                android:layout_alignParentEnd="true"
                android:layout_alignParentRight="true"
                android:layout_marginBottom="20dp"
                android:layout_marginEnd="20dp"
                android:layout_marginRight="20dp"
                android:src="@drawable/flamme"
                app:fabSize="normal"
                />
        </RelativeLayout>

        <android.support.design.widget.NavigationView
            android:id="@+id/navigation_view"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_gravity="start"
            android:background="@android:color/black"
            **app:headerLayout="@layout/drawer_header"**
            app:itemTextColor="@color/drawer_item_color_selector"
            app:menu="@menu/menu_drawer"/>

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

and I am using binding for the activity so I don't have to use the findViewById and cast it etc.. like this:

ActivityMainBinding binding = DataBindingUtil.setContentView(this, R.layout.activity_main);

        Toolbar toolbar = binding.myAwesomeToolbar;
        toolbarTitle = binding.toolbarTitle;
        BalrogFontsHelper.SetKhandBoldToView(toolbarTitle);
        setSupportActionBar(toolbar);
        final ActionBar actionBar = getSupportActionBar();
        if (actionBar != null) {
            actionBar.setHomeAsUpIndicator(R.drawable.ic_dehaze_white_24);
            actionBar.setDisplayHomeAsUpEnabled(true);
            actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_STANDARD);
            actionBar.setDisplayShowTitleEnabled(false);
        }


        drawerLayout = binding.drawerLayout;
        **tvLoggedUserEmail = (TextView) findViewById(R.id.tv_logged_user_email);**
        BalrogFontsHelper.SetKhandBoldToView(tvLoggedUserEmail);

As you can see, I can get the views that are directly in the activity_main.xml layout by binding but when the view I am trying to get is not there I can't see the variable in the binding object.

drawer_header.xml:

    <?xml version="1.0" encoding="utf-8"?>

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                android:layout_width="match_parent"
                android:layout_height="96dp"
                xmlns:tools="http://schemas.android.com/tools"
                android:background="@android:color/black"
                android:theme="@style/ThemeOverlay.AppCompat.Dark">


    <TextView
        android:id="@+id/tv_logged_user_email"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:layout_marginLeft="16dp"
        tools:text="@string/login_placeholder_email"
        android:textAllCaps="true"
        android:textAppearance="@style/TextAppearance.AppCompat.Body2"
        android:textSize="20sp"/>


</RelativeLayout>

How could I get this tv_logged_user_email TextView in a binding way so I have:

**tvLoggedUserEmail = binding.tvLoggedUserEmail;**
leejaycoke

Updated solution (13/11/2015)

Solution: Update your Design Support Library to 23.1.1:

Changes for Design Support library 23.1.1:

  • Added the getHeaderView method to the NavigationView class.
  • Fixed a transparent background issue for a FloatingActionButton object on devices running Android 4.0 (API level 15) and lower. (Issue 183315)

See https://developer.android.com/tools/support-library/index.html for more info


Original solution

I don't know why there is no method which provides header view attached programmatically.

Instead, here's two solutions:

NavigationView navigationView = (NavigationView) findViewById(R.id.navigation);
View headerView = navigationView.inflateHeaderView(R.layout.header_layout)
ImageView iv = (ImageView)headerview.findViewById(R.id.your_image_view)

Or:

NavigationView navigationView = (NavigationView) findViewById(R.id.navigation);
View headerView = LayoutInflater.from(this).inflate(R.layout.header_layout, navigationView, false);
navigationView.addHeaderView(headerView);

ImageView iv = (ImageView) headerView.findViewById(R.id.yourImageView)

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to set text to view from drawer header layout in navigation drawer without inflating view

From Dev

Change layout from the drawer activity example

From Dev

How to add an image header in navigation drawer layout

From Dev

Error to pass from activity with Navigation Drawer to activity with relative layout Android

From Dev

How to extend the view out of drawer layout?

From Dev

How to add a view above drawer layout?

From Dev

Android - how to get the layout id from a view?

From Dev

How to call main activity from the navigation drawer

From Dev

How to attach layout/activitise to menu in Navigation Drawer Activity

From Dev

How to disable drawer layout from one side?

From Dev

How to change/update a item from a Drawer layout?

From Dev

How to disable drawer layout from one side?

From Dev

How to change/update a item from a Drawer layout?

From Dev

calling a view from outside the layout of the activity

From Dev

how to get the clickevent of image view, but the imageview is in another layout working as header of the current layout

From Dev

Navigation Drawer layout over activity layout

From Dev

How to change selected Item in the navigation drawer depending on the activity/view?

From Dev

How to get items in Navigation Drawer to change view

From Dev

How to get items in Navigation Drawer to change view

From Dev

how to GET THE selected value from the list view to be displayed on the second activity?

From Dev

how to GET THE selected value from the list view to be displayed on the second activity?

From Dev

How does a custom view get data from the activity?

From Dev

How to get reference to a View inside CardView from an activity?

From Dev

how to get id from list view and send to other activity

From Dev

How to get values from another view and use it in same activity?

From Dev

How can I edit the header view element in Navigation Drawer programatically?

From Dev

How to get Activity's view?

From Dev

How to get Activity's view?

From Dev

Navigation drawer - Header View with ListView

Related Related

  1. 1

    How to set text to view from drawer header layout in navigation drawer without inflating view

  2. 2

    Change layout from the drawer activity example

  3. 3

    How to add an image header in navigation drawer layout

  4. 4

    Error to pass from activity with Navigation Drawer to activity with relative layout Android

  5. 5

    How to extend the view out of drawer layout?

  6. 6

    How to add a view above drawer layout?

  7. 7

    Android - how to get the layout id from a view?

  8. 8

    How to call main activity from the navigation drawer

  9. 9

    How to attach layout/activitise to menu in Navigation Drawer Activity

  10. 10

    How to disable drawer layout from one side?

  11. 11

    How to change/update a item from a Drawer layout?

  12. 12

    How to disable drawer layout from one side?

  13. 13

    How to change/update a item from a Drawer layout?

  14. 14

    calling a view from outside the layout of the activity

  15. 15

    how to get the clickevent of image view, but the imageview is in another layout working as header of the current layout

  16. 16

    Navigation Drawer layout over activity layout

  17. 17

    How to change selected Item in the navigation drawer depending on the activity/view?

  18. 18

    How to get items in Navigation Drawer to change view

  19. 19

    How to get items in Navigation Drawer to change view

  20. 20

    how to GET THE selected value from the list view to be displayed on the second activity?

  21. 21

    how to GET THE selected value from the list view to be displayed on the second activity?

  22. 22

    How does a custom view get data from the activity?

  23. 23

    How to get reference to a View inside CardView from an activity?

  24. 24

    how to get id from list view and send to other activity

  25. 25

    How to get values from another view and use it in same activity?

  26. 26

    How can I edit the header view element in Navigation Drawer programatically?

  27. 27

    How to get Activity's view?

  28. 28

    How to get Activity's view?

  29. 29

    Navigation drawer - Header View with ListView

HotTag

Archive