• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2010 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.searchabledict;
18 
19 import android.app.Activity;
20 import android.app.ActionBar;
21 import android.app.SearchManager;
22 import android.content.Context;
23 import android.content.Intent;
24 import android.database.Cursor;
25 import android.net.Uri;
26 import android.os.Bundle;
27 import android.view.Menu;
28 import android.view.MenuInflater;
29 import android.view.MenuItem;
30 import android.widget.SearchView;
31 import android.widget.TextView;
32 
33 /**
34  * Displays a word and its definition.
35  */
36 public class WordActivity extends Activity {
37 
38     @Override
onCreate(Bundle savedInstanceState)39     protected void onCreate(Bundle savedInstanceState) {
40         super.onCreate(savedInstanceState);
41         setContentView(R.layout.word);
42 
43         ActionBar actionBar = getActionBar();
44         actionBar.setDisplayHomeAsUpEnabled(true);
45 
46         Uri uri = getIntent().getData();
47         Cursor cursor = managedQuery(uri, null, null, null, null);
48 
49         if (cursor == null) {
50             finish();
51         } else {
52             cursor.moveToFirst();
53 
54             TextView word = (TextView) findViewById(R.id.word);
55             TextView definition = (TextView) findViewById(R.id.definition);
56 
57             int wIndex = cursor.getColumnIndexOrThrow(DictionaryDatabase.KEY_WORD);
58             int dIndex = cursor.getColumnIndexOrThrow(DictionaryDatabase.KEY_DEFINITION);
59 
60             word.setText(cursor.getString(wIndex));
61             definition.setText(cursor.getString(dIndex));
62         }
63     }
64 
65     @Override
onCreateOptionsMenu(Menu menu)66     public boolean onCreateOptionsMenu(Menu menu) {
67         MenuInflater inflater = getMenuInflater();
68         inflater.inflate(R.menu.options_menu, menu);
69 
70         SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
71         SearchView searchView = (SearchView) menu.findItem(R.id.search).getActionView();
72         searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
73         searchView.setIconifiedByDefault(false);
74 
75         return true;
76     }
77 
78     @Override
onOptionsItemSelected(MenuItem item)79     public boolean onOptionsItemSelected(MenuItem item) {
80         switch (item.getItemId()) {
81             case R.id.search:
82                 onSearchRequested();
83                 return true;
84             case android.R.id.home:
85                 Intent intent = new Intent(this, SearchableDictionary.class);
86                 intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
87                 startActivity(intent);
88                 return true;
89             default:
90                 return false;
91         }
92     }
93 }
94