Removing AppBar SearchView Hint Icon - java

I'm trying to remove the icon in the hint of my app bar search view.
View of what i have
So i would like to remove the magnifying glass in the searchView.
I'm a beginner in android.
Here is my main activity layout
<?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/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:openDrawer="start">
<include
layout="#layout/app_bar_main"
android:layout_width="match_parent"
android:layout_height="match_parent" />
...
</androidx.drawerlayout.widget.DrawerLayout>
Here is the app_bar_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout 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"
tools:context=".MainActivity">
<androidx.cardview.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp">
<com.google.android.material.appbar.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="#style/AppTheme.AppBarOverlay">
<androidx.appcompat.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="#color/colorWhite"
app:subtitleTextColor="#color/colorPrimary"
app:titleTextColor="#color/colorPrimary"
app:popupTheme="#style/AppTheme.Toolbar"
app:theme="#style/AppTheme.Icons" />
</com.google.android.material.appbar.AppBarLayout>
</androidx.cardview.widget.CardView>
<include layout="#layout/content_main" />
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:id="#+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"
android:layout_margin="#dimen/fab_margin"
app:srcCompat="#android:drawable/ic_dialog_email" />
</androidx.coordinatorlayout.widget.CoordinatorLayout>
Here is my main.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="#+id/search"
android:title="Recherche"
android:icon="#drawable/ic_search"
app:showAsAction="ifRoom"
app:actionViewClass="android.widget.SearchView"/>
</menu>
Here is my searchable.xml
<?xml version="1.0" encoding="utf-8"?>
<searchable xmlns:android="http://schemas.android.com/apk/res/android"
android:label="#string/app_name"
android:hint="#string/search_hint"
android:voiceSearchMode="showVoiceSearchButton|launchRecognizer"
/>
And here is my Activity java
package com.example.test3;
import android.app.SearchManager;
import android.content.Context;
import android.graphics.Color;
import android.os.Bundle;
import com.google.android.material.floatingactionbutton.FloatingActionButton;
import com.google.android.material.snackbar.Snackbar;
import android.view.MenuInflater;
import android.view.View;
import androidx.navigation.NavController;
import androidx.navigation.Navigation;
import androidx.navigation.ui.AppBarConfiguration;
import androidx.navigation.ui.NavigationUI;
import com.google.android.material.navigation.NavigationView;
import androidx.drawerlayout.widget.DrawerLayout;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import android.view.Menu;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.SearchView;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
private AppBarConfiguration mAppBarConfiguration;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
FloatingActionButton fab = findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
DrawerLayout drawer = findViewById(R.id.drawer_layout);
NavigationView navigationView = findViewById(R.id.nav_view);
// Passing each menu ID as a set of Ids because each
// menu should be considered as top level destinations.
mAppBarConfiguration = new AppBarConfiguration.Builder(
R.id.nav_home, R.id.nav_gallery, R.id.nav_slideshow,
R.id.nav_tools, R.id.nav_share, R.id.nav_send)
.setDrawerLayout(drawer)
.build();
NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment);
NavigationUI.setupActionBarWithNavController(this, navController, mAppBarConfiguration);
NavigationUI.setupWithNavController(navigationView, navController);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main, menu);
SearchManager searchManager = (SearchManager)getSystemService(Context.SEARCH_SERVICE);
SearchView searchView = (SearchView)menu.findItem(R.id.search).getActionView();
searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
System.out.println(searchManager.getSearchableInfo(getComponentName()));
int searchPlateId = searchView.getContext().getResources().getIdentifier("android:id/search_plate", null, null);
View searchPlate = searchView.findViewById(searchPlateId);
searchPlate.setBackgroundColor(Color.TRANSPARENT);
int submitAreaId = searchView.getContext().getResources().getIdentifier("android:id/submit_area", null, null);
View submitAreaView = searchView.findViewById(submitAreaId);
submitAreaView.setBackgroundColor(Color.TRANSPARENT);
return true;
}
#Override
public boolean onSupportNavigateUp() {
NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment);
return NavigationUI.navigateUp(navController, mAppBarConfiguration)
|| super.onSupportNavigateUp();
}
}
I can't find out what i'm supposed to edit to remove this icon
Thank your for your answers
EDIT : If it's easier for you here is the github https://github.com/BenoitBonavia/geoloc-indoor-application

