-1

I have a textview in my xml and I want to change the text from an intent. I've tried it before in the same class but on another xml and it works there. But after changing the contentView it don't work anymore. I want to set the displayName.

package de.myapp.app;

import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.view.View;
import android.support.design.widget.NavigationView;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.EditText;
import android.widget.TextView;

public class UserAreaActivity extends AppCompatActivity
        implements NavigationView.OnNavigationItemSelectedListener {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setTitle("");
        //setContentView(R.layout.nav_header_user_area);
        //final TextView displayName = (TextView) findViewById(R.id.displayName);
        //Intent intent = getIntent();
        //String dpdisplayName = intent.getStringExtra("displayName");
        //displayName.setText(dpdisplayName);

        setContentView(R.layout.activity_user_area);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
                this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
        drawer.setDrawerListener(toggle);
        toggle.syncState();

        NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
        navigationView.setNavigationItemSelectedListener(this);
    }

    @Override
    public void onBackPressed() {
        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        if (drawer.isDrawerOpen(GravityCompat.START)) {
            drawer.closeDrawer(GravityCompat.START);
        } else {
            super.onBackPressed();
        }
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        //if (id == R.id.action_settings) {
        //    return true;
        //}

        return super.onOptionsItemSelected(item);
    }

    @SuppressWarnings("StatementWithEmptyBody")
    @Override
    public boolean onNavigationItemSelected(MenuItem item) {
        // Handle navigation view item clicks here.
        int id = item.getItemId();

        if (id == R.id.nav_camera) {
            // Handle the camera action
        } else if (id == R.id.nav_gallery) {

        } else if (id == R.id.nav_slideshow) {

        } else if (id == R.id.nav_manage) {

        } else if (id == R.id.nav_share) {

        } else if (id == R.id.nav_send) {

        }

        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        drawer.closeDrawer(GravityCompat.START);
        return true;
    }
}

XML:

<TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:paddingTop="@dimen/nav_header_vertical_spacing"
        android:text="Mr. Android"
        android:textAppearance="@style/TextAppearance.AppCompat.Body1"
        android:id="@+id/displayName" />
ItsOdi1
  • 219
  • 3
  • 22

3 Answers3

1

At First Remove

Calling Multiple time

  setContentView(R.layout.activity_user_area); // Remove from 2nd time 

Final View

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    //Dont call setTitle(""); Here
    setContentView(R.layout.nav_header_user_area);
    final TextView displayName = (TextView) findViewById(R.id.displayName);
    Intent intent = getIntent();
    String dpdisplayName = intent.getStringExtra("displayName");
    displayName.setText(dpdisplayName);

    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    setTitle("");
  //code goes on here
  }
IntelliJ Amiya
  • 74,896
  • 15
  • 165
  • 198
1

[UPDATED] Use like this:

@Override
protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setTitle("");
 setContentView(R.layout.activity_user_area);

 Intent intentMain = getIntent();
 String dpdisplayName = intentMain.getStringExtra("displayName");

 Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
 setSupportActionBar(toolbar);

 DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
 ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
 drawer.setDrawerListener(toggle);
 toggle.syncState();

 NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
 View headerView = navigationView.getHeaderView(0);
 TextView navdisplayName = (TextView) headerView.findViewById(R.id.displayName);
 navdisplayName.setText(dpdisplayName);
}
W4R10CK
  • 5,502
  • 2
  • 19
  • 30
0

while setting setContentView() second time, you have eventually changes your attached view hierarchy tree. Your changes to textview will no longer be visible since its not even attached to the activity