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 a
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();
.
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.
Check Your manifest file In the Activity declaration if this line is there then delete it.
android:theme="@style/AppTheme.NoActionBar"
Make sure you don't use:
requestWindowFeature(Window.FEATURE_NO_TITLE);
in your layout .xml file.
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
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{ }