Tutorial 7 - Navigation Drawer

 


This is Sample Project Which you can improve UI design & Coding skills .

If you get any trouble. Please Contact me on WhatsApp!!

Newly Added


1)First You need to create menu file in "res" 
2)Then create new layout called "header"

then paste this code in header.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="200dp"
android:background="@color/coloryellow"
android:gravity="bottom"
android:orientation="vertical"
android:padding="20dp">

<ImageView
android:layout_width="75dp"
android:layout_height="75dp"
android:src="@drawable/ic_phone"

/>

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Travelling"
android:fontFamily="@font/baloo"
android:textSize="24sp"

/>

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="freesourcecodelk"
android:fontFamily="@font/baloo"
android:textSize="14sp"

/>

</LinearLayout>

HomeActivity

activity_home

main_menu

package com.rrmchathura.myfirstapp;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.RequiresApi;
import androidx.appcompat.app.ActionBarDrawerToggle;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.view.GravityCompat;
import androidx.drawerlayout.widget.DrawerLayout;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.view.MenuItem;
import android.view.View;
import android.view.WindowManager;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import androidx.appcompat.widget.Toolbar;

import com.google.android.gms.tasks.OnFailureListener;
import com.google.android.gms.tasks.OnSuccessListener;
import com.google.android.material.navigation.NavigationView;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.auth.FirebaseUser;
import com.google.firebase.firestore.DocumentReference;
import com.google.firebase.firestore.DocumentSnapshot;
import com.google.firebase.firestore.EventListener;
import com.google.firebase.firestore.FirebaseFirestore;
import com.google.firebase.firestore.FirebaseFirestoreException;

public class HomeActivity extends AppCompatActivity implements
NavigationView.OnNavigationItemSelectedListener {

DrawerLayout drawerLayout;
NavigationView navigationView;
Toolbar toolbar;

private EditText fullName,email,phone;
private TextView verifyMsg;
FirebaseAuth fAuth;
FirebaseFirestore fStore;
String userID;
Button resendCode;

private Button logout;


@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_home);

fullName = findViewById(R.id.profileName);
email = findViewById(R.id.profileEmail);
phone = findViewById(R.id.profilePhone);

logout = findViewById(R.id.btn_logout);

resendCode = findViewById(R.id.resendCode);
verifyMsg = findViewById(R.id.verifyMsg);

//Drawer hooks
drawerLayout = findViewById(R.id.drawer_layout);
navigationView = findViewById(R.id.nav_view);
toolbar = findViewById(R.id.toolbar);

/////////////////////////////Tool Bar///////////////////////////
setSupportActionBar(toolbar);

/////////////////////////// //Navigation Drawer Menu///////////
navigationView.bringToFront();
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(this,
drawerLayout,toolbar,R.string.navigation_drawer_open,
R.string.navigation_drawer_close);

drawerLayout.addDrawerListener(toggle);
toggle.syncState();


///////////////////////Navigation Drawner item Clicked////////////

navigationView.setNavigationItemSelectedListener(this);

navigationView.setCheckedItem(R.id.nav_home);

verifyMsg.startAnimation((Animation) AnimationUtils.
loadAnimation(HomeActivity.this,R.anim.animation_logo));



fAuth = FirebaseAuth.getInstance();
fStore = FirebaseFirestore.getInstance();



userID = fAuth.getCurrentUser().getUid();
FirebaseUser user = fAuth.getCurrentUser();

if (!user.isEmailVerified()){
resendCode.setVisibility(View.VISIBLE);
verifyMsg.setVisibility(View.VISIBLE);


//verify Button Ckicked


resendCode.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(final View v) {

user.sendEmailVerification().
addOnSuccessListener(new OnSuccessListener<Void>() {
@Override
public void onSuccess(Void unused) {
Toast.makeText(HomeActivity.
this,"Verification Email Has been Sent",
Toast.LENGTH_SHORT).show();
}
}).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
Log.d("TAG","onFailure Email sent " +
e.getMessage());
}
});
}
});
}






DocumentReference documentReference = fStore.
collection("users").document(userID);
documentReference.
addSnapshotListener(new EventListener<DocumentSnapshot>() {
@Override
public void onEvent(@Nullable DocumentSnapshot documentSnapshot,
@Nullable FirebaseFirestoreException error) {
phone.setText(documentSnapshot.getString("phone"));
fullName.setText(documentSnapshot.getString("fName"));
email.setText(documentSnapshot.getString("email"));
}
});

logout.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
FirebaseAuth.getInstance().signOut();
Toast.makeText(HomeActivity.
this,"Logeed Out!",Toast.LENGTH_SHORT).show();
startActivity(new Intent(HomeActivity.
this,LoginActivity.class));
finish();
}
});
}


///////////////////////Navigation Drawner item Clicked//////////////////////
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem menuItem) {

switch (menuItem.getItemId()){
case R.id.nav_home:
break;
case R.id.nav_profile:
Intent intent = new Intent(HomeActivity.this,
ProfileActivity.class);
startActivity(intent);
break;

case R.id.nav_bus:
Toast.makeText(HomeActivity.this,"You clicked Bus",
Toast.LENGTH_SHORT).show();
break;
}
drawerLayout.closeDrawer(GravityCompat.START);
return true;
}

// @Override
// public void onBackPressed() {
// if (drawerLayout.isDrawerOpen(GravityCompat.START)){
// drawerLayout.closeDrawer(GravityCompat.START);
// }else {
// super.onBackPressed();
// }
//
// }
}

<?xml version="1.0" encoding="utf-8"?>
<androidx.drawerlayout.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
tools:context=".HomeActivity"
android:fitsSystemWindows="true"
tools:openDrawer="start"
android:id="@+id/drawer_layout"
>



