• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2007 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.example.android.apis.view;
18 
19 import com.example.android.apis.R;
20 
21 import android.app.Activity;
22 import android.content.Context;
23 import android.content.res.TypedArray;
24 import android.os.Bundle;
25 import android.view.ContextMenu;
26 import android.view.MenuItem;
27 import android.view.View;
28 import android.view.ViewGroup;
29 import android.view.ContextMenu.ContextMenuInfo;
30 import android.widget.AdapterView;
31 import android.widget.BaseAdapter;
32 import android.widget.Gallery;
33 import android.widget.ImageView;
34 import android.widget.Toast;
35 import android.widget.AdapterView.AdapterContextMenuInfo;
36 import android.widget.AdapterView.OnItemClickListener;
37 
38 public class Gallery1 extends Activity {
39 
40     @Override
onCreate(Bundle savedInstanceState)41     public void onCreate(Bundle savedInstanceState) {
42         super.onCreate(savedInstanceState);
43         setContentView(R.layout.gallery_1);
44 
45         // Reference the Gallery view
46         Gallery g = (Gallery) findViewById(R.id.gallery);
47         // Set the adapter to our custom adapter (below)
48         g.setAdapter(new ImageAdapter(this));
49 
50         // Set a item click listener, and just Toast the clicked position
51         g.setOnItemClickListener(new OnItemClickListener() {
52             public void onItemClick(AdapterView parent, View v, int position, long id) {
53                 Toast.makeText(Gallery1.this, "" + position, Toast.LENGTH_SHORT).show();
54             }
55         });
56 
57         // We also want to show context menu for longpressed items in the gallery
58         registerForContextMenu(g);
59     }
60 
61     @Override
onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo)62     public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
63         menu.add(R.string.gallery_2_text);
64     }
65 
66     @Override
onContextItemSelected(MenuItem item)67     public boolean onContextItemSelected(MenuItem item) {
68         AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();
69         Toast.makeText(this, "Longpress: " + info.position, Toast.LENGTH_SHORT).show();
70         return true;
71     }
72 
73     public class ImageAdapter extends BaseAdapter {
74         int mGalleryItemBackground;
75 
ImageAdapter(Context c)76         public ImageAdapter(Context c) {
77             mContext = c;
78             // See res/values/attrs.xml for the <declare-styleable> that defines
79             // Gallery1.
80             TypedArray a = obtainStyledAttributes(R.styleable.Gallery1);
81             mGalleryItemBackground = a.getResourceId(
82                     R.styleable.Gallery1_android_galleryItemBackground, 0);
83             a.recycle();
84         }
85 
getCount()86         public int getCount() {
87             return mImageIds.length;
88         }
89 
getItem(int position)90         public Object getItem(int position) {
91             return position;
92         }
93 
getItemId(int position)94         public long getItemId(int position) {
95             return position;
96         }
97 
getView(int position, View convertView, ViewGroup parent)98         public View getView(int position, View convertView, ViewGroup parent) {
99             ImageView i = new ImageView(mContext);
100 
101             i.setImageResource(mImageIds[position]);
102             i.setScaleType(ImageView.ScaleType.FIT_XY);
103             i.setLayoutParams(new Gallery.LayoutParams(136, 88));
104 
105             // The preferred Gallery item background
106             i.setBackgroundResource(mGalleryItemBackground);
107 
108             return i;
109         }
110 
111         private Context mContext;
112 
113         private Integer[] mImageIds = {
114                 R.drawable.gallery_photo_1,
115                 R.drawable.gallery_photo_2,
116                 R.drawable.gallery_photo_3,
117                 R.drawable.gallery_photo_4,
118                 R.drawable.gallery_photo_5,
119                 R.drawable.gallery_photo_6,
120                 R.drawable.gallery_photo_7,
121                 R.drawable.gallery_photo_8
122         };
123     }
124 
125 }
126