I am trying to show two icons in the actionbar, one of which is a menu with a few children. However, I am not able to. I have tested a few answers from other similar questions in SO but to no avail. Not even one icon is showing up. Here is the code. Thanks for your help.
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
tools:context="com.semantic.semanticOrganizer.sem.activities.HomeActivity" >
<item android:id="@+id/viewTags"
android:icon="@drawable/ic_action_labels"
app:showAsAction="always"
android:title="Tags"
/>
<item
android:id="@+id/a_More"
android:icon="@drawable/ic_more_vert_white_36dp"
app:showAsAction="always"
android:title="More">
<menu>
<item android:id="@+id/action_view_notes"
android:title="Notes"
android:orderInCategory="100"
app:showAsAction="never" />
<item android:id="@+id/action_view_habits"
android:title="Habits"
android:orderInCategory="100"
app:showAsAction="never" />
<item android:id="@+id/action_view_checklists"
android:title="Checklists"
android:orderInCategory="100"
app:showAsAction="never" />
<item android:id="@+id/action_view_tags"
android:title="Tags"
android:orderInCategory="100"
app:showAsAction="never" />
<item android:id="@+id/action_settings"
android:title="@string/action_settings"
android:orderInCategory="100"
app:showAsAction="never" />
</menu>
</item>
</menu>
Java Code
public class HomeActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
stuffWhichDoesNotInvolveGettingActionBar();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu items for use in the action bar
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.home, menu);
return super.onCreateOptionsMenu(menu);
}
@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();
if (id == R.id.action_settings) {
return true;
}
if (id == R.id.action_view_tags) {
return true;
}
if (id == R.id.action_view_notes) {
return true;
}
if (id == R.id.action_view_checklists) {
return true;
}
if (id == R.id.action_view_habits) {
return true;
}
return super.onOptionsItemSelected(item);
}
private void stuffWhichDoesNotInvolveGettingActionBar(){
//Code
}
Edit: As Neutrino suggested, I tried to shift to appCompat Theme. But then this error showed up on Win64 machine. Resource not found error to the attribute "actionModeShareDrawable". I changed the compileSdkVersion to 21 and it dint work. So, I might have to do it without AppCompat for now. I am currently using Holo. So please suggest a fix for Holo theme.
Edit I found a work around for the error I mentioned by using v20 of the support library instead of v21 since I found out that Gradle 2.3 build system is not around in Android yet and the bug was only fixed in that version (A Bug Fixed Jar of Groovy 2.3.5 was released). Then I was able to use ActionBarActivity and now the action bar icons are visible. Thanks everyone and especially Quark.
Extend ActionBarActivity
instead of Activity
, ie make
public class HomeActivity extends Activity
as
public class HomeActivity extends ActionBarActivity
ActionBarActivity
is now deprecated to support toolbar. To use toolbar, use -
public class HomeActivity extends AppCompatActivity
Be sure to include the latest version of support library in build.gradle (Module: app)
-
dependencies {
compile 'com.android.support:appcompat-v7:22.1.1'
}
Read more about toolbar and AppCompatActivity
here , here and here .
I tried compiling your code and the following worked for me. Replace app:showAsAction
with android:showAsAction
for all the menu items for which you want an icon to appear.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.