xml file
======
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
    android:id="@+id/phone"
    android:title="phone"
    android:icon="@drawable/phone"
    android:showAsAction="ifRoom|withText"
/>
<item
    android:id="@+id/computer"
    android:title="computer"
    android:icon="@drawable/computer"
     android:showAsAction="ifRoom|withText"
/>
<item
    android:id="@+id/gamepad"
    android:title="gamepad"
    android:icon="@drawable/gamepad"
     android:showAsAction="ifRoom|withText"
/>
<item
    android:id="@+id/camera"
    android:title="camera"
    android:icon="@drawable/camera"
     android:showAsAction="ifRoom|withText"
/>
<item
    android:id="@+id/video"
    android:title="video"
    android:icon="@drawable/video"
      android:showAsAction="ifRoom|withText"
/>
<item
    android:id="@+id/email"
    android:title="email"
    android:icon="@drawable/email"
android:showAsAction="ifRoom|withText"
/>
 </menu>
java file
======
public class MainActivity extends Activity {
 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
  /*ActionBar actionBar=getActionBar();
  actionBar.hide();*/
       try {
          ViewConfiguration config = ViewConfiguration.get(this);
          Field menuKeyField =      ViewConfiguration.class.getDeclaredField("sHasPermanentMenuKey");
          if(menuKeyField != null) {
              menuKeyField.setAccessible(true);
              menuKeyField.setBoolean(config, false);
          }
      } catch (Exception e) {
          e.printStackTrace();
      }
 }
 @Override
 public boolean onCreateOptionsMenu(Menu menu) {
  // Inflate the menu; this adds items to the action bar if it is present.
  getMenuInflater().inflate(R.menu.action_bar_menu, menu);
  return true;
 }
 @Override
 public boolean onOptionsItemSelected(MenuItem item) {
   switch (item.getItemId()) {
   case R.id.phone:
    Toast.makeText(getApplicationContext(), "you clicked:"+item.getTitle(), Toast.LENGTH_SHORT).show(); 
    break;
   case R.id.computer:
    Toast.makeText(getApplicationContext(), "you clicked:"+item.getTitle(), Toast.LENGTH_SHORT).show(); 
    break;
   case R.id.gamepad:
    Toast.makeText(getApplicationContext(), "you clicked:"+item.getTitle(), Toast.LENGTH_SHORT).show(); 
    break;
   case R.id.camera:
     Toast.makeText(getApplicationContext(), "you clicked:"+item.getTitle(), Toast.LENGTH_SHORT).show(); 
     break;
   case R.id.video:
      Toast.makeText(getApplicationContext(), "you clicked:"+item.getTitle(), Toast.LENGTH_SHORT).show(); 
      break;
   case R.id.email:
      Toast.makeText(getApplicationContext(), "you clicked:"+item.getTitle(), Toast.LENGTH_SHORT).show(); 
      break;
   default:
    break;
   }
  return super.onOptionsItemSelected(item);
 }
}
manifest.xml
==========
<activity
android:name="com.example.actionbarexample.MainActivity"
android:label="@string/app_name"
android:uiOptions="splitActionBarWhenNarrow"
>


 
No comments:
Post a Comment