アンドロイドには標準アイコンがあります。アイコン一覧から探せばわざわざアイコンを作ったりしなくてもいいのと、ユーザーからみてもアイコンにAndroidで共通の意味があれば便利です。
2021.2.1
Android標準アイコン
標準アイコンを使うメリットは、Androidアプリとして標準的なアイコンを提示することにより、ユーザーが暗黙的に意味を素早く理解できることです。
またアイコンリストは結構たくさんあるのでGirdViewで表示できるようにしてみました。
標準アイコンの呼び出し
画像を用意しませんが、それぞれ特別な呼び出しをします。
javaコードで記述する場合: android.R.drawable.XXX
1 |
image.setImageResource(android.R.drawable.ic_input_add); |
xmlレイアウトに記述する場合:
1 |
android:src="@android:drawable/alert_dark_frame" |
まとめるとこうなります。
MainActivity.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
import android.app.Activity; import android.os.Bundle; import android.widget.ImageView; public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // コードで指定する場合 ImageView image = findViewById(R.id.icon_1); image.setImageResource(android.R.drawable.ic_input_add); } } |
レイアウトです
activity_main.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:gravity="center" tools:context=".MainActivity"> <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_margin="10dp" android:src="@android:drawable/alert_dark_frame" /> <ImageView android:id="@+id/icon_1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_margin="10dp" /> </LinearLayout> |
GridView で一覧表示
にあるアイコンをリストアップしたアプリを作ってみましょう。
アイコンなのでGridViewがいいでしょう。またアイコンをタップすると名前が表示されるように設計してみます。
アイコンの数が多いので、クラスに入れてみました。わかりやすくするために最初の24個のみを記述してみます。
IconList.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
//package your.package.name; class IconList { private final String[] iListName = { "alert_dark_frame", "alert_light_frame", "arrow_down_float", "arrow_up_float", "bottom_bar", "btn_default", "btn_default_small", "btn_dialog", "btn_dropdown", "btn_minus", "btn_plus", "btn_radio", "btn_star", "btn_star_big_off", "btn_star_big_on", "button_onoff_indicator_off", "button_onoff_indicator_on", "checkbox_off_background", "checkbox_on_background", "dark_header", "dialog_frame", "dialog_holo_dark_frame", "dialog_holo_light_frame", "divider_horizontal_bright", }; private final Integer[] iList = { android.R.drawable.alert_dark_frame ,android.R.drawable.alert_light_frame ,android.R.drawable.arrow_down_float ,android.R.drawable.arrow_up_float ,android.R.drawable.bottom_bar ,android.R.drawable.btn_default ,android.R.drawable.btn_default_small ,android.R.drawable.btn_dialog ,android.R.drawable.btn_dropdown ,android.R.drawable.btn_minus ,android.R.drawable.btn_plus ,android.R.drawable.btn_radio ,android.R.drawable.btn_star ,android.R.drawable.btn_star_big_off ,android.R.drawable.btn_star_big_on ,android.R.drawable.button_onoff_indicator_off ,android.R.drawable.button_onoff_indicator_on ,android.R.drawable.checkbox_off_background ,android.R.drawable.checkbox_on_background ,android.R.drawable.dark_header ,android.R.drawable.dialog_frame ,android.R.drawable.dialog_holo_dark_frame ,android.R.drawable.dialog_holo_light_frame ,android.R.drawable.divider_horizontal_bright }; IconList(){ } int maxNum(){ return iList.length; } String getName(int number){ return iListName[number]; } int getIcon(int number){ return iList[number]; } } |
MainActivity.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 |
//package your.package.name; import androidx.appcompat.app.AppCompatActivity; import android.os.Bundle; import java.util.ArrayList; import java.util.List; import android.content.Context; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.AdapterView; import android.widget.BaseAdapter; import android.widget.GridView; import android.widget.ImageView; import android.widget.TextView; public class MainActivity extends AppCompatActivity implements AdapterView.OnItemClickListener { private IconList il; // 要素をArrayListで設定 private final List iconList = new ArrayList<>(); private TextView textView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); il = new IconList(); for (int i = 0; i < il.maxNum() ; i++){ iconList.add(il.getIcon(i)); } // GridViewのインスタンスを生成 GridView gridview = findViewById(R.id.gridview); // BaseAdapter を継承したGridAdapterのインスタンスを生成 GridAdapter adapter = new GridAdapter(this.getApplicationContext(), R.layout.grid_items, iconList); // gridViewにadapterをセット gridview.setAdapter(adapter); // item clickのListenerをセット gridview.setOnItemClickListener(this); textView = (TextView)findViewById(R.id.text_view); } static class ViewHolder { ImageView imageView; } // BaseAdapter を継承した GridAdapter クラスのインスタンス生成 class GridAdapter extends BaseAdapter{ private final LayoutInflater inflater; private final int layoutId; private final List icList; GridAdapter(Context context, int layoutId, List iconList) { super(); this.inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); this.layoutId = layoutId; icList = iconList; } @Override public View getView(int position, View convertView, ViewGroup parent) { ViewHolder holder; if (convertView == null) { // main.xml の に grid_items.xml を inflate して convertView とする convertView = inflater.inflate(layoutId, parent, false); // ViewHolder を生成 holder = new ViewHolder(); holder.imageView = (ImageView) convertView.findViewById(R.id.image_view); convertView.setTag(holder); } else { holder = (ViewHolder) convertView.getTag(); } holder.imageView.setImageResource(icList.get(position)); return convertView; } @Override public int getCount() { // 全要素数を返す return iconList.size(); } @Override public Object getItem(int position) { return null; } @Override public long getItemId(int position) { return 0; } } @Override public void onItemClick(AdapterView<?> parent, View v, int position, long id) { // アイコンタップでTextViewにその名前をひょうじする textView.setText(il.getName(position)); } } |
activity_main.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:background="#888" tools:context=".MainActivity"> <TextView android:id="@+id/text_view" android:textSize="30sp" android:textColor="#fff" android:layout_margin="20dp" android:gravity="center" android:layout_width="match_parent" android:layout_height="wrap_content" /> <GridView android:id="@+id/gridview" android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="center" android:background="#888" android:horizontalSpacing="2dp" android:verticalSpacing="2dp" android:numColumns="4" android:stretchMode="columnWidth" /> </LinearLayout> |
grid_items.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="100dp" android:background="#afc" android:orientation="vertical" android:gravity="center" android:padding="3dp" > <ImageView android:id="@+id/image_view" android:layout_width="match_parent" android:layout_height="match_parent" android:scaleType="fitCenter" android:contentDescription="@string/description" /> </LinearLayout> |
strings.xml
1 2 3 4 |
<resources> <string name="app_name">YourAppName</string> <string name="description">icon</string> </resources> |
サンプル動画
android icon 一覧
全一覧を表示したい場合は以下の配列を使ってください。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 |
private String[] iListName = { "alert_dark_frame", "alert_light_frame", "arrow_down_float", "arrow_up_float", "bottom_bar", "btn_default", "btn_default_small", "btn_dialog", "btn_dropdown", "btn_minus", "btn_plus", "btn_radio", "btn_star", "btn_star_big_off", "btn_star_big_on", "button_onoff_indicator_off", "button_onoff_indicator_on", "checkbox_off_background", "checkbox_on_background", "dark_header", "dialog_frame", "dialog_holo_dark_frame", "dialog_holo_light_frame", "divider_horizontal_bright", "divider_horizontal_dark", "divider_horizontal_dim_dark", "divider_horizontal_textfield", "edit_text", "editbox_background", "editbox_background_normal", "editbox_dropdown_dark_frame", "editbox_dropdown_light_frame", "gallery_thumb", "ic_btn_speak_now", "ic_delete", "ic_dialog_alert", "ic_dialog_dialer", "ic_dialog_email", "ic_dialog_info", "ic_dialog_map", "ic_input_add", "ic_input_delete", "ic_input_get", "ic_lock_idle_alarm", "ic_lock_idle_charging", "ic_lock_idle_lock", "ic_lock_idle_low_battery", "ic_lock_lock", "ic_lock_power_off", "ic_lock_silent_mode", "ic_lock_silent_mode_off", "ic_media_ff", "ic_media_next", "ic_media_pause", "ic_media_play", "ic_media_previous", "ic_media_rew", "ic_menu_add", "ic_menu_agenda", "ic_menu_always_landscape_portrait", "ic_menu_call", "ic_menu_camera", "ic_menu_close_clear_cancel", "ic_menu_compass", "ic_menu_crop", "ic_menu_day", "ic_menu_delete", "ic_menu_directions", "ic_menu_edit", "ic_menu_gallery", "ic_menu_help", "ic_menu_info_details", "ic_menu_manage", "ic_menu_mapmode", "ic_menu_month", "ic_menu_more", "ic_menu_my_calendar", "ic_menu_mylocation", "ic_menu_myplaces", "ic_menu_preferences", "ic_menu_recent_history", "ic_menu_report_image", "ic_menu_revert", "ic_menu_rotate", "ic_menu_save", "ic_menu_search", "ic_menu_send", "ic_menu_set_as", "ic_menu_share", "ic_menu_slideshow", "ic_menu_sort_alphabetically", "ic_menu_sort_by_size", "ic_menu_today", "ic_menu_upload", "ic_menu_upload_you_tube", "ic_menu_view", "ic_menu_week", "ic_menu_zoom", "ic_notification_clear_all", "ic_notification_overlay", "ic_partial_secure", "ic_popup_disk_full", "ic_popup_reminder", "ic_popup_sync", "ic_search_category_default", "ic_secure", "list_selector_background", "menu_frame", "menu_full_frame", "menuitem_background", "picture_frame", "presence_audio_away", "presence_audio_busy", "presence_audio_online", "presence_away", "presence_busy", "presence_invisible", "presence_offline", "presence_online", "presence_video_away", "presence_video_busy", "presence_video_online", "progress_horizontal", "progress_indeterminate_horizontal", "radiobutton_off_background", "radiobutton_on_background", "screen_background_dark", "screen_background_dark_transparent", "screen_background_light", "screen_background_light_transparent", "spinner_background", "spinner_dropdown_background", "star_big_off", "star_big_on", "star_off", "star_on", "stat_notify_call_mute", "stat_notify_chat", "stat_notify_error", "stat_notify_missed_call", "stat_notify_more", "stat_notify_sdcard", "stat_notify_sdcard_prepare", "stat_notify_sdcard_usb", "stat_notify_sync", "stat_notify_sync_noanim", "stat_notify_voicemail", "stat_sys_data_bluetooth", "stat_sys_download", "stat_sys_download_done", "stat_sys_headset", "stat_sys_phone_call", "stat_sys_phone_call_forward", "stat_sys_phone_call_on_hold", "stat_sys_speakerphone", "stat_sys_upload", "stat_sys_upload_done", "stat_sys_vp_phone_call", "stat_sys_vp_phone_call_on_hold", "stat_sys_warning", "status_bar_item_app_background", "status_bar_item_background", "sym_action_call", "sym_action_chat", "sym_action_email", "sym_call_incoming", "sym_call_missed", "sym_call_outgoing", "sym_contact_card", "sym_def_app_icon", "title_bar", "title_bar_tall", "toast_frame", "zoom_plate" }; private Integer[] iList = { android.R.drawable.alert_dark_frame ,android.R.drawable.alert_light_frame ,android.R.drawable.arrow_down_float ,android.R.drawable.arrow_up_float ,android.R.drawable.bottom_bar ,android.R.drawable.btn_default ,android.R.drawable.btn_default_small ,android.R.drawable.btn_dialog ,android.R.drawable.btn_dropdown ,android.R.drawable.btn_minus ,android.R.drawable.btn_plus ,android.R.drawable.btn_radio ,android.R.drawable.btn_star ,android.R.drawable.btn_star_big_off ,android.R.drawable.btn_star_big_on ,android.R.drawable.button_onoff_indicator_off ,android.R.drawable.button_onoff_indicator_on ,android.R.drawable.checkbox_off_background ,android.R.drawable.checkbox_on_background ,android.R.drawable.dark_header ,android.R.drawable.dialog_frame ,android.R.drawable.dialog_holo_dark_frame ,android.R.drawable.dialog_holo_light_frame ,android.R.drawable.divider_horizontal_bright ,android.R.drawable.divider_horizontal_dark ,android.R.drawable.divider_horizontal_dim_dark ,android.R.drawable.divider_horizontal_textfield ,android.R.drawable.edit_text ,android.R.drawable.editbox_background ,android.R.drawable.editbox_background_normal ,android.R.drawable.editbox_dropdown_dark_frame ,android.R.drawable.editbox_dropdown_light_frame ,android.R.drawable.gallery_thumb ,android.R.drawable.ic_btn_speak_now ,android.R.drawable.ic_delete ,android.R.drawable.ic_dialog_alert ,android.R.drawable.ic_dialog_dialer ,android.R.drawable.ic_dialog_email ,android.R.drawable.ic_dialog_info ,android.R.drawable.ic_dialog_map ,android.R.drawable.ic_input_add ,android.R.drawable.ic_input_delete ,android.R.drawable.ic_input_get ,android.R.drawable.ic_lock_idle_alarm ,android.R.drawable.ic_lock_idle_charging ,android.R.drawable.ic_lock_idle_lock ,android.R.drawable.ic_lock_idle_low_battery ,android.R.drawable.ic_lock_lock ,android.R.drawable.ic_lock_power_off ,android.R.drawable.ic_lock_silent_mode ,android.R.drawable.ic_lock_silent_mode_off ,android.R.drawable.ic_media_ff ,android.R.drawable.ic_media_next ,android.R.drawable.ic_media_pause ,android.R.drawable.ic_media_play ,android.R.drawable.ic_media_previous ,android.R.drawable.ic_media_rew ,android.R.drawable.ic_menu_add ,android.R.drawable.ic_menu_agenda ,android.R.drawable.ic_menu_always_landscape_portrait ,android.R.drawable.ic_menu_call ,android.R.drawable.ic_menu_camera ,android.R.drawable.ic_menu_close_clear_cancel ,android.R.drawable.ic_menu_compass ,android.R.drawable.ic_menu_crop ,android.R.drawable.ic_menu_day ,android.R.drawable.ic_menu_delete ,android.R.drawable.ic_menu_directions ,android.R.drawable.ic_menu_edit ,android.R.drawable.ic_menu_gallery ,android.R.drawable.ic_menu_help ,android.R.drawable.ic_menu_info_details ,android.R.drawable.ic_menu_manage ,android.R.drawable.ic_menu_mapmode ,android.R.drawable.ic_menu_month ,android.R.drawable.ic_menu_more ,android.R.drawable.ic_menu_my_calendar ,android.R.drawable.ic_menu_mylocation ,android.R.drawable.ic_menu_myplaces ,android.R.drawable.ic_menu_preferences ,android.R.drawable.ic_menu_recent_history ,android.R.drawable.ic_menu_report_image ,android.R.drawable.ic_menu_revert ,android.R.drawable.ic_menu_rotate ,android.R.drawable.ic_menu_save ,android.R.drawable.ic_menu_search ,android.R.drawable.ic_menu_send ,android.R.drawable.ic_menu_set_as ,android.R.drawable.ic_menu_share ,android.R.drawable.ic_menu_slideshow ,android.R.drawable.ic_menu_sort_alphabetically ,android.R.drawable.ic_menu_sort_by_size ,android.R.drawable.ic_menu_today ,android.R.drawable.ic_menu_upload ,android.R.drawable.ic_menu_upload_you_tube ,android.R.drawable.ic_menu_view ,android.R.drawable.ic_menu_week ,android.R.drawable.ic_menu_zoom ,android.R.drawable.ic_notification_clear_all ,android.R.drawable.ic_notification_overlay ,android.R.drawable.ic_partial_secure ,android.R.drawable.ic_popup_disk_full ,android.R.drawable.ic_popup_reminder ,android.R.drawable.ic_popup_sync ,android.R.drawable.ic_search_category_default ,android.R.drawable.ic_secure ,android.R.drawable.list_selector_background ,android.R.drawable.menu_frame ,android.R.drawable.menu_full_frame ,android.R.drawable.menuitem_background ,android.R.drawable.picture_frame ,android.R.drawable.presence_audio_away ,android.R.drawable.presence_audio_busy ,android.R.drawable.presence_audio_online ,android.R.drawable.presence_away ,android.R.drawable.presence_busy ,android.R.drawable.presence_invisible ,android.R.drawable.presence_offline ,android.R.drawable.presence_online ,android.R.drawable.presence_video_away ,android.R.drawable.presence_video_busy ,android.R.drawable.presence_video_online ,android.R.drawable.progress_horizontal ,android.R.drawable.progress_indeterminate_horizontal ,android.R.drawable.radiobutton_off_background ,android.R.drawable.radiobutton_on_background ,android.R.drawable.screen_background_dark ,android.R.drawable.screen_background_dark_transparent ,android.R.drawable.screen_background_light ,android.R.drawable.screen_background_light_transparent ,android.R.drawable.spinner_background ,android.R.drawable.spinner_dropdown_background ,android.R.drawable.star_big_off ,android.R.drawable.star_big_on ,android.R.drawable.star_off ,android.R.drawable.star_on ,android.R.drawable.stat_notify_call_mute ,android.R.drawable.stat_notify_chat ,android.R.drawable.stat_notify_error ,android.R.drawable.stat_notify_missed_call ,android.R.drawable.stat_notify_more ,android.R.drawable.stat_notify_sdcard ,android.R.drawable.stat_notify_sdcard_prepare ,android.R.drawable.stat_notify_sdcard_usb ,android.R.drawable.stat_notify_sync ,android.R.drawable.stat_notify_sync_noanim ,android.R.drawable.stat_notify_voicemail ,android.R.drawable.stat_sys_data_bluetooth ,android.R.drawable.stat_sys_download ,android.R.drawable.stat_sys_download_done ,android.R.drawable.stat_sys_headset ,android.R.drawable.stat_sys_phone_call ,android.R.drawable.stat_sys_phone_call_forward ,android.R.drawable.stat_sys_phone_call_on_hold ,android.R.drawable.stat_sys_speakerphone ,android.R.drawable.stat_sys_upload ,android.R.drawable.stat_sys_upload_done ,android.R.drawable.stat_sys_vp_phone_call ,android.R.drawable.stat_sys_vp_phone_call_on_hold ,android.R.drawable.stat_sys_warning ,android.R.drawable.status_bar_item_app_background ,android.R.drawable.status_bar_item_background ,android.R.drawable.sym_action_call ,android.R.drawable.sym_action_chat ,android.R.drawable.sym_action_email ,android.R.drawable.sym_call_incoming ,android.R.drawable.sym_call_missed ,android.R.drawable.sym_call_outgoing ,android.R.drawable.sym_contact_card ,android.R.drawable.sym_def_app_icon ,android.R.drawable.title_bar ,android.R.drawable.title_bar_tall ,android.R.drawable.toast_frame ,android.R.drawable.zoom_plate }; |
関連記事:
- GridView 作り方使い方
- 標準アイコンをGridViewで表示
- Picasso を使ってネット上の画像をGridViewで表示してみた
References:
R.drawable | Android Developers
Grid View | Android Developers