menuInflater to create a menu in the onCreateOptionsMenu not working - android

I have used a menuInflater to create a menu in the onCreateOptionsMenu method of your activity. I have used this to display arrows on the toolbar for a calendar so that the user can go to previous or next month. But for some reason the arrows are not getting displayed
Pls can someone help.
MonthGridActivity:
private MonthGridFragment monthGridFragment;
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_calendar_grid, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.action_previous:
monthGridFragment.loadLastMonth();
return true;
case R.id.action_next:
monthGridFragment.loadNextMonth();
return true;
case R.id.all_events:
monthGridFragment.showAllEvents();
return true;
}
return super.onOptionsItemSelected(item);
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_calendar_grid);
monthGridFragment = new MonthGridFragment();
getSupportFragmentManager().beginTransaction()
.add(R.id.activity_calendar_grid_container, monthGridFragment)
.commit();
}
menu_calendar_grid.xml
<item
android:id="#+id/action_previous"
android:title="#string/prev_month"
android:orderInCategory="100"
app:showAsAction="ifRoom"
android:icon="#drawable/arrow_previous" />
<item
android:id="#+id/action_next"
android:title="#string/next_month"
android:orderInCategory="100"
app:showAsAction="ifRoom"
android:icon="#drawable/arrow_next" />
<item
android:id="#+id/all_events"
android:title="#string/view_all_events"
android:orderInCategory="101"
app:showAsAction="never"
android:icon="#android:drawable/ic_menu_view" />
MonthGridFragment.java
public void loadNextMonth() {
calendar.setTime(CalUtil.addMonth(calendar.getTime(), 1));
refresh();
}
public void loadLastMonth() {
calendar.setTime(CalUtil.subtractMonth(calendar.getTime(), 1));
refresh();
}
activity_calendar_grid.xml
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:ignore="MergeRootFrame">
<android.support.v7.widget.Toolbar
android:id="#+id/myActivity_toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<android.support.v4.view.ViewPager xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/pager"
android:layout_width="match_parent"
android:layout_height="match_parent" />
fragment_calendar_grid.xml
<FrameLayout 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:orientation="vertical"
android:id="#+id/calendar_grid_layout">
<include layout="#layout/calendar_grid_header" />
<GridView
android:paddingTop="1dp"
android:gravity="center"
android:layout_gravity="center"
android:layout_margin="0dp"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/calendar_grid_view"
android:numColumns="7"
android:stretchMode="columnWidth"
android:background="#color/white_gray"
android:horizontalSpacing="1dp"
android:verticalSpacing="1dp" />

in onCreate method create a toolbar:
Toolbar toolbar = (Toolbar) findViewById(R.id.myActivity_toolbar);
setActionBar(toolbar);
so in the layout add the view:
<Toolbar
android:id="#+id/myActivity_toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
UPD:
You have to use either Toolbar from the support library or the standard one. If MonthGridActivity extends from Activity, use Toolbar (not android.support.v7.widget.Toolbar) in the layout. Otherwise MonthGridActivity must be extended from AppCompatActivity

Related

Icon inside Toolbar is not clickable

