Defaults drawable(s) in Android

Recently I have begun to play with Android, for one of my project I wanted to use some default images embedded in the framework. I have found the page of the documentation with these images but there is no visual preview of images. So I have made a little application which displays all of these icons. I though it could be a good training and might be useful (for me at least).

Here is what the application will look like:

Image

There is nothing special about it just a simple GridView with a TextView and an ImageView for each row.

For this application only four files (in addition to standard ones) are required:

  • main.xml this will be our main layout which contains the GridView
  • DrawingTestActivity.java this will be our main activity
  • gridrow.xml this will be the layout of each row
  • GridAdapter.java which will be the adapter for the grid used
main.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >

 <!-- The grid view -->
 <GridView
    android:id="@+id/gridViewMain"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:numColumns="auto_fit">

</GridView>

</LinearLayout>

 

DrawingTestActivity.java:
package com.rphbck;

import android.app.Activity;
import android.os.Bundle;
import android.widget.GridView;

public class DrawingTestActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //We set the layout
        setContentView(R.layout.main);

        //We get the GridView by its id
        GridView gridview = (GridView) findViewById(R.id.gridViewMain);

        //We set the GridAdapter to the gridview
        gridview.setAdapter(new GridAdapter(this));
    }
}
We need to set an adapter to the GridView to tell it how to display. There are some standard ones but here we want to use our own.

 

gridrow.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >

 <TextView
    android:id="@+id/draw_name"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:paddingBottom="50dip"/>

 <LinearLayout android:id="@+id/thumbnail"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:padding="15dip"
    android:layout_alignParentLeft="true">

    <ImageView
        android:id="@+id/draw_image"
        android:layout_width="50dip"
        android:layout_height="50dip" />

 </LinearLayout>
</RelativeLayout>
We use for one row layout a TextView to display the name of the drawable property and an ImageView to display it as an image.

 

