I've been searching for a solution for this for hours now. I just want to add a switch under the ActionBar (like in Bluetooth settings). I found a similar question on here, but it was probably old. Anyways here's my code:
MainActivity:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu_main, menu);
MenuItem item = menu.findItem(R.id.myswitch);
switchButton = (Switch) item.getActionView();
return super.onCreateOptionsMenu(menu);
}
menu_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/menu_switch"
android:title="off/on"
app:showAsAction="always"
app:actionLayout="#layout/switchlayout"
app:actionViewClass="android.support.v7.widget.Switch" />
</menu>
switchlayout.xml
<?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"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<Switch
android:id="#+id/myswitch"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:background="#1E88E5" />
</RelativeLayout>
But no matter what I do I always get:
Attempt to invode ... getActionView()' on a null object reference
I'm confused because i just defined item the line before R.id.myswitch is defined, did I mess that up?
In addition to the simple mistype (it should be menu_switch to match your XML), per the action view training, you have to use MenuItemCompat.getActionView() to extract the ActionView (and, in your case, cast it to SwitchCompat as there is no android.support.v7.widget.Switch).
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu_main, menu);
MenuItem item = menu.findItem(R.id.menu_switch);
switchButton = (SwitchCompat) MenuItemCompat.getActionView(item);
return super.onCreateOptionsMenu(menu);
}
Replace
MenuItem item = menu.findItem(R.id.myswitch);
with
MenuItem item = menu.findItem(R.id.menu_switch);
Beacause your item's id in menu xml is menu_switch,not myswitch.
How about using MenuItemCompat with static function:
MenuItemCompat.getActionView (MenuItem item)
Related
I am trying to implement search functionality into an android app using the Search Dialog. After following the documentation, I got this search dialog when pressing the search button in my app.
As you can see, the search dialog is too small( the app bar can be seen below it), it is not centered, and contains the app icon near the back button.
The Search Dialog showed in the guide is centered vertically in the app bar and doesn't have the app icon near the back button, which is what I want to do for my search dialog as well. But I can't seem to find any resource on how to do it. Can anyone help me out?
Thanks in advance!
Edit: Here is the code that enables the Search Dialog
MainActivity.kt
class MainActivity : AppCompatActivity() {
private lateinit var binding: ActivityMainBinding
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = ActivityMainBinding.inflate(layoutInflater)
val root = binding.root
setContentView(root)
//Setup the app bar
setSupportActionBar(binding.toolbar);
}
override fun onCreateOptionsMenu(menu: Menu?): Boolean {
val inflater = menuInflater;
inflater.inflate(R.menu.app_bar_menu, menu)
return true;
}
override fun onOptionsItemSelected(item: MenuItem) = when (item.itemId) {
R.id.action_search -> {
onSearchRequested()
true
}
else -> {
super.onOptionsItemSelected(item)
}
}
}
searchable.xml
<?xml version="1.0" encoding="utf-8"?>
<searchable xmlns:android="http://schemas.android.com/apk/res/android"
android:hint="#string/search_hint"
android:label="#string/app_name">
</searchable>
activity_main.xml
<?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"
tools:context=".MainActivity">
<androidx.appcompat.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:background="#color/colorPrimary"
android:minHeight="?attr/actionBarSize"
android:theme="?attr/actionBarTheme"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:title="#string/app_name" />
</androidx.constraintlayout.widget.ConstraintLayout>
Using the method that you want for create a search dialog, this is the most similar aspect that it can achieve:
https://i.stack.imgur.com/hwTlF.png
This is the code that I implemented:
MainActivity.class code:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
#SuppressLint("ResourceType")
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.options_menu, menu);
SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
SearchView searchView = (SearchView) menu.findItem(R.id.search).getActionView();
searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
searchView.setIconifiedByDefault(true);
searchView.setIconified(false);
//Here you can set the parameters of the search view
return true;
}
I created a menu resource file called options_menu.xml and that's the code:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="#+id/search"
android:title="title"
android:icon="#drawable/search_icon"
app:showAsAction="collapseActionView|ifRoom"
app:actionViewClass="android.widget.SearchView" />
</menu>
And the searchable xml file called search.xml
<?xml version="1.0" encoding="utf-8"?>
<Searchable xmlns:android="http://schemas.android.com/apk/res/android"
android:hint="Your hint"
android:label="Label text">
</Searchable>
I created the icon of the menu in the drawable folder but you can change to other icon if you want.
Also I leave several links to all the customizations that you can add to the searchView:
https://developer.android.com/guide/topics/search/searchable-config
https://developer.android.com/reference/android/widget/SearchView
I hope this is the right solution for you.
I made a different search method, I used the Android Studio search view,
that's the main activity code:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu, menu);
MenuItem item = menu.findItem(R.id.mySearchButton);
SearchView searchView = (SearchView)item.getActionView();
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
#Override
public boolean onQueryTextSubmit(String query) {
return false;
}
#Override
public boolean onQueryTextChange(String newText) {
return false;
}
});
return super.onCreateOptionsMenu(menu);
}
You have to create an Android Resource File, select resource type "menu", and call it as you want, after that, add this item:
<item android:id="#+id/mySearchButton"
android:title="Button"
android:icon="#drawable/search_icon"
app:actionViewClass="android.widget.SearchView"
app:showAsAction="always"/>
This code worked for me, images here:
https://i.stack.imgur.com/XNRZy.png
https://i.stack.imgur.com/hUt4x.png
I hope it works for you aswell.
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
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;
}
Is some possible way how to hide the toast after long-press on the ActionBar item? I didn't setup a title for the item but it is still there - empty toast.
<item
android:id="#+id/ab_main_menu_dots"
android:icon="#drawable/action_icons_dots"
android:showAsAction="always">
<menu>
<item
android:id="#+id/ab_main_menu_my_profile"
android:showAsAction="never"
android:title="#string/ab_my_profile">
</item>
<item
android:id="#+id/ab_main_menu_settings"
android:showAsAction="never"
android:title="#string/menu_settings">
</item>
<item
android:id="#+id/ab_main_menu_help"
android:showAsAction="never"
android:title="#string/tv_help_login">
</item>
<item
android:id="#+id/ab_main_menu_about_us"
android:showAsAction="never"
android:title="#string/ab_about_us">
</item>
<item
android:id="#+id/ab_main_menu_logout"
android:showAsAction="never"
android:title="#string/bt_logout_main">
</item>
</menu>
</item>
The only way to hide the toast is when you set the ActionBar menu item to be displayed with text. android:showAsAction="withText". Otherwise the toast adds clarification of what each action item represents even if there is no title set for menu item.
Probably the cleanest way to go about this is to assign a custom action view to your menu item that mimics the look of a regular one.
Since you mentioned you're using ActionBarSherlock, here's a simple example.
Imagine the following menu.xml, which gets inflated in an Activity.
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="#+id/ab_main_menu_dots"
android:actionLayout="#layout/ab_main_menu_dots_layout"
android:showAsAction="always"/>
</menu>
You can define ab_main_menu_dots_layout.xml to mimic the overflow button like this:
<ImageButton xmlns:android="http://schemas.android.com/apk/res/android"
style="#style/Widget.Sherlock.ActionButton.Overflow"
android:layout_width="match_parent"
android:layout_height="match_parent" />
The result is a menu item that looks like an overflow button and does not display a Toast message when you long-press it, regardless of whether the native ActionBar is used or ABS. Up to you to take it from here. You want to reconsider and abide by the guidelines instead.
You can modify onLongClickin ActionMenuItemView Class to stop Toasting on long click.
but be careful, It's only working on devices with API less than 11, because sherlockactionbar library checking your device API level by Build.VERSION.SDK_INT and if you have newer device it just use default system actionbar which you're not modifying.
In onCreateOptionsMenu schedule a task to disable long click on desired menu item. Here is the sample
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.my_menu, menu);
new Handler().post(new Runnable() {
#Override
public void run() {
final View v = getActivity().findViewById(R.id.your_menu_item);
if (v != null) {
v.setOnLongClickListener(new View.OnLongClickListener() {
#Override
public boolean onLongClick(View v) {
return false;
}
});
}
}
});
}
This is how I did it:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_main, menu);
MenuItem item = menu.findItem(R.id.no_toast);
item.setActionView(R.layout.custom_view);
item.getActionView().setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
//handle click (just this item)
}
});
return true;
}
and this is my 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:title="Never gonna see me in a toast!"
app:showAsAction="always"
android:id="#+id/no_toast" />
</menu>
my custom view is just an ImageButton:
<?xml version="1.0" encoding="utf-8"?>
<ImageButton xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
style="#style/Widget.AppCompat.Toolbar.Button.Navigation"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="#drawable/icon" />
Note: don't forget to set style, Widget.AppCompat.Toolbar.Button.Navigation makes IamgeButton to be shown correctly in toolbar.
P.S: Personally I prefer the default behavior but this was my case:
I disabled right to left support for my application and after that when I set default locale to a rtl language, toast was showing up in the wrong side! Honestly I was in hurry and didn't find out the reason but I'll appreciate if someone let me know the why, anyway this is how I passed through.
You can achieve this by using custom action view as follow:
action_menu.xml
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:support="http://schemas.android.com/apk/res-auto" >
<item
android:id="#+id/item1"
support:showAsAction="always">
</item>
</menu>
custom_action_view.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="10dp"
android:paddingRight="5dp" >
<ImageButton
android:id="#+id/customActionItem"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:background="#drawable/abc_item_background_holo_dark"
android:src="#drawable/bulb_icon" />
</RelativeLayout>
and menu inflater code is as follow:
public boolean onCreateOptionsMenu(Menu menu) {
// TODO Auto-generated method stub
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.action_menu, menu);
final MenuItem item1= menu.findItem(R.id.item1);
MenuItemCompat.setActionView(item1, R.layout.custom_action_view);
View vItem1= MenuItemCompat.getActionView(item1);
final ImageButton customActionItem= (ImageButton) vItem1.findViewById(R.id.customActionItem);
customActionItem.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
// do something here
}
});
return super.onCreateOptionsMenu(menu);
}
You could try to create your own custom ActionBar. Here is a tutorial on how to do that:
Custom Action Bar
For me the solution was using:
android.support.v4.view.MenuItemCompat
So instead of inflating the menu from the XML:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.refresh_menu, menu);
return true;
}
I created the items programmatically using MenuItemCompat:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuItem refreshItem = menu.add(Menu.NONE, R.id.menu_item_refresh, Menu.NONE, R.string.general_pop_up_dialog_btn_cancel);
MenuItemCompat.setActionView(refreshItem, R.layout.actionbar_custom_view_refresh);
MenuItemCompat.setShowAsAction(refreshItem, MenuItemCompat.SHOW_AS_ACTION_ALWAYS);
return true;
}
There is a way, if you're using ActionBarSherlock. Find the ActionMenuItemView.java file in library and just comment whole onLongClick method.
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);
}