Advertisement
Guest User

MainActivity.java

a guest
May 1st, 2016
36,971
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.13 KB | None | 0 0
  1. package com.okedroid.contohnavigationdrawer;
  2.  
  3. import android.os.Bundle;
  4. import android.support.design.widget.NavigationView;
  5. import android.support.v4.widget.DrawerLayout;
  6. import android.support.v7.app.ActionBarDrawerToggle;
  7. import android.support.v7.app.AppCompatActivity;
  8. import android.support.v7.widget.Toolbar;
  9. import android.view.MenuItem;
  10. import android.view.View;
  11. import android.widget.Toast;
  12. public class MainActivity extends AppCompatActivity {
  13.     //Mendefinisikan variabel
  14.     private Toolbar toolbar;
  15.     private NavigationView navigationView;
  16.     private DrawerLayout drawerLayout;
  17.     @Override
  18.     protected void onCreate(Bundle savedInstanceState) {
  19.         super.onCreate(savedInstanceState);
  20.         setContentView(R.layout.activity_main);
  21.         // Menginisiasi Toolbar dan mensetting sebagai actionbar
  22.         toolbar = (Toolbar) findViewById(R.id.toolbar);
  23.         setSupportActionBar(toolbar);
  24.         // Menginisiasi  NavigationView
  25.         navigationView = (NavigationView) findViewById(R.id.navigation_view);
  26.         //Mengatur Navigasi View Item yang akan dipanggil untuk menangani item klik menu navigasi
  27.         navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
  28.             // This method will trigger on item Click of navigation menu
  29.             @Override
  30.             public boolean onNavigationItemSelected(MenuItem menuItem) {
  31.                 //Memeriksa apakah item tersebut dalam keadaan dicek  atau tidak,
  32.                 if(menuItem.isChecked()) menuItem.setChecked(false);
  33.                 else menuItem.setChecked(true);
  34.                 //Menutup  drawer item klik
  35.                 drawerLayout.closeDrawers();
  36.                 //Memeriksa untuk melihat item yang akan dilklik dan melalukan aksi
  37.                 switch (menuItem.getItemId()){
  38.                     // pilihan menu item navigasi akan menampilkan pesan toast klik kalian bisa menggantinya
  39.                     //dengan intent activity
  40.                     case R.id.navigation1:
  41.                         Toast.makeText(getApplicationContext(), "Beranda Telah Dipilih", Toast.LENGTH_SHORT).show();
  42.                         return true;
  43.                     case R.id.navigation2:
  44.                         Toast.makeText(getApplicationContext(),"Profil Telah Dipilih",Toast.LENGTH_SHORT).show();
  45.                         return true;
  46.                     case R.id.navigation3:
  47.                         Toast.makeText(getApplicationContext(),"Daftar Telah Dipilih",Toast.LENGTH_SHORT).show();
  48.                         return true;
  49.                     case R.id.navigation4:
  50.                         Toast.makeText(getApplicationContext(),"Setting telah dipilih",Toast.LENGTH_SHORT).show();
  51.                         return true;
  52.                     case R.id.navigation5:
  53.                         Toast.makeText(getApplicationContext(),"About telah dipilih",Toast.LENGTH_SHORT).show();
  54.                         return true;
  55.                     default:
  56.                         Toast.makeText(getApplicationContext(),"Kesalahan Terjadi ",Toast.LENGTH_SHORT).show();
  57.                         return true;
  58.                 }
  59.             }
  60.         });
  61.         // Menginisasi Drawer Layout dan ActionBarToggle
  62.         drawerLayout = (DrawerLayout) findViewById(R.id.drawer);
  63.         ActionBarDrawerToggle actionBarDrawerToggle = new ActionBarDrawerToggle(this,drawerLayout,toolbar,R.string.openDrawer, R.string.closeDrawer){
  64.             @Override
  65.             public void onDrawerClosed(View drawerView) {
  66.                 // Kode di sini akan merespons setelah drawer menutup disini kita biarkan kosong
  67.                 super.onDrawerClosed(drawerView);
  68.             }
  69.             @Override
  70.             public void onDrawerOpened(View drawerView) {
  71.                 //  Kode di sini akan merespons setelah drawer terbuka disini kita biarkan kosong
  72.                 super.onDrawerOpened(drawerView);
  73.             }
  74.         };
  75.         //Mensetting actionbarToggle untuk drawer layout
  76.         drawerLayout.setDrawerListener(actionBarDrawerToggle);
  77.         //memanggil synstate
  78.         actionBarDrawerToggle.syncState();
  79.     }
  80.  
  81.  
  82. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement