In android how to set navigation drawer header image and name programmatically in class file?

前端 未结 16 850
半阙折子戏
半阙折子戏 2020-12-04 07:53

In android studio 1.4.1, I have created new Navigation Drawer Project which is default.My issue is in this project there is nav_header_main.xml file which is for navigation

相关标签:
16条回答
  • 2020-12-04 08:14

    In Kotlin

        val hView = nav_view.getHeaderView(0)
        val textViewName = hView.findViewById(R.id.textViewName) as TextView
        val textViewEmail = hView.findViewById(R.id.textViewEmail) as TextView
        val imgvw = hView.findViewById(R.id.imageView) as ImageView
        imgvw.setImageResource(R.drawable.ic_menu_gallery)
    
    0 讨论(0)
  • 2020-12-04 08:15
    val navigationView: NavigationView =  findViewById(R.id.nv)
    val header: View = navigationView.getHeaderView(0)
    val teext: TextView = header.findViewById(R.id.profilename)
    teext.text = "eejfgjt"
    

    this will fix your problem <3

    0 讨论(0)
  • 2020-12-04 08:16

    It's and old post, but it's new for me. So, it is straight forward! In this part of the code:

     public boolean onNavigationItemSelected(MenuItem item) {
    

    } , I bound an ImageView to the LinearLayout, which contains the ImageView from the example, listed below. Mind: it's the same code you get when you start a new project, and choose the template "Navigation Drawer Activity":

    <ImageView
    android:id="@+id/imageView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:paddingTop="@dimen/nav_header_vertical_spacing"
    android:src="@android:drawable/sym_def_app_icon" />
    

    I gave the LinearLayout and ID, inside nav_header_main.xml (in my case I chose 'navigation_header_container' , so it went this way:

    LinearLayout lV = (LinearLayout) findViewById(R.id.navigation_header_container);
    
        ivCloseDrawer = (ImageView) lV.findViewById(R.id.imageView);
        ivCloseDrawer.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View v)
            {
                drawer.closeDrawer(GravityCompat.START);
            }
        });
    

    Note: I have a private ImageView ivCloseDrawer declared at the top, before onCreate (MainActivity).

    It worked fine! Hope it helps, Best Regards.

    0 讨论(0)
  • 2020-12-04 08:21

    First you need to access the navigation drawer in your MainActivity(or the calling activity) like this:

        NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
    

    Then you need to remove the header layout from the activity_main.xml because the layout will be inflated programatically in the MainActivity. Your activity_main.xml should look like this:

    <?xml version="1.0" encoding="utf-8"?>
    <android.support.v4.widget.DrawerLayout            
    
    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"
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:openDrawer="start">
    
    <include
        layout="@layout/app_bar_main"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
    
    <android.support.design.widget.NavigationView
        android:id="@+id/nav_view"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:fitsSystemWindows="true"
        app:menu="@menu/activity_main_drawer" />
    
        </android.support.v4.widget.DrawerLayout>
    

    Then in your MainActivity, we inflate the nav_header_main layout and get access to its views, in this case the ImageView and TextView

    //inflate header layout
    View navView =  navigationView.inflateHeaderView(R.layout.nav_header_main);
    //reference to views
    ImageView imgvw = (ImageView)navView.findViewById(R.id.imageView);
    TextView tv = (TextView)navView.findViewById(R.id.textview);
    //set views
    imgvw.setImageResource(R.drawable.your_image);
    tv.setText("new text");
    
    navigationView.setNavigationItemSelectedListener(this);
    

    You can read more here

    0 讨论(0)
  • 2020-12-04 08:25

    EDIT : Works with design library upto 23.0.1 but doesn't work on 23.1.0

    In main layout xml you will have NavigationView defined, in that use app:headerLayout to set the header view.

    <android.support.design.widget.NavigationView
            android:id="@+id/navigation_view"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_gravity="start"
            app:headerLayout="@layout/nav_drawer_header"
            app:menu="@menu/navigation_drawer_menu" />
    

    And the @layout/nav_drawer_header will be the place holder of the image and texts.

    nav_drawer_header.xml

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="170dp"
    android:orientation="vertical">
    
    <RelativeLayout
        android:id="@+id/headerRelativeLayout"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    
        <ImageView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:scaleType="fitXY"
            android:src="@drawable/background" />
    
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="@dimen/action_bar_size"
            android:layout_alignParentBottom="true"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true"
            android:background="#40000000"
            android:gravity="center"
            android:orientation="horizontal"
            android:paddingBottom="5dp"
            android:paddingLeft="16dp"
            android:paddingRight="10dp"
            android:paddingTop="5dp">
    
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_marginLeft="35dp"
                android:orientation="vertical"
                android:weightSum="2">
    
    
                <TextView
                    android:id="@+id/navHeaderTitle"
                    android:layout_width="wrap_content"
                    android:layout_height="0dp"
                    android:layout_weight="1"
                    android:textAppearance="?android:attr/textAppearanceMedium"
                    android:textColor="@android:color/white" />
    
                <TextView
                    android:id="@+id/navHeaderSubTitle"
                    android:layout_width="wrap_content"
                    android:layout_height="0dp"
                    android:layout_weight="1"
                    android:textAppearance="?android:attr/textAppearanceSmall"
                    android:textColor="@android:color/white" />
    
            </LinearLayout>
    
        </LinearLayout>
    </RelativeLayout>
    </LinearLayout>
    

    And in your main class, you can take handle of Imageview and TextView as like normal other views.

    TextView navHeaderTitle = (TextView) findViewById(R.id.navHeaderTitle);
    navHeaderTitle.setText("Application Name");
    
    TextView navHeaderSubTitle = (TextView) findViewById(R.id.navHeaderSubTitle);
    navHeaderSubTitle.setText("Application Caption");
    

    Hope this helps.

    0 讨论(0)
  • Here is the method you can use to get header view and set data accourdingly

    val headerView: View? = navigationView.getHeaderView(0) // Index of the added headerView  
    
    // Now you can access child views of the header view
    val titleTextView: TextView? = headerView?.findViewById(R.id.titleTextView)
    
    0 讨论(0)
提交回复
热议问题