Reputation: 373
I am trying to add an action item
to the action bar
but the action item's icon is not showing.
XML file res/menu/actionbar:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:id="@+id/action_search"
android:icon="@drawable/ic_action_search"
android:title="test"
android:showAsAction="always" />
<item android:id="@+id/action_settings"
android:title="@string/action_settings"
android:showAsAction="never" />"
</menu>
Adding the action bar
import android.support.v7.app.ActionBarActivity;
public class MainActivity extends ActionBarActivity {
@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.
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.actionbar, 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();
switch(id)
{
case R.id.action_search:
Context context = getApplicationContext();
CharSequence text = "Action Button Pressed!";
int duration = Toast.LENGTH_SHORT;
Toast toast = Toast.makeText(context, text, duration);
toast.show();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
}
I have the minimum sdk
in the manifest file
set to 7
<uses-sdk android:minSdkVersion="7" />
Upvotes: 0
Views: 105
Reputation: 3112
try this for your menu 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" >
<item android:id="@+id/action_search"
android:icon="@drawable/ic_action_search"
android:title="test"
app:showAsAction="always" />
<item android:id="@+id/action_settings"
android:title="@string/action_settings"
app:showAsAction="never" />"
</menu>
Upvotes: 1