Java tutorial
/* * Copyright (C) 2014 The Android Open Source Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package com.example.android.movies.app; import android.content.Intent; import android.content.SharedPreferences; import android.graphics.Color; import android.net.Uri; import android.os.Bundle; import android.preference.PreferenceManager; import android.support.v4.view.MenuItemCompat; import android.support.v7.app.ActionBarActivity; import android.support.v7.widget.SearchView; import android.util.Log; import android.view.Menu; import android.view.MenuInflater; import android.view.MenuItem; import android.view.View; import android.widget.TextView; public class MainActivity extends ActionBarActivity { private final String LOG_TAG = MainActivity.class.getSimpleName(); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); if (savedInstanceState == null) { getSupportFragmentManager().beginTransaction().add(R.id.container, new SearchFragment()).commit(); } } @Override public boolean onPrepareOptionsMenu(Menu menu) { menu.clear(); //getMenuInflater().inflate(R.menu.main, menu); MenuInflater inflater = getMenuInflater(); inflater.inflate(R.menu.main, menu); MenuItem searchItem = menu.findItem(R.id.search); android.support.v7.widget.SearchView searchView = (android.support.v7.widget.SearchView) MenuItemCompat .getActionView(searchItem); searchView.setQueryHint("Find movies..."); int searchPlateId = searchView.getContext().getResources().getIdentifier("android:id/search_plate", null, null); View searchPlate = searchView.findViewById(searchPlateId); if (searchPlate != null) { searchPlate.setBackgroundColor(Color.DKGRAY); int searchTextId = searchPlate.getContext().getResources().getIdentifier("android:id/search_src_text", null, null); TextView searchText = (TextView) searchPlate.findViewById(searchTextId); if (searchText != null) { searchText.setTextColor(Color.WHITE); searchText.setHintTextColor(Color.WHITE); } } SearchView.OnQueryTextListener queryTextListener = new SearchView.OnQueryTextListener() { public boolean onQueryTextChange(String newText) { return true; } public boolean onQueryTextSubmit(String query) { SearchFragment fragmentS1 = new SearchFragment(); Bundle bundle = new Bundle(); bundle.putString("search", query); fragmentS1.setArguments(bundle); getSupportFragmentManager().beginTransaction().replace(R.id.container, fragmentS1).commit(); return true; } }; searchView.setOnQueryTextListener(queryTextListener); searchView.setIconifiedByDefault(false); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { // Handle action bar item clicks here. The action bar will // automatically handle clicks on the Home/Up button, so long // as you specify a parent activity in AndroidManifest.xml. int id = item.getItemId(); //noinspection SimplifiableIfStatement if (id == R.id.action_watchlist) { startActivity(new Intent(this, WatchlistActivity.class)); return true; } return super.onOptionsItemSelected(item); } }