S.A
S.A

Reputation: 46

How to pass information to a navigation header in Android Studio

I have an activity which asks for information from the user, AskUserForDetailsActivity and I have another TabActivity (which is NOT immediately after the AskUser.. activity) which has a navigation drawer.

I want to send information from AskUser.. to the navigation header of the drawer, which is again in another layout.

I tried:

 sendButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent intent = new Intent(getApplicationContext(), TabActivity.class);
            intent.putExtra("Name", textView1.getText().toString());
            intent.putExtra("Age", textView2.getText().toString());
            intent.putExtra("Area", textView3.getText().toString());
            intent.putExtra("Occupation", textView4.getText().toString());
        }
    });

This comes in AskForUser.. And this goes in TabActivity:

 View v = getLayoutInflater().inflate(R.layout.nav_header_navigation,null);
    TextView receive1 = (TextView) v.findViewById(R.id.myName);
    receive1.setText(getIntent().getStringExtra("Name"));
    TextView receive2 = (TextView) v.findViewById(R.id.myAge);
    receive2.setText(getIntent().getStringExtra("Age"));
    TextView receive3 = (TextView) v.findViewById(R.id.myArea);
    receive3.setText(getIntent().getStringExtra("Area"));
    TextView receive4 = (TextView) v.findViewById(R.id.myOccupation);
    receive4.setText(getIntent().getStringExtra("Occupation"));

But the text is not appearing in my navigation header. What am I doing wrong?

Upvotes: 3

Views: 678

Answers (1)

Baran Çavuşoğlu
Baran Çavuşoğlu

Reputation: 111

Instead of:

View v = getLayoutInflater().inflate(R.layout.nav_header_navigation,null);

Can you try the code below

NavigationView navigationView = findViewById(R.id.nav_view);
View v = navigationView.getHeaderView(0);

Upvotes: 2

Related Questions