This is my main.xml file--
<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:id="#+id/main_content"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context="com.example.pawan.googlesignin.SongsActivity">
<android.support.design.widget.AppBarLayout
android:id="#+id/appbar"
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="wrap_content"
app:titleTextColor="#color/colorAccent"
android:background="#color/colorPrimary"
android:minHeight="?attr/actionBarSize"
app:theme="#style/ThemeOverlay.AppCompat.ActionBar" />
<android.support.design.widget.TabLayout
android:id="#+id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.design.widget.TabItem
android:id="#+id/tabItem"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="live music" />
<android.support.design.widget.TabItem
android:id="#+id/tabItem1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="scheduled" />
</android.support.design.widget.TabLayout>
</android.support.design.widget.AppBarLayout>
<android.support.v4.view.ViewPager
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="#string/appbar_scrolling_view_behavior" /></RelativeLayout>
This is my items.xml
<?xml version="1.0" encoding="utf-8"?>
<item android:id="#+id/share"
android:title="share"
android:orderInCategory="100"
app:showAsAction="ifRoom" />
<item android:id="#+id/feedback"
android:title="feedback"
android:orderInCategory="100"
app:showAsAction="ifRoom" />
<item android:id="#+id/profile"
android:title="profile"
android:orderInCategory="100"
app:showAsAction="ifRoom" />
This is my MainActivity.xml--
public class SongsActivity extends AppCompatActivity {
private static final String TAG="MainActivity";
private SectionsPageAdapter mSectionPageAdapter;
private ViewPager mViewpager;
private Toolbar toolbar;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_songs);
Log.d(TAG, "onCreate:starting");
mSectionPageAdapter = new SectionsPageAdapter(getSupportFragmentManager());
mViewpager =findViewById(R.id.container);
setupViewPager(mViewpager);
TabLayout tabLayout = findViewById(R.id.tabs);
tabLayout.setupWithViewPager(mViewpager);
toolbar =findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setDisplayShowHomeEnabled(true);
getSupportActionBar().setTitle("My app");
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu,menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
Intent intent = new Intent(this, VenuesActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
return true;
default:
return super.onOptionsItemSelected(item);
}
}
public void setupViewPager(ViewPager viewPager){
SectionsPageAdapter adapter=new SectionsPageAdapter(getSupportFragmentManager());
adapter.addFragment(new live(),"live music");
adapter.addFragment(new scheduled(),"scheduled");
viewPager.setAdapter(adapter);
}
}
Friends, I am not able to click on the back navigation button and also on the three dots on the top right corner of the Toolbar.
Please give a solution for this, I have tried all the solutions on provided here.
I can't find any error here.Please help me...
in my case :
changing activity_main.xml from this:
<include
android:id="#+id/toolbar"
layout="#layout/asset_bar_layout"/>
to this:
<com.google.android.material.appbar.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<androidx.appcompat.widget.Toolbar
android:theme="#style/Base.ThemeOverlay.AppCompat.Dark.ActionBar"
android:id="#+id/toolbar"
android:background="#color/colorPrimaryDark"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:popupTheme="#style/MenuStyle"/>
</com.google.android.material.appbar.AppBarLayout>
solved the problem
Use this instead to handle the back button on the actionbar.
case android.R.id.home:
super.onBackPressed();
return true;
Hello,i have find the solution for your query
Please update your code like this
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
System.out.println("back button pressed");
Intent intent = new Intent(this, MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
finish();
return true;
case R.id.share:
System.out.println("share button pressed");
case R.id.feedback:
System.out.println("feedback button pressed");
case R.id.profile:
System.out.println("profile button pressed");
default:
return super.onOptionsItemSelected(item);
}
}
You have forgot to call finish()method and defining the click listener of your menu item.
Please ask me if you need any more help.

how to create the click event in sidebar navigation

