I am doing menu item. But i am not able to view the menu item in the screen. Can anybody tell me what is getting wrong or what other need to be added to get the code working. I have got totally stuck.
package com.example.androiddemo;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
public class MenuItem extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.menu.menupage);
}
public boolean onCreateOptionsMenu(Menu menu){
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menupage, menu);
return true;
}
}
xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="#+id/about"
android:icon="#drawable/images"
android:title="About"
android:showAsAction="ifRoom"
/>
<item
android:id="#+id/setting"
android:icon="#drawable/ic_launcher"
android:title="App Setting"
/>
</menu>
Here you have to set your activity's xml file.
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main); // Your activity's xml file.
}
public boolean onCreateOptionsMenu(Menu menu){
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menupage, menu); // Here you are setting the menu whatever options you want.
return true;
}
}
After running this, just press on menu, It should show your options. If not post back.
Related
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);
}
the button more information is not visible!!!!
xml of menu :-
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="#+id/more_information"
android:title="#string/action_mnn"
/>
</menu>
main activity :-
package com.example.yashchaudhary.modernartui;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
public class MainActivity extends AppCompatActivity {
#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.main_menu, menu);
return true;
}
public boolean onOptionsItemSelected(MenuItem item){
switch (item.getItemId()){
case R.id.more_information:
return true;
default :
return super.onOptionsItemSelected(item);
}
}
}
screenshot after clickin on 3 dots
In my case it was a Theme specific for the action bar in the activity layout
<androidx.appcompat.widget.Toolbar
android:id="#+id/toolbar"
android:theme="#style/ThemeOverlay.MaterialComponents.Dark.ActionBar"
.../>
Removing the theme property solved my problem
<androidx.appcompat.widget.Toolbar android:id="#+id/toolbar" android:theme="#style/ThemeOverlay.MaterialComponents.Dark.ActionBar"/>
Removing the theme attribute will resolve this problem.
Try to add to the item on the menu xml
android:orderInCategory="100"
and add break; after
case R.id.more_information:
return true;
My ActionBar app is closing unexpectedly. Please help me with it ...
I want to build action bar for 2.3 version I have add all support libraries also ...but that giving unexpected closed message in android emulator.
main.xml code :
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<!-- Search / will display always -->
<item android:id="#+id/action_search"
android:icon="#drawable/ic_launcher"
android:title="#string/action_search"
android:showAsAction="ifRoom"/>
<!-- Location Found -->
<item android:id="#+id/action_location_found"
android:icon="#drawable/ic_launcher"
android:title="#string/action_location_found"
android:showAsAction="ifRoom" />
</menu>
and main_activity.java code:
package com.jinesh.m_learning;
import android.app.Activity ;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
public class MainActivity extends Activity{
#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.main, menu);
return super.onCreateOptionsMenu(menu);
}
}
I have
import com.actionbarsherlock.app.SherlockFragment;
import com.actionbarsherlock.app.SherlockFragmentActivity;
import com.actionbarsherlock.view.Menu;
import com.actionbarsherlock.view.MenuInflater;
import com.actionbarsherlock.view.MenuItem;
public class ActivityHome extends SherlockFragmentActivity {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
...........
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getSupportMenuInflater();
inflater.inflate(R.menu.menu_main, menu);
return true;
}
but the menu is not showing in a title bar, but in the bottom of a screen(by clicking "menu" button on device). Like by normal activity... what am i doing wrong??
menu xml is:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="#+id/menu_add_record"
android:icon="#drawable/ic_action_plus"
android:title="#string/add"
>
</item>
</menu>
please help! :)
you can get individual menu items to show in the ActionBar by adding the android:showAsAction="always" attribute
http://developer.android.com/guide/topics/resources/menu-resource.html
I can not make an Action Bar in my app. It crashes on startup. I have tested several code examples from the internet. I have made a completely new project in Eclipse with the only purpose of adding an action bar. But I can't.
Here is my code
package com.example.testmenu;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
public class MainActivity extends Activity {
#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.activity_main, menu);
return true;
}
}
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="#+id/menu_save"
android:icon="#drawable/icon_one"
android:title="#string/menu_one"
android:showAsAction="ifRoom|withText" />
</menu>
java.lang.RuntimeException:
Unable to start activity ComponentInfo{com.example.testmenu/com.example.testmenu.MainActivity}:
android.view.InflateException:
Binary XML file line #2:
Error inflating class menu
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
Without the menu-tag the app runs ok.
What am I doing wrong?
Your Menu should be from actionbarsherlock package. Try like this:
public boolean onCreateOptionsMenu(com.actionbarsherlock.view.Menu menu) {
//...
or you can add import:
import com.actionbarsherlock.view.Menu;