<com.google.android.material.navigation.NavigationView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:id="@+id/nav_view"
app:headerLayout="@layout/header"
app:menu="@menu/main_menu"
android:layout_gravity="start"
/>

<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">

<LinearLayout
android:layout_width="match_parent"
android:layout_height="50dp"
/>
<!-- android:background="@drawable/top_gradient"-->

<androidx.appcompat.widget.Toolbar
android:id="@+id/toolbar"
android:background="@color/coloryellow"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:navigationIcon="@drawable/ic_baseline_menu_24"


/>

<!-- <TextView-->

<!-- android:layout_width="match_parent"-->
<!-- android:layout_height="wrap_content"-->
<!-- android:text="Covid Tracker"-->
<!-- android:textStyle="bold"-->
<!-- android:layout_marginTop="10dp"-->
<!-- android:height="30dp"-->
<!-- android:fontFamily="sans-serif-smallcaps"-->
<!-- android:textAlignment="center"-->
<!-- android:textSize="25dp"-->
<!-- android:textColor="@color/white"-->
<!-- android:gravity="center_horizontal" />-->

<ScrollView

android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="50dp">

<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="10dp"
android:layout_marginTop="40dp"
android:layout_marginRight="10dp"
android:orientation="vertical">

<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="30dp"
android:text="Your Profile"
android:fontFamily="@font/baloo"
android:textAlignment="center"
android:textSize="20dp"
android:gravity="center_horizontal" />

<ImageView
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_gravity="center"
android:layout_marginTop="20dp"
android:src="@drawable/ic_user"
android:scaleType="centerCrop"
/>

<EditText
android:id="@+id/profileName"
android:layout_width="350dp"
android:layout_height="45dp"
android:editable="false"
android:textColor="@color/textcolor"
android:background="@drawable/input_style"
android:drawableRight="@drawable/ic_user"
android:hint="Your Name"
android:layout_gravity="center"
android:paddingRight="10dp"
android:paddingLeft="10dp"
android:layout_marginTop="10dp"
android:fontFamily="@font/baloo"
/>

<EditText
android:id="@+id/profileEmail"
android:layout_width="350dp"
android:layout_height="45dp"
android:editable="false"
android:background="@drawable/input_style"
android:drawableRight="@drawable/ic_email"
android:hint="Your Email Address"
android:textColor="@color/textcolor"
android:layout_gravity="center"
android:paddingRight="10dp"
android:paddingLeft="10dp"
android:layout_marginTop="10dp"
android:fontFamily="@font/baloo"
/>

<EditText
android:id="@+id/profilePhone"
android:layout_width="350dp"
android:layout_height="45dp"
android:editable="false"
android:background="@drawable/input_style"
android:drawableRight="@drawable/ic_phone"
android:hint="Your Phone"
android:textColor="@color/textcolor"
android:layout_gravity="center"
android:paddingRight="10dp"
android:paddingLeft="10dp"
android:layout_marginTop="10dp"
android:fontFamily="@font/baloo"
/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">

<TextView
android:id="@+id/verifyMsg"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Email Not Verified !"
android:visibility="invisible"
android:textAlignment="center"
android:gravity="center_horizontal"
android:layout_marginTop="30dp"
android:fontFamily="@font/brandon_grotesque_black"
android:textSize="20dp"
android:textColor="@color/col_red"
/>
<Button
android:id="@+id/resendCode"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:visibility="invisible"
android:text="Verify Now"
android:background="@drawable/button_style"
android:textAllCaps="true"
android:layout_marginTop="5dp"
android:textStyle="bold"
android:textColor="#FFF"
/>

</LinearLayout>



</LinearLayout>
</ScrollView>
<Button
android:id="@+id/btn_logout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/button_style"
android:layout_alignParentEnd="true"
android:text="Log Out"
android:textSize="18dp"
android:textStyle="bold"
android:textColor="#FFF"
android:layout_alignParentRight="true"
android:layout_alignParentBottom="true"/>

</RelativeLayout>

</androidx.drawerlayout.widget.DrawerLayout>

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">

<group android:checkableBehavior="single">


<item
android:id="@+id/nav_home"
android:icon="@drawable/ic_home"
android:title="Home" />
<item
android:id="@+id/nav_profile"
android:icon="@drawable/ic_user"
android:title="Profile" />
<item
android:id="@+id/nav_bus"
android:icon="@drawable/ic_email"
android:title="Bus" />
<item
android:id="@+id/nav_plane"
android:icon="@drawable/ic_phone"
android:title="Plane" />

</group>


<item android:title="Pro">

<menu>
<group android:checkableBehavior="single">
<item
android:id="@+id/nav_login"
android:icon="@drawable/ic_phone"
android:title="Plane" />

<item
android:id="@+id/nav_noplane"
android:icon="@drawable/ic_phone"
android:title="Plane" />

<item
android:id="@+id/nav_logout"
android:icon="@drawable/ic_phone"
android:title="Plane" />
</group>
</menu>

</item>


<item android:title="Communicate">

<menu>

<item
android:id="@+id/nav_share"
android:icon="@drawable/ic_phone"
android:title="Login" />

<item
android:id="@+id/nav_rate"
android:icon="@drawable/ic_phone"
android:title="Rate us" />

</menu>


</item>
</menu>

Post a Comment

0 Comments

Youtube Channel Image
Coding With Chathura Subscribe To watch more Project Tutorials
Subscribe
Do you have any doubts? chat with us on WhatsApp
Hello, How can I help you? ...
Click me to start the chat...

Welcome to freesourcecodelk

X