Found it !
In my activity.java in the onCreateOptionsMenu method i added this :
int magId = getResources().getIdentifier("android:id/search_mag_icon", null, null);
ImageView magImage = searchView.findViewById(magId);
magImage.setLayoutParams(new LinearLayout.LayoutParams(0, 0));

Related

ListView isn't existing in navigation drawer activity

My ListView isn't coming up in android studio
I need to make a listView in navigation drawer activity's one page
I also need to know how to take an element's id in from another .xml file
I need to use id of imageView in app_content_main.xml file
Which layout is android studio showing? I can't realize it. is it appbar_main.xml file or content.main.xml file. It is considering content.xml file in appbar_main.xml file, but not listView
here is my code in Mainactivity.java file:
package com.example.sede;
enter code here
import android.os.Bundle;
import android.view.View;
import android.view.Menu;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import com.google.android.material.snackbar.Snackbar;
import com.google.android.material.navigation.NavigationView;
import androidx.navigation.NavController;
import androidx.navigation.Navigation;
import androidx.navigation.ui.AppBarConfiguration;
import androidx.navigation.ui.NavigationUI;
import androidx.drawerlayout.widget.DrawerLayout;
import androidx.appcompat.app.AppCompatActivity;
import com.example.sede.databinding.ActivityMainBinding;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity {
private AppBarConfiguration mAppBarConfiguration;
private ActivityMainBinding binding;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
binding = ActivityMainBinding.inflate(getLayoutInflater());
setContentView(binding.getRoot());
setSupportActionBar(binding.appBarMain.toolbar);
binding.appBarMain.fab.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
ListView Habits = findViewById(R.id.Habits);
ArrayList<String> names = new ArrayList<String>();
names.add("My name");
names.add("My name");
names.add("My name");
names.add("My name");
ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1);
Habits.setAdapter(arrayAdapter);
Habits.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
}
});
DrawerLayout drawer = binding.drawerLayout;
NavigationView navigationView = binding.navView;
// Passing each menu ID as a set of Ids because each
// menu should be considered as top level destinations.
mAppBarConfiguration = new AppBarConfiguration.Builder(
R.id.nav_home, R.id.nav_gallery, R.id.nav_slideshow)
.setOpenableLayout(drawer)
.build();
NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment_content_main);
NavigationUI.setupActionBarWithNavController(this, navController, mAppBarConfiguration);
NavigationUI.setupWithNavController(navigationView, navController);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
#Override
public boolean onSupportNavigateUp() {
NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment_content_main);
return NavigationUI.navigateUp(navController, mAppBarConfiguration)
|| super.onSupportNavigateUp();
}
}
this one is in appbar_main.xml file :
<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout
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"
tools:context=".MainActivity">
<com.google.android.material.appbar.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="#style/Theme.SEDE.AppBarOverlay">
<androidx.appcompat.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="#style/Theme.SEDE.PopupOverlay" />
</com.google.android.material.appbar.AppBarLayout>
<include layout="#layout/content_main" />
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:id="#+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"
android:layout_marginEnd="#dimen/fab_margin"
android:layout_marginBottom="16dp"
app:srcCompat="#android:drawable/ic_dialog_email" />
</androidx.coordinatorlayout.widget.CoordinatorLayout>
this is in content_main.xml file:
enter code here
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
tools:showIn="#layout/app_bar_main">
<fragment
android:id="#+id/nav_host_fragment_content_main"
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:defaultNavHost="true"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:navGraph="#navigation/mobile_navigation" />
<ImageView
android:id="#+id/addingHabit"
style="#style/round"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="270dp"
android:layout_marginTop="550dp"
app:srcCompat="#drawable/plus" />
<ListView
android:id="#+id/Habits"
android:layout_width="match_parent"
android:layout_marginTop="100dp"
android:layout_height="500dp" />
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:textSize="30dp"
android:text="Today's plans"
android:layout_marginTop="20dp"/>

navigation drawer made from android studio : android.view.InflateException: Error inflating class fragment

I am a complete beginner in android studio with java and I have run across this error which seems to be very frequent here . I have seen solutions but nothing has helped so far . In my code I have created a navigation drawer using the already made one from android studio . When I try to run the app I get :
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.covidmobileapp/com.example.covidmobileapp.MainActivity}: android.view.InflateException: Binary XML file line #15 in com.example.covidmobileapp:layout/activity_main: Binary XML file line #20 in com.example.covidmobileapp:layout/content_main: Error inflating class fragment
Which seems to spot errors in 2 xml files I have : activity_main.xml and content_main.xml
Goind down the stack trace it seems that the error hits when the app view is created
at com.example.covidmobileapp.MainActivity.onCreate(MainActivity.java:55)
This is my code for the files above :
MainActivity.java
package com.example.covidmobileapp;
import android.os.Bundle;
import android.view.View;
import android.view.Menu;
import com.google.android.material.snackbar.Snackbar;
import com.google.android.material.navigation.NavigationView;
import androidx.navigation.NavController;
import androidx.navigation.Navigation;
import androidx.navigation.ui.AppBarConfiguration;
import androidx.navigation.ui.NavigationUI;
import androidx.drawerlayout.widget.DrawerLayout;
import androidx.appcompat.app.AppCompatActivity;
import com.example.covidmobileapp.databinding.ActivityMainBinding;
public class MainActivity extends AppCompatActivity {
private AppBarConfiguration mAppBarConfiguration;
private ActivityMainBinding binding;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
binding = ActivityMainBinding.inflate(getLayoutInflater());
setContentView(binding.getRoot());
setSupportActionBar(binding.appBarMain.toolbar);
binding.appBarMain.fab.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
DrawerLayout drawer = binding.drawerLayout;
NavigationView navigationView = binding.navView;
// Passing each menu ID as a set of Ids because each
// menu should be considered as top level destinations.
mAppBarConfiguration = new AppBarConfiguration.Builder(
R.id.nav_home, R.id.nav_gallery, R.id.nav_slideshow , R.id.nav_homepage)
.setDrawerLayout(drawer)
.build();
NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment_content_main);
NavigationUI.setupActionBarWithNavController(this, navController, mAppBarConfiguration);
NavigationUI.setupWithNavController(navigationView, navController);
setContentView(R.layout.activity_main);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
#Override
public boolean onSupportNavigateUp() {
NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment_content_main);
return NavigationUI.navigateUp(navController, mAppBarConfiguration)
|| super.onSupportNavigateUp();
}
}
content_main.xml I have commented the line where the fragment error hits
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
tools:showIn="#layout/app_bar_main">
<fragment
android:id="#+id/nav_host_fragment_content_main"
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:defaultNavHost="true"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:navGraph="#navigation/mobile_navigation" //this is where the fragment error hits
/>
</androidx.constraintlayout.widget.ConstraintLayout>
activity_main.xml I have also commented where the error is here
<?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/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:openDrawer="start">
<include
android:id="#+id/app_bar_main"
layout="#layout/app_bar_main"
android:layout_width="match_parent"
android:layout_height="match_parent" /> //this is where error for activity_main is
<com.google.android.material.navigation.NavigationView
android:id="#+id/nav_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:fitsSystemWindows="true"
app:headerLayout="#layout/nav_header_main"
app:menu="#menu/activity_main_drawer" />
</androidx.drawerlayout.widget.DrawerLayout>
I would appreciate your help as this is really frustrating . If more info is needed I will upload it .
I think you should declare:
NavigationView leftNavigationView;
in your main activity and get you nav view in on create method:
leftNavigationView = findViewById(R.id.nav_view);
setLeftNavigationView();
Then you havae to use setNavigationItemSelectedListener. You can create a method like this:
private void setLeftNavigationView() {
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
this,
drawerLayout,
toolbar,
R.string.navigation_drawer_open,
R.string.navigation_drawer_close);
drawerLayout.addDrawerListener(toggle);
toggle.syncState();
leftNavigationView.setNavigationItemSelectedListener(item -> {
Fragment selectedFragment = null;
switch (item.getItemId()) {
case R.id.nav_yourFragment:
selectedFragment = new YourFragment();
drawerLayout.closeDrawer(GravityCompat.START);
break;
}
getSupportFragmentManager()
.beginTransaction()
.replace(R.id.fragment_container, selectedFragment).addToBackStack("tag")
.commit();
return true;
});
and so on. R.id.nav_yourFragment is your item from your menu
Also you have to put a FrameLayout container in your xml file:
<FrameLayout
android:id="#+id/fragment_container"
android:layout_width="match_parent"
android:layout_height="match_parent" />
I hope this is useful for you

I am trying to access navigation drawer so that when i click on the top left corner of app bar, I can access settings in a new activity

I am trying to access navigation drawer so that when i click on the top left corner of app bar, I can access settings in a new activity. Right now what I get when I click on the upper left corner and then click settings is the menu retracts and nothing else happens. I have a bottom navigation bar also. Please help. Here's what I have so far:
NavigationActivity.java
package com.tt.lateoclock;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;
import androidx.appcompat.widget.Toolbar;
import com.google.android.material.bottomnavigation.BottomNavigationView;
import com.google.android.material.navigation.NavigationView;
import com.google.firebase.database.DatabaseReference;
import com.tt.lateoclock.Prevalent.Prevalent;
import com.tt.lateoclock.ui.findFood.FindFoodFragment;
import androidx.annotation.NonNull;
import androidx.appcompat.app.ActionBarDrawerToggle;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.content.ContextCompat;
import androidx.core.view.GravityCompat;
import androidx.drawerlayout.widget.DrawerLayout;
import androidx.navigation.NavController;
import androidx.navigation.Navigation;
import androidx.navigation.ui.AppBarConfiguration;
import androidx.navigation.ui.NavigationUI;
import androidx.recyclerview.widget.RecyclerView;
import de.hdodenhof.circleimageview.CircleImageView;
public class NavigationActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener, BottomNavigationView.OnNavigationItemSelectedListener {
DrawerLayout drawer;
NavigationView navigationView;
Menu menu;
private DatabaseReference merchantRef;
RecyclerView recyclerView;
RecyclerView.LayoutManager layoutManager;
ActionBarDrawerToggle toggle;
Intent settingsIntent;
NavController navController;
AppBarConfiguration appBarConfiguration;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_navigation);
//merchantRef = FirebaseDatabase.getInstance().getReference().child("Merchants");
Toolbar toolbar = (Toolbar)findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
toolbar.setTitle("Home");
drawer = findViewById(R.id.container);
toggle = new ActionBarDrawerToggle(this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.addDrawerListener(toggle);
toggle.syncState();
navigationView = findViewById(R.id.nav_view);
BottomNavigationView navView = findViewById(R.id.bot_nav_view);
// Passing each menu ID as a set of Ids because each
// menu should be considered as top level destinations.
AppBarConfiguration appBarConfiguration = new AppBarConfiguration.Builder(
R.id.navigation_find_food, R.id.navigation_favorites, R.id.navigation_map, R.id.navigation_receipts, R.id.navigation_settings)
.build();
navController = Navigation.findNavController(this, R.id.nav_host_fragment);
NavigationUI.setupActionBarWithNavController(this, navController, appBarConfiguration);
NavigationUI.setupWithNavController(navView, navController);
View headerView = navigationView.getHeaderView(0);
TextView name = (TextView)headerView.findViewById(R.id.username);
CircleImageView user_image = (CircleImageView)headerView.findViewById(R.id.user_image);
navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
#Override
public boolean onNavigationItemSelected(#NonNull MenuItem menuItem) {
int id=menuItem.getItemId();
Toast.makeText(getApplicationContext(), id, Toast.LENGTH_SHORT).show();
//it's possible to do more actions on several items, if there is a large amount of items I prefer switch(){case} instead of if()
if (id==R.id.navigation_settings){
Toast.makeText(getApplicationContext(), "Settings", Toast.LENGTH_SHORT).show();
}
//This is for maintaining the behavior of the Navigation view
NavigationUI.onNavDestinationSelected(menuItem, navController);
//This is for closing the drawer after acting on it
drawer.closeDrawer(GravityCompat.START);
return true;
}
});
name.setText(Prevalent.currentUser.getName());
navigationView.setNavigationItemSelectedListener(this);
}
#Override
public boolean onSupportNavigateUp() {
NavController navController=Navigation.findNavController(this, R.id.nav_host_fragment);
return NavigationUI.navigateUp(navController, appBarConfiguration)
|| super.onSupportNavigateUp();
}
#Override
public void onBackPressed()
{
if(drawer.isDrawerOpen(GravityCompat.START))
{
drawer.closeDrawer(GravityCompat.START);
}
else{
super.onBackPressed();
}
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle item selection
switch (item.getItemId()) {
case R.id.navigation_settings:
settingsIntent = new Intent(this, SettingsActivity.class);
startActivity(settingsIntent);
default:
return super.onOptionsItemSelected(item);
}
}
#Override
public boolean onNavigationItemSelected(#NonNull MenuItem item) {
switch (item.getItemId()) {
case R.id.navigation_find_food:
getSupportFragmentManager().beginTransaction().replace(R.id.nav_host_fragment, new FindFoodFragment()).commit();
break;
case R.id.navigation_favorites:
getSupportFragmentManager().beginTransaction().replace(R.id.nav_host_fragment, new com.tt.lateoclock.ui.favorites.FavoritesFragment()).commit();
break;
case R.id.navigation_receipts:
getSupportFragmentManager().beginTransaction().replace(R.id.nav_host_fragment, new com.tt.lateoclock.ui.receipts.ReceiptsFragment()).commit();
break;
case R.id.navigation_settings:
Toast.makeText(this, "Settings selected", Toast.LENGTH_SHORT).show();
Intent settingsIntent = new Intent(this, SettingsActivity.class);
startActivity(settingsIntent);
Toast.makeText(this, "Settings selected", Toast.LENGTH_SHORT).show();
return true;
}
return true;
}
}
activity_navigation.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/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".NavigationActivity">
<com.google.android.material.navigation.NavigationView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:id="#+id/nav_view"
app:headerLayout="#layout/nav_header"
app:menu="#menu/drawer_menu"/>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<fragment
android:id="#+id/nav_host_fragment"
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_marginTop="?android:attr/actionBarSize"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:defaultNavHost="true"
app:navGraph="#navigation/mobile_navigation" />
<androidx.appcompat.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="#color/colorPrimaryDark"
android:elevation="4dp"
android:theme="#style/ThemeOverlay.AppCompat.Dark.ActionBar"
app:popupTheme="#style/ThemeOverlay.AppCompat.Light"
/>
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="#+id/bot_nav_view"
android:layout_width="match_parent"
android:layout_height="196dp"
android:layout_below="#id/nav_host_fragment"
android:layout_marginTop="-100dp"
android:layout_gravity="bottom"
android:background="?android:attr/windowBackground"
app:menu="#menu/bottom_nav_menu" >
</com.google.android.material.bottomnavigation.BottomNavigationView>
</RelativeLayout>
</androidx.drawerlayout.widget.DrawerLayout>
bottom_nav_menu.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="#+id/navigation_find_food"
android:icon="#drawable/ic_local_dining_black_24dp"
android:title="#string/title_find_food" />
<item
android:id="#+id/navigation_favorites"
android:icon="#drawable/ic_favorite_black_24dp"
android:title="#string/title_favorites" />
<item
android:id="#+id/navigation_map"
android:icon="#drawable/ic_map_black_24dp"
android:title="#string/title_map" />
<item
android:id="#+id/navigation_receipts"
android:icon="#drawable/ic_receipt_black_24dp"
android:title="#string/title_receipts" />
</menu>
drawer_menu.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="#+id/nav_message"
android:title="Message"
android:icon="#drawable/ic_message"/>
<item android:id="#+id/nav_profile"
android:title="Profile"
android:icon="#drawable/ic_profile"/>
<item android:id="#+id/navigation_settings"
android:title="Settings"
android:icon="#drawable/ic_settings_black_24dp"
android:enabled="true"/>
</menu>
mobile_navigation.xml
<?xml version="1.0" encoding="utf-8"?>
<navigation 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/mobile_navigation"
app:startDestination="#+id/navigation_find_food">
<fragment
android:id="#+id/navigation_find_food"
android:name="com.tt.lateoclock.ui.findFood.FindFoodFragment"
android:label="#string/title_find_food"
tools:layout="#layout/fragment_find_food" />
<fragment
android:id="#+id/navigation_favorites"
android:name="com.tt.lateoclock.ui.favorites.FavoritesFragment"
android:label="#string/title_favorites"
tools:layout="#layout/fragment_favorites" />
<fragment
android:id="#+id/navigation_map"
android:name="com.tt.lateoclock.ui.map.MapFragment"
android:label="#string/title_map"
tools:layout="#layout/fragment_map" />
<fragment
android:id="#+id/navigation_receipts"
android:name="com.tt.lateoclock.ui.receipts.ReceiptsFragment"
android:label="#string/title_receipts"
tools:layout="#layout/fragment_receipts" />
</navigation>
Just change the activity_navigation.xml to
<?xml version="1.0" encoding="utf-8"?>
<androidx.drawerlayout.widget.DrawerLayout
...>
<RelativeLayout...>
<com.google.android.material.navigation.NavigationView
android:layout_gravity="start"
.../>
</androidx.drawerlayout.widget.DrawerLayout>
Also you have to handle the click on the settings item with navigationView.setNavigationItemSelectedListener.
About the DrawerLayout check the doc:
Note: When using NavigationUI, the top app bar helpers automatically transition between the drawer icon and the Up icon as the current destination changes. You don't need to use ActionBarDrawerToggle.
In your code remove the code about the ActionBarDrawerToggle and use:
AppBarConfiguration appBarConfiguration =
new AppBarConfiguration.Builder(...........)
.setDrawerLayout(drawerLayout)
.build();
You can add an <activity> destination to your navigation graph for starting another activity:
<activity
android:id="#+id/navigation_settings"
android:name="com.your.package.SettingsActivity" />
Then, the default behavior of setupWithNavController() / onNavDestinationSelected() will start your activity for you. You wouldn't need to write any custom code or manual calls to setNavigationItemSelectedListener to get this behavior.
You should also strongly consider adding menuCategory="secondary" to your navigation_settings menu item as per the onNavDestinationSelected() documentation to avoid your current destination from being popped off the back stack.
Here your Setting activity is not a part of the NavigationGraph (i.e. not a fragment it's a separate activity).
And when you hit the drawer burger and choose the Setting, it won't go to the Setting as you didn't launch the intent on the OnNavigationItemSelectedListener of the NavigationView
To fix that:
navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
#Override
public boolean onNavigationItemSelected(#NonNull MenuItem menuItem) {
int id=menuItem.getItemId();
Toast.makeText(getApplicationContext(), id, Toast.LENGTH_SHORT).show();
//it's possible to do more actions on several items, if there is a large amount of items I prefer switch(){case} instead of if()
if (id==R.id.navigation_settings){
Toast.makeText(getApplicationContext(), "Settings", Toast.LENGTH_SHORT).show();
Intent settingsIntent = new Intent(NavigationActivity.this, SettingsActivity.class); // <<< Here is the change
startActivity(settingsIntent); // <<< Here is the change
}
//This is for maintaining the behavior of the Navigation view
NavigationUI.onNavDestinationSelected(menuItem, navController);
//This is for closing the drawer after acting on it
drawer.closeDrawer(GravityCompat.START);
return true;
}
});
Update
Replace
navigationView.setNavigationItemSelectedListener(this);
With
navView.setNavigationItemSelectedListener(this);
Add the OnClick method in the menu item
<item
android:id="#+id/nav_whats_app"
android:icon="#drawable/ic_menu_settings"
android:title="#string/menu_settings"
android:onClick="onOpenSettings" />
Then add the method in the activity like this
//Kotlin Android
fun onOpenSettings(item: MenuItem) {
//... open settings here
}
Java Code
//Java Android
void onOpenSettings(MenuItem item) {
//... open settings here
}
To close any Navigation drawer, just call
val drawerLayout: DrawerLayout = findViewById(R.id.drawer_layout)
if (drawerLayout.isDrawerOpen(GravityCompat.START)) {
drawerLayout.closeDrawer(GravityCompat.START)
}

