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 !!
Related
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
I'm implementing searchview for my activity. It works well but I also want to show textview at first launch. But when i run app, then textview is not shown but only search menu item appears and when I search anything and click search button, then only textview appears and I can similarly search any item as before. But problem is on First launch the item in my xml don't appear and only toolbar with search menu item appears. What is the cause of it?
Here is my code
public class MainActivity extends AppCompatActivity {
MaterialSearchView searchView;
Toolbar toolbar;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
searchViewCode();
}
private void searchViewCode() {
searchView = (MaterialSearchView) findViewById(R.id.search_view);
searchView.setSuggestions(getResources().getStringArray(R.array.query_suggestions));
searchView.setEllipsize(true);
searchView.setOnQueryTextListener(new MaterialSearchView.OnQueryTextListener() {
#Override
public boolean onQueryTextSubmit(String query) {
Toast.makeText(getApplicationContext(), query, Toast.LENGTH_SHORT).show();
return false;
}
#Override
public boolean onQueryTextChange(String newText) {
return false;
}
});
searchView.setOnSearchViewListener(new MaterialSearchView.SearchViewListener() {
#Override
public void onSearchViewShown() {
}
#Override
public void onSearchViewClosed() {
}
});
} /*click alt+insert key */
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.material_search_menu, menu);
MenuItem item = menu.findItem(R.id.action_search);
searchView.setMenuItem(item);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.action_search:
return true;
default:
return super.onOptionsItemSelected(item);
}
}
#Override
public void onBackPressed() {
if (searchView.isSearchOpen()) {
searchView.closeSearch();
} else {
super.onBackPressed();
}
}
}
and my xml is
<Linearlayout>
<FrameLayout
android:id="#+id/toolbar_container"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="#color/appcolor"
android:elevation="4dp"
android:fitsSystemWindows="true"
android:minHeight="?attr/actionBarSize"
android:theme="#style/AppTheme.AppBarOverlay" />
<com.miguelcatalan.materialsearchview.MaterialSearchView
android:id="#+id/search_view"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</FrameLayout>
<TextView
android:id="#+id/ev_title"
android:text="uis sed nisi arcu"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</Linearlayout>
Thank in advance.
You should Constraint Layout
<?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">
<FrameLayout
android:id="#+id/toolbar_container"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="#color/appcolor"
android:elevation="4dp"
android:fitsSystemWindows="true"
android:minHeight="?attr/actionBarSize"
android:theme="#style/AppTheme.AppBarOverlay" />
<com.miguelcatalan.materialsearchview.MaterialSearchView
android:id="#+id/search_view"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</FrameLayout>
<TextView
android:id="#+id/ev_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="uis sed nisi arcu"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#id/toolbar_container" />
</androidx.constraintlayout.widget.ConstraintLayout>
I am trying to change an icon in the actionbar, but I can not get it work. If I send to the log the getIcon().toString(), It seems that the icon is modified, but the actionbar is not updated.
This is the main activity layout:
<?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=".MainActivity"
tools:layout_editor_absoluteY="81dp">
<android.support.v7.widget.Toolbar
android:id="#+id/appbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?attr/colorPrimary"
android:elevation="4dp"
android:minHeight="?attr/actionBarSize"
android:theme="#style/AppTheme.AppBarOverlay"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:popupTheme="#style/AppTheme">
</android.support.v7.widget.Toolbar>
The menu xml file:
<menu 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"
tools:context=".MainActivity">
<item
android:id="#+id/action_settings"
android:title="#string/action_settings"
android:orderInCategory="100"
app:showAsAction="never" />
<item
android:id="#+id/about"
android:title="#string/about"
android:orderInCategory="110"
app:showAsAction="never" />
<item
android:id="#+id/connect"
android:title="Connect"
android:icon="#drawable/ic_play"
android:orderInCategory="130"
app:showAsAction="always" />
<item
android:id="#+id/testin"
android:title="Test_in"
android:icon="#drawable/ic_testin"
android:orderInCategory="120"
app:showAsAction="always" />
And the main activity code:
//Menu Items
MenuItem testinMenu, connectMenu;
Menu actionMenu;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Action Bar
toolbar = findViewById( R.id.appbar);
setSupportActionBar( toolbar);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menumain, menu);
actionMenu = menu;
connectMenu = (MenuItem) menu.findItem(R.id.connect);
testinMenu = (MenuItem) menu.findItem(R.id.testin);
return true;
}
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.connect: {
connectMenu.setIcon( R.drawable.ic_stop);
invalidateOptionsMenu();
if( isRainbowConnected == false) {
connect();
} else
disconnect();
break;
}
case R.id.testin: {
if( isRainbowConnected == true)
sendTestIn();
break;
}
// case blocks for other MenuItems (if any)
}
return true;
}
I have tried everything, but nothing seems to work.
Thanks in advance!
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;
}
I tried Action Bar like this
I want like when i click on Actionbar's Item it will print a Toast but it is not working tried a lot.
I Created 2 custom layout,one for Notification custom item and another for Task custom item.
custom_action_notification_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
style="?attr/actionButtonStyle"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:clipToPadding="false"
android:focusable="true">
<ImageView
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_gravity="center"
android:src="#drawable/noti"/>
<TextView
android:id="#+id/cart_badge"
android:layout_width="20dp"
android:layout_height="20dp"
android:layout_gravity="right|end|top"
android:layout_marginTop="3dp"
android:layout_marginLeft="15dp"
android:background="#drawable/badge_circle"
android:gravity="center"
android:padding="3dp"
android:textColor="#android:color/white"
android:text="0"
android:textSize="10sp"/>
</RelativeLayout>
custom_action_task_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
style="?attr/actionButtonStyle"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:clipToPadding="false"
android:focusable="true">
<ImageView
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_gravity="center"
android:src="#drawable/task"
android:id="#+id/imageView5" />
<TextView
android:id="#+id/cart_badge1"
android:layout_width="20dp"
android:layout_height="20dp"
android:layout_marginLeft="15dp"
android:layout_gravity="right|end|top"
android:background="#drawable/badge_circle"
android:gravity="center"
android:padding="3dp"
android:textColor="#android:color/white"
android:text="0"
android:textSize="10sp"
android:layout_alignTop="#+id/imageView5"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
</RelativeLayout>
navigation.menu
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:apps="http://schemas.android.com/tools">
<item
android:id="#+id/action_refresh"
android:icon="#drawable/noti"
app:showAsAction="always"
android:actionLayout="#layout/custom_action_notification_layout"
android:title="Refresh"/>
<item
android:id="#+id/action_task"
android:orderInCategory="100"
android:actionLayout="#layout/custom_action_task_layout"
android:title="Task"
android:icon="#drawable/task"
app:showAsAction="always" /></menu>
Now in my navigationdrawer.class i put thiscode onCreateOptionsMenu(), get ActionView of Notification and Task items and Set OnClick listeners to those ActionView.
navigation.class
#Override
public boolean onCreateOptionsMenu(final Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.navigation, menu);
// Notification
final MenuItem itemNotification = menu.findItem(R.id.action_refresh);
MenuItemCompat.setActionView(itemNotification, R.layout.custom_action_notification_layout);
View actionViewNotification = MenuItemCompat.getActionView(itemNotification);
// RelativeLayout notifCount = (RelativeLayout) MenuItemCompat.getActionView(itemNotification);
actionViewNotification.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
onOptionsItemSelected(itemNotification);
}
});
final MenuItem itemNotification1 = menu.findItem(R.id.action_task);
MenuItemCompat.setActionView(itemNotification1, R.layout.custom_action_task_layout);
View actionViewNotification1 = MenuItemCompat.getActionView(itemNotification1);
// RelativeLayout notifCount1 = (RelativeLayout) MenuItemCompat.getActionView(itemNotification1);
actionViewNotification1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
onOptionsItemSelected(itemNotification1);
}
});
Now finally in onOptionsItemSelected() the code is like
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.action_refresh: {
// Do something
Toast toast = Toast.makeText(this, "Notification clicked", Toast.LENGTH_LONG);
toast.setGravity(Gravity.CENTER, 0, 0);
toast.show();
return true;
}
case R.id.action_task: {
// Do something
Toast toast = Toast.makeText(this, "Task clicked", Toast.LENGTH_SHORT);
toast.setGravity(Gravity.CENTER, 0, 0);
toast.show();
return true;
}
}
Tried in many ways but not working at all please help out from this..
1. Create two custom layout, one for Notification custom item and another for Task custom item.
custom_action_notification_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
style="?attr/actionButtonStyle"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:clipToPadding="false"
android:focusable="true">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:src="#drawable/icon_notification"/>
<TextView
android:id="#+id/cart_badge"
android:layout_width="20dp"
android:layout_height="20dp"
android:layout_gravity="right|end|top"
android:layout_marginEnd="-5dp"
android:layout_marginRight="-5dp"
android:layout_marginTop="3dp"
android:background="#drawable/badge_background"
android:gravity="center"
android:padding="3dp"
android:textColor="#android:color/white"
android:text="0"
android:textSize="10sp"/>
</FrameLayout>
custom_action_task_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
style="?attr/actionButtonStyle"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:clipToPadding="false"
android:focusable="true">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:src="#drawable/icon_task"/>
<TextView
android:id="#+id/cart_badge"
android:layout_width="20dp"
android:layout_height="20dp"
android:layout_gravity="right|end|top"
android:layout_marginEnd="-5dp"
android:layout_marginRight="-5dp"
android:layout_marginTop="3dp"
android:background="#drawable/badge_background"
android:gravity="center"
android:padding="3dp"
android:textColor="#android:color/white"
android:text="0"
android:textSize="10sp"/>
</FrameLayout>
2. Create a menu XML containing Notification and Task item. Use attribute app:actionLayout to set custom layout to each item.
custom_menu.xml
<menu
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto" >
<item
android:id="#+id/action_notification"
android:icon="#drawable/icon_notification"
android:title="Notification"
app:actionLayout="#layout/custom_action_notification_layout"
app:showAsAction="always"/>
<item
android:id="#+id/action_task"
android:icon="#drawable/icon_task"
android:title="Task"
app:actionLayout="#layout/custom_action_task_layout"
app:showAsAction="always"/>
</menu>
3. In your Activity onCreateOptionsMenu(), get ActionView of Notification and Task items and Set OnClick listeners to those ActionView.
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.custom_menu, menu);
// Notification
final MenuItem itemNotification = menu.findItem(R.id.action_notification);
View actionViewNotification = MenuItemCompat.getActionView(itemNotification);
actionViewNotification.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
onOptionsItemSelected(itemNotification);
}
});
// Task
final MenuItem itemTask = menu.findItem(R.id.action_task);
View actionViewTask = MenuItemCompat.getActionView(itemTask);
actionViewTask.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
onOptionsItemSelected(itemTask);
}
});
return true;
}
4. Finally, In onOptionsItemSelected() do the rest:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.action_notification: {
// Do something
Toast toast = Toast.makeText(this, "Notification clicked", Toast.LENGTH_SHORT);
toast.setGravity(Gravity.CENTER, 0, 0);
toast.show();
return true;
}
case R.id.action_task: {
// Do something
Toast toast = Toast.makeText(this, "Task clicked", Toast.LENGTH_SHORT);
toast.setGravity(Gravity.CENTER, 0, 0);
toast.show();
return true;
}
}
return super.onOptionsItemSelected(item);
}
FYI, I have checked Notification and Task menu item click event by showing Toast messages.
OUTPUT:
UPDATE:
In your updated code use:
MenuItemCompat.setActionView(itemNotification, R.layout.custom_action_notification_layout);
View actionViewNotification = MenuItemCompat.getActionView(itemNotification);
Instead of
View actionViewNotification = MenuItemCompat.getActionView(itemNotification);
MenuItemCompat.setActionView(itemNotification, R.layout.custom_action_notification_layout);
Hope this will help~