Menu not showing in SherlockFragmentActivity - android

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

Related

Option in Option menu are not visible instead it is showing only white blank space,here are my menu xml and main activity

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;

Android - unwanted submenu appearing after screen rotation

I have created very simple demo project to demonstrate the bug:
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
public class MenuBugActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = new MenuInflater(this);
inflater.inflate(R.menu.options_bug_demo, menu);
return true;
}
}
Main layout - main.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"/>
Menu items options_bug_demo.xml:
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="#+id/menu_bug"
android:title="Bug"
android:icon="#drawable/ic_action_settings"
android:showAsAction="always">
<menu>
<item
android:id="#+id/menu_bug_demo"
android:title="Bug demo">
<menu>
<item android:title="Settings"/>
</menu>
</item>
</menu>
</item>
</menu>
1.Clicking on "Preferences" icon:
2.Clicking on "Bug demo":
3.Clicking on "Settings":
4.Changing screen orientation to the horizontal:
Submenu "Settings" (closed at step 3) appears again after screen rotation! The only way to prevent the submenu from appearing after rotation - to remove string
android:id="#+id/menu_bug_demo"
from resource file options_bug_demo.xml
The question is how to avoid this unwanted behaviour of the submenu?
P.S. The bug was observed on 4.0.4 and 4.1.1 and was not observed on 4.4.2

action bar app unexpectedly closing

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);
}
}

Action bar inflateException

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;

Menu item not working

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.

Categories

Resources