I am following this tutorial.
I want to add a SearchView
in TabsViewpager as link: Picture 1 http://www.uphinh.vn/image/stream/1386201.png
But my app ,SearchView not in TabsViewpager as link:Picture 2 http://www.uphinh.vn/image/stream/1386202.png
I can't fix it.
This is my code:
This is FragmentTab3.java:
public class FragmentTab3 extends SherlockFragment implements OnQueryTextListener {
TextView mSearchText;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
setHasOptionsMenu(true);
// Get the view from fragmenttab1.xml
View view = inflater.inflate(R.layout.fragmenttab3, container, false);
mSearchText =(TextView)view.findViewById(R.id.test);
return view;
}
@Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
setUserVisibleHint(true);
}
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
// TODO Auto-generated method stub
super.onCreateOptionsMenu(menu, inflater);
inflater.inflate(R.menu.menutab3, menu);
SearchView searchView = (SearchView) menu.findItem(R.id.action_search).getActionView();
searchView.setQueryHint("Search");
//searchView.setBackgroundColor(Color.WHITE);
searchView.setOnQueryTextListener(this);
}
@Override
public boolean onQueryTextChange(String newText) {
// TODO Auto-generated method stub
mSearchText.setText("Query so far:"+newText);
mSearchText.setTextColor(Color.GREEN);
return false;
}
@Override
public boolean onQueryTextSubmit(String query) {
// TODO Auto-generated method stub
mSearchText.setText("Searching for: " + query + "...");
mSearchText.setTextColor(Color.RED);
return false;
}
}
This is fragmenttab3.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:id="@+id/test"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="@string/Fragment3" />
</RelativeLayout>
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:id="@+id/action_search"
android:icon="@android:drawable/ic_menu_search"
android:title="Action Bar Search"
android:showAsAction="ifRoom|withText|collapseActionView"
android:actionViewClass="android.widget.SearchView" />
<item
android:id="@+id/gamepad"
android:title="@string/gamepad"
android:icon="@drawable/gamepad"
android:showAsAction="ifRoom|withText"
/>
<item
android:id="@+id/camera"
android:title="@string/camera"
android:icon="@drawable/camera"
android:showAsAction="ifRoom|withText"
/>
</menu>
I using libs actionbarsherlock.
I CAN'T FIX IT.
Here is simple example of searchview with viewpager
Interface IDataCallback
import java.util.ArrayList;
public interface IDataCallback {
void onFragmentCreated( ArrayList<String> listData);
}
Interface IFragmentListener
public interface IFragmentListener {
void addiSearch(ISearch iSearch);
void removeISearch(ISearch iSearch);
}
Interface ISearch
public interface ISearch {
void onTextQuery(String text);
}
your Activity MainActivity
import android.app.SearchManager;
import android.content.Context;
import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.SearchView;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity implements TabLayout.OnTabSelectedListener, SearchView.OnQueryTextListener, IFragmentListener {
//This is our tablayout
private TabLayout tabLayout;
//This is our viewPager
private ViewPager viewPager;
ArrayList<ISearch> iSearch = new ArrayList<>();
private MenuItem searchMenuItem;
private String newText;
private PageAdapter adapter;
ArrayList<String> listData = null;
IDataCallback iDataCallback = null;
public void setiDataCallback(IDataCallback iDataCallback) {
this.iDataCallback = iDataCallback;
iDataCallback.onFragmentCreated(listData);
}
@Override
public void addiSearch(ISearch iSearch) {
this.iSearch.add(iSearch);
}
@Override
public void removeISearch(ISearch iSearch) {
this.iSearch.remove(iSearch);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Adding toolbar to the activity
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
listData = new ArrayList<>();
//Initializing the tablayout
tabLayout = (TabLayout) findViewById(R.id.tabLayout);
//Adding the tabs using addTab() method
tabLayout.addTab(tabLayout.newTab().setText("Tab1"));
tabLayout.addTab(tabLayout.newTab().setText("Tab2"));
tabLayout.addTab(tabLayout.newTab().setText("Tab3"));
tabLayout.setTabGravity(TabLayout.GRAVITY_FILL);
//Initializing viewPager
viewPager = (ViewPager) findViewById(R.id.pager);
//Creating our pager adapter
adapter = new PageAdapter(getSupportFragmentManager(), tabLayout.getTabCount(), newText);
//Adding adapter to pager
viewPager.setAdapter(adapter);
//Adding onTabSelectedListener to swipe views
tabLayout.setOnTabSelectedListener(this);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu, menu);
SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
searchMenuItem = menu.findItem(R.id.action_search);
SearchView searchView = (SearchView) searchMenuItem.getActionView();
searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
searchView.setSubmitButtonEnabled(true);
searchView.setOnQueryTextListener(this);
return super.onCreateOptionsMenu(menu);
}
@Override
public void onTabSelected(TabLayout.Tab tab) {
viewPager.setCurrentItem(tab.getPosition());
}
public void getDataFromFragment_one(ArrayList<String> listData) {
this.listData = listData;
Log.e("-->", "" + listData.toString());
}
@Override
public void onTabUnselected(TabLayout.Tab tab) {
}
@Override
public void onTabReselected(TabLayout.Tab tab) {
}
@Override
public boolean onQueryTextChange(String newText) {
this.newText = newText;
adapter.setTextQueryChanged(newText);
for (ISearch iSearchLocal : this.iSearch)
iSearchLocal.onTextQuery(newText);
return true;
}
@Override
public boolean onQueryTextSubmit(String query) {
return false;
}
}
Main activity xml activity_main
<LinearLayout
android:id="@+id/main_layout"
android:orientation="vertical"
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"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<!-- our toolbar -->
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?attr/colorPrimary"
android:minHeight="?attr/actionBarSize"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light"/>
<!-- our tablayout to display tabs -->
<android.support.design.widget.TabLayout
android:id="@+id/tabLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?attr/colorPrimary"
android:minHeight="?attr/actionBarSize"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"/>
<!-- View pager to swipe views -->
<android.support.v4.view.ViewPager
android:id="@+id/pager"
android:layout_width="match_parent"
android:layout_height="fill_parent"/>
</LinearLayout>
page adpater PageAdapter
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
public class PageAdapter extends FragmentStatePagerAdapter {
private String mSearchTerm;
//integer to count number of tabs
int tabCount;
//Constructor to the class
public PageAdapter(FragmentManager fm, int tabCount, String searchTerm) {
super(fm);
//Initializing tab count
this.tabCount= tabCount;
this.mSearchTerm= searchTerm;
}
//Overriding method getItem
@Override
public Fragment getItem(int position) {
//Returning the current tabs
switch (position) {
case 0:
Tab1 tab1 = Tab1.newInstance(mSearchTerm);
return tab1;
case 1:
Tab2 tab2 = Tab2.newInstance(mSearchTerm);
return tab2;
case 2:
Tab3 tab3 = Tab3.newInstance(mSearchTerm);
return tab3;
default:
return null;
}
}
//Overriden method getCount to get the number of tabs
@Override
public int getCount() {
return tabCount;
}
public void setTextQueryChanged(String newText) {
mSearchTerm = newText;
}
}
First Fragment Tab1
import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import java.util.ArrayList;
public class Tab1 extends Fragment implements ISearch {
private static final String ARG_SEARCHTERM = "search_term";
private String mSearchTerm = null;
ArrayList<String> strings = null;
private IFragmentListener mIFragmentListener = null;
ArrayAdapter<String> arrayAdapter = null;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view;
//Returning the layout file after inflating
//Change R.layout.tab1 in you classes
view = inflater.inflate(R.layout.tab1, container, false);
ListView listView = (ListView) view.findViewById(R.id.listview1);
strings = new ArrayList<>();
for (int i = 0; i < 20; i++) {
strings.add(String.valueOf(i));
}
strings.add("11");
arrayAdapter = new ArrayAdapter<>(getActivity(), android.R.layout.simple_list_item_1, strings);
listView.setAdapter(arrayAdapter);
MainActivity mainActivity = (MainActivity) getActivity();
mainActivity.getDataFromFragment_one(strings);
if (getArguments() != null) {
mSearchTerm = (String) getArguments().get(ARG_SEARCHTERM);
}
return view;
}
@Override
public void onResume() {
super.onResume();
if (null != mSearchTerm) {
onTextQuery(mSearchTerm);
}
}
public Tab1() {
}
public static Tab1 newInstance(String searchTerm) {
Tab1 fragment = new Tab1();
Bundle bundle = new Bundle();
bundle.putString(ARG_SEARCHTERM, searchTerm);
fragment.setArguments(bundle);
return fragment;
}
@Override
public void onTextQuery(String text) {
arrayAdapter.getFilter().filter(text);
arrayAdapter.notifyDataSetChanged();
}
@Override
public void onAttach(Context context) {
super.onAttach(context);
mIFragmentListener = (IFragmentListener) context;
mIFragmentListener.addiSearch(Tab1.this);
}
@Override
public void onDetach() {
super.onDetach();
if (null != mIFragmentListener)
mIFragmentListener.removeISearch(Tab1.this);
}
}
First Fragment xml tab1.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/listview1">
</ListView>
</RelativeLayout>
Second Fragment Tab2
import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class Tab2 extends Fragment implements ISearch {
private static final String ARG_SEARCHTERM = "search_term";
private String mSearchTerm = null;
private IFragmentListener mIFragmentListener = null;
//Overriden method onCreateView
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
//Returning the layout file after inflating
//Change R.layout.tab1 in you classes
View view = inflater.inflate(R.layout.tab2, container, false);
if(getArguments()!=null) {
mSearchTerm = (String) getArguments().get(ARG_SEARCHTERM);
}
return view;
}
@Override
public void onTextQuery(String text) {
}
@Override
public void onResume() {
super.onResume();
if(null!=mSearchTerm) {
onTextQuery(mSearchTerm);
}
}
public static Tab2 newInstance(String searchTerm){
Tab2 fragment = new Tab2();
Bundle bundle = new Bundle();
bundle.putString(ARG_SEARCHTERM, searchTerm);
fragment.setArguments(bundle);
return fragment;
}
@Override
public void onAttach(Context context) {
super.onAttach(context);
mIFragmentListener = (IFragmentListener) context;
mIFragmentListener.addiSearch(Tab2.this);
}
@Override
public void onDetach() {
super.onDetach();
if(null!=mIFragmentListener)
mIFragmentListener.removeISearch(Tab2.this);
}
}
Second fragment xml tab2.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="Tab 2"
android:textAppearance="?android:attr/textAppearanceLarge"/>
</RelativeLayout>
Third Fragment Tab3
import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import java.util.ArrayList;
//Our class extending fragment
public class Tab3 extends Fragment implements ISearch, IDataCallback {
private static final String ARG_SEARCHTERM = "search_term";
private String mSearchTerm = null;
ArrayList<String> strings = null;
private IFragmentListener mIFragmentListener = null;
ArrayAdapter<String> arrayAdapter = null;
//Overriden method onCreateView
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view;
//Returning the layout file after inflating
//Change R.layout.tab1 in you classes
view = inflater.inflate(R.layout.tab3, container, false);
ListView listView = (ListView) view.findViewById(R.id.listview2);
((MainActivity) getActivity()).setiDataCallback(this);
arrayAdapter = new ArrayAdapter<>(getActivity(), android.R.layout.simple_list_item_1, strings);
listView.setAdapter(arrayAdapter);
if (getArguments() != null) {
mSearchTerm = (String) getArguments().get(ARG_SEARCHTERM);
}
return view;
}
@Override
public void onTextQuery(String text) {
arrayAdapter.getFilter().filter(text);
arrayAdapter.notifyDataSetChanged();
}
@Override
public void onResume() {
super.onResume();
if (null != mSearchTerm) {
onTextQuery(mSearchTerm);
}
}
public static Tab3 newInstance(String searchTerm) {
Tab3 fragment = new Tab3();
Bundle bundle = new Bundle();
bundle.putString(ARG_SEARCHTERM, searchTerm);
fragment.setArguments(bundle);
return fragment;
}
@Override
public void onAttach(Context context) {
super.onAttach(context);
mIFragmentListener = (IFragmentListener) context;
mIFragmentListener.addiSearch(Tab3.this);
}
@Override
public void onDetach() {
super.onDetach();
if (null != mIFragmentListener)
mIFragmentListener.removeISearch(Tab3.this);
}
@Override
public void onFragmentCreated(ArrayList<String> listData) {
strings = listData;
}
}
Third fragment xml tab3.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/listview2">
</ListView>
</RelativeLayout>
Menu for Activity menu.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:yourapp="http://schemas.android.com/apk/res-auto">
<item
android:id="@+id/action_search"
android:icon="@android:drawable/ic_menu_search"
yourapp:showAsAction="collapseActionView|always"
yourapp:actionViewClass="android.support.v7.widget.SearchView"
android:title="Search"/>
</menu>