I have created the sidebar navigation in my app. Now I want to create a click event on that.
I have searched for it and I got the documentation but I'm unable to understand the same.
so please anyone can suggest a better source for it will be helpful. tutorial on youtube will be helpful.
My navigation_menu
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="#+id/nav_account"
android:icon="#mipmap/ic_person_black_24dp"
android:title="My Account"/>
<item android:id="#+id/nav_setting"
android:icon="#mipmap/ic_settings_black_24dp"
android:title="Settings"/>
<item android:id="#+id/nav_logout"
android:icon="#mipmap/ic_logout_black_24dp"
android:title="Logout"/>
</menu>
My 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: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"
tools:context="com.example.lenovo.jdstudio.MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<include
layout="#layout/navigation_action"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<TextView
android:id="#+id/textView2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Main Layout"
android:textAlignment="center"
android:textSize="24dp" />
</LinearLayout>
<android.support.design.widget.NavigationView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
app:headerLayout="#layout/navigation_header"
app:menu="#menu/navigation_menu">
</android.support.design.widget.NavigationView>
</android.support.v4.widget.DrawerLayout>
Simply implement NavigationView.OnNavigationItemSelectedListener interface and then override the method onNavigationItemSelected(MenuItem menuItem) like the following:
public class MainActivity extends AppCompatActivity implements
NavigationView.OnNavigationItemSelectedListener{
NavigationView navigationView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// declaring the NavigationView
navigationView = (NavigationView) findViewById(R.id.navigationView);
// assigning the listener to the NavigationView
navigationView.setNavigationItemSelectedListener(this);
}
#Override
public boolean onNavigationItemSelected(MenuItem menuItem) {
menuItem.setChecked(true);
switch (menuItem.getItemId()) {
case R.id.nav_account:
// do you click actions for the first selection
break;
case R.id.nav_setting:
// do you click actions for the second selection
break;
case R.id.nav_logout:
// do you click actions for the third selection
break;
}
return true;
}
}
And for sure don't forget to give id to the NavigationView in your xml-layout:
<android.support.design.widget.NavigationView
android:id="#+id/navigationView"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
app:headerLayout="#layout/navigation_header"
app:menu="#menu/navigation_menu">
</android.support.design.widget.NavigationView>

How to add Spinner as an item in Navigation Drawer

I want to add spinner as an item in my navigation drawer. Where should I put the spinner as an item? Where to inflate the layout for the spinner? Where to initialize the spinner? I want it to look like this:
This is where I add my items:
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<group
android:id="#+id/group1"
android:checkableBehavior="single">
<item
android:id="#+id/nav_login"
android:icon="#drawable/ic_login"
android:title="#string/login_menu_item"/>
<item
android:id="#+id/nav_signup"
android:icon="#drawable/ic_signup"
android:title="#string/signup_menu_item"/>
</group>
<item android:title="#string/language">
<menu>
<item
android:id="#+id/nav_eng"
android:title="#string/english">
</item>
<item
android:id="#+id/nav_heb"
android:title="#string/hebrew">
</item>
</menu>
This is my layout with the drawer:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
android:orientation="vertical">
<android.support.v7.widget.Toolbar
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/toolbar_home"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/colorPrimary"
android:minHeight="?attr/actionBarSize"
android:theme="#style/ThemeOverlay.AppCompat.ActionBar"
app:title="Masü"
/>
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:openDrawer="start">
<FrameLayout
android:id="#+id/fragment_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
/>
<android.support.design.widget.NavigationView
android:id="#+id/nav_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
app:itemIconTint="#color/colorAccent"
app:itemTextColor="#color/textColorSecondary"
app:menu="#menu/activity_home_drawer"/>
</android.support.v4.widget.DrawerLayout>
</LinearLayout>
And on click of an item this is how it works:
public boolean onNavigationItemSelected(MenuItem item) {
int id = item.getItemId();
mDrawerLayout.closeDrawers();
if (id == R.id.nav_login) {
if (mIsLoggedin) {
logout();
} else {
mFragmentTransaction = mFragmentManager.beginTransaction();
mFragmentTransaction.replace(R.id.fragment_container, new LoginFragment()).commit();
}
Step 1. Please add item in menu.xml
<item
android:id="#+id/navigation_drawer_item3"
android:icon="#android:drawable/ic_menu_share"
android:title=""
app:actionLayout="#layout/spinner"/>
Step 2. Please create layout for spinner view
<?xml version="1.0" encoding="utf-8"?>
<Spinner xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/spinner"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center_vertical"
android:gravity="center_vertical" />
Step 3. set spinner data in your activity file
Spinner spinner = (Spinner) navigationView.getMenu().findItem(R.id.navigation_drawer_item3).getActionView();
spinner.setAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_spinner_dropdown_item,language));
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(MainActivity.this,language[position],Toast.LENGTH_SHORT).show();
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
Step 4. please add android support design library into project if need.
In kotlin
override fun onCreateOptionsMenu(menu: Menu?): Boolean {
menuInflater.inflate(R.menu.main_menu, menu);
val nv = findViewById<View>(R.id.navigation_view) as NavigationView
val item01 = nv.menu.findItem(R.id.smarket_item)
val spinner = item01.actionView as Spinner
val adapter = ArrayAdapter.createFromResource(
this,
R.array.superMarket, android.R.layout.simple_spinner_item
)
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item)
spinner.adapter = adapter
return true
return super.onCreateOptionsMenu(menu)
}

