OnCreateOptionMenu does not work on Theme.Material.Light.Dialog theme - android

I met a problem in overflow menu. In general, the codes in MainActivity below will work on normal theme:
package pokon548.example;
import android.app.*;
import android.os.*;
import android.view.*;
public class MainActivity extends Activity
{
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
#Override
public boolean onCreateOptionsMenu(Menu menu)
{
// TODO: Implement this method
getMenuInflater().inflate(R.menu.menu, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item)
{
// TODO: Implement this method
switch (item.getItemId()) {
case R.id.item:
break;
}
return true;
}
}
and here is menu.xml:
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="#+id/item"
android:title="Item"/>
</menu>
Also styles.xml:
<resources>
<style name="AppTheme" parent="#android:style/Theme.Material.Light.Dialog">
<item name="android:colorPrimary">#FFEC5F67</item>
<item name="android:colorPrimaryDark">#FFEC5F67</item>
<item name="android:colorAccent">#FFEC5F67</item>
<item name="android:textColorSecondary">#FFEC5F67</item>
</style>
</resources>
Layout file:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical"
>
<TextView
android:text="#string/hello_world"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
But if I changed theme from Theme.Material.Light to Theme.Material.Light.Dialog, Overflow menu button (three dots in ActionBar) will not be shown in Activity.
Is there's any ways to show Overflow menu in this situation?

Related

Menu (three dots) doesn't appear in emulator but visible in studio

I am just starting learning Android and have some troubles with creating menu in app.
I tried all options to create menu but no one work for me.
When I run emulator or real device menu doesn't appear.
I have tried "Ctrl+M" and different devices but it doesn't work.
What is the problem?
My MainActivity
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public boolean onCreateOptionMenu(final Menu menu) {
MenuInflater menuInflater = getMenuInflater();
menuInflater.inflate(R.menu.main_menu, menu);
return super.onCreateOptionsMenu(menu);
}
}
my activity_main.xml
<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="com.genaepic.p013_contextmenu.MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Just do it!"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
my main_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_settings"
android:orderInCategory="100"
android:title="#string/item1"
app:showAsAction="ifRoom"/>
<item
android:id="#+id/action_settings2"
android:orderInCategory="100"
android:title="#string/item2"
app:showAsAction="ifRoom" />
<item
android:id="#+id/action_settings3"
android:orderInCategory="100"
android:title="#string/item3"
app:showAsAction="never" />
<item
android:id="#+id/action_settings4"
android:checkable="false"
android:orderInCategory="100"
android:title="#string/item4"
app:showAsAction="always" />
enter image description here
The problem is due to the showAsAction tag. Your item that inflates your overflow menu MUST BE showAsAction="always", otherwise menu may hide in devices. An example snippet:
<item
android:id="#+id/settings"
android:icon="#drawable/my_drawable"
app:showAsAction="always"
android:title="Setting">
</item>
EDIT
You can override the onCreatOption to inflate the menu.
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return super.onCreateOptionsMenu(menu);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
Log.d(TAG, "onCreateOptionsMenu: ");
getMenuInflater().inflate(R.menu.main_menu, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
Log.d(TAG, "onOptionsItemSelected: ");
switch (item.getItemId()) {
case android.R.id.home:
onBackPressed();
break;
}
return super.onOptionsItemSelected(item);
}

How to remove three dots from the toolbar of navigation drawer

