I have simple a navigation drawer app and the navigation drawer and hamburger is work fine in my main activity.
In the "Visiting Place" activity I do all things the same as the Main activity. All I am trying to do is to open a NavigationDrawer by pressing the "hamburger" icon in the top left corner. The "hamburger" button is visible, and when I start to pull from the left I see the animation on the button but pressing the button does not open/close the NavigationDrawer as I expect in My "Visiting Place activity".
Sorry for any confusion, below is the simplified code I am currently attempting to use:
This is my main activity code:
package com.example.android.raidighitour;
import androidx.annotation.NonNull;
import androidx.appcompat.app.ActionBarDrawerToggle;
import androidx.appcompat.app.AppCompatActivity;
import androidx.drawerlayout.widget.DrawerLayout;
import android.content.ClipData;
import android.content.Intent;
import android.os.Bundle;
import android.view.MenuItem;
import android.view.View;
import android.widget.ImageView;
import android.widget.Toast;
import com.google.android.material.navigation.NavigationView;
public class MainActivity extends AppCompatActivity {
private DrawerLayout drawerLayout;
private ActionBarDrawerToggle actionBarDrawerToggle;
private NavigationView navigationView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
drawerLayout =(DrawerLayout)findViewById(R.id.activity_main);
actionBarDrawerToggle=new ActionBarDrawerToggle(this,drawerLayout,R.string.open,R.string.close);
drawerLayout.addDrawerListener(actionBarDrawerToggle);
actionBarDrawerToggle.syncState();
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
navigationView=(NavigationView)findViewById(R.id.navigation);
Intent i1= new Intent(this,VisitingPlace.class);
navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
int id= item.getItemId();
switch (id){
case R.id.home:
Toast.makeText(MainActivity.this,"THis is home",Toast.LENGTH_SHORT).show();
break;
case R.id.visiting_place:
startActivity(i1);
break;
default:
return true;
}
return true;
}
});
ImageView visiting_place=(ImageView)findViewById(R.id.image_visiting_place);
visiting_place.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent i2= new Intent(MainActivity.this,VisitingPlace.class);
startActivity(i2);
}
});
}
@Override
public boolean onOptionsItemSelected(@NonNull MenuItem item) {
if(actionBarDrawerToggle.onOptionsItemSelected(item))return true;
return super.onOptionsItemSelected(item);
}
}
This is my Acitivity_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/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
android:background="@android:color/holo_green_dark"
tools:context=".MainActivity"
tools:openDrawer="start">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<ViewFlipper
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:autoStart="true"
android:id="@+id/sliding_image"
android:flipInterval="3000">
<ImageView
android:layout_width="match_parent"
android:layout_height="250dp"
android:background="@drawable/jatardeul"/>
<ImageView
android:layout_width="match_parent"
android:layout_height="250dp"
android:background="@drawable/raj_hotel"/>
<ImageView
android:layout_width="match_parent"
android:layout_height="250dp"
android:background="@drawable/raidighi_college"/>
<ImageView
android:layout_width="match_parent"
android:layout_height="250dp"
android:background="@drawable/raidighi_high_school"/>
<ImageView
android:layout_width="match_parent"
android:layout_height="250dp"
android:background="@drawable/raidighi_rural_hospital"/>
</ViewFlipper>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/sliding_image"
android:id="@+id/first_row_circular_images"
android:orientation="horizontal">
<ImageView
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="100dp"
android:background="@drawable/circular"
android:src= "@drawable/visiting_place"
android:layout_marginTop="25dp"
android:layout_marginLeft="25dp"
android:id="@+id/image_visiting_place"/>
<ImageView
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="100dp"
android:background="@drawable/circular"
android:src= "@drawable/emergency_help"
android:layout_marginTop="25dp"
android:layout_marginLeft="25dp"
android:layout_marginStart="25dp" />
<ImageView
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="100dp"
android:background="@drawable/circular"
android:src= "@drawable/public_place"
android:layout_marginTop="25dp"
android:layout_marginLeft="25dp"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/first_row_circular_images"
android:id="@+id/second_row_circular_images"
android:orientation="horizontal">
<ImageView
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="100dp"
android:background="@drawable/circular"
android:src= "@drawable/exiting_point"
android:layout_marginTop="25dp"
android:layout_marginLeft="25dp"
android:layout_marginStart="25dp" />
<ImageView
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="100dp"
android:background="@drawable/circular"
android:src= "@drawable/events"
android:layout_marginTop="25dp"
android:layout_marginLeft="25dp"
android:layout_marginStart="25dp" />
<ImageView
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="100dp"
android:background="@drawable/circular"
android:src= "@drawable/education"
android:layout_marginTop="25dp"
android:layout_marginLeft="25dp"
android:layout_marginStart="25dp" />
</LinearLayout>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/second_row_circular_images"
android:text="Hi this is a editable text. U can edit it."
android:textColor="@android:color/holo_purple"
android:textSize="49sp"
android:textStyle="bold"
android:layout_margin="60dp"
android:layout_centerInParent="true"/>
</RelativeLayout>
<com.google.android.material.navigation.NavigationView
android:layout_width="wrap_content"
android:layout_height="match_parent"
app:menu="@menu/navigation_menu"
app:headerLayout="@layout/nav_header"
android:fitsSystemWindows="true"
android:id="@+id/navigation"
android:layout_gravity="start">
</com.google.android.material.navigation.NavigationView>
</androidx.drawerlayout.widget.DrawerLayout>
Visiting Place Code:
package com.example.android.raidighitour;
import androidx.annotation.NonNull;
import androidx.appcompat.app.ActionBarDrawerToggle;
import androidx.appcompat.app.AppCompatActivity;
import androidx.drawerlayout.widget.DrawerLayout;
import android.content.Intent;
import android.os.Bundle;
import android.view.MenuItem;
import android.widget.Toast;
import com.google.android.material.navigation.NavigationView;
public class VisitingPlace extends AppCompatActivity {
private DrawerLayout drawerLayout;
private ActionBarDrawerToggle actionBarDrawerToggle;
private NavigationView navigationView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_visiting_place);
drawerLayout =(DrawerLayout)findViewById(R.id.visiting_place);
actionBarDrawerToggle=new ActionBarDrawerToggle(this,drawerLayout,R.string.open,R.string.close);
drawerLayout.addDrawerListener(actionBarDrawerToggle);
actionBarDrawerToggle.syncState();
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
navigationView=(NavigationView)findViewById(R.id.navigation);
Intent home_acitivity= new Intent(this,MainActivity.class);
navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
int id= item.getItemId();
switch (id){
case R.id.home:
startActivity(home_acitivity);
break;
case R.id.visiting_place:
break;
default:
return true;
}
return true;
}
});
}
}
Visiting_Place XML code:
<?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/visiting_place"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
android:background="@android:color/holo_green_dark"
tools:context=".VisitingPlace"
tools:openDrawer="start">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="45sp"
android:text="This is visiting place"
android:textColor="@color/design_default_color_primary_dark"/>
</RelativeLayout>
<com.google.android.material.navigation.NavigationView
android:layout_width="wrap_content"
android:layout_height="match_parent"
app:menu="@menu/navigation_menu"
app:headerLayout="@layout/nav_header"
android:fitsSystemWindows="true"
android:id="@+id/navigation"
android:layout_gravity="start">
</com.google.android.material.navigation.NavigationView>
</androidx.drawerlayout.widget.DrawerLayout>
You need to implement onOptionsItemSelected
in the VisitingPlace
activity as well.
Are you implementing this?
@Override
public boolean onOptionsItemSelected(@NonNull MenuItem item) {
if (actionBarDrawerToggle.onOptionsItemSelected(item)) {
return true;
}
return super.onOptionsItemSelected(item);
}