Android - Searchview in fragment is not working as expected - android

I’m facing some problems with fragments.
My MainActivity contains a DrawerLayout and a FrameLayout like this:
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<!—- Main view (fragments) -->
<FrameLayout
android:id="#+id/content_frame"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/cor_view"/>
<!-- Navigation drawer -->
<ListView android:id="#+id/lista"
android:layout_width="200dp"
android:layout_height="fill_parent"
android:layout_gravity="start"
android:choiceMode="singleChoice"
android:divider="#color/cor_divider_drawer"
android:dividerHeight="1dp"
android:background="#color/background_drawer"
android:textColor="#color/cor_texto_drawer"/>
</android.support.v4.widget.DrawerLayout>
According to each item from my navigation drawer, a specific fragment will be shown at the id/content_frame. This is working fine, but the problem is that one specific fragment has a search option at the action bar and when I submit the query, the result is not shown at the same “place” of this fragment. I mean, the result of the search don’t fill the framelayout (id/content_frame).
How could I do that ? A fragment replacement ? Does this work only using activities ?
This is my MainActivity:
public class MainActivity extends Activity
{
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
navigation.NavList.setOnItemClickListener(new AdapterView.OnItemClickListener()
{
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int position, long id)
{
// According to each item from menu, I’ll choose which fragment will be shown.
navigation.showFragment(position, MainActivity.this);
}
});
// When the app starts, by default, the home will be shown.
navigation.showFragment(0, MainActivity.this);
}
}
Inside the method showFragment I creat the fragment object like this
(in this example, the position will be set to point to ABCFragment):
Fragment fragment = new ABCFragment();
FragmentManager fragmentManager = activity.getFragmentManager();
fragmentManager.beginTransaction().replace(R.id.content_frame, fragment).commit();
So this is my fragment that has the searchview:
public class ABCFragment extends Fragment implements OnQueryTextListener
{
/*
onCreateView …
*/
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater)
{
// Action bar.
inflater.inflate(R.menu.activity_main_actions, menu);
searchManager = (SearchManager) context.getSystemService(Context.SEARCH_SERVICE);
searchView = (SearchView) menu.findItem(R.id.action_search).getActionView();
searchView.
setSearchableInfo(searchManager.getSearchableInfo(getActivity()
.getComponentName()));
}
#Override
public boolean onQueryTextSubmit(String query)
{
searchView.clearFocus();
return false;
}
#Override
public boolean onQueryTextChange(String newText)
{
return false;
}
}
My action bar in menu folder:
<item android:id="#+id/action_search"
android:icon="#drawable/ic_action_search"
android:title="#string/action_search"
android:showAsAction="ifRoom"
android:actionViewClass="android.widget.SearchView"/>
<item android:id="#+id/action_new"
android:icon="#drawable/ic_action_new_event"
android:title="#string/action_new_event"
android:showAsAction="ifRoom" />
<item android:id="#+id/action_refresh"
android:icon="#drawable/ic_action_refresh"
android:title="#string/action_refresh"
android:showAsAction="ifRoom" />
My searchable file in XML folder:
<searchable xmlns:android="http://schemas.android.com/apk/res/android"
android:hint="#string/search_hint"
android:inputType="number"
android:label="#string/app_name" />
My Android Manifest:
<!-- Main Activity -->
<activity
android:name="com.test.MainActivity"
android:label="#string/app_name">
</activity>
<!-- ABC Fragment -->
<activity
android:name="com.test.ABCFragment"
android:label="#string/app_name">
<meta-data
android:name="android.app.default_searchable"
android:value="com.test.SearchResultsActivity" />
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<!-- Search results activity -->
<activity android:name="com.test.SearchResultsActivity"
android:parentActivityName="com.test.ABCFragment" >
<intent-filter>
<action android:name="android.intent.action.SEARCH" />
</intent-filter>
<meta-data
android:name="android.app.searchable"
android:resource="#xml/searchable" />
</activity>

