I have used navigation drawer in the code.When clicking items noting happens.Tried various things from Stackoverflow and net.
import android.os.Bundle;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.appcompat.app.ActionBarDrawerToggle;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.view.GravityCompat;
import androidx.drawerlayout.widget.DrawerLayout;
import com.google.android.material.navigation.NavigationView;
public class searchActivity extends AppCompatActivity implements
NavigationView.OnNavigationItemSelectedListener {
public DrawerLayout drawerLayout;
public ActionBarDrawerToggle actionBarDrawerToggle;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_search);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
drawerLayout=findViewById(R.id.drawer_layout);
actionBarDrawerToggle=new
ActionBarDrawerToggle(this,drawerLayout,R.string.open_drawer,R.string.close_drawer);
drawerLayout.addDrawerListener(actionBarDrawerToggle);
actionBarDrawerToggle.syncState();
NavigationView navigationView=(NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
navigationView.bringToFront();
drawerLayout.requestLayout();
}
@Override
public boolean onOptionsItemSelected(@NonNull MenuItem item)
{
DrawerLayout drawerLayout=(DrawerLayout) findViewById(R.id.drawer_layout);
if(drawerLayout.isDrawerOpen(GravityCompat.START))
{
drawerLayout.closeDrawer(GravityCompat.START);
}
else {
drawerLayout.openDrawer(GravityCompat.START);
}
if(actionBarDrawerToggle.onOptionsItemSelected(item)){
return true;
}
return super.onOptionsItemSelected(item);
}
@Override
public boolean onNavigationItemSelected(MenuItem item)
{
Toast toast=Toast.makeText(searchActivity.this,"On navigation item
selected",Toast.LENGTH_SHORT);
toast.show();
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}
}
XML file
<?xml version="1.0" encoding="utf-8"?>
<androidx.drawerlayout.widget.DrawerLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@ id/drawer_layout"
tools:context=".searchActivity">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:id="@ id/search_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#FFEB3B"
android:text="@string/add"
android:textColor="#BF360C"
app:layout_constraintBottom_toTopOf="@ id/food_listview"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.413"
app:layout_constraintStart_toEndOf="@ id/search_box"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.552" />
<com.google.android.material.textfield.TextInputEditText
android:id="@ id/search_box"
android:layout_width="289dp"
android:layout_height="81dp"
android:hint="@string/type_food"
android:textColorHint="#757575"
app:layout_constraintBottom_toTopOf="@ id/food_listview"
app:layout_constraintEnd_toStartOf="@ id/search_button"
app:layout_constraintHorizontal_bias="0.432"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<ListView
android:id="@ id/food_listview"
android:layout_width="357dp"
android:layout_height="496dp"
android:layout_marginBottom="44dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
<com.google.android.material.navigation.NavigationView
android:id="@ id/nav_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="start"
android:fitsSystemWindows="true"
app:menu="@menu/navigation_menu"/>
</androidx.drawerlayout.widget.DrawerLayout>
As suggested in many have put the navigationView in the last of the xml also. still does not help. Made many changes. as suggested in many have put the navigationView in the last of the xml also. still does not help.
CodePudding user response:
Check this answer:
search_activity.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:orientation="vertical">
<androidx.drawerlayout.widget.DrawerLayout
android:id="@ id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="false"
tools:openDrawer="start">
<include
layout="@layout/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<com.google.android.material.navigation.NavigationView
android:id="@ id/nav_view"
android:layout_width="@dimen/_240sdp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:background="@color/white_color"
android:fitsSystemWindows="true"
app:headerLayout="@layout/nav_header_drawer"
app:menu="@menu/drawer_menu"/>
</androidx.drawerlayout.widget.DrawerLayout>
</LinearLayout>
SearchActivity.java
public class SearchActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener{
DrawerLayout drawer;
NavigationView navigationView;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.search_activity);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setTitle("Home");
toolbar.setTitleTextColor(getResources().getColor(android.R.color.white));
drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
navigationView = (NavigationView) findViewById(R.id.nav_view);
View hView = navigationView.getHeaderView(0);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.addDrawerListener(toggle);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setHomeButtonEnabled(true);
toggle.syncState();
toggle.getDrawerArrowDrawable().setColor(getResources().getColor(R.color.white_color));
navigationView.setNavigationItemSelectedListener(this);
navigationView.setItemIconTintList(null);
}
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem menuItem) {
switch (menuItem.getItemId()) {
case R.id.nav_home:
Intent home = new Intent(SearchActivity.this, Your_Acivity.class); //here place your Activity Class
startActivity(home);
break;
case R.id.nav_car_info:
Intent car_info = new Intent(SearchActivity.this,Your_Activity.class); //here place your Activity Class
startActivity(car_info);
break;
}
return false;
}
}