Android Toolbar Menu items not showing when I have

I have an ImageButton, as well as an TextView defined within my android.support.v7.widget.Toolbar. I also have a menu item, but this is not showing up when I run the app.
I have claeed the getMenuInflater().inflate(R.menu.menu, menu) in my activity, but not sure what I am missing here.
Here is my tool_bar.xml:
<android.support.v7.widget.Toolbar
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="80dp"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:background="#color/colorPrimary"
android:id="#+id/tool_bar">
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_nav_icon"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/app_name"
android:textSize="30dp"
android:layout_marginLeft="20dp"
android:textColor="#ffffff"/>
</android.support.v7.widget.Toolbar>
Here is my menu item:
<menu
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:icon=" #drawable/ic_search"
android:title="Search"
app:showAsAction="always" />
</menu>
And here is my HomeActivity.java class
private Toolbar toolbar;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
toolbar = (Toolbar)findViewById(R.id.tool_bar);
this.setSupportActionBar(toolbar);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem menuItem)
{
//implement logic here to get selected item
return super.onOptionsItemSelected(menuItem);
}
Why is the menu item not showing?
try this,
add this code in home.xml
<include android:id="#+id/tool_bar" layout="#layout/tool_bar"></include>
and also change in style
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">

list view not shown over main fragment having map

i am showing map in main content view which is a fragment. i want to show a listView over the main content view having map on a button click in an action bar or swipe the screen from left. i have tried alot but all in vain. my problem is how can i show the listview over the main content fragment such that the list view comes over the map.
upto now the listview appears when i swipe but it appears below the map in the fragment
i hav'nt add the functionality for clicking the button in actionbar. i am just swiping the list view from left. thanks in advance
my code is
DrawerClass.java
public class DrawerClass extends Activity {
DrawerLayout drawerLayout;
View drawerView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.drawarclass_layout);
drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
drawerView = (View) findViewById(R.id.drawer);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = new MenuInflater(this);
inflater.inflate(R.menu.actionbaricons_layout, menu);
return super.onCreateOptionsMenu(menu);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.save:
return true;
default:
return super.onOptionsItemSelected(item);
}
}
}
drawerclass_layout.xml
<LinearLayout
android:id="#+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<fragment
android:id="#+id/mapid"
android:name="com.google.android.gms.maps.MapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
<LinearLayout
android:id="#+id/drawer"
android:layout_width="140dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:background="#android:color/background_light"
android:orientation="vertical"
android:padding="5dp" >
<fragment
android:id="#+id/fragment1"
android:name="open.way.iscope.MyListFragment1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1" />
</LinearLayout>
actionbaricons_layout.xml
<item
android:id="#+id/category"
android:gravity="center"
android:icon="#drawable/ic_action_select_all"
android:showAsAction="always"
android:title="#string/categories"/>
<item
android:id="#+id/previous"
android:icon="#drawable/ic_action_previous_item"
android:showAsAction="always"
android:title="#string/previous"/>
<item
android:id="#+id/next"
android:icon="#drawable/ic_action_next_item"
android:showAsAction="always"
android:title="#string/next"/>
<item
android:id="#+id/save"
android:icon="#drawable/ic_action_save"
android:showAsAction="always"
android:title="#string/save"/>
This is happening because the drawer element is not the root element in your drawerclass_layout.xml. Make Drawer element as your root element and the map fragment as the child in the layout file and you will be able to see your slider over the top of the main fragment that has map. Here is the code snippet.
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<!-- google map -->
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:name="com.google.android.gms.maps.MapFragment"/>
Hope this would help!!

Categories

Resources