i'm using the searchview through the custum action bar directly in the fragment
Here we can not find the activity but using the getActivity can get all widgets
searchView = (SearchView) getActivity().findViewById(R.id.searchView1);

Related

I want something like this : Search button in actionbar

I want to make the Search view like the flowing picture.
i use this code. But did't get the result i expected .
<item
android:id="#+id/search"
android:title="#string/app_name"
android:hint="#string/search_hint"
android:icon="#drawable/ic_search_white_48px"
app:showAsAction="collapseActionView|ifRoom"
app:actionViewClass="android.support.v7.widget.SearchView"/>
I suggest you to use Toolbar instead Actionbar,try this way
<FrameLayout
android:id="#+id/toolbar_container"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="#color/theme_primary" />
<com.miguelcatalan.materialsearchview.MaterialSearchView
android:id="#+id/search_view"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</FrameLayout>
Check this for more https://github.com/MiguelCatalan/MaterialSearchView
menu_main.xml:
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="#+id/search"
android:title="#string/search_title"
android:icon="#drawable/ic_search"
android:showAsAction="collapseActionView|ifRoom"
android:actionViewClass="android.widget.SearchView" />
\res\xml\searchable.xml:
<searchable xmlns:android="http://schemas.android.com/apk/res/android"
android:label="#string/app_name"
android:hint="#string/search_hint" />
AndroidManifest.xml:
<activity
android:name=".MainActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<meta-data android:name="android.app.searchable"
android:resource="#xml/searchable" />
</activity>
styles.xml:
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="android:Theme.Holo.Light">
<!-- Customize your theme here. -->
</style>
MainActivity.java:
package com.example.searchview;
import android.app.Activity;
import android.app.SearchManager;
import android.content.Context;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.SearchView;
public class MainActivity extends Activity {
private SearchView.OnQueryTextListener queryTextListener;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
// Associate searchable configuration with the SearchView
SearchManager searchManager =
(SearchManager) getSystemService(Context.SEARCH_SERVICE);
SearchView searchView =
(SearchView) menu.findItem(R.id.search).getActionView();
searchView.setSearchableInfo(
searchManager.getSearchableInfo(getComponentName()));
queryTextListener = new SearchView.OnQueryTextListener() {
#Override
public boolean onQueryTextChange(String newText) {
Log.i("onQueryTextChange", newText);
return true;
}
#Override
public boolean onQueryTextSubmit(String query) {
Log.i("onQueryTextSubmit", query);
return true;
}
};
searchView.setOnQueryTextListener(queryTextListener);
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.
return super.onOptionsItemSelected(item);
}
}

Android Search Widget in Actionbar and second unnecessary search on top

I want to create a search for the user in the ActionBar. I use the SearchView Widget as an Action View. But when I include the
part in the manifest, it also pops up a second search on top. Both the view on top and the ActionBar Search work. When I remove the part from the manifest, the second Search doesn't pop up and only the one in the ActionBar does, but nothing happens when I hit the search button.
This is how it looks:
I only want the second view not the white search on top. Perhaps somebody easily sees whats wrong.
This is my AndroidManifest.xml
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/BlueTheme">
<activity
android:name=".MainActivity"
android:configChanges="orientation|screenSize"
android:label="#string/app_name"
android:windowSoftInputMode="adjustPan">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<meta-data android:name="android.app.default_searchable"
android:value=".helper.SearchResultsActivity" />
</activity>
<activity android:name=".helper.SearchResultsActivity">
<intent-filter>
<action android:name="android.intent.action.SEARCH" />
</intent-filter>
<meta-data android:name="android.app.searchable"
android:resource="#xml/searchable"/>
</activity>
</application>
The part of the Fragment(as part of MainActivity) which handles the search:
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.persons, menu);
Log.d("ml Person", "creating Options Menu for Persons");
// Associate searchable configuration with the SearchView
SearchManager searchManager = (SearchManager) getActivity().getSystemService(Context.SEARCH_SERVICE);
SearchView searchView = (SearchView) menu.findItem(R.id.action_search).getActionView();
searchView.setSearchableInfo(searchManager.getSearchableInfo(getActivity().getComponentName()));
super.onCreateOptionsMenu(menu, inflater);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
switch (id) {
case R.id.action_search:
getActivity().onSearchRequested();
return true;
}
return super.onOptionsItemSelected(item);
}
widget in menu:
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="#+id/action_search"
android:icon="#drawable/ic_search_white_24dp"
android:orderInCategory="10"
android:title="#string/action_search"
app:showAsAction="ifRoom|collapseActionView"
app:actionViewClass="android.support.v7.widget.SearchView"/>
</menu>
searchable.xml
<searchable xmlns:android="http://schemas.android.com/apk/res/android"
android:label="#string/app_name"
android:hint="#string/search_hint_persons" />

