how to change fragment on button click navigation drawer

Solutions on MaxInterview for how to change fragment on button click navigation drawer by the best coders in the world

showing results for - "how to change fragment on button click navigation drawer"
Mira
22 Apr 2017
1import android.os.Bundle;
2import android.support.design.widget.FloatingActionButton;
3import android.support.design.widget.Snackbar;
4import android.view.View;
5import android.support.design.widget.NavigationView;
6import android.support.v4.view.GravityCompat;
7import android.support.v4.widget.DrawerLayout;
8import android.support.v7.app.ActionBarDrawerToggle;
9import android.support.v7.app.AppCompatActivity;
10import android.support.v7.widget.Toolbar;
11import android.view.Menu;
12import android.view.MenuItem;
13import android.support.v4.app.Fragment;
14import android.support.v4.app.FragmentManager;
15import android.support.v4.app.FragmentTransaction;
16
17public class MainActivity extends AppCompatActivity
18        implements NavigationView.OnNavigationItemSelectedListener {
19
20    @Override
21    protected void onCreate(Bundle savedInstanceState) {
22        super.onCreate(savedInstanceState);
23        setContentView(R.layout.activity_main);
24        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
25        setSupportActionBar(toolbar);
26
27        FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
28        fab.setOnClickListener(new View.OnClickListener() {
29            @Override
30            public void onClick(View view) {
31                Snackbar.make(view, "bryanposvoc1339.garcia@gmail.com", Snackbar.LENGTH_LONG)
32                        .setAction("Action", null).show();
33            }
34        });
35
36        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
37        ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
38                this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
39        drawer.setDrawerListener(toggle);
40        toggle.syncState();
41
42        NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
43        navigationView.setNavigationItemSelectedListener(this);
44    }
45
46    @Override
47    public void onBackPressed() {
48        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
49        if (drawer.isDrawerOpen(GravityCompat.START)) {
50            drawer.closeDrawer(GravityCompat.START);
51        } else {
52            super.onBackPressed();
53        }
54    }
55
56    @Override
57    public boolean onCreateOptionsMenu(Menu menu) {
58        // Inflate the menu; this adds items to the action bar if it is present.
59        getMenuInflater().inflate(R.menu.main, menu);
60        return true;
61    }
62
63    @Override
64    public boolean onOptionsItemSelected(MenuItem item) {
65        // Handle action bar item clicks here. The action bar will
66        // automatically handle clicks on the Home/Up button, so long
67        // as you specify a parent activity in AndroidManifest.xml.
68        int id = item.getItemId();
69
70        //noinspection SimplifiableIfStatement
71        if (id == R.id.action_settings) {
72            return true;
73        }
74
75        return super.onOptionsItemSelected(item);
76    }
77
78    @SuppressWarnings("StatementWithEmptyBody")
79    @Override
80    public boolean onNavigationItemSelected(MenuItem item) {
81        int id = item.getItemId();
82        Fragment fragment;
83
84        if (id == R.id.nav_cough) {
85             fragment = new first();
86            FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
87            ft.replace(R.id.mainFrame, fragment);
88            ft.commit();
89        }
90        else if (id == R.id.nav_colds) {
91              fragment = new second();
92            FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
93            ft.replace(R.id.mainFrame, fragment);
94            ft.commit();
95        }
96        else if (id == R.id.nav_fever) {
97            fragment = new third();
98            FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
99            ft.replace(R.id.mainFrame, fragment);
100            ft.commit();
101        }
102        else if (id == R.id.nav_vegetables) {
103            fragment = new fourth();
104            FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
105            ft.replace(R.id.mainFrame, fragment);
106            ft.commit();
107        } else if (id == R.id.nav_fruits) {
108            fragment = new fifth();
109            FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
110            ft.replace(R.id.mainFrame, fragment);
111            ft.commit();
112        } else if (id == R.id.nav_about) {
113            fragment = new sixth();
114            FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
115            ft.replace(R.id.mainFrame, fragment);
116            ft.commit();
117        }
118        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
119        drawer.closeDrawer(GravityCompat.START);
120        return true;
121    }
122}
123