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