Search code examples
androidandroid-fragmentsoptionmenu

How to do an option menu under the action bar in one fragment


I'm developing a social Android app, in which I have three fragments. In my second fragment (friends), I want to have an options menu (search, contacts, etc.)

Something like this:

enter image description here


Solution

  • You have to implement Pageviewer here is example ViewPager

    add in viewpager library in xml file

              <android.support.v4.view.ViewPager
                android:id="@+id/pager"
                android:layout_width="match_parent"
               android:visibility="gone"
                android:layout_height="match_parent">
    
            </android.support.v4.view.ViewPager>
    

    herre is instance of viewpager in oncreate method of your Mainactivity

              viewPager = (ViewPager) findViewById(R.id.pager);
        mAdapter = new MultiPagerAdapter(getSupportFragmentManager());
    
        viewPager.setAdapter(mAdapter);
        viewPager.setCurrentItem(1);
        GetAllApplications();
    
        custmAdapter = new CustomAdapter(this, systemAppsInfo);
        init();
        viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
    
            @Override
            public void onPageSelected(int position) {
    
            }
    
            @Override
            public void onPageScrolled(int arg0, float arg1, int arg2) {
            }
    
            @Override
            public void onPageScrollStateChanged(int arg0) {
            }
        });
        viewPager.setOnTouchListener(new View.OnTouchListener() {
    
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                viewPager.getParent().requestDisallowInterceptTouchEvent(true);
                return false;
            }
        });
    

    Adapter for the pager is here

            public class MultiPagerAdapter extends FragmentPagerAdapter {
    
    public MultiPagerAdapter(FragmentManager fm) {
        super(fm);
    }
    
    @Override
    public Fragment getItem(int index) {
    
        switch (index) {
        case 1:
            // Top Rated fragment activity
            return new ApplicationsFragment();
        case 2:
            // Games fragment activity
            return new SearchAppFragment();
        case 0:
                // Games fragment activity
                return new RecentAppsFragment();
        }
    
    
        return null;
    }
    
    @Override
    public int getCount() {
        // get item count - equal to number of tabs
        return 3;
    }
    

    }

    here is fragment where your search is implemented

    public class SearchAppFragment extends Fragment {
    Utitlity utitlity;
    
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
    
        View rootView = inflater.inflate(R.layout.sec_fragment, container, false);
        final EditText editSearch = (EditText) rootView.findViewById(R.id.edit_search_fragment);
        ListView lv = (ListView) rootView.findViewById(R.id.app_list);
        final CustomAdapter customAdapter = new CustomAdapter(getContext(), MainActivity.systemAppsInfo);
        utitlity = new Utitlity(getContext());
        lv.setAdapter(customAdapter);
        lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                try {
                    Intent LaunchIntent = getContext().getPackageManager().getLaunchIntentForPackage(MainActivity.systemAppsInfo.get(position).getPname());
                    startActivity(LaunchIntent);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });