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

on Wednesday, October 29, 2014


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>

0 comments:

Post a Comment