I want to remove that 3 dots from toolbar and also want to add an image with textview on that position.That image and textviews are different for each fragment activity.
Also can anyone tell me how to make the toolbar transparent.
In your MainActivity you will have optionmenu, just make it false
#Override
public boolean onCreateOptionsMenu(Menu menu) {
return false;
}
Remove this item from menu.
<item
android:id="#+id/action_settings"
android:orderInCategory="100"
android:showAsAction="never"
visibility=false
android:title="#string/action_settings"/>
1)your First Question about removing three dots (Aditya -Vyas )solved your prob
#Override
public boolean onCreateOptionsMenu(Menu menu) {
return false;
}
2) Now to put image view
Crate Menu Resource
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="#+id/action_submit"
android:title="#string/submit"
android:icon="#drawable/ask_price_back"
app:showAsAction="always" />
</menu>
Then declare onCrateOptionsMenu and onOptionsImtemSelected as below
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_submit, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.action_submit) {
//Your code to handle click
return true;
}
return super.onOptionsItemSelected(item);
}
3) Handling The menu from the fragment loaded in activity try this
override this method in your fragment
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
getActivity().getMenuInflater().inflate(R.menu.menu_of_fragment, menu);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.action_fragment) {
// Your code to handle Click
}
return super.onOptionsItemSelected(item);
}
1) From your menu.xml remove all items with tag .
This will remove three dots from your menu area.
2) Add a new item in your xml and give properties
android:icon="#drawable/image_you_want to show in the menu area"
app:showAsAction="always"
set your style like
<!-- Application theme. -->
<style name="AppTheme" parent="AppBaseTheme">
<!-- All customizations that are NOT specific to a particular API-level can go here. -->
<item name="android:actionOverflowButtonStyle">#style/MyActionButtonOverflow</item>
</style>
<!-- Style to replace actionbar overflow icon. set item 'android:actionOverflowButtonStyle' in AppTheme -->
<style name="MyActionButtonOverflow" parent="android:style/Widget.Holo.Light.ActionButton.Overflow">
<item name="android:src">#drawable/ic_launcher</item>
</style>
check this post : post
> for adding imageview and text
` <item android:id="#+id/action_cart"
android:icon="#drawable/cart2"
android:title="image"
app:showAsAction="always" />
<item
android:id="#+id/badge"
app:actionLayout="#layout/counter"
android:title="image"
android:icon="#drawable/shape"
app:showAsAction="always">
</item>`
In Layout counter
`<TextView
android:id="#+id/notif_count"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:minWidth="20dp"
android:minHeight="20dp"
android:background="#drawable/shape"
android:text="0"
android:textSize="14sp"
android:textColor="#fff"
android:gravity="center"
android:padding="2dp"
android:singleLine="true">
</TextView>`
Just add this to you theme:
<item name="actionOverflowButtonStyle">#id/invisible</item>

Unable to show Top right corner Menu bar in Android Studio1.3.1

Please tell what to do if i want menu bar in top right corner of the activity bar .
And i am using Android Studio studio 1.3.1
And activity_main.xml design seleted options is Below:
Nexus4 LIght MainActivity 21;
My menu_main.xml file is:
<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">
<group android:checkableBehavior="single">
<item
android:id="#+id/menu_red"
android:orderInCategory="1"
app:showAsAction="never"
android:title="#string/red_string"/>
<item
android:id="#+id/menu_green"
android:orderInCategory="2"
app:showAsAction="never"
android:title="#string/green_string"/>
<item
android:id="#+id/menu_yellow"
android:orderInCategory="3"
app:showAsAction="never"
android:title="#string/yellow_string"/>
</group>
And my MainActivity.java File is:
package abcd.shravankr.basic;
import android.graphics.Color;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.RelativeLayout;
public class MainActivity extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
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.menu_main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
RelativeLayout main_view=(RelativeLayout)findViewById(R.id.main_view);
switch (item.getItemId()) {
case R.id.menu_red:
if(item.isChecked())
item.setChecked(false);
else
item.setChecked(true);
main_view.setBackgroundColor(Color.RED);
return true;
case R.id.menu_green:
if(item.isChecked())
item.setChecked(false);
else
item.setChecked(true);
main_view.setBackgroundColor(Color.GREEN);
return true;
case R.id.menu_yellow:
if(item.isChecked())
item.setChecked(false);
else
item.setChecked(true);
main_view.setBackgroundColor(Color.YELLOW);
return true;
default:
return super.onOptionsItemSelected(item);
}
}
}
Please give me ten points so that i can insert image in it to understand problem more clearly.
Try this
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">#color/colorPrimary</item>
<item name="colorPrimaryDark">#color/colorPrimaryDark</item>
<item name="colorAccent">#color/colorAccent</item>
<item name="alertDialogTheme">#style/Theme.AppCompat.Light.Dialog.Alert</item>
<item name="windowActionBar">false</item>
<item name="actionOverflowButtonStyle">#style/OverFlow</item>
</style>
<style name="OverFlow" parent="Widget.AppCompat.ActionButton.Overflow">
<item name="android:src">#drawable/overflow</item>
</style>
actionOverflowButtonStyle is used to show the custom overflow menu icon.
EDIT::
1. Extend your activity class to AppCompactActivity
2. add tollbar to your xml layout file.
<android.support.v7.widget.Toolbar
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/toolbar_top"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/colorPrimary"
android:minHeight="?attr/actionBarSize"
android:theme="#android:style/Theme.Light">
</android.support.v7.widget.Toolbar>
In onCreate() Activity
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);

NavigationView Unresponsive

I am creating a navigationview in android.I have added items under draweritems under menu folder.Whenever I clicked a menuitem a toast appeared before,but now it is unresponsive.Below is the code.Help me please...
draweritems.xml:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<group android:checkableBehavior="single">
<item
android:id="#+id/motel"
android:title="Motel"
android:icon="#drawable/motel"
>
</item>
<item
android:id="#+id/packages"
android:title="Packages"
android:icon="#drawable/packages">
</item>
<item
android:id="#+id/ayurveda"
android:title="Ayurveda"
android:icon="#drawable/ayurveda">
</item>
<item
android:id="#+id/marketing"
android:title="Marketing"
android:icon="#drawable/marketing">
</item>
<item
android:id="#+id/Tours"
android:title="ConductedTours"
android:icon="#drawable/tours">
</item>
<item
android:id="#+id/Locate"
android:title="Locate"
android:icon="#drawable/locate">
</item>
<item
android:id="#+id/News"
android:title="News"
android:icon="#drawable/news">
</item>
<item
android:id="#+id/Login"
android:title="Login"
android:icon="#drawable/login">
</item>
</group>
</menu>
MainActivity.java:
import android.content.Context;
import android.os.Bundle;
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.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
private Toolbar toolbar;
private Context context=this;
private NavigationView navigationView;
private DrawerLayout drawerLayout;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar=(Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
navigationView=(NavigationView) findViewById(R.id.navigation_view);
navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
#Override
public boolean onNavigationItemSelected(MenuItem menuItem) {
Log.i("Clicked", "Clicked motel");
if (menuItem.isChecked()) {
menuItem.setChecked(false);
} else menuItem.setChecked(true);
drawerLayout.closeDrawers();
switch (menuItem.getItemId()) {
case R.id.motel:
Toast.makeText(getApplicationContext(), "Motels", Toast.LENGTH_LONG).show();
return true;
case R.id.packages:
Toast.makeText(getApplicationContext(), "Packages", Toast.LENGTH_LONG).show();
return true;
case R.id.ayurveda:
Toast.makeText(getApplicationContext(), "Ayurveda", Toast.LENGTH_LONG).show();
return true;
}
return true;
}
});
drawerLayout = (DrawerLayout) findViewById(R.id.drawerLayout);
ActionBarDrawerToggle actionBarDrawerToggle = new ActionBarDrawerToggle(this,drawerLayout,toolbar,R.string.openDrawer, R.string.closeDrawer){
#Override
public void onDrawerClosed(View drawerView) {
// Code here will be triggered once the drawer closes as we dont want anything to happen so we leave this blank
super.onDrawerClosed(drawerView);
}
#Override
public void onDrawerOpened(View drawerView) {
// Code here will be triggered once the drawer open as we dont want anything to happen so we leave this blank
super.onDrawerOpened(drawerView);
}
};
//Setting the actionbarToggle to drawer layout
drawerLayout.setDrawerListener(actionBarDrawerToggle);
//calling sync state is necessay or else your hamburger icon wont show up
actionBarDrawerToggle.syncState();
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if(id==R.id.ayurveda){
Toast.makeText(getApplicationContext(),"Clicked Ayurveda",Toast.LENGTH_LONG).show();
return true;
}
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
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"
android:id="#+id/drawerLayout"
android:fitsSystemWindows="true"
tools:context=".MainActivity">
<android.support.design.widget.NavigationView
android:id="#+id/navigation_view"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:layout_gravity="start"
app:headerLayout="#layout/header"
app:menu="#menu/draweritems"
app:theme="#style/NavigationViewStyle">
</android.support.design.widget.NavigationView>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/relative"
android:orientation="vertical">
<include
android:id="#+id/toolbar"
layout="#layout/toolbar"/>
</RelativeLayout>
</android.support.v4.widget.DrawerLayout>
styles.xml:
<!-- Base application theme. -->
<!-- Customize your theme here. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<item name="windowActionBar">false</item>
<item name="windowActionModeOverlay">true</item>
<item name="android:windowDrawsSystemBarBackgrounds">true</item>
</style>
<style name="NavigationViewStyle">
<item name="android:textSize">20sp</item>
<!-- menu item text size-->
<item name="android:listPreferredItemHeightSmall">80dp</item><!-- menu item height-->
</style>
This code change
public class MainActivity extends AppCompatActivity
The following code
public class MainActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener
I'm not sure, but I hope you make it.

