How to show menu items in Toolbar in Fragment class? - java

I have added a menu in my fragment and would like to display menu items in the toolbar.
the menu xml is as follows:
<?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/action_note"
android:icon="#drawable/ic_more_24dp"
app:showAsAction="always"
android:visible="true"
android:orderInCategory="1"
android:title="Note"/>
<item
android:id="#+id/action_submit"
android:icon="#drawable/ic_more_24dp"
app:showAsAction="always"
android:orderInCategory="2"
android:title="Submit"/>
</menu>
The view that holds the container, which holds the fragments is as follows:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.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"
tools:context=".DashboardActivity">
<include layout="#layout/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</include>
<FrameLayout
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_above="#+id/navigationView"
android:layout_height="match_parent"
android:layout_marginBottom="56dp"
android:layout_marginTop="56dp"
/>
<android.support.design.widget.BottomNavigationView
android:id="#+id/navigationView"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginEnd="0dp"
android:layout_marginStart="0dp"
android:background="?android:attr/windowBackground"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:itemBackground="#android:color/white"
app:itemIconTint="#color/cardview_dark_background"
app:itemTextColor="#android:color/black"
app:menu="#menu/navigation_menu"
/>
</android.support.constraint.ConstraintLayout>
The code to display the menu in the fragment is as follows:
#Override
public void onViewCreated(#NonNull View view, #Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
setHasOptionsMenu(true);
init();
}
Then the code for the menu
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
super.onCreateOptionsMenu(menu, inflater);
inflater.inflate(R.menu.menu_attendance, menu);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle item selection
switch (item.getItemId()) {
case R.id.action_submit:
return true;
case R.id.action_note:
return true;
default:
return super.onOptionsItemSelected(item);
}
}
I have added the hasOptionMenu (true) and still the menu items will not display in the toolbar

Try replacing this code:
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
super.onCreateOptionsMenu(menu, inflater);
inflater.inflate(R.menu.menu_attendance, menu);
}
to
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_attendance, menu);
return true;
}

Related

Can not go to onOptionsItemSelected in Android studio

I follow the tutorial https://www.youtube.com/watch?v=iEXh1-KVeVc&t=213s for making bottom menu.
Then i use onCreateOptionsMenu and onOptionsItemSelected to set action when click in, but it still not working.
Here is my menu code:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="#+id/leftNav"
android:icon="#drawable/ic_baseline_stars_24"
android:title="News"/>
<item
android:id="#+id/home"
android:title=""/>
<item
android:id="#+id/rightNav"
android:icon="#drawable/ic_baseline_menu_24"
android:title="Menu"/>
</menu>
MainActivity.java code:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate menu
getMenuInflater().inflate(R.menu.bottom_nav_menu, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()){
case R.id.leftNav:
// Left nav
Toast.makeText(this,"Click left",Toast.LENGTH_LONG);
return true;
case R.id.rightNav:
return true;
case R.id.home:
Toast.makeText(this,"Click home", Toast.LENGTH_LONG);
return true;
default:
return super.onOptionsItemSelected(item);
}
}
activity_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.bottomappbar.BottomAppBar
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:id="#+id/bottomAppBar"
app:fabCradleMargin="10dp"
app:fabCradleRoundedCornerRadius="10dp"
app:fabCradleVerticalOffset="10dp">
<com.google.android.material.bottomnavigation.BottomNavigationView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/bottomNavigationView"
android:layout_marginRight="16dp"
app:menu="#menu/bottom_nav_menu"
android:background="#drawable/transparent_background"/>
</com.google.android.material.bottomappbar.BottomAppBar>
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/home"
android:src="#drawable/ic_baseline_sports_basketball_24"
app:layout_anchor="#id/bottomAppBar"/>
</androidx.coordinatorlayout.widget.CoordinatorLayout>
Any one can help me, i'm new with android studio. Thanks in advance

Menu won't show in Activity

I have a menu that won't show up in my activity. I have a main activity and a separate activity that contains a list of items I want the user to be able to search through. However, the menu isn't showing up in the activity for searching, so nothing happens when entering in a string for the search.
The goal was the get the menu in the search activity in order to allow the user to search through items using a SearchView and RecyclerView
I'm very new to Android Studio, so any help would be greatly appreciated.
Here is the menu for the SearchView
<?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/action_search"
android:icon="#drawable/ic_search"
android:title="Search"
app:showAsAction="ifRoom|collapseActionView"
app:actionViewClass="android.support.v7.widget.SearchView"
/>
<item android:id="#+id/action_search2"
android:icon="#drawable/ic_search"
android:title="Search"
app:showAsAction="ifRoom|collapseActionView"
app:actionViewClass="android.support.v7.widget.SearchView"
/>
</menu>
Here is the layout xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical">
<include
layout="#layout/activity_search_toolbar"/>
<android.support.v7.widget.SearchView
android:id="#+id/action_search"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:menu="#menu/search_menu">
</android.support.v7.widget.SearchView>
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?attr/colorPrimary"
android:minHeight="?attr/actionBarSize"
android:theme="?attr/actionBarTheme">
<Button
android:id="#+id/GoBackbutton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="GO BACK" />
<!--<android.support.v7.widget.SearchView-->
<!--android:id="#+id/searchBar"-->
<!--android:layout_width="wrap_content"-->
<!--android:layout_height="wrap_content">-->
<!--<!–app:menu="#menu/search_menu"–>-->
<!--</android.support.v7.widget.SearchView>-->
</android.support.v7.widget.Toolbar>
<android.support.v7.widget.RecyclerView
android:id="#+id/recycler_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</android.support.v7.widget.RecyclerView>
</LinearLayout>
and the toolbar to add to the layout
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:id="#+id/search_toolbar"
android:background="?attr/colorPrimary">
</android.support.v7.widget.Toolbar>
Here is where I inflate the search_menu
#Override
public boolean onCreateOptionsMenu(Menu menu){
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.search_menu, menu);
MenuItem searchItem = menu.findItem(R.id.action_search);
SearchView searchView = (SearchView) searchItem.getActionView();
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
#Override
public boolean onQueryTextSubmit(String query) {
return false;
}
#Override
public boolean onQueryTextChange(String newText) {
adapter.getFilter().filter(newText);
return false;
}
});
return true;
}
Finally this is part of my Adapter class to filter the items in a RecyclerView
#Override
public Filter getFilter() {
return locationFilter;
}
private Filter locationFilter = new Filter(){
#Override
protected FilterResults performFiltering(CharSequence constraint){
List<Location> filteredLocationList = new ArrayList<>();
if(constraint == null || constraint.length() == 0){
filteredLocationList.addAll(locationListFull);
} else{
String filterPattern = constraint.toString().toLowerCase().trim();
for(Location location : locationListFull){
if(location.getTitle().toLowerCase().contains(filterPattern)){
filteredLocationList.add(location);
}
// Add another if statement here if we want to be able to search
// descriptions as well
}
}
FilterResults results = new FilterResults();
results.values = filteredLocationList;
return results;
}
#Override
protected void publishResults(CharSequence constraint, FilterResults results){
locationList.clear();
locationList.addAll((List)results.values);
notifyDataSetChanged();
}
};
Also if there's another way to use the SearchView with RecyclerView besides creating a menu for the SearchView, that would be very helpful. I have tried researching ways to access the SearchView besides through creating a new menu, but haven't found anything useful.
Prepare the Screen's standard options menu to be displayed. This is called right before the menu is shown, every time it is shown. You can use this method to efficiently enable/disable items or otherwise dynamically modify the contents.
https://developer.android.com/reference/android/app/Activity.html#onPrepareOptionsMenu(android.view.Menu)
You need to override onPrepareOptionsMenu and setup your searchview there instead of doing that in onCreateOptionsMenu

OnCheckedChangeListener don't work with a Switch in the Toolbar