GridAdapter.java:
And finally we make our grid adapter
package com.rphbck;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class GridAdapter extends BaseAdapter {//Don't forget to extends BaseAdapter

 private Context mContext; //The context of the application
 private static LayoutInflater mInflater=null; //A helper for setting a layout from the code
 private ArrayList mThumbsLabels; //The list of our labels
 private ArrayList mThumbsValues; //The list of our image references

 public GridAdapter(Context c) {
  mContext = c;
  mThumbsLabels = new ArrayList();
  mThumbsValues = new ArrayList();

  //We populate our arrays
  for (Map.Entry entry : mThumbs.entrySet()) {
    mThumbsLabels.add(entry.getKey());
    mThumbsValues.add(entry.getValue());
  }
  //We get the layout manager
  mInflater = (LayoutInflater)mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
 }

 @Override
 public int getCount() {
  return mThumbs.size();
 }

 @Override
 public Object getItem(int arg0) {
  return null;
 }

 @Override
 public long getItemId(int arg0) {
  return 0;
 }

 //Here is our important function to set every row of the gridview
 @Override
 public View getView(int position, View convertView, ViewGroup parent) {
  String label = mThumbsLabels.get(position);//We get the current drawable name
  Integer value = mThumbsValues.get(position);//We get the current drawable value
  View vi=convertView;

  // if it's not recycled, initialize some attributes
  if(convertView==null) vi = mInflater.inflate(R.layout.gridrow, parent, false);
 //We search the text view and the image view
  TextView tvLabel = (TextView) vi.findViewById(R.id.draw_name);
  ImageView ivValue = (ImageView) vi.findViewById(R.id.draw_image);
 //And we set their attributes
  tvLabel.setText(label);
  ivValue.setImageResource(value);

  return vi;
 }

//Here is the list of default system drawables. The key is only for displaying purposes and the value is the reference to the drawable
 private HashMap mThumbs = new HashMap(){{
	put("android.R.drawable.alert_dark_frame", android.R.drawable.alert_dark_frame);
	put("android.R.drawable.alert_light_frame", android.R.drawable.alert_light_frame);
	put("android.R.drawable.arrow_down_float", android.R.drawable.arrow_down_float);
	put("android.R.drawable.arrow_up_float", android.R.drawable.arrow_up_float);
	put("android.R.drawable.bottom_bar", android.R.drawable.bottom_bar);
	put("android.R.drawable.btn_default", android.R.drawable.btn_default);
	put("android.R.drawable.btn_default_small", android.R.drawable.btn_default_small);
	put("android.R.drawable.btn_dialog", android.R.drawable.btn_dialog);
	put("android.R.drawable.btn_dropdown", android.R.drawable.btn_dropdown);
	put("android.R.drawable.btn_minus", android.R.drawable.btn_minus);
	put("android.R.drawable.btn_plus", android.R.drawable.btn_plus);
	put("android.R.drawable.btn_radio", android.R.drawable.btn_radio);
	put("android.R.drawable.btn_star", android.R.drawable.btn_star);
	put("android.R.drawable.btn_star_big_off", android.R.drawable.btn_star_big_off);
	put("android.R.drawable.btn_star_big_on", android.R.drawable.btn_star_big_on);
	put("android.R.drawable.button_onoff_indicator_off", android.R.drawable.button_onoff_indicator_off);
	put("android.R.drawable.button_onoff_indicator_on", android.R.drawable.button_onoff_indicator_on);
	put("android.R.drawable.checkbox_off_background", android.R.drawable.checkbox_off_background);
	put("android.R.drawable.checkbox_on_background", android.R.drawable.checkbox_on_background);
	put("android.R.drawable.dark_header", android.R.drawable.dark_header);
	put("android.R.drawable.dialog_frame", android.R.drawable.dialog_frame);
	put("android.R.drawable.dialog_holo_dark_frame", android.R.drawable.dialog_holo_dark_frame);
	put("android.R.drawable.dialog_holo_light_frame", android.R.drawable.dialog_holo_light_frame);
	put("android.R.drawable.divider_horizontal_bright", android.R.drawable.divider_horizontal_bright);
	put("android.R.drawable.divider_horizontal_dark", android.R.drawable.divider_horizontal_dark);
	put("android.R.drawable.divider_horizontal_dim_dark", android.R.drawable.divider_horizontal_dim_dark);
	put("android.R.drawable.divider_horizontal_textfield", android.R.drawable.divider_horizontal_textfield);
	put("android.R.drawable.edit_text", android.R.drawable.edit_text);
	put("android.R.drawable.editbox_background", android.R.drawable.editbox_background);
	put("android.R.drawable.editbox_background_normal", android.R.drawable.editbox_background_normal);
	put("android.R.drawable.editbox_dropdown_dark_frame", android.R.drawable.editbox_dropdown_dark_frame);
	put("android.R.drawable.editbox_dropdown_light_frame", android.R.drawable.editbox_dropdown_light_frame);
	put("android.R.drawable.gallery_thumb", android.R.drawable.gallery_thumb);
	put("android.R.drawable.ic_btn_speak_now", android.R.drawable.ic_btn_speak_now);
	put("android.R.drawable.ic_delete", android.R.drawable.ic_delete);
	put("android.R.drawable.ic_dialog_alert", android.R.drawable.ic_dialog_alert);
	put("android.R.drawable.ic_dialog_dialer", android.R.drawable.ic_dialog_dialer);
	put("android.R.drawable.ic_dialog_email", android.R.drawable.ic_dialog_email);
	put("android.R.drawable.ic_dialog_info", android.R.drawable.ic_dialog_info);
	put("android.R.drawable.ic_dialog_map", android.R.drawable.ic_dialog_map);
	put("android.R.drawable.ic_input_add", android.R.drawable.ic_input_add);
	put("android.R.drawable.ic_input_delete", android.R.drawable.ic_input_delete);
	put("android.R.drawable.ic_input_get", android.R.drawable.ic_input_get);
	put("android.R.drawable.ic_lock_idle_alarm", android.R.drawable.ic_lock_idle_alarm);
	put("android.R.drawable.ic_lock_idle_charging", android.R.drawable.ic_lock_idle_charging);
	put("android.R.drawable.ic_lock_idle_lock", android.R.drawable.ic_lock_idle_lock);
	put("android.R.drawable.ic_lock_idle_low_battery", android.R.drawable.ic_lock_idle_low_battery);
	put("android.R.drawable.ic_lock_lock", android.R.drawable.ic_lock_lock);
	put("android.R.drawable.ic_lock_power_off", android.R.drawable.ic_lock_power_off);
	put("android.R.drawable.ic_lock_silent_mode", android.R.drawable.ic_lock_silent_mode);
	put("android.R.drawable.ic_lock_silent_mode_off", android.R.drawable.ic_lock_silent_mode_off);
	put("android.R.drawable.ic_media_ff", android.R.drawable.ic_media_ff);
	put("android.R.drawable.ic_media_next", android.R.drawable.ic_media_next);
	put("android.R.drawable.ic_media_pause", android.R.drawable.ic_media_pause);
	put("android.R.drawable.ic_media_play", android.R.drawable.ic_media_play);
	put("android.R.drawable.ic_media_previous", android.R.drawable.ic_media_previous);
	put("android.R.drawable.ic_media_rew", android.R.drawable.ic_media_rew);
	put("android.R.drawable.ic_menu_add", android.R.drawable.ic_menu_add);
	put("android.R.drawable.ic_menu_agenda", android.R.drawable.ic_menu_agenda);
	put("android.R.drawable.ic_menu_always_landscape_portrait", android.R.drawable.ic_menu_always_landscape_portrait);
	put("android.R.drawable.ic_menu_call", android.R.drawable.ic_menu_call);
	put("android.R.drawable.ic_menu_camera", android.R.drawable.ic_menu_camera);
	put("android.R.drawable.ic_menu_close_clear_cancel", android.R.drawable.ic_menu_close_clear_cancel);
	put("android.R.drawable.ic_menu_compass", android.R.drawable.ic_menu_compass);
	put("android.R.drawable.ic_menu_crop", android.R.drawable.ic_menu_crop);
	put("android.R.drawable.ic_menu_day", android.R.drawable.ic_menu_day);
	put("android.R.drawable.ic_menu_delete", android.R.drawable.ic_menu_delete);
	put("android.R.drawable.ic_menu_directions", android.R.drawable.ic_menu_directions);
	put("android.R.drawable.ic_menu_edit", android.R.drawable.ic_menu_edit);
	put("android.R.drawable.ic_menu_gallery", android.R.drawable.ic_menu_gallery);
	put("android.R.drawable.ic_menu_help", android.R.drawable.ic_menu_help);
	put("android.R.drawable.ic_menu_info_details", android.R.drawable.ic_menu_info_details);
	put("android.R.drawable.ic_menu_manage", android.R.drawable.ic_menu_manage);
	put("android.R.drawable.ic_menu_mapmode", android.R.drawable.ic_menu_mapmode);
	put("android.R.drawable.ic_menu_month", android.R.drawable.ic_menu_month);
	put("android.R.drawable.ic_menu_more", android.R.drawable.ic_menu_more);
	put("android.R.drawable.ic_menu_my_calendar", android.R.drawable.ic_menu_my_calendar);
	put("android.R.drawable.ic_menu_mylocation", android.R.drawable.ic_menu_mylocation);
	put("android.R.drawable.ic_menu_myplaces", android.R.drawable.ic_menu_myplaces);
	put("android.R.drawable.ic_menu_preferences", android.R.drawable.ic_menu_preferences);
	put("android.R.drawable.ic_menu_recent_history", android.R.drawable.ic_menu_recent_history);
	put("android.R.drawable.ic_menu_report_image", android.R.drawable.ic_menu_report_image);
	put("android.R.drawable.ic_menu_revert", android.R.drawable.ic_menu_revert);
	put("android.R.drawable.ic_menu_rotate", android.R.drawable.ic_menu_rotate);
	put("android.R.drawable.ic_menu_save", android.R.drawable.ic_menu_save);
	put("android.R.drawable.ic_menu_search", android.R.drawable.ic_menu_search);
	put("android.R.drawable.ic_menu_send", android.R.drawable.ic_menu_send);
	put("android.R.drawable.ic_menu_set_as", android.R.drawable.ic_menu_set_as);
	put("android.R.drawable.ic_menu_share", android.R.drawable.ic_menu_share);
	put("android.R.drawable.ic_menu_slideshow", android.R.drawable.ic_menu_slideshow);
	put("android.R.drawable.ic_menu_sort_alphabetically", android.R.drawable.ic_menu_sort_alphabetically);
	put("android.R.drawable.ic_menu_sort_by_size", android.R.drawable.ic_menu_sort_by_size);
	put("android.R.drawable.ic_menu_today", android.R.drawable.ic_menu_today);
	put("android.R.drawable.ic_menu_upload", android.R.drawable.ic_menu_upload);
	put("android.R.drawable.ic_menu_upload_you_tube", android.R.drawable.ic_menu_upload_you_tube);
	put("android.R.drawable.ic_menu_view", android.R.drawable.ic_menu_view);
	put("android.R.drawable.ic_menu_week", android.R.drawable.ic_menu_week);
	put("android.R.drawable.ic_menu_zoom", android.R.drawable.ic_menu_zoom);
	put("android.R.drawable.ic_notification_clear_all", android.R.drawable.ic_notification_clear_all);
	put("android.R.drawable.ic_notification_overlay", android.R.drawable.ic_notification_overlay);
	put("android.R.drawable.ic_partial_secure", android.R.drawable.ic_partial_secure);
	put("android.R.drawable.ic_popup_disk_full", android.R.drawable.ic_popup_disk_full);
	put("android.R.drawable.ic_popup_reminder", android.R.drawable.ic_popup_reminder);
	put("android.R.drawable.ic_popup_sync", android.R.drawable.ic_popup_sync);
	put("android.R.drawable.ic_search_category_default", android.R.drawable.ic_search_category_default);
	put("android.R.drawable.ic_secure", android.R.drawable.ic_secure);
	put("android.R.drawable.list_selector_background", android.R.drawable.list_selector_background);
	put("android.R.drawable.menu_frame", android.R.drawable.menu_frame);
	put("android.R.drawable.menu_full_frame", android.R.drawable.menu_full_frame);
	put("android.R.drawable.menuitem_background", android.R.drawable.menuitem_background);
	put("android.R.drawable.picture_frame", android.R.drawable.picture_frame);
	put("android.R.drawable.presence_audio_away", android.R.drawable.presence_audio_away);
	put("android.R.drawable.presence_audio_busy", android.R.drawable.presence_audio_busy);
	put("android.R.drawable.presence_audio_online", android.R.drawable.presence_audio_online);
	put("android.R.drawable.presence_away", android.R.drawable.presence_away);
	put("android.R.drawable.presence_busy", android.R.drawable.presence_busy);
	put("android.R.drawable.presence_invisible", android.R.drawable.presence_invisible);
	put("android.R.drawable.presence_offline", android.R.drawable.presence_offline);
	put("android.R.drawable.presence_online", android.R.drawable.presence_online);
	put("android.R.drawable.presence_video_away", android.R.drawable.presence_video_away);
	put("android.R.drawable.presence_video_busy", android.R.drawable.presence_video_busy);
	put("android.R.drawable.presence_video_online", android.R.drawable.presence_video_online);
	put("android.R.drawable.progress_horizontal", android.R.drawable.progress_horizontal);
	put("android.R.drawable.progress_indeterminate_horizontal", android.R.drawable.progress_indeterminate_horizontal);
	put("android.R.drawable.radiobutton_off_background", android.R.drawable.radiobutton_off_background);
	put("android.R.drawable.radiobutton_on_background", android.R.drawable.radiobutton_on_background);
	put("android.R.drawable.screen_background_dark", android.R.drawable.screen_background_dark);
	put("android.R.drawable.screen_background_dark_transparent", android.R.drawable.screen_background_dark_transparent);
	put("android.R.drawable.screen_background_light", android.R.drawable.screen_background_light);
	put("android.R.drawable.screen_background_light_transparent", android.R.drawable.screen_background_light_transparent);
	put("android.R.drawable.spinner_background", android.R.drawable.spinner_background);
	put("android.R.drawable.spinner_dropdown_background", android.R.drawable.spinner_dropdown_background);
	put("android.R.drawable.star_big_off", android.R.drawable.star_big_off);
	put("android.R.drawable.star_big_on", android.R.drawable.star_big_on);
	put("android.R.drawable.star_off", android.R.drawable.star_off);
	put("android.R.drawable.star_on", android.R.drawable.star_on);
	put("android.R.drawable.stat_notify_call_mute", android.R.drawable.stat_notify_call_mute);
	put("android.R.drawable.stat_notify_chat", android.R.drawable.stat_notify_chat);
	put("android.R.drawable.stat_notify_error", android.R.drawable.stat_notify_error);
	put("android.R.drawable.stat_notify_missed_call", android.R.drawable.stat_notify_missed_call);
	put("android.R.drawable.stat_notify_more", android.R.drawable.stat_notify_more);
	put("android.R.drawable.stat_notify_sdcard", android.R.drawable.stat_notify_sdcard);
	put("android.R.drawable.stat_notify_sdcard_prepare", android.R.drawable.stat_notify_sdcard_prepare);
	put("android.R.drawable.stat_notify_sdcard_usb", android.R.drawable.stat_notify_sdcard_usb);
	put("android.R.drawable.stat_notify_sync", android.R.drawable.stat_notify_sync);
	put("android.R.drawable.stat_notify_sync_noanim", android.R.drawable.stat_notify_sync_noanim);
	put("android.R.drawable.stat_notify_voicemail", android.R.drawable.stat_notify_voicemail);
	put("android.R.drawable.stat_sys_data_bluetooth", android.R.drawable.stat_sys_data_bluetooth);
	put("android.R.drawable.stat_sys_download", android.R.drawable.stat_sys_download);
	put("android.R.drawable.stat_sys_download_done", android.R.drawable.stat_sys_download_done);
	put("android.R.drawable.stat_sys_headset", android.R.drawable.stat_sys_headset);
	put("android.R.drawable.stat_sys_phone_call", android.R.drawable.stat_sys_phone_call);
	put("android.R.drawable.stat_sys_phone_call_forward", android.R.drawable.stat_sys_phone_call_forward);
	put("android.R.drawable.stat_sys_phone_call_on_hold", android.R.drawable.stat_sys_phone_call_on_hold);
	put("android.R.drawable.stat_sys_speakerphone", android.R.drawable.stat_sys_speakerphone);
	put("android.R.drawable.stat_sys_upload", android.R.drawable.stat_sys_upload);
	put("android.R.drawable.stat_sys_upload_done", android.R.drawable.stat_sys_upload_done);
	put("android.R.drawable.stat_sys_vp_phone_call", android.R.drawable.stat_sys_vp_phone_call);
	put("android.R.drawable.stat_sys_vp_phone_call_on_hold", android.R.drawable.stat_sys_vp_phone_call_on_hold);
	put("android.R.drawable.stat_sys_warning", android.R.drawable.stat_sys_warning);
	put("android.R.drawable.status_bar_item_app_background", android.R.drawable.status_bar_item_app_background);
	put("android.R.drawable.status_bar_item_background", android.R.drawable.status_bar_item_background);
	put("android.R.drawable.sym_action_call", android.R.drawable.sym_action_call);
	put("android.R.drawable.sym_action_chat", android.R.drawable.sym_action_chat);
	put("android.R.drawable.sym_action_email", android.R.drawable.sym_action_email);
	put("android.R.drawable.sym_call_incoming", android.R.drawable.sym_call_incoming);
	put("android.R.drawable.sym_call_missed", android.R.drawable.sym_call_missed);
	put("android.R.drawable.sym_call_outgoing", android.R.drawable.sym_call_outgoing);
	put("android.R.drawable.sym_contact_card", android.R.drawable.sym_contact_card);
	put("android.R.drawable.sym_def_app_icon", android.R.drawable.sym_def_app_icon);
	put("android.R.drawable.title_bar", android.R.drawable.title_bar);
	put("android.R.drawable.title_bar_tall", android.R.drawable.title_bar_tall);
	put("android.R.drawable.toast_frame", android.R.drawable.toast_frame);
	put("android.R.drawable.zoom_plate", android.R.drawable.zoom_plate);
 }};

}
You can find the final (unsigned) apk here, just think to remove the png extension that I’ve added because of wordpress.
Advertisements
This entry was posted in Android, tutorial and tagged , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s