How can I add drawerLayout and not to damage existed LinerLayout style?

I began doing a project without DrawableLayout. I am using mostly LinearLayout. I want to add drawer navigation android, and as I know, I need DrawableLayout.
How can I add this, and not damage everything I have now?
Well don't be afraid. It's not so difficult. Just follow the following steps to get it:-
<android.support.v4.widget.DrawerLayout 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/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#android:color/holo_green_dark">
<!-- put you linear layout here --!>
<android.support.design.widget.NavigationView
android:layout_width="wrap_content"
android:layout_height="match_parent"
app:menu="#menu/navigation_menu"
android:layout_gravity="start"
app:headerLayout="#layout/nav_header"
android:id="#+id/nv">
</android.support.design.widget.NavigationView>
</android.support.v4.widget.DrawerLayout>
Navigation Header:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:background="#color/colorPrimaryDark">
<ImageView
android:layout_width="180dp"
android:layout_height="130dp"
android:layout_marginBottom="15dp"
android:layout_marginTop="15dp"
android:background="#drawable/ic_android_black_24dp"/>
</LinearLayout>
**Create a menu for the navigation drawer**
Right-click the res folder →Select new →Android resource file →Choose ‘menu’ under the resource type drop-down list.
Name the file as ‘navigation_menu.xml’ and copy-paste the following code into the file.
`<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="#+id/account"
android:icon="#drawable/ic_portrait_black_24dp"
android:title="My Account"/>
<item android:id="#+id/settings"
android:icon="#drawable/ic_brightness_high_black_24dp"
android:title="Settings"/>
<item android:id="#+id/mycart"
android:icon="#drawable/ic_local_grocery_store_black_24dp"
android:title="My Cart"/>
</menu>
** Lastly, in you activity:** <br>
In this last and final step, we will write Java code in the MainActivity.java file.
import android.support.annotation.NonNull;
import android.support.design.widget.NavigationView;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.MenuItem;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
private DrawerLayout dl;
private ActionBarDrawerToggle t;
private NavigationView nv;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
dl = (DrawerLayout)findViewById(R.id.activity_main);
t = new ActionBarDrawerToggle(this, dl,R.string.Open, R.string.Close);
dl.addDrawerListener(t);
t.syncState();
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
nv = (NavigationView)findViewById(R.id.nv);
nv.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
#Override
public boolean onNavigationItemSelected(#NonNull MenuItem item) {
int id = item.getItemId();
switch(id)
{
case R.id.account:
Toast.makeText(MainActivity.this, "My Account",Toast.LENGTH_SHORT).show();break;
case R.id.settings:
Toast.makeText(MainActivity.this, "Settings",Toast.LENGTH_SHORT).show();break;
case R.id.mycart:
Toast.makeText(MainActivity.this, "My Cart",Toast.LENGTH_SHORT).show();break;
default:
return true;
}
return true;
}
});
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
if(t.onOptionsItemSelected(item))
return true;
return super.onOptionsItemSelected(item);
}
}`

