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