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