Reputation: 347
I am trying to add an option menu in actionbar to my existing app but it is not working. If I create a new project with "hello world" default app I can see the button in action bar. The onCreateOptionMenu() method seems never caught in debug with breakpoint, what's wrong ??
I'm working with API 14 on both app and this is my MainActivity code.
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
public class Principale extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.ecran_d_acceuil);
}
public boolean onCreateOptionMenu(Menu menu) {
getMenuInflater().inflate(R.menu.principale, menu);
return true;
}
}
My menu.xml code
<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"/>
</menu>
manifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.myapp"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="14"
android:targetSdkVersion="14" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.myapp.Principale"
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.myapp.Home"
android:label="@string/title_activity_home" >
</activity>
<activity
android:name="com.example.myapp.ListDir"
android:label="@string/title_activity_list_dir" >
</activity>
</application>
</manifest>
Upvotes: 25
Views: 49232
Reputation: 1
My Android studio version is 4.2.1 for Windows 64-bit (933 MiB). In my case, I did not have the "Show Layout Decorations" option in the design view. What I did is I solved my problem of the action bar by going on the layout XML file and then pressing on View Option -> Show System UI
ActionBar visibility in android studio:
Upvotes: -1
Reputation: 21
What solved my issue in this regard was, in the design view
, click the eye
icon (view options
) and select Show Layout Decorations
.
Upvotes: 2
Reputation: 71
For your information, I solved my problem of the actionBar by going on the layout XML file and then pressing on View Option -> Show Layout Decorations.
Hope it will solve your problem.
Cheers.
Upvotes: 7
Reputation: 563
Check Your manifest file In the Activity declaration if this line is there then delete it.
android:theme="@style/AppTheme.NoActionBar"
Upvotes: 3
Reputation: 1
getSupportActionBar().show();
Simply paste this code in your java onCreate function.
Upvotes: 0
Reputation: 240
your main activity needs to be like this:
import android.os.Bundle;
import android.app.ActionBar;
import android.app.Activity;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.Toast;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ActionBar actionBar = getActionBar();
actionBar.hide();
actionBar.show();
actionBar.setSubtitle("subtitle");
actionBar.setTitle("title");
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main, menu);
return true;
}
@Override public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.action_refresh:
Toast.makeText(this, "Menu Item 1 selected", Toast.LENGTH_SHORT)
.show();
break;
default:
break;
}
return true;
}
}
and your main.xml like this:
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="@+id/action_refresh"
android:orderInCategory="100"
android:showAsAction="always"
android:icon="@drawable/ic_action_search"
android:title="Refresh"/>
<item
android:id="@+id/action_settings"
android:title="Settings">
</item>
</menu>
this is a good and simple guide to creating an action bar: http://www.vogella.com/tutorials/AndroidActionBar/article.html
Upvotes: 2
Reputation: 91
Spent 3 hours and eventually fixed it! Forget removing android:theme="@style/AppTheme" from your manifest. It makes run-time error. This is how I fixed the problem. Your activity should extend AppCompatActivity instead of Activity.
Upvotes: 6
Reputation: 2897
Have you tried to set the app theme to a theme that supports the ActionBar
, like Theme.Holo.Light
?
Also, in your Activity
you can try getActionBar().show();
.
Upvotes: 15
Reputation: 1689
I had the same problem and I confirm that if you delete android:theme="@style/AppTheme"
from AndroidManifest.xml
, it will solve your problem.
Upvotes: 1
Reputation: 576
on create of your activity.
getSupportActionBar().setDisplayShowCustomEnabled(true);
getSupportActionBar().setDisplayShowHomeEnabled(false);
customNav = LayoutInflater.from(this).inflate(
R.layout.action_bar_customvieew, null);
getSupportActionBar().setCustomView(customNav);
Upvotes: 2
Reputation: 1
i am using Samsung young 2 as android emulator and i have the same problem ,i reslov this by removing
android:theme =""
from the manifest.xml and changing the theme to another theme support action bar
Upvotes: 0
Reputation: 141
You have extended Activity. Try extending ActionBarActivity in java code
Upvotes: 8
Reputation: 133
In my case Action bar menu was not visible when used public class MainActivity extends Activity { } but was visible when used public class MainActivity extends ActionBarActivity{ }
Upvotes: 1
Reputation: 469
Make sure you don't use:
requestWindowFeature(Window.FEATURE_NO_TITLE);
in your layout .xml file.
Upvotes: 1
Reputation: 6500
Principale extends ActionBarActivity should take care of the issue
Upvotes: 14
Reputation: 2417
you have to call the method setHasOptionsMenu(true); in the OnCreate Method
Upvotes: 0
Reputation: 5336
In my case, what fixed the issue, was the answer of this question; to remove
android:theme="@style/AppTheme"
from the tag in the Manifest.
Upvotes: 22