Java tutorial
package com.example.intern01.driverlicense.activity; import android.content.Context; import android.content.Intent; import android.os.Bundle; import android.os.Handler; import android.support.design.widget.FloatingActionButton; import android.support.design.widget.NavigationView; import android.support.design.widget.Snackbar; import android.support.v4.app.Fragment; import android.support.v4.app.FragmentTransaction; 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.MenuItem; import android.view.View; import android.widget.ImageView; import android.widget.TextView; import com.bumptech.glide.Glide; import com.bumptech.glide.load.engine.DiskCacheStrategy; import com.example.intern01.driverlicense.LoginActivity; import com.example.intern01.driverlicense.R; import com.example.intern01.driverlicense.fragment.About; import com.example.intern01.driverlicense.fragment.MyCars; import com.example.intern01.driverlicense.fragment.Profile; import com.example.intern01.driverlicense.other.CircleTransform; import com.facebook.login.LoginManager; import com.google.firebase.auth.FirebaseAuth; public class MainActivity extends AppCompatActivity { private NavigationView navigationView; private DrawerLayout drawer; private View navHeader; private ImageView imgNavHeaderBg, imgProfile; private TextView txtName; private Toolbar toolbar; //!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!DECLARARE BUTON //private FloatingActionButton fab; // urls to load navigation header background image // and profile image private static final String urlNavHeaderBg = "http://www.topofandroid.com/wp-content/uploads/2015/05/ahagzjsozh.jpg"; private static final String urlProfileImg = "https://lh3.googleusercontent.com/eCtE_G34M9ygdkmOpYvCag1vBARCmZwnVS6rS5t4JLzJ6QgQSBquM0nuTsCpLhYbKljoyS-txg"; // index to identify current nav menu item public static int navItemIndex = 0; // tags used to attach the fragments private static final String TAG_PROFILE = "home"; private static final String TAG_ABOUT = "about"; private static final String TAG_CARS = "cars"; public static String CURRENT_TAG = TAG_PROFILE; // toolbar titles respected to selected nav menu item private String[] activityTitles; // flag to load home fragment when user presses back key private boolean shouldLoadHomeFragOnBackPress = true; private Handler mHandler; Context context; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); toolbar = (Toolbar) findViewById(R.id.toolbar); setSupportActionBar(toolbar); context = this; init(); mHandler = new Handler(); drawer = (DrawerLayout) findViewById(R.id.drawer_layout); navigationView = (NavigationView) findViewById(R.id.nav_view); //!!!!!!!!!!!!!!!!!!!! ID BUTON //fab = (FloatingActionButton) findViewById(R.id.fab); // Navigation view header navHeader = navigationView.getHeaderView(0); txtName = (TextView) navHeader.findViewById(R.id.name); imgNavHeaderBg = (ImageView) navHeader.findViewById(R.id.img_header_bg); imgProfile = (ImageView) navHeader.findViewById(R.id.img_profile); // load toolbar titles from string resources activityTitles = getResources().getStringArray(R.array.nav_item_activity_titles); //!!!!!!!!!!!!!!! adaugare actiune buton //fab.setOnClickListener(new View.OnClickListener() { // @Override // public void onClick(View view) { // Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG) // .setAction("Action", null).show(); //} // }); // load nav menu header data loadNavHeader(); // initializing navigation menu setUpNavigationView(); if (savedInstanceState == null) { navItemIndex = 0; CURRENT_TAG = TAG_PROFILE; loadHomeFragment(); } } /*** * Load navigation menu header information * like background image, profile image * name, website, notifications action view (dot) */ private void loadNavHeader() { // name, website txtName.setText("Name of the User"); // loading header background image Glide.with(this).load(urlNavHeaderBg).crossFade().diskCacheStrategy(DiskCacheStrategy.ALL) .into(imgNavHeaderBg); // Loading profile image Glide.with(this).load(urlProfileImg).crossFade().thumbnail(0.5f).bitmapTransform(new CircleTransform(this)) .diskCacheStrategy(DiskCacheStrategy.ALL).into(imgProfile); } /*** * Returns respected fragment that user * selected from navigation menu */ private void loadHomeFragment() { // selecting appropriate nav menu item selectNavMenu(); // set toolbar title setToolbarTitle(); // if user select the current navigation menu again, don't do anything // just close the navigation drawer if (getSupportFragmentManager().findFragmentByTag(CURRENT_TAG) != null) { drawer.closeDrawers(); // !!!!!!!!!!!!!!!!!!show or hide the fab button //toggleFab(); return; } // Sometimes, when fragment has huge data, screen seems hanging // when switching between navigation menus // So using runnable, the fragment is loaded with cross fade effect // This effect can be seen in GMail app Runnable mPendingRunnable = new Runnable() { @Override public void run() { // update the main content by replacing fragments Fragment fragment = getHomeFragment(); FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction(); fragmentTransaction.setCustomAnimations(android.R.anim.fade_in, android.R.anim.fade_out); fragmentTransaction.replace(R.id.frame, fragment, CURRENT_TAG); fragmentTransaction.commitAllowingStateLoss(); } }; // If mPendingRunnable is not null, then add to the message queue if (mPendingRunnable != null) { mHandler.post(mPendingRunnable); } // show or hide the fab button //!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! //toggleFab(); //Closing drawer on item click drawer.closeDrawers(); // refresh toolbar menu invalidateOptionsMenu(); } private Fragment getHomeFragment() { switch (navItemIndex) { case 0: // home - profile fragment Profile profileFragment = new Profile(); return profileFragment; case 1: // about fragment About aboutFragment = new About(); return aboutFragment; case 2: // cars fragment MyCars myCarsFragment = new MyCars(); return myCarsFragment; default: return new Profile(); } } private void setToolbarTitle() { getSupportActionBar().setTitle(activityTitles[navItemIndex]); } private void selectNavMenu() { navigationView.getMenu().getItem(navItemIndex).setChecked(true); } //LOG OUT private FirebaseAuth auth = null; private boolean init() { auth = FirebaseAuth.getInstance(); return true; } private void setUpNavigationView() { //Setting Navigation View Item Selected Listener to handle the item click of the navigation menu navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() { // This method will trigger on item Click of navigation menu @Override public boolean onNavigationItemSelected(MenuItem menuItem) { //Check to see which item was being clicked and perform appropriate action switch (menuItem.getItemId()) { //Replacing the main content with ContentFragment Which is our Inbox View; case R.id.nav_profile: navItemIndex = 0; CURRENT_TAG = TAG_PROFILE; break; case R.id.nav_cars: navItemIndex = 1; CURRENT_TAG = TAG_CARS; break; case R.id.nav_about: navItemIndex = 2; CURRENT_TAG = TAG_ABOUT; break; case R.id.nav_logout: navItemIndex = 3; getSharedPreferences("loginC", MODE_PRIVATE).edit().putString("lc_email", null) .putString("lc_pass", null).apply(); auth.signOut(); LoginManager.getInstance().logOut(); Intent i = new Intent(context, LoginActivity.class); context.startActivity(i); finish(); //CURRENT_TAG = TAG_; break; default: navItemIndex = 0; } //Checking if the item is in checked state or not, if not make it in checked state if (menuItem.isChecked()) { menuItem.setChecked(false); } else { menuItem.setChecked(true); } menuItem.setChecked(true); loadHomeFragment(); return true; } }); ActionBarDrawerToggle actionBarDrawerToggle = new ActionBarDrawerToggle(this, drawer, toolbar, R.string.openDrawer, R.string.closeDrawer) { @Override public void onDrawerClosed(View drawerView) { // Code here will be triggered once the drawer closes as we dont want anything to happen so we leave this blank super.onDrawerClosed(drawerView); } @Override public void onDrawerOpened(View drawerView) { // Code here will be triggered once the drawer open as we dont want anything to happen so we leave this blank super.onDrawerOpened(drawerView); } }; //Setting the actionbarToggle to drawer layout //drawer.setDrawerListener(actionBarDrawerToggle); //calling sync state is necessary or else your hamburger icon wont show up actionBarDrawerToggle.syncState(); } @Override public void onBackPressed() { if (drawer.isDrawerOpen(GravityCompat.START)) { drawer.closeDrawers(); return; } // This code loads home fragment when back key is pressed // when user is in other fragment than home if (shouldLoadHomeFragOnBackPress) { // checking if user is on other navigation menu // rather than home if (navItemIndex != 0) { navItemIndex = 0; CURRENT_TAG = TAG_PROFILE; loadHomeFragment(); return; } } super.onBackPressed(); } // !!!!!!!!!!!!!!!!!!!show or hide the fab //private void toggleFab() { // if (navItemIndex == 0) // fab.show(); //else // fab.hide(); // } }