Android dialog box with icons in the menu

The idea was conceived Insanely simple and I would even say stupidAnd when I startedto do something here that I wanted to do dramatically beautiful dialog box by selecting themenu icons. Such a dialogue is present in the standard Android, for example, a long tapon the desktop opens the dialog to add content (widget oboiny, etc.). So, welcome to acat ...


As mentioned above, in front of me was to make a dialog box like this:



Googling for a while and reading the official docks on the platform I could not find how to implement such a dialogue. After some time of digging, I found a result that will most quickly apparent to experienced Android developers.
The answer was simple and he was lying on the surface. Its essence is that for the dialogyou just need to substitute the Builder the appropriate data provider (of course, the provider must write themselves.)
So now there will be some amount of code with comments about what's going on.

The list of (ISP) the possible types of accounts



  1. public final class AccountTypesProvider {
  2.   public static List<AccountType> accountTypes = Collections.unmodifiableList(Arrays.asList(
  3.       new AccountType(AccountType.TWITTER_ACCOUNT, "Twitter", R.drawable.twitter_icon_big),
  4.       new AccountType(AccountType.FACEBOOK_ACCOUNT, "Facebook", R.drawable.facebook_icon_big),
  5.       new AccountType(AccountType.BUZZ_ACCOUNT, "Google Buzz", R.drawable.buzz_icon_big),
  6.       new AccountType(AccountType.LINKEDIN_ACCOUNT, "LinkedIn", R.drawable.linkedin_icon_big),
  7.       new AccountType(AccountType.VKONTAKTE_ACOUNT, "ВКонтакте", R.drawable.vkontakte_icon_big)
  8.   ));
  9. }
* This source code was highlighted with Source Code Highlighter.

This is a simple wrapper around a list of possible types of accounts. Account Type is a simple POJO class that consists of an identifier (a constant), the name of the service andthe resource identifier icons for this type of service.

ListAdapter to display a list of account types in a given layout'e



  1. public final class AccountsTypesListAdapter extends ArrayAdapter<AccountType> {
  2.   private Activity context;
  3.   private List<AccountType> accountTypes;
  4.   
  5.   public AccountsTypesListAdapter(Activity context, List<AccountType> accountTypes) {
  6.     super(context, R.layout.select_account_item, accountTypes);
  7.     
  8.     this.context = context;
  9.     this.accountTypes = accountTypes;
  10.   }
  11.   
  12.   @Override
  13.   public View getView(int position, View convertView, ViewGroup parent) {
  14.     LayoutInflater inflater = context.getLayoutInflater();
  15.     View row = inflater.inflate(R.layout.select_account_item, parent, false);
  16.     
  17.     TextView label = (TextView) row.findViewById(R.id.text_item);
  18.     label.setText(accountTypes.get(position).title);
  19.     
  20.     ImageView icon = (ImageView) row.findViewById(R.id.icon_item);
  21.     icon.setImageResource(accountTypes.get(position).bigIconId);
  22.     
  23.     return row;
  24.   }
  25. }
* This source code was highlighted with Source Code Highlighter.

First you need to pass the constructor a list of types that will work with the adapter list.Binding to specific item'a leyautu occurs in the overridden method getView (). It is loadedleyaut of the specified resource is retrieved widgets and they recorded data on a particular element of the list. Incidentally, the index of that element is automatically available through the option position.

This list adapter works with just such a layout



  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3.   android:orientation="horizontal" android:layout_width="fill_parent"
  4.   android:layout_height="fill_parent" android:padding="10px">
  5.   
  6.   <ImageView android:id="@+id/icon_item" android:layout_width="wrap_content"
  7.     android:layout_height="fill_parent"/>
  8.     <TextView android:id="@+id/text_item" android:layout_width="wrap_content"
  9.       android:layout_height="fill_parent" android:paddingLeft="10px"
  10.       android:paddingTop="5px" android:textStyle="bold"
  11.       android:textColor="#000000"/>
  12. </LinearLayout>
* This source code was highlighted with Source Code Highlighter.

All that now remains - is designed to tie a sheet-adapter-specific dialog

  1. public static void showSelectAccountTypeDialog(Activity context, String title, OnClickListener dialogListener) {
  2.     AlertDialog.Builder builder = new AlertDialog.Builder(context);
  3.     builder.setTitle(title);
  4.     builder.setAdapter(new AccountsTypesListAdapter(context, AccountTypesProvider.accountTypes), dialogListener);
  5.     builder.create().show();
  6.   }
* This source code was highlighted with Source Code Highlighter.

and call activity dialogue in the right place

  1. private void displaySelectAccountTypeDialog() {
  2.     ApplicationDialogs.showSelectAccountTypeDialog(this"Select network"new OnClickListener() {
  3.       @Override
  4.       public void onClick(DialogInterface dialogInterface, int selectedItemId) {
  5.         setupAccount(selectedItemId);
  6.       }
  7.     });
  8.   }
* This source code was highlighted with Source Code Highlighter.

Dialogue after its completion will return the index of the parameter item'a selectedItemIdin the listener, which is specified when calling the dialogue. In this simple case, this indexcoincides with the ID type of account (item'a in the list), so no additional changes orextractions are required. For my problem the index is more than enough.
In the end, I turned a nice dialogue