OnClick for navigation drawer header not working

前端 未结 6 580
鱼传尺愫
鱼传尺愫 2020-12-05 07:25

I have a navigation drawer in my app which contains a header and some list items. The header has a textview which i want to make clickable but I am not able to do it.

6条回答
  •  星月不相逢
    2020-12-05 08:00

    i know its late this is for those who facing the same problem.

    place your header layout in the navigation view like this

    this is in activity_main.xml

    
    

    create a layout, name it layout_header_profile.xml and put the fill it what ever view you want.

    layout_header_profile.xml 
    
    
    
    
    
    
        
    
            
    
            
        
        
        
    

    then this header layout file will be in your activity_main.xml only

    so in your MainActivity.java you can declare it as you do views from activity_main.xml and perform actions on it, no special code required.

    do like this in your onCreate()

    TextView tvUserName = (TextView) findViewById(R.id.id_user_name);
    tvUserName.setText("My Name");
       tvUserName.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(getBaseContext(),"clicking textview",Toast.LENGTH_LONG).show();
            }
        });
    

    Hope it works Happy coding.

提交回复
热议问题