Implement searchview in toolbar in android

Hi i want to implement search functionality in my app over toolbar.Currently i have added SearchView element in activity_main.xml , but I don't know how to implement remaining logic. Please guide me to solve this
<android.support.v7.widget.Toolbar
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#99CC33"
android:id="#+id/toolbar"
android:theme="#style/ThemeOverlay.AppCompat.Dark.ActionBar"
app:title="#string/app_name">
<SearchView
android:id="#+id/mySearchView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:iconifiedByDefault="true"
android:layout_gravity="end"
android:queryHint="Search by name"/>
</android.support.v7.widget.Toolbar>
my AndroidManifest.xml is
<application
android:allowBackup="true"
android:icon="#drawable/splash_img"
android:label="Call Logger"
android:theme="#style/AppTheme" >
<!-- Splash screen -->
<activity
android:name="com.comforters.callLogger.SplashScreen"
android:label="#string/app_name"
android:screenOrientation="portrait"
android:theme="#android:style/Theme.Black.NoTitleBar" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<!-- Main activity -->
<activity
android:name="com.comforters.callLogger.MainActivity"
android:label="Call Logger" >
</activity>
<receiver android:name=".PhonecallReceiver" >
<intent-filter>
<action android:name="android.intent.action.PHONE_STATE" />
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.NEW_OUTGOING_CALL" />
</intent-filter>
</receiver>
</application>
Create a menu item inside your menu folder say /main/res/menu have the following
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
tools:context=".MainActivity">
<item
android:id="#+id/action_search"
android:icon="#drawable/ic_action_search"
android:title="#string/action_search"
app:showAsAction="collapseActionView|always"
app:actionViewClass="android.support.v7.widget.SearchView" />
</item>
That menu should be bound to the activity that will handle the search. In your Activity have something like this
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.menu_activity_main, menu);
super.onCreateOptionsMenu(menu, inflater);
SearchManager searchManager = (SearchManager) getActivity().getSystemService(Context.SEARCH_SERVICE);
MenuItem searchItem = menu.findItem(R.id.action_search);
SearchView searchView = (SearchView) MenuItemCompat.getActionView(searchItem);
searchView.setSearchableInfo(searchManager.getSearchableInfo(context.getComponentName()));
searchView.setIconifiedByDefault(false);
SearchView.OnQueryTextListener textChangeListener = new SearchView.OnQueryTextListener()
{
#Override
public boolean onQueryTextChange(String query)
{
adapter.getFilter().filter(query);
return true;
}
#Override
public boolean onQueryTextSubmit(String query)
{
adapter.getFilter().filter(query);
return true;
}
};
searchView.setOnQueryTextListener(textChangeListener);
}
Then in your adapter implement you filter logic, this will depend on what you are searching and also on how the data is presented
Hope this points you in the right direction

Android SearchView shows different layouts

