📜 ⬆️ ⬇️

Android dialog with iconized menu

Some time ago I was fascinated by the idea of ​​developing applications for the Android platform. In order not to study the platform on simple hello-world programs, I decided to do something that would allow me to get comfortable with the UI part of the framework, work with the database, the network and social services.
The idea was invented to the insane simple and I would even say stupid. And when I started doing something, then I suddenly wanted to make a beautiful dialog box with a selection of a menu item with icons. Such a dialog is present in the standard Android, for example, a long tap on the desktop opens a dialog to select the content to be added (widget, wallpaper, etc.). So, welcome under the cat ...


As mentioned above, I had the task to make a dialog box like this:

Googling for some time and reading the official docks on the platform, I still could not find how to implement such a dialogue. After some time digging, I found a result that would be the fastest obvious to experienced Android developers.
The answer was simple and it lay on the surface. Its essence lies in the fact that for a dialog builder you just need to substitute the appropriate data provider (naturally, you need to write the provider yourself).
So, now there will be some amount of code with comments about what is happening.

List (provider) of possible types of accounts


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

This is a simple wrapper around a list of possible account types. The account type here is a simple POJO class, which consists of an identifier (constant), the name of the service and the identifier of the resource icon for this type of service.
')

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


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


First you need to pass to the designer a list of types with which the list adapter will work. The binding of a specific item to the layout occurs in the overridden getView () method. It loads the layout from the specified resource, retrieves widgets and records information about a specific item in the list. By the way, the index of this element is automatically available through the position parameter.

This adapter sheet works with 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. < ImageView android: id = "@ + id / icon_item" android: layout_width = "wrap_content"
  6. android: layout_height = "fill_parent" />
  7. < TextView android: id = "@ + id / text_item" android: layout_width = "wrap_content"
  8. android: layout_height = "fill_parent" android: paddingLeft = "10px"
  9. android: paddingTop = "5px" android: textStyle = "bold"
  10. android: textColor = "# 000000" />
  11. </ LinearLayout >
* This source code was highlighted with Source Code Highlighter .


All that is now left is to bind the developed adapter sheet to a specific dialog box.
  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 in the right place activity dialogue
  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 .


The dialog after its completion returns the index of the selected item by the selectedItemId parameter to the listener that was specified when the dialog was called. In this simple case, this index will match the account type ID (item'a in the list), so no additional conversions or extracts are required. For my task, this index is more than enough.

In the end, I got such a nice dialogue


Maybe I just described simple truths in this topic, maybe not. To decide, as they say, to you, Habrayuzer ...

Thank you and good luck to everyone in the exciting development process for the Android platform;)

Source: https://habr.com/ru/post/103347/


All Articles