Another
most useful feature of action bar is adding widgets to it. For example
like adding search widget to action bar. Search widget will be useful
when user wants to search for something in the app or across android OS.
Adding search widget involves these steps.
Adding the Search Widget to action bar action item
Defining the searchable configuration in the xml
Creating the activity to handle search query and display the results
Defining the default searchable activity and SEARCH intent filter in AndroidManifest.xml file
1. So first we add the search widget to action bar. Open your activity_main_actions.xml which is located under menu folder and add search widget to search action item as below.
android:actionViewClass=”android.widget.SearchView
2. Create a searchable configuration file under res ⇒ xml folder named searchable.xml (If you don’t see xml folder under res, create a new folder with the name xml). See list of Searchable Configuration options.
3. We need another activity to handle search query and results. Create a new class named SearchResultsActivity.java and paste following code. Also create layout file too named activity_search_results.xml.
Here I just passed the search query to another activity. You have to take appropriate action to display the search results using the query either from SQLite database or making a request to server and getting the results or some other way.
4. Finally in the AndroidManifest.xml file define the searchable configuration, default searchable activity and the activity performing the search.
android.app.default_searchable – Defines the default searchable activity handle search. You can add this block anywhere in the manifest file either inside tag or tag.
android.app.searchable – Defines the searchable configuration which was written in searchable.xml file
android.intent.action.SEARCH – Should be defined as a intent filter for the activity which receives the search query.
Adding search widget involves these steps.
Adding the Search Widget to action bar action item
Defining the searchable configuration in the xml
Creating the activity to handle search query and display the results
Defining the default searchable activity and SEARCH intent filter in AndroidManifest.xml file
1. So first we add the search widget to action bar. Open your activity_main_actions.xml which is located under menu folder and add search widget to search action item as below.
android:actionViewClass=”android.widget.SearchView
< item android:id = "@+id/action_search" android:icon = "@drawable/ic_action_search" android:title = "@string/action_search" android:showAsAction = "always" android:actionViewClass = "android.widget.SearchView" /> |
<xml
version
=
"1.0"
encoding
=
"utf-8"
?>
android:hint
=
"@string/search_hint"
android:label
=
"@string/app_name"
/>
It is suggested that always assign the values of android:hint and android:label from strings.xml file only instead of writing the values directly.
Now open the main activity and modify the code in the onCreateOptionsMenu() method.@Override
public
boolean
onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.activity_main_actions, menu);
// Associate searchable configuration with the SearchView
SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
SearchView searchView = (SearchView) menu.findItem(R.id.action_search)
.getActionView();
searchView.setSearchableInfo(searchManager
.getSearchableInfo(getComponentName()));
return
super
.onCreateOptionsMenu(menu);
}
Here I just passed the search query to another activity. You have to take appropriate action to display the search results using the query either from SQLite database or making a request to server and getting the results or some other way.
android:layout_width
=
"match_parent"
android:layout_height
=
"match_parent"
android:paddingBottom
=
"@dimen/activity_vertical_margin"
android:paddingLeft
=
"@dimen/activity_horizontal_margin"
android:paddingRight
=
"@dimen/activity_horizontal_margin"
android:paddingTop
=
"@dimen/activity_vertical_margin"
tools:context
=
".MainActivity"
>
<
TextView
android:id
=
"@+id/txtQuery"
android:layout_width
=
"wrap_content"
android:layout_height
=
"wrap_content"
/>
</
RelativeLayout
>
package
info.dreamitsanjay.actionbar;
import
android.app.ActionBar;
import
android.app.Activity;
import
android.app.SearchManager;
import
android.content.Intent;
import
android.os.Bundle;
import
android.util.Log;
import
android.widget.TextView;
public
class
SearchResultsActivity
extends
Activity {
private
TextView txtQuery;
@Override
protected
void
onCreate(Bundle savedInstanceState) {
super
.onCreate(savedInstanceState);
setContentView(R.layout.activity_search_results);
// get the action bar
ActionBar actionBar = getActionBar();
// Enabling Back navigation on Action Bar icon
actionBar.setDisplayHomeAsUpEnabled(
true
);
txtQuery = (TextView) findViewById(R.id.txtQuery);
handleIntent(getIntent());
}
@Override
protected
void
onNewIntent(Intent intent) {
setIntent(intent);
handleIntent(intent);
}
/**
* Handling intent data
*/
private
void
handleIntent(Intent intent) {
if
(Intent.ACTION_SEARCH.equals(intent.getAction())) {
String query = intent.getStringExtra(SearchManager.QUERY);
/**
* Use this query to display search results like
* 1. Getting the data from SQLite and showing in listview
* 2. Making webrequest and displaying the data
* For now we just display the query only
*/
txtQuery.setText(
"Search Query: "
+ query);
}
}
}
android.app.default_searchable – Defines the default searchable activity handle search. You can add this block anywhere in the manifest file either inside
android.app.searchable – Defines the searchable configuration which was written in searchable.xml file
android.intent.action.SEARCH – Should be defined as a intent filter for the activity which receives the search query.
xml
version
=
"1.0"
encoding
=
"utf-8"
?>
package
=
"info.dreamitsanjay.actionbar"
android:versionCode
=
"1"
android:versionName
=
"1.0"
>
<
uses-sdk
android:minSdkVersion
=
"11"
android:targetSdkVersion
=
"18"
/>
<
application
android:allowBackup
=
"true"
android:icon
=
"@drawable/ic_launcher"
android:label
=
"@string/app_name"
android:theme
=
"@style/AppTheme"
>
<
activity
android:name
=
"info.dreamitsanjay.actionbar.MainActivity"
android:label
=
"@string/app_name"
>
<
meta-data
android:name
=
"android.app.default_searchable"
android:value
=
".SearchResultsActivity"
/>
<
intent-filter
>
<
action
android:name
=
"android.intent.action.MAIN"
/>
<
category
android:name
=
"android.intent.category.LAUNCHER"
/>
</
intent-filter
>
</
activity
>
<
activity
android:name
=
".SearchResultsActivity"
android:parentActivityName
=
"info.dreamitsanjay.actionbar.MainActivity"
>
<
intent-filter
>
<
action
android:name
=
"android.intent.action.SEARCH"
/>
</
intent-filter
>
<
meta-data
android:name
=
"android.app.searchable"
android:resource
=
"@xml/searchable"
/>
</
activity
>
<
activity
android:name
=
"info.dreamitsanjay.actionbar.LocationFound"
android:label
=
"@string/activity_location_found"
android:parentActivityName
=
"info.dreamitsanjay.actionbar.MainActivity"
>
</
activity
>
</
application
>
</
manifest
>
If you are searching life partner. your searching end with kpmarriage.com. now kpmarriage.com offer free matrimonial website which offer free message, free chat, free view contact information. so register here : kpmarriage.com- Free matrimonial website
0 comments:
Post a Comment