My overflow menu is not appearing in my action bar/App bar (even running emulator) but it only appears in menu_main.xml. What can I do?
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.mo.thesis">
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity
android:name=".MainActivity"
android:label="#string/title_activity_main">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
app_bar.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="?actionBarSize"
android:background="#ccc">
</android.support.v7.widget.Toolbar>
MainActivity.java
package com.mo.thesis;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Toast;
public class MainActivity extends ActionBarActivity {
private Toolbar toolbar;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
toolbar = (Toolbar) findViewById(R.id.app_bar);
setSupportActionBar(toolbar);
}
protected boolean onCreateOptionMenu(Menu menu) {
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();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<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"
tools:context="com.mo.thesis.MainActivity">
<include
android:id="#+id/app_bar"
layout="#layout/app_bar" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Text"
android:id="#+id/textView"
android:layout_alignParentTop="true"
android:layout_alignParentStart="true"
android:layout_marginStart="78dp"
android:layout_marginTop="119dp" />
</RelativeLayout>
activity_main.xml Screen Preview:
menu_main.xml
<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">
<item
android:id="#+id/action_settings"
android:title="#string/action_settings"
android:orderInCategory="100"
app:showAsAction="never" />
</menu>
menu_main.xml Screen Preview:
styles.xml
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="AppTheme.Base">
<!-- Customize your theme here. -->
</style>
<style name="AppTheme.Base" parent="Theme.AppCompat.Light.NoActionBar">
<item name="colorPrimary">#color/primaryColor</item>
<item name="colorPrimaryDark">#color/primaryColorDark</item>
<item name="colorAccent">#color/accentColor</item>
</style>
</resources>
Hope this helps, From your code did not found any issue.
There might be other issue ---
The on-screen overflow menu button only appears in the action bar on devices that lack an off-screen MENU button. Try pressing the MENU button on your device or emulator.
please read this Menu Button - Missing answer.
Your menu is set to never show
<item
android:id="#+id/action_settings"
android:title="#string/action_settings"
android:orderInCategory="100"
app:showAsAction="never" />
Change last line there to "always"
Related
I am so close. I have the android studio project set up correctly with libs, the project compiles, and the emulators and device run the app. I am trying to get rid of the bottom button popup menu of older devices and replace with v7 appcompat toolbar. The old action bar is gone but the menu inflate still inflates to the old popup menu.
I start off using a splashscreen then the app main shows the database view activity. This means I have to dynamically load the toolbar.
My thinking now is the only place I have left to work out is the styles/themes or the order of setcontent(this) to display the main app/database view screen.
Also I dont have the correct emulator for the HTC evo device so the device buttons dont show. I believe this is why alot of developers post that the toolbar doesnt show. When I install apk on real device i can the use the menu button. This is how I am able to see that the menu is not in the toolbar. The emulator shows no buttons plus no toolbar so a developer on an emulator of newer devices would never see this.
I hope this enough to go on for any one to catch my pitfall here.
Thanks in advance.
Here are codes snippets:
styles.xml:
<resources>
<!--
Base application theme, dependent on API level. This theme is replaced
by AppBaseTheme from res/values-vXX/styles.xml on newer devices.
-->
<style name="AppBaseTheme" parent="Theme.AppCompat.Light.NoActionBar">
<!--
Theme customizations available in newer API levels can go in
res/values-vXX/styles.xml, while customizations related to
backward-compatibility can go here.
-->
</style>
<!-- Application theme. -->
<!--<style name="AppTheme" parent="AppBaseTheme">-->
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<!-- All customizations that are NOT specific to a particular API-level can go here. -->
<item name="windowNoTitle">true</item>
<item name="windowActionBar">true</item>
<item name="android:textColor">#000000</item>
</style>
</resources>
Toolbarmenu.xml:
<?xml version="1.0" encoding="utf-8"?>
<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">
<item
android:id="#+id/add"
android:icon="#android:drawable/ic_menu_add"
app:showAsAction="ifRoom"
android:title="#string/app_name"/>
<item
android:id="#+id/reset"
android:icon="#android:drawable/ic_menu_revert"
app:showAsAction="ifRoom"
android:title="#string/action_settings"/>
<item
android:id="#+id/about"
android:icon="#android:drawable/ic_dialog_info"
app:showAsAction="ifRoom"
android:title="#string/hello_world">
</item>
<item
android:id="#+id/exit"
app:showAsAction="ifRoom"
android:title="#string/Menu">
</item>
</menu>
Manifest.xlm:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.loadrunner"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="18" />
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="com.loadrunner.SplashScreenActivity"
android:label="#string/app_name"
android:theme="#android:style/Theme.Translucent.NoTitleBar">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="com.loadrunner.MainActivity"
android:exported="true"
android:label="#string/app_name"
android:theme="#style/Theme.AppCompat.Light.NoActionBar">
<intent-filter>
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
</application>
</manifest>
lrtoolbar.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:background="#2196f3"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:elevation="4dp"
android:minHeight="200dp">
</android.support.v7.widget.Toolbar>
</LinearLayout>
Layout_main.xml:
<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:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context=".SplashScreenActivity"
android:visibility="visible"
android:background="#color/black">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/imageViewSplashLogo"
android:src="#drawable/shoppingcartloading"
android:visibility="visible"
android:contentDescription="#string/app_name"
android:scaleType="fitCenter"
android:cropToPadding="true"
android:layout_alignParentTop="true"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:background="#color/black" />
</RelativeLayout>
And finally the code:
import android.app.ActionBar;
import android.app.Activity;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.SpinnerAdapter;
import android.widget.Toast;
import com.Table.TableMainLayout;
public class MainActivity extends AppCompatActivity {
final String TAG = "MainActivity.java";
public static DatabaseHandler db;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//Log.d("SplashMainActivityonCre", "made it");
db = new DatabaseHandler(this);
db.getWritableDatabase();
db.insertFast(100);
int dbreccnt = db.countRecords();
//Log.d("SplashAfterDBCreate", "made it");
//Log.d("AppLoadrunner ", "Loadrunner record count " + dbreccnt);
Toolbar LrToolBar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(LrToolBar);
//getSupportActionBar().setDisplayShowHomeEnabled(true);
//LrToolBar.setNavigationContentDescription(getResources().getString(R.string.app_name));
setContentView(new TableMainLayout(this));
TableMainLayout start:
package com.Table;
import android.content.Context;
import android.graphics.Color;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.widget.HorizontalScrollView;
import android.widget.RelativeLayout;
import android.widget.ScrollView;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;
import android.widget.Toast;
//import android.database.Cursor;
import com.loadrunner.DatabaseHandler;
import com.loadrunner.R;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.ListIterator;
public class TableMainLayout extends RelativeLayout {
public final String TAG = "TableMainLayout.java";
I have maybe the answer : you just get your toolbar before setContentView() method...
Just try this :
setContentView(new TableMainLayout(this));
Toolbar LrToolBar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(LrToolBar);
//getSupportActionBar().setDisplayShowHomeEnabled(true);
//LrToolBar.setNavigationContentDescription(getResources().getString(R.string.app_name));
Hope it will help you
Make your Toolbar layout like thie:
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:background="#2196f3"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:elevation="4dp"
android:minHeight="200dp" />
and in your Activity code replace the Toolbar LrToolBar = (Toolbar) findViewById(R.id.toolbar); with the:
Toolbar toolbar = LayoutInflater.from(this).inflate(R.layout.toolbar_layout_name, null)
setSupportActionBar(toolbar);
Hope it works.
Try this:
<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="windowActionModeOverlay">true</item>
<item name="windowActionBar">false</item> //see this line
<item name="android:windowNoTitle">true</item>
</style>
Try setting <item name="windowActionBar">false</item>
I hope this helps!
UPDATE: The problem is only in the xml view. On running the app it works just fine. Some error with the rendering in android studio i think.
Im watching a video tutorial in which the action bar shows the overflow menu by default. When i create a new project the overflow menu isnt visible even though menu_main.xml contains an item. i tried using different minimum SDK settings while creating the project and also tried different app themes but it didnt work. here is the code->
menu_main.xml
<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">
<item android:id="#+id/action_settings" android:title="#string/action_settings"
android:orderInCategory="100" app:showAsAction="never" />
</menu>
activity_main.xml
<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=".MainActivity">
<TextView android:text="#string/hello_world" android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</RelativeLayout>
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.shreyans.overflowmenu" >
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
MainActivity.java
package com.example.shreyans.overflowmenu;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
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) {
// 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();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
Can anyone tell me a way to make the overflow menu visible?
You can try this
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="#+id/i1"
android:icon="#drawable/icon"
android:title="#string/title_i1"/>
<item
android:id="#+id/i2"
android:icon="#drawable/icon"
android:title="#string/title_i2"/>
<item
android:id="#+id/i3"
android:icon="#drawable/icon"
android:title="#string/title_i3"/>
<item
android:id="#+id/overflow"
android:icon="#drawable/ic_action_overflow"
android:orderInCategory="100"
android:showAsAction="always"
android:title="#string/title_menu">
<menu>
<item
android:id="#+id/i1"
android:icon="#drawable/icon"
android:title="#string/title_i1"/>
<item
android:id="#+id/i2"
android:icon="#drawable/icon"
android:title="#string/title_i2"/>
<item
android:id="#+id/i3"
android:icon="#drawable/icon"
android:title="#string/title_i3"/>
</menu>
</item>
I have been through all the posts here and the suggested solutions but nothing worked for me.I have already spent 2 days and on the verge of giving up Android. So here I am seeking help from you guys,any help you suggest is highly appreciated. Here is my code ,please let me know if I am missing anything.
build.gradle
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:appcompat-v7:21.0.3'
}
toolbar.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/colorPrimary"
app:titleTextAppearance="#android:color/black"
android:minHeight="?attr/actionBarSize" />
styles.xml
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<item name="colorPrimary">#color/colorPrimary</item>
<!-- Customize your theme here. -->
</style>
</resources>
colors.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="colorPrimary">#E64A19</color>
</resources>
activity_maim.xml
<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" tools:context=".MainActivity">
<include
android:id="#+id/tool_bar"
layout="#layout/toolbar"
/>
<TextView android:text="#string/hello_world" android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_below="#+id/tool_bar"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />``
</RelativeLayout>
Manifest file
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.toolbarexample" >
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme"
>
<activity
android:name=".MainActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
MainActivity.java
package com.example.toolbarexample;
import android.app.Activity;
import android.os.Bundle;
import android.support.v7.app.ActionBar;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Toast;
public class MainActivity extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
if (toolbar != null) {
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayShowHomeEnabled(true);
ActionBar actionBar = getSupportActionBar();
actionBar.setTitle("Groups history");
//also tested using
//this.setTitle("My ToolBar");
// toolbar.setTitle("My ToolBar");
}
}
}
You dont need to check if (toolbar != null), it always must be not null.
You dont need to set id to include layout <include layout="#layout/toolbar"/>. And you can calling R.id.toolbar.
You better use vertical orientation LinearLayout instead of RelativeLayout in your activity layout.
You better extends AppCompatActivity
I am complete newbie when it comes to Android applications. Please bear with me. I am trying to make an menu options bar but somehow the icons are not displaying.
I had looked for answers to this question on stackoverflow and the answer that I came across was "On Android 3.0+, the preferred approach for the options menu (a spillover menu in the action bar) will not show icons. If you have android targetSdkVersion of 11 or higher, icons will never show up in menus on Android 3.0+. The icons will show up if you promote an options menu item to be a toolbar button, and the icons will show up on Android 1.x/2.x devices." I am sorry but I am not sure what that means. Can someone please direct me in the right direction? Any help will be appreciated!
I am not sure what I am doing incorrectly. Here is my code so far.
MainActivity.java
package com.example.useoptionsmenu;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
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) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.about:
startActivity(new Intent(this, About.class));
return true;
case R.id.help:
startActivity(new Intent(this, Help.class));
return true;
default:
return super.onOptionsItemSelected(item);
}
}
}
main.xml
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="#+id/action_settings"
android:orderInCategory="100"
android:showAsAction="never"
android:title="#string/action_settings"/>
<item
android:id="#+id/about"
android:showAsAction="always"
android:orderInCategory="1"
android:icon="#drawable/ic_action_about"
android:title="About"/>
<item
android:id="#+id/help"
android:showAsAction="always"
android:orderInCategory="2"
android:icon="#drawable/ic_action_help"
android:title="Help"/>
</menu>
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.useoptionsmenu"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="18" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="com.example.useoptionsmenu.MainActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="com.example.useoptionsmenu.About"
android:label="#string/title_activity_about" >
</activity>
<activity
android:name="com.example.useoptionsmenu.Help"
android:label="#string/title_activity_help" >
</activity>
</application>
</manifest>
You have to add this to the item you want to display in the header:
android:showAsAction="ifRoom"
so that your menu will be:
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="#+id/action_settings"
android:orderInCategory="100"
android:showAsAction="ifRoom"
android:title="#string/action_settings"/>
<item
android:id="#+id/about"
android:showAsAction="ifRoom"
android:orderInCategory="1"
android:icon="#drawable/ic_action_about"
android:title="About"/>
<item
android:id="#+id/help"
android:showAsAction="ifRoom"
android:orderInCategory="2"
android:icon="#drawable/ic_action_help"
android:title="Help"/>
</menu>
remove the line android:showAsAction="ifRoom" from the item you won't display in your header.
i need to have a menu or an action bar that must be display when the user use the application but the problem is that the menu items do not show only ofter i click the menu button on the emulator can anyone help me ????
test.java
package com.example.test;
import com.example.test.R;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
public class test extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_test);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main_activity_menu, menu);
return true;
}
}
mani_activity_menu.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="#+id/menu_settings"
android:orderInCategory="1"
android:showAsAction="always"
android:title="settings"/>
<item
android:id="#+id/twitter"
android:orderInCategory="2"
android:showAsAction="always"
android:title="twitter"/>
<item
android:id="#+id/facebook"
android:orderInCategory="3"
android:showAsAction="always"
android:title="facebook"/>
<item
android:id="#+id/refresh"
android:title="refresh"
android:showAsAction="always"
android:orderInCategory="4"
/>
</menu>
Manifest
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.test"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="10"
android:targetSdkVersion="15" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.INTERNET" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name=".test"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
style.xml
<resources>
<!--
Base application theme, dependent on API level. This theme is replaced
by AppBaseTheme from res/values-vXX/styles.xml on newer devices.
-->
<style name="AppBaseTheme" parent="android:Theme.Light">
<!--
Theme customizations available in newer API levels can go in
res/values-vXX/styles.xml, while customizations related to
backward-compatibility can go here.
-->
</style>
<!-- Application theme. -->
<style name="AppTheme" parent="AppBaseTheme">
<!-- All customizations that are NOT specific to a particular API-level can go here. -->
</style>
</resources>
Tried your code . Its working without any problems
You have made spelling mistake at mani_activity_menu.xml ?
Add
setHasOptionsMenu(true); //in oncreate
item.setEnabled(false); //add this in oncreated option
package com.example.exammmm;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
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) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
(source: mediafire.com)
Read -----> http://developer.android.com/guide/topics/ui/actionbar.html