Android Menu does not inflate

I am pulling my hairs out in trying to get menu to infate on my main activity but to no avail.
I am using Android Studio and the design view showed that the XML menu items are rightfully defined.
However, when I run the codes either in the phone(running Android 4.4.2) or emulator, the menu items are not showing up. Nothing happens when I press on the menu.
Any enlightenment will be most welcome.
Menu XML - my.xml
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:id="#+id/item1" android:title="Option1"></item>
<item android:id="#+id/item2" android:title="Option2"></item>
<item android:id="#+id/item3" android:title="Option3"></item>
<item android:id="#+id/item4" android:title="Option4"></item>
<item android:id="#+id/item5" android:title="Option5"></item>
<item android:id="#+id/item6" android:title="Option6"></item>
</menu>
Activity Class - myActivity.java
package com.example.mymenu;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.util.Log;
import android.widget.Toast;
public class MyActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.my, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.item1:
Toast.makeText(this, " " +
"Option1", Toast.LENGTH_SHORT).show();
return true;
case R.id.item2:
Toast.makeText(this, "Option2", Toast.LENGTH_SHORT).show();
return true;
case R.id.item3:
Toast.makeText(this, "Option3", Toast.LENGTH_SHORT).show();
return true;
case R.id.item4:
Toast.makeText(this, "Option4", Toast.LENGTH_SHORT).show();
return true;
case R.id.item5:
Toast.makeText(this, "Option5", Toast.LENGTH_SHORT).show();
return true;
case R.id.item6:
Toast.makeText(this, "Option6", Toast.LENGTH_SHORT).show();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
}
Layout File
<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:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin"
tools:context=".MyActivity">
<TextView
android:text="#string/hello_world"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</RelativeLayout>
I have solved the problem.
My phone has a menu button, and pressing that button will display the menu list.
I tried with a newer phone that does not have the menu button, and the menu shows up within the action bar.
Regards
I think the problem is you must have created my.xml in layout folder .Create my.xml in menu folder.
Cross check if the menu.xml is in menu folder and not any other res folder like layout.
Trying to inflate it in action bar?
If yes, then try using the following code in the menu.xml
android:showAsAction="always"
Refer : http://developer.android.com/guide/topics/ui/menus.html
Hi try this if you want to display it in Actionbar.
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="#+id/action_settings"
android:icon="#drawable/action_overflow"
app:showAsAction="always"/>
<menu>
<item android:id="#+id/item1" android:title="Option1"></item>
<item android:id="#+id/item2" android:title="Option2"></item>
<item android:id="#+id/item3" android:title="Option3"></item>
<item android:id="#+id/item4" android:title="Option4"></item>
<item android:id="#+id/item5" android:title="Option5"></item>
<item android:id="#+id/item6" android:title="Option6"></item>
</menu>
</menu>

Categories

Resources