I am defining a search action in my action bar. The search works perfectly. However, when touching the search icon a white background layout occurs (containing the app icon) and when touching the back arrow a blue layout occur (without the app icon).
My problem is that I only want to show the blue layout (appearing after touching the back arrow on the white layout).
Here are code relative to the search action bar implementation.
AndroidManifest:
<meta-data
android:name="android.app.default_searchable"
android:value=".RecipeListActivity" />
</activity>
<activity
android:name=".RecipeListActivity"
android:label="#string/title_activity_recipe_list"
android:parentActivityName=".RecipeFolderActivity" >
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.cookbook.android.mycookbook.RecipeFolderActivity" />
<meta-data
android:name="android.app.searchable"
android:resource="#xml/searchable" />
<intent-filter>
<action android:name="android.intent.action.SEARCH" />
</intent-filter>
</activity>
This is the definition of the menu in xml
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
tools:context="com.cookbook.android.mycookbook.MainFolderActivity" >
<item android:id="#+id/search"
android:title="#string/search"
android:icon="#drawable/icon_loupe"
app:showAsAction="ifRoom|collapseActionView"
app:actionViewClass="android.support.v7.widget.SearchView"/>
<item android:id="#+id/add_main_folder"
android:title="#string/add_main_folder"
android:orderInCategory="101"
app:showAsAction="never" />
</menu>
Here is the java code:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main_folder, menu);
SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
SearchView searchView = (SearchView) menu.findItem(R.id.search).getActionView();
searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
return super.onCreateOptionsMenu(menu);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if(id==R.id.search){
Log.d(LOG_TAG,"In onOptionItemSelected, for search");
onSearchRequested();
}
return super.onOptionsItemSelected(item);
}
Any help will be greatly appreciated. :-)

Cannot able to make SearchActivity to start

I try to make SearchView in action bar to start a SearchResultsActivity which is registered to handle ACTION_SEARCH. I do all required steps but still didn't work!
Here's the code:
1. Search View menu item: (menu/main.xml)
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="#+id/search_action"
android:icon="#android:drawable/ic_menu_search"
android:actionViewClass="android.widget.SearchView"
android:showAsAction="always|collapseActionView"
/>
</menu>
2. Searchable configuration (xml/searchable.xml):
<?xml version="1.0" encoding="utf-8"?>
<searchable xmlns:android="http://schemas.android.com/apk/res/android"
android:label="#string/app_name"
android:hint="#string/search_hint"/>
2.1 adding Searchable configuration link to MainActivity in AndroidManifest.xml:
<activity
android:name="com.me.searchonpre3.MainActivity"
android:label="#string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
<meta-data
android:name="android.app.searchable"
android:resource="#xml/searchable"/>
</activity>
3. Bind the Search View menu item with the search configuration (MainActivity.java):
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB){
SearchView searchView = (SearchView) menu.findItem(R.id.search_action).getActionView();
SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
if (searchView != null) {
searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
}
}
return true;
}
4. add new Activity "SearchResultsActivity" and mark it to handle SEARCH_ACTIONs:
public class SearchResultsActivity extends ListActivity {
public static final String TAG = SearchResultsActivity.class.getSimpleName();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.i(TAG, "In SearchResult OnCreate");
setContentView(R.layout.activity_search_results);
this.setListAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,
new String[]{"Product1", "Product2"}));
}
}
AndroidMainfest.xml:
<activity
android:name="com.me.searchonpre3.SearchResultsActivity"
android:label="#string/title_activity_search_results">
<intent-filter>
<action android:name="android.intent.action.SEARCH"/>
</intent-filter>
</activity>
All the above settings and the SearchResultsActivity is not started when press the Search keyboard icon.
What else Should I do???
Note: When I add a setOnQueryTextListener, the callbacks being called on the MainActivity class.
It seems that Android tutorials are misleading.
The answer is here: https://stackoverflow.com/questions/11699206#11704661

Categories

Resources