I tried to add an "OnCheckedChangeListener" to a Switch in the Toolbar of my Android App. But if I click at the Switch don`t get the Log output in the Android Monitor (logcat). There are also no Exceptions in the log.
The MainActivity:
LayoutInflater factory = getLayoutInflater();
View textEntryView = factory.inflate(R.layout.toolbar_switch, null);
toolbarSwitch = (Switch) textEntryView.findViewById(R.id.switch_toolbar);
toolbarSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
Log.d("SMSAT", "Test");
}
});
This is the Layout of the Switch:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:weightSum="1">
<Switch
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/switch_toolbar"
android:text=""
android:layout_weight="0.16"
android:checked="true" />
</LinearLayout>
The Menu:
<?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/menu_switch_toolbar"
android:title="#string/menu_switch"
app:showAsAction="always"
app:actionLayout="#layout/toolbar_switch"></item>
</menu>
Thank you for your help!
I had to move my Code in the onCreateOptionsMenu Method.
Here is the Code:
#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);
View textEntryView = menu.findItem(R.id.menu_switch_toolbar).getActionView();
toolbarSwitch = (Switch) textEntryView.findViewById(R.id.switch_toolbar);
toolbarSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
Log.d("SMSAT", "Test");
}
});
return super.onCreateOptionsMenu(menu);
}

Switch bottom clicked event in menu toolbar

i want to add switch button in toolbar. so i made thislayout
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="horizontal">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_centerInParent="true">
<TextView
android:id="#+id/textview_filter"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:text="#string/filter"
android:gravity="center"
android:textSize="15dp"
android:layout_marginRight="-10dp"/>
<com.kyleduo.switchbutton.SwitchButton
android:id="#+id/sb_ios"
android:paddingTop="15dp"
style="#style/SwitchButtonStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:kswAnimationDuration="300"
app:kswBackDrawable="#drawable/ios_back_drawable"
app:kswBackMeasureRatio="1.4"
app:kswThumbDrawable="#drawable/ios_thumb_selector"
app:kswThumbMarginBottom="-8dp"
app:kswThumbMarginLeft="-5dp"
app:kswThumbMarginRight="-5dp"
app:kswThumbMarginTop="-2.5dp"/>
</LinearLayout>
and menu
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="#+id/filterButton"
android:title="Open Filter"
android:icon="#drawable/ic_action_filter"
android:orderInCategory="100"
android:visible="false"
app:showAsAction="always"/>
<item
android:id="#+id/myswitch"
android:title=""
android:orderInCategory="200"
app:showAsAction="always"/>
and in fragment
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
super.onCreateOptionsMenu(menu, inflater);
menu.clear();
inflater.inflate(R.menu.menu_cvs_fav, menu);
MenuItem item = menu.findItem(R.id.myswitch);
item.setActionView(R.layout.switch_button_toolbar);
item.setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() {
#Override
public boolean onMenuItemClick(MenuItem menuItem) {
Toast.makeText(getContext(), "Clicked", Toast.LENGTH_SHORT).show();
return true;
}
});
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
Log.i("Whatt", "enter");
int id = item.getItemId();
if (id == R.id.myswitch) {
Log.i("Whatt", "enter1");
if (bSwitch) {
Log.i("Whatt", "enter2");
Toast.makeText(getContext(), "On", Toast.LENGTH_SHORT).show();
bSwitch = false;
} else {
Log.i("Whatt", "enter3");
Toast.makeText(getContext(), "Off", Toast.LENGTH_SHORT).show();
bSwitch = true;
}
}
return false;
}
but the problem here the setOnMenuItemClickListener not working in item and when try onOptionsItemSelected not enter in function which log.i() not showing anything also although on menu item is add
please help !!

Android: Share Menu, My menu inflater cannot Resolve my resource

I am trying to add a share menu to my app that sends text from textview 2 to another app I.E KiK but currently i cannot get it to identify the Resource file as the menu heres my code:
public class ME extends Activity {
/**
* Called when the activity is first created.
*/
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(android.R.menu.main, menu);
MenuItem shareItem = menu.findItem(R.id.action_share);
ShareActionProvider mShare = (ShareActionProvider)shareItem.getActionProvider();
Intent shareIntent = new Intent(Intent.ACTION_SEND);
shareIntent.setAction(Intent.ACTION_SEND);
shareIntent.setType("text/plain");
shareIntent.putExtra(Intent.EXTRA_TEXT, "Hi");
mShare.setShareIntent(shareIntent);
return super.onCreateOptionsMenu(menu);
}
}
The getMenuInflater().inflate(android.R.menu.main, menu); is the problem
And my XML Resource file
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<TextView
android:="#+id/TextView 1"
android:orientation="vertical"
android:layout_width="400dp"
android:layout_height="300dp" >
</TextView>
<TextView
android:="#+id/TextView 2"
android:orientation="vertical"
android:layout_width="400dp"
android:layout_height="300dp" >
</TextView>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:android="http://schemas.android.com/apk/res-auto" >
<item android:id="#+id/action_share"
android:orderInCategory="100"
android:icon="#drawable/ic_menu_share"
android:actionProviderClass= "android.widget.ShareActionProvider" />
</menu>
</LinearLayout>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:android="http://schemas.android.com/apk/res-auto" >
<item android:id="#+id/action_share"
android:orderInCategory="100"
android:icon="#drawable/ic_menu_share"
android:actionProviderClass= "android.widget.ShareActionProvider"
/>
</menu>
Should be put in a seperate xml file in the menu folder (say main.xml).
And,
getMenuInflater().inflate(android.R.menu.main, menu);
should be corrected as
getMenuInflater().inflate(R.menu.main, menu);

Categories

Resources