Sunday, July 8, 2012

How to create context menu in Android

Following steps required to create Context menu in android application. When user click long on any item present on the screen, we usually show option in terms of context menu.


  1. Register View for context menu.
  2. Prepare menu items.
  3. Define action on selecting the menu item.



Context menus are bind with the view, you need to get the view and register for context menu as shown following.



public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
TextView t=(TextView)findViewById(R.id.textView);
registerForContextMenu(t);
}



Prepare Menu Items


Override the "onCreateContextMenu" method of Activity class.

public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
menu.setHeaderTitle("Hello");
// Assuming that you already have an array in your string.xml
String[] menuItems = getResources().getStringArray(R.array.menu_options);
for (int i = 0; i < menuItems.length; i++) {
menu.add(Menu.NONE, i, i, menuItems[i]);
}
}


Define Action on selecting the menu item.


Override the "onContextItemSelected" method of Activity Class.

public boolean onContextItemSelected(MenuItem item) {
Toast.makeText(this, "Context menu item " + item.getItemId(), Toast.LENGTH_LONG).show();
return super.onContextItemSelected(item);
}




Landing page of the application



Long click on Hello World Text View



On Click at add menu item


Monday, June 25, 2012

jQuery autoComplete view all on click


$(function() {
    $('#id').autocomplete({
        source: ["option1","option2","option3"],
        minLength: 0
    }).click(function(){     
       $(this).autocomplete('search',"");
    });
});