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

前端 未结 16 847
半阙折子戏
半阙折子戏 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:34
       FirebaseAuth firebaseauth = FirebaseAuth.getInstance(); 
    
       NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);   //displays text of header of nav drawer.
        View headerview = navigationView.getHeaderView(0);
    
        TextView tt1 = (TextView) headerview.findViewById(R.id.textview_username);
        tt1.setText(firebaseauth.getCurrentUser().getDisplayName());//username of logged in user.  
    
       TextView tt = (TextView) headerview.findViewById(R.id.textView_emailid);
        tt.setText(firebaseauth.getCurrentUser().getEmail());    //email id of logged in user.
    
        final ImageView img1 = (ImageView) headerview.findViewById(R.id.imageView_userimage);
        Glide.with(getApplicationContext())
                .load(firebaseauth.getCurrentUser().getPhotoUrl()).asBitmap().atMost().error(R.drawable.ic_selfie_point_icon)   //asbitmap after load always.
                .into(new SimpleTarget<Bitmap>() {
                    @Override
                    public void onResourceReady(Bitmap resource, GlideAnimation<? super Bitmap> glideAnimation) {
                        img1.setImageBitmap(resource);
                    }
                });
    

    I have made this code by myself with some logic...Its 100% working.....pls do upvote my ans.

    The textview and imageview are from @layout/nav_header_main.xml

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

    If you're using bindings you can do

    val headerView = binding.navView.getHeaderView(0)
    val headerBinding = NavDrawerHeaderBinding.bind(headerView)
    headerBinding.textView.text = "Your text here"
    
    0 讨论(0)
  • 2020-12-04 08:37

    As mentioned in the bug 190226, Since version 23.1.0 getting header layout view with: navigationView.findViewById(R.id.navigation_header_text) no longer works.

    A workaround is to inflate the headerview programatically and find view by ID from the inflated header view.

    For example:

    View headerView = navigationView.inflateHeaderView(R.layout.navigation_header);
    headerView.findViewById(R.id.navigation_header_text);
    

    Ideally there should be a method getHeaderView() but it has already been proposed, let's see and wait for it to be released in the feature release of design support library.

    0 讨论(0)
  • I know this is an old post but i am sure that this might help someone down the road.

    You can simply get the headerView element of the Navigation view by doing this:

     NavigationView mView = ( NavigationView ) findViewById( R.id.nav_view );
    
     if( mView != null ){
         LinearLayout mParent = ( LinearLayout ) mView.getHeaderView( 0 );
    
         if( mParent != null ){
            // Set your values to the image and text view by declaring and setting as you need to here. 
         }
     }
    

    I hope that this helps someone.

    0 讨论(0)
提交回复
热议问题