1 /*
2 * Copyright 2013 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 
18 package com.example.android.donebar;
19 
20 import android.app.Activity;
21 import android.content.Intent;
22 import android.content.res.Resources;
23 import android.os.Bundle;
24 import android.view.View;
25 import android.view.ViewGroup;
26 import android.view.ViewTreeObserver;
27 import android.widget.AdapterView;
28 import android.widget.BaseAdapter;
29 import android.widget.GridView;
30 import android.widget.TextView;
31 
32 /**
33  * A simple launcher activity offering access to the individual samples in this project.
34  */
35 public class MainActivity extends Activity implements AdapterView.OnItemClickListener {
36     private Sample[] mSamples;
37     private GridView mGridView;
38 
onCreate(Bundle savedInstanceState)39     protected void onCreate(Bundle savedInstanceState) {
40         super.onCreate(savedInstanceState);
41         setContentView(R.layout.activity_main);
42 
43         // Prepare list of samples in this dashboard.
44         mSamples = new Sample[]{
45             new Sample(R.string.donebaractivity_title, R.string.donebaractivity_description,
46                     DoneBarActivity.class),
47             new Sample(R.string.donebuttonactivity_title, R.string.donebuttonactivity_description,
48                     DoneButtonActivity.class),
49         };
50 
51         // Prepare the GridView
52         mGridView = (GridView) findViewById(android.R.id.list);
53         mGridView.setAdapter(new SampleAdapter());
54         mGridView.setOnItemClickListener(this);
55     }
56 
57     @Override
onItemClick(AdapterView<?> container, View view, int position, long id)58     public void onItemClick(AdapterView<?> container, View view, int position, long id) {
59         startActivity(mSamples[position].intent);
60     }
61 
62     private class SampleAdapter extends BaseAdapter {
63         @Override
getCount()64         public int getCount() {
65             return mSamples.length;
66         }
67 
68         @Override
getItem(int position)69         public Object getItem(int position) {
70             return mSamples[position];
71         }
72 
73         @Override
getItemId(int position)74         public long getItemId(int position) {
75             return mSamples[position].hashCode();
76         }
77 
78         @Override
getView(int position, View convertView, ViewGroup container)79         public View getView(int position, View convertView, ViewGroup container) {
80             if (convertView == null) {
81                 convertView = getLayoutInflater().inflate(R.layout.sample_dashboard_item,
82                         container, false);
83             }
84 
85             ((TextView) convertView.findViewById(android.R.id.text1)).setText(
86                     mSamples[position].titleResId);
87             ((TextView) convertView.findViewById(android.R.id.text2)).setText(
88                     mSamples[position].descriptionResId);
89             return convertView;
90         }
91     }
92 
93     private class Sample {
94         int titleResId;
95         int descriptionResId;
96         Intent intent;
97 
Sample(int titleResId, int descriptionResId, Intent intent)98         private Sample(int titleResId, int descriptionResId, Intent intent) {
99             this.intent = intent;
100             this.titleResId = titleResId;
101             this.descriptionResId = descriptionResId;
102         }
103 
Sample(int titleResId, int descriptionResId, Class<? extends Activity> activityClass)104         private Sample(int titleResId, int descriptionResId,
105                 Class<? extends Activity> activityClass) {
106             this(titleResId, descriptionResId,
107                     new Intent(MainActivity.this, activityClass));
108         }
109     }
110 }
111