Monday, December 2, 2013

OptionMenu+PopUpMenu+ContextMenu+SubMenu

Menu
-------
main.xml
contextmenu.xml
-----------------
main.xml
---------
<menu xmlns:android="http://schemas.android.com/apk/res/android" >

    <item
        android:id="@+id/action_settings"
        android:orderInCategory="100"
        android:showAsAction="never"
        
        android:title="@string/action_settings">
       <menu>  // sub menu
            <item android:id="@+id/create_new"
                  android:title="New" />
            <item android:id="@+id/open"
                  android:title="Open" />
        </menu>
        </item>
    <item
        android:id="@+id/action_music"
        android:orderInCategory="100"
        android:showAsAction="always|withText"
       
        android:title="@string/action_music"/>
    <item
        android:id="@+id/action_game"
        android:orderInCategory="100"
        android:showAsAction="always|withText"
       
        android:title="@string/action_games"/>

</menu>

contextmenu.xml
------------------
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
    <item
        android:id="@+id/action_edit"       
        android:title="@string/action_edit"/>
    <item
        android:id="@+id/action_delete"        
        android:title="@string/action_delete"/>
    <item
        android:id="@+id/action_rename"        
        android:title="@string/action_rename"/>

</menu>

MainActivity.java
-------------------


public class MainActivity extends Activity {
Button btn;
ListView lv;
    String[]name={"Android","IOS","Symbion"};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
 btn=(Button) findViewById(R.id.button1 );
 lv=(ListView) findViewById(R.id.listView1);
 ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,name);
 lv.setAdapter(adapter);
 registerForContextMenu(lv);
 btn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
PopupMenu popup = new PopupMenu(getApplicationContext(), v);
   //MenuInflater inflater = popup.getMenuInflater();
   getMenuInflater().inflate(R.menu.contextmenu, popup.getMenu());
   popup.show();
}
});
 
 
}


@Override
public void onCreateContextMenu(ContextMenu menu, View v,
ContextMenuInfo menuInfo) {
// TODO Auto-generated method stub
super.onCreateContextMenu(menu, v, menuInfo);
getMenuInflater().inflate(R.menu.contextmenu, menu);
}



@Override
public boolean onContextItemSelected(MenuItem item) {
// TODO Auto-generated method stub
switch(item.getItemId()){
case R.id.action_edit:
Toast toast=Toast.makeText(getApplicationContext(), item.getTitle() , Toast.LENGTH_SHORT);
toast.setGravity(Gravity.CENTER_HORIZONTAL, 50, 50);
toast.show();
break;
case R.id.action_delete:
Toast toast1=Toast.makeText(getApplicationContext(), item.getTitle() , Toast.LENGTH_SHORT);
toast1.setGravity(Gravity.CENTER_HORIZONTAL, 0, 0);
toast1.show();
break;
case R.id.action_rename:
Toast toast2=Toast.makeText(getApplicationContext(), item.getTitle() , Toast.LENGTH_SHORT);
toast2.setGravity(Gravity.CENTER_HORIZONTAL, 0, 0);
toast2.show();
break;
}
return super.onContextItemSelected(item);
}



@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 onOptionsItemSelected(MenuItem item) {
// TODO Auto-generated method stub
switch(item.getItemId()){
case R.id.action_settings:
Toast toast=Toast.makeText(getApplicationContext(), item.getTitle() , Toast.LENGTH_SHORT);
toast.setGravity(Gravity.CENTER_HORIZONTAL, 50, 50);
toast.show();
break;
case R.id.action_music:
Toast toast1=Toast.makeText(getApplicationContext(), item.getTitle() , Toast.LENGTH_SHORT);
toast1.setGravity(Gravity.CENTER_HORIZONTAL, 50, 50);
toast1.show();
break;
case R.id.action_game:
Toast toast2=Toast.makeText(getApplicationContext(), item.getTitle() , Toast.LENGTH_SHORT);
toast2.setGravity(Gravity.CENTER_HORIZONTAL, 50, 50);
toast2.show();
break;
}
return super.onOptionsItemSelected(item);
}
}


No comments:

Post a Comment