Android dialog box with an “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, network and social services.
The idea was thought up before stupefaction is simple and I would even say stupid. And when I started to do something, then I suddenly wanted to make a beautiful dialog box with a choice of a menu item with icons. Such a dialogue is present in the standard Android, for example, a long tap on the desktop opens a dialog for selecting the content to be added (widget, wallpaper, etc.). So, welcome to the cat ...
As mentioned above, I had the task of making a dialog box like this:
After 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 most likely be obvious to experienced Android developers.
The answer was simple and lay on the surface. Its essence is that for a dialogue builder you just need to substitute the appropriate data provider (naturally, the provider needs to be written by yourself).
So, now there will be some code with comments about what is happening.
This is a simple wrapper around the list of possible account types. The type of account here is a simple POJO class, which consists of an identifier (constant), a service name and an icon resource identifier for this type of service.
First you need to pass the list of types with which the list adapter will work to the designer. The binding of a specific item to the layout occurs in the redefined getView () method. It loads the layout from the specified resource, extracts the widgets and writes data about a specific list item in them. By the way, the index of this element is automatically available through the position parameter.
All that remains now is to bind the developed adapter sheet to a specific dialog box
and call in the right place activity dialogue
The dialog after its completion will return the index of the selected item with the selectedItemId parameter to the listener that is specified when the dialog is called. In this simple case, this index will coincide with the account type ID (item in the list), therefore, no additional transformations or extractions are required. For my task, this index is more than enough.
As a result, I got such a nice dialogue.
Maybe I just described common truths in this topic, maybe not. To decide, as they say, to you, Habrausers ...
Thank you and good luck to everyone in the exciting development process for the Android platform;)
The idea was thought up before stupefaction is simple and I would even say stupid. And when I started to do something, then I suddenly wanted to make a beautiful dialog box with a choice of a menu item with icons. Such a dialogue is present in the standard Android, for example, a long tap on the desktop opens a dialog for selecting the content to be added (widget, wallpaper, etc.). So, welcome to the cat ...
As mentioned above, I had the task of making a dialog box like this:
After 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 most likely be obvious to experienced Android developers.
The answer was simple and lay on the surface. Its essence is that for a dialogue builder you just need to substitute the appropriate data provider (naturally, the provider needs to be written by yourself).
So, now there will be some code with comments about what is happening.
List (provider) of possible account types
- public final class AccountTypesProvider {
- public static List
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 the list of possible account types. The type of account here is a simple POJO class, which consists of an identifier (constant), a service name and an icon resource identifier for this type of service.
ListAdapter for displaying a list of account types in a given layout
- public final class AccountsTypesListAdapter extends ArrayAdapter
{ - private Activity context;
- private List
accountTypes; -
- public AccountsTypesListAdapter(Activity context, List
accountTypes) { - super(context, R.layout.select_account_item, accountTypes);
-
- this.context = context;
- this.accountTypes = accountTypes;
- }
-
- @Override
- public View getView(int position, View convertView, ViewGroup parent) {
- LayoutInflater inflater = context.getLayoutInflater();
- View row = inflater.inflate(R.layout.select_account_item, parent, false);
-
- TextView label = (TextView) row.findViewById(R.id.text_item);
- label.setText(accountTypes.get(position).title);
-
- ImageView icon = (ImageView) row.findViewById(R.id.icon_item);
- icon.setImageResource(accountTypes.get(position).bigIconId);
-
- return row;
- }
- }
* This source code was highlighted with Source Code Highlighter.
First you need to pass the list of types with which the list adapter will work to the designer. The binding of a specific item to the layout occurs in the redefined getView () method. It loads the layout from the specified resource, extracts the widgets and writes data about a specific list item in them. By the way, the index of this element is automatically available through the position parameter.
This adapter sheet works with such a layout
- android:orientation="horizontal" android:layout_width="fill_parent"
- android:layout_height="fill_parent" android:padding="10px">
-
-
- android:layout_height="fill_parent"/>
-
- android:layout_height="fill_parent" android:paddingLeft="10px"
- android:paddingTop="5px" android:textStyle="bold"
- android:textColor="#000000"/>
* This source code was highlighted with Source Code Highlighter.
All that remains now is to bind the developed adapter sheet to a specific dialog box
- public static void showSelectAccountTypeDialog(Activity context, String title, OnClickListener dialogListener) {
- AlertDialog.Builder builder = new AlertDialog.Builder(context);
- builder.setTitle(title);
- builder.setAdapter(new AccountsTypesListAdapter(context, AccountTypesProvider.accountTypes), dialogListener);
- builder.create().show();
- }
* This source code was highlighted with Source Code Highlighter.
and call in the right place activity dialogue
- private void displaySelectAccountTypeDialog() {
- ApplicationDialogs.showSelectAccountTypeDialog(this, "Select network", new OnClickListener() {
- @Override
- public void onClick(DialogInterface dialogInterface, int selectedItemId) {
- setupAccount(selectedItemId);
- }
- });
- }
* This source code was highlighted with Source Code Highlighter.
The dialog after its completion will return the index of the selected item with the selectedItemId parameter to the listener that is specified when the dialog is called. In this simple case, this index will coincide with the account type ID (item in the list), therefore, no additional transformations or extractions are required. For my task, this index is more than enough.
As a result, I got such a nice dialogue.
Maybe I just described common truths in this topic, maybe not. To decide, as they say, to you, Habrausers ...
Thank you and good luck to everyone in the exciting development process for the Android platform;)