Navigation Drawer Android App
Navigation Drawer Android App
1. menu_item.xml :
- <?xml version="1.0" encoding="utf-8"?>
- <menu xmlns:android="http://schemas.android.com/apk/res/android">
- <item
- android:id="@+id/home"
- android:icon="@drawable/home"
- android:title="Home"/>
- <item
- android:id="@+id/share"
- android:icon="@drawable/share"
- android:title="Share"/>
- <item
- android:id="@+id/star"
- android:icon="@drawable/star"
- android:title="Rate This App"/>
- </menu>
2.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:orientation="vertical"
android:background="@drawable/toolbar"
android:layout_height="match_parent">
<ImageView
android:layout_width="100dp"
android:layout_height="100dp"
android:src="@drawable/text"
android:tint="@color/white"
android:layout_marginTop="20dp"
android:layout_marginRight="20dp"
android:layout_marginLeft="20dp"
android:layout_marginBottom="10dp"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="App Name"
android:fontFamily="@font/font"
android:textStyle="bold"
android:layout_marginLeft="20dp"
android:textSize="22sp"
android:textColor="@color/white"/>
<View
android:layout_width="match_parent"
android:layout_height="2dp"
android:background="@color/dark_grey"
android:layout_marginTop="10dp"/>
</LinearLayout>
3. activity.main.xml:
<?xml version="1.0" encoding="utf-8"?>
<androidx.drawerlayout.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/drawerLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:id="@+id/toolbar"
android:background="@color/backgroundColor"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Toolbar
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="@color/backgroundColor">
<ImageView
android:id="@+id/menu"
android:layout_width="40dp"
android:layout_height="40dp"
android:src="@drawable/menu"
/>
<TextView
android:layout_marginLeft="40dp"
android:layout_marginTop="5dp"
android:id="@+id/textView"
android:textColor="@color/white"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="App Name"
android:textStyle="bold"
android:fontFamily="@font/hind"
android:textSize="20sp"/>
</Toolbar>
</LinearLayout>
</RelativeLayout>
<com.google.android.material.navigation.NavigationView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:id="@+id/navigationView"
app:menu="@menu/menu_item"
app:headerLayout="@layout/header"
android:layout_gravity="start"
/>
</androidx.drawerlayout.widget.DrawerLayout>
4.MainActivity.java
package com.abdulahad.abdulahad;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.MenuItem;
import android.view.View;
import android.widget.ImageView;
import android.widget.Toast;
import androidx.activity.EdgeToEdge;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.view.GravityCompat;
import androidx.drawerlayout.widget.DrawerLayout;
import com.google.android.material.navigation.NavigationView;
public class MainActivity extends AppCompatActivity {
DrawerLayout drawerLayout;
NavigationView navigationView;
ImageView menu;
View header;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
drawerLayout = findViewById(R.id.drawerLayout);
navigationView = findViewById(R.id.navigationView);
menu = findViewById(R.id.menu);
header = navigationView.getHeaderView(0);
menu.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (drawerLayout.isDrawerOpen(GravityCompat.START)){
drawerLayout.closeDrawer(GravityCompat.START);
}
else {
drawerLayout.openDrawer(GravityCompat.START);
}
}
});
navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
if (item.getItemId()==R.id.share){
String shareBody = "Hye,Online Quiz App"+"http://play.google.com/store/app/details?id ="+MainActivity.this.getPackageName();
Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType("text/plain");
intent.putExtra(Intent.EXTRA_TEXT,shareBody);
startActivity(intent);
drawerLayout.closeDrawer(GravityCompat.START);
}
else if (item.getItemId()==R.id.star) {
Intent rateIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("market://details?id=" + MainActivity.this.getPackageName()));
startActivity(rateIntent);
drawerLayout.closeDrawer(GravityCompat.START);
}
else if (item.getItemId()==R.id.home) {
Toast.makeText(MainActivity.this, "Home", Toast.LENGTH_SHORT).show();
drawerLayout.closeDrawer(GravityCompat.START);
}
return false;
}
});
}
}
Comments
Post a Comment