Navigation Drawer with duplicity

1

I have a NavigationDrawer with two EditText and I would like these two fields to show up the user's name and email data, however in my TelaPrincipal class when I try to throw the information to one of the two fields it is demonstrated twice in the NavigationDrawer.

XML

<?xmlversion="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="@dimen/nav_header_height"
    android:background="@drawable/side_nav_bar"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:theme="@style/ThemeOverlay.AppCompat.Dark"
    android:orientation="vertical"
    android:gravity="bottom">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/telaprincipal_nav_header_et_meuperfil"
        android:layout_marginBottom="2dp"
        android:layout_marginTop="60dp"
        style="@style/TextoMenuDrawerTituloPerfil"/>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <TextView
            android:id="@+id/nav_header_telaprincipal_tv_nome"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            style="@style/TextoMenuDrawerTitulo"
            android:text=""
            android:textAppearance="@style/TextAppearance.AppCompat.Body1" />

        <TextView
            android:id="@+id/nav_header_telaprincipal_tv_email"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="2dp"
            style="@style/TextoMenuDrawerNormal"
            android:text="" />
    </LinearLayout>
</LinearLayout>

Java class

public class TelaPrincipal extends BaseActivity
        implements NavigationView.OnNavigationItemSelectedListener {

    //Componentes da Activity
    private Context mContext;
    private DrawerLayout mDrawerLayout;
    private NavigationView navigationView;
    private View mView;
    private TextView tvNome;
    private TextView tvEmail;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.telaprincipal);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        //Titulo da Activity
        setTitle(getString(R.string.telaprincipal_titulo_activity));

        //inicializando as variaveis
        inicializaVariavel();
        inicializaAcao();

        //Método do Navegation Drawer
        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(this, drawer, toolbar, R.string.telaprincipal_drawer_aberto, R.string.telaprincipal_drawer_fechado);
        drawer.setDrawerListener(toggle);
        toggle.syncState();

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

        LayoutInflater layoutInflater = getLayoutInflater();
        mView = layoutInflater.inflate(R.layout.nav_header_tela_principal, navigationView, false);

        tvNome = (TextView) mView.findViewById(R.id.nav_header_telaprincipal_tv_nome);
        tvEmail = (TextView) mView.findViewById(R.id.nav_header_telaprincipal_tv_email);

        tvNome.setText("Denis");
        tvNome.setText("[email protected]");

        navigationView.addHeaderView(mView);
    }
    
asked by anonymous 22.03.2016 / 17:32

1 answer

2

Good morning! All right? There are 2 errors in your code, let's see the first one:

You have in your main screen layout, a NavigationView that you gave the id nav_view so note the following, in it you have already defined the header, but in your code you are inflating an extra header! Soon there are two headers in your drawer, or how many you wish to inflate! There are two ways to fix this , use the one you think is best:

Form 1: Simply remove the header that has within nav_view in the layout by removing the line:

    app:headerLayout="@layout/nav_header_tela_principal"

Keeping the code exactly the way it is, you will no longer have the problem of doubling your header.

Form 2: Removing these lines of code:

    LayoutInflater layoutInflater = getLayoutInflater();
    mView = layoutInflater.inflate(R.layout.nav_header_tela_principal, navigationView, false);

//O código entre estes dois trechos não deve ser alterado!

    navigationView.addHeaderView(mView);

Replacing with:

    mView = navigationView.getHeaderView(0);

With this it will get the first header defined in the layout, from this, follow with your code normally.

Either way will solve your problem.

In your second problem, notice that you are setting the text of TextView tvNome twice as noted by @ Luênne!

    
23.03.2016 / 13:41