Diego Barreiro Diego Barreiro - 1 month ago 5
Android Question

How to change the display content in a NavigationDrawer?

How can I change content to display after selecting an item in a NavigationDrawer?


I mean, for example, I selected LogIn option in NavigationDrawer, and now I want to change the content that is displayed to another in an XML file.

MainActivity (LauncherActivity) Java file:

package com.dotapps.sosgram;


import android.os.Bundle;
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 net.hockeyapp.android.UpdateManager;
import net.hockeyapp.android.CrashManager;
import net.hockeyapp.android.LoginManager;



public class LauncherActivity extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.navbar);
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);

LoginManager.register(this, "SECRET", LoginManager.LOGIN_MODE_EMAIL_PASSWORD);
LoginManager.verifyLogin(this, getIntent());

checkForUpdates();
}

@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 onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.launcher, menu);
return true;
}

@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_login) {
// Here I want to change the layout
} else if (id == R.id.nav_signup) {

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

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

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

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

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

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

}

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



// HockeyApp
@Override
public void onResume() {
super.onResume();
checkForCrashes();
}

@Override
public void onPause() {
super.onPause();
unregisterManagers();
}

@Override
public void onDestroy() {
super.onDestroy();
unregisterManagers();
}

private void checkForCrashes() {
CrashManager.register(this);
}

private void checkForUpdates() {
UpdateManager.register(this);
}

private void unregisterManagers() {
UpdateManager.unregister();
}
}


Content:
Google Drive - (Due to my few rep, I have to share it via Drive)

Anyone know how to do it?




PD: I've tryed with
setContentView(R.layout.login)
but the app crashes

Answer
Comments