I am working on an app but there is a problem with hamburger icon as it is not working. It is not opening the navigation menu when i am clicking on it. what is the problem please tell me? I am trying to solve it but dont know what is the problem with it. I am new to code please help me.
Here is my fragment code
public class HomeFragment extends Fragment{
NavigationView navigationView;
ActionBarDrawerToggle toggle;
DrawerLayout drawerLayout;
Toolbar toolbar;
private int[] images;
private SliderAdapter sliderAdapter;
public HomeFragment() {
// Required empty public constructor
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_home, container, false);
navigationView = view.findViewById(R.id.navmenu);
drawerLayout = view.findViewById(R.id.drawerlayout);
toolbar = view.findViewById(R.id.toolbar);
ActionBar mActionBar;
((AppCompatActivity) getActivity()).setSupportActionBar(toolbar);
Objects.requireNonNull(((AppCompatActivity) getActivity()).getSupportActionBar()).setTitle("");
toggle = new ActionBarDrawerToggle(getActivity(), drawerLayout, R.string.open, R.string.close);
drawerLayout.addDrawerListener(toggle);
toggle.syncState();
((AppCompatActivity) getActivity()).getSupportActionBar().setDisplayHomeAsUpEnabled(true);
((AppCompatActivity) getActivity()).getSupportActionBar().setHomeAsUpIndicator(R.drawable.menu3);
return view;
}
}
Here is my XML file
<?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:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/drawerlayout"
tools:openDrawer="start"
android:background="@color/white"
tools:context=".HomeFragment">
<androidx.coordinatorlayout.widget.CoordinatorLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<com.google.android.material.appbar.AppBarLayout
android:id="@+id/appBarLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/ThemeOverlay.AppCompat.Dark"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<androidx.appcompat.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="@color/reddark"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_scrollFlags="scroll|enterAlways"
app:popupTheme="@style/Theme.AppCompat.Light">
<ImageView
android:layout_width="150dp"
android:layout_height="50dp"
android:layout_gravity="center_horizontal"
android:src="@drawable/backsssososos" />
</androidx.appcompat.widget.Toolbar>
<RelativeLayout
android:id="@+id/search_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/toolbar"
android:background="@color/reddark"
android:padding="10dp"
app:layout_behavior="@string/appbar_scrolling_view_behavior">
<EditText
android:id="@+id/searchbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/searchbardesign"
android:backgroundTint="#F8F8F8"
android:drawableLeft="@android:drawable/ic_menu_camera"
android:drawableRight="@android:drawable/ic_menu_search"
android:drawablePadding="22dp"
android:gravity="left|center"
android:hint="Enter City,Colony name..."
android:imeOptions="actionSearch"
android:inputType="text"
android:maxLines="1"
android:padding="10dp"
android:textColor="@color/black"
android:textColorHint="@android:color/darker_gray"
android:textSize="16dp" />
</RelativeLayout>
</com.google.android.material.appbar.AppBarLayout>
<androidx.core.widget.NestedScrollView
android:id="@+id/nestedScrollView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:clipToPadding="true"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="10dp"
android:orientation="vertical">
<com.smarteist.autoimageslider.SliderView
android:id="@+id/imageSlider"
android:layout_width="match_parent"
android:layout_height="300dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:sliderAnimationDuration="600"
app:sliderAutoCycleDirection="back_and_forth"
app:sliderAutoCycleEnabled="true"
app:sliderIndicatorAnimationDuration="600"
app:sliderIndicatorGravity="center_horizontal|bottom"
app:sliderIndicatorMargin="15dp"
app:sliderIndicatorOrientation="horizontal"
app:sliderIndicatorPadding="3dp"
app:sliderIndicatorRadius="2dp"
app:sliderIndicatorSelectedColor="#5A5A5A"
app:sliderIndicatorUnselectedColor="#FFF"
app:sliderScrollTimeInSec="1"
app:sliderStartAutoCycle="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/text_file_2"></TextView>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/text_file_2">
</TextView>
</LinearLayout>
</androidx.core.widget.NestedScrollView>
</androidx.coordinatorlayout.widget.CoordinatorLayout>
<com.google.android.material.navigation.NavigationView
android:id="@+id/navmenu"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:fitsSystemWindows="true"
app:itemIconTint="@color/black"
android:theme="@style/NavigationDrawerStyle"
app:headerLayout="@layout/navheader"
app:itemTextColor="#151515"
app:menu="@menu/navigationmenu">
</com.google.android.material.navigation.NavigationView>
</androidx.drawerlayout.widget.DrawerLayout>
2
Answers
you have to handle this action by yourself
also
syncState()
call should be placed inonPostCreate
or even inonResume
btw. your empty constructor should at least call
super();
you must attach toolbar in ActionBarDrawerToggle