Missing Hamburger Icon Android

I tried searching for different answers regarding the hamburger icon, but haven't found anything similar. I'm trying to create a hamburger icon and have been following a tutorial. The code runs fine, but the icon is missing when I open it in the virtual device. Any suggestions are appreciated.
activity_main xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout
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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.android.navigationapp.navigationapp.MainActivity"
android:id="#+id/drawerLayout">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:text="Main Layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/textView"
android:layout_gravity="center_vertical"
android:textAlignment="center"
android:textSize="24dp" />
</LinearLayout>
<android.support.design.widget.NavigationView
android:layout_width="wrap_content"
android:layout_height="match_parent"
app:menu="#menu/navigation_menu"
android:layout_gravity="start">
</android.support.design.widget.NavigationView>
</android.support.v4.widget.DrawerLayout>
MainActivity Java
package com.android.navigationapp.navigationapp;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBar;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.MenuItem;
public class MainActivity extends AppCompatActivity {
private DrawerLayout mDrawerLayout;
private ActionBarDrawerToggle mToggle;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawerLayout);
mToggle = new ActionBarDrawerToggle(this, mDrawerLayout, R.string.open, R.string.close);
mDrawerLayout.addDrawerListener(mToggle);
mToggle.syncState();
getSupportActionBar().setDefaultDisplayHomeAsUpEnabled(true);
getSupportActionBar().setHomeButtonEnabled(true);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
if (mToggle.onOptionsItemSelected(item)) {
return true;
}
return super.onOptionsItemSelected(item);
}
}

Categories