menu.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="com.example.mydemoactionbar.MainActivity" >
<item
android:id="@+id/action_settings"
android:orderInCategory="100"
android:title="@string/action_settings"
app:showAsAction="never"/>
<item
android:id="@+id/item1"
android:icon="@drawable/ic_checkin"
android:orderInCategory="1"
android:title="@string/item_1"
app:showAsAction="ifRoom|withText"/>
<item
android:id="@+id/item2"
android:icon="@drawable/ic_latitude"
android:orderInCategory="2"
android:title="@string/item_2"
app:showAsAction="ifRoom|withText"/>
<item
android:id="@+id/item3"
android:icon="@drawable/ic_location"
android:orderInCategory="3"
android:title="@string/item_3"
app:showAsAction="ifRoom|withText"/>
</menu>
MainActivity.class
@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;
}
@Override
public boolean onMenuOpened(int featureId, Menu menu)
{
if(featureId == Window.FEATURE_ACTION_BAR && menu != null){
if(menu.getClass().getSimpleName().equals("MenuBuilder")){
try{
Method m = menu.getClass().getDeclaredMethod("setOptionalIconsVisible", Boolean.TYPE);
m.setAccessible(true);
m.invoke(menu, true);
}
catch(NoSuchMethodException e){
String TAG = null;
Log.e(TAG, "onMenuOpened", e);
}
catch(Exception e){
throw new RuntimeException(e);
}
}
}
return super.onMenuOpened(featureId, 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.
switch (item.getItemId()) {
case R.id.item1:
Intent i1 = new Intent(MainActivity.this,MainActivity.class);
startActivity(i1);
break;
case R.id.item2:
Intent i2 = new Intent(MainActivity.this,MainActivity.class);
startActivity(i2);
break;
case R.id.item3:
Intent i3 = new Intent(MainActivity.this,MainActivity.class);
startActivity(i3);
break;
}
return super.onOptionsItemSelected(item);
}
Output :
By Sravankumar
<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="com.example.mydemoactionbar.MainActivity" >
<item
android:id="@+id/action_settings"
android:orderInCategory="100"
android:title="@string/action_settings"
app:showAsAction="never"/>
<item
android:id="@+id/item1"
android:icon="@drawable/ic_checkin"
android:orderInCategory="1"
android:title="@string/item_1"
app:showAsAction="ifRoom|withText"/>
<item
android:id="@+id/item2"
android:icon="@drawable/ic_latitude"
android:orderInCategory="2"
android:title="@string/item_2"
app:showAsAction="ifRoom|withText"/>
<item
android:id="@+id/item3"
android:icon="@drawable/ic_location"
android:orderInCategory="3"
android:title="@string/item_3"
app:showAsAction="ifRoom|withText"/>
</menu>
MainActivity.class
@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;
}
@Override
public boolean onMenuOpened(int featureId, Menu menu)
{
if(featureId == Window.FEATURE_ACTION_BAR && menu != null){
if(menu.getClass().getSimpleName().equals("MenuBuilder")){
try{
Method m = menu.getClass().getDeclaredMethod("setOptionalIconsVisible", Boolean.TYPE);
m.setAccessible(true);
m.invoke(menu, true);
}
catch(NoSuchMethodException e){
String TAG = null;
Log.e(TAG, "onMenuOpened", e);
}
catch(Exception e){
throw new RuntimeException(e);
}
}
}
return super.onMenuOpened(featureId, 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.
switch (item.getItemId()) {
case R.id.item1:
Intent i1 = new Intent(MainActivity.this,MainActivity.class);
startActivity(i1);
break;
case R.id.item2:
Intent i2 = new Intent(MainActivity.this,MainActivity.class);
startActivity(i2);
break;
case R.id.item3:
Intent i3 = new Intent(MainActivity.this,MainActivity.class);
startActivity(i3);
break;
}
return super.onOptionsItemSelected(item);
}
Output :
By Sravankumar