Thursday, March 17, 2016

How to set navigation drawer header image and name programmatically in android

First  invoke navigation drawer in your main class then use this code for  header image or user name

don't add header in xml add using code by inflating layout
View hView =  navigationView.inflateHeaderView(R.layout.nav_header_main);
ImageView imgvw = (ImageView)hView.findViewById(R.id.imageView);
TextView tv = (TextView)hView.findViewById(R.id.textview);
imgvw .setImageResource();
tv.settext("new text");
Hint of work
@Overrideprotected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.service_main);

    // Initializing Toolbar and setting it as the actionbar    toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    
    navigationView = (NavigationView) findViewById(R.id.navigation_view);


    View hView =  navigationView.inflateHeaderView(R.layout.abc);
    ImageView imgvw = (ImageView)hView.findViewById(R.id.profile_image);
    TextView tv = (TextView)hView.findViewById(R.id.username);
    imgvw .setImageResource(R.drawable.logo);
    tv.setText("UserName");

5 comments:

  1. THANKYOU!
    MY APP ITS WORKING NOW!
    GODBLESS!

    ReplyDelete
  2. Obrigado. Essas dicas deram certo pra mim.

    ReplyDelete
  3. it works but it duplicates the image and text

    ReplyDelete
  4. I admire this article for the well-researched content and excellent wording. I got so involved in this material that I couldn’t stop reading. I am impressed with your work and skill. Thank you so much. Viking Ice Maker Repair in Los Angeles

    ReplyDelete
  5. I admire this article for the well-researched content and excellent wording. I got so involved in this material that I couldn’t stop reading. I am impressed with your work and skill. Thank you so much. Installing the Sub-Zero Under-counter Refrigerator

    ReplyDelete