Search code examples
androidbuttonandroid-fragmentsactionbarsherlock

How do you start a fragment when a button is clicked?


I have tried several methods but I can't seem to make them work.

Here's my main activity

and when the button is clicked, it should open the start of the fragment,

like so

A little help will be very much appreciated.

By the way, I'm using the actionSherlock plugin for the fragments


Solution

  • Here is your Activity with 2 tabs,

    public class MainActivity extends SherlockFragmentActivity {
        /** Called when the activity is first created. */
    
        public Fragment frag1;
        public Fragment frag2;
        @Override
        public void onCreate(Bundle savedInstanceState) {
    
            super.onCreate(savedInstanceState);
    
            ActionBar bar = getSupportActionBar();
            bar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
    
            ActionBar.Tab tab1 = bar.newTab();
            ActionBar.Tab tab2 = bar.newTab();
    
            tab1.setText("");
            tab1.setIcon(R.drawable.abs__ic_menu_share_holo_dark);
            tab2.setText("");
            tab2.setIcon(R.drawable.abs__ic_voice_search);
    
    
    
    
                tab1.setTabListener(new MyTabListener());
                tab2.setTabListener(new MyTabListener());
    
                bar.addTab(tab1);
                bar.addTab(tab2);
    
    
    
        }
    
        private class MyTabListener implements ActionBar.TabListener {
            @Override
            public void onTabSelected(Tab tab, FragmentTransaction ft) {
    
                switch (tab.getPosition()) {
                case 0:
    
                    if (frag1 == null) {
                        // If not, instantiate and add it to the activity
                        frag1 = Fragment.instantiate(getApplicationContext(),
                                FeedsActivity.class.getName());
                        ft.add(android.R.id.content, frag1, "Feeds");
                    } else {
                        // If it exists, simply attach it in order to show it
                        ft.show(frag1);
                    }
                    return;
    
                case 1:
                    if (frag2 == null) {
                        // If not, instantiate and add it to the activity
                        frag2 = Fragment.instantiate(getApplicationContext(),
                                ProfileActivity.class.getName());
                        ft.add(android.R.id.content, frag2, "Profile");
                    } else {
                        // If it exists, simply attach it in order to show it
                        ft.show(frag2);
                    }
                    return;
    
    
            }
    
            @Override
            public void onTabUnselected(Tab tab, FragmentTransaction ft) {
                // TODO Auto-generated method stub
                if (frag1 != null) {
                    // Detach the fragment, because another one is being attached
                    switch (tab.getPosition()) {
                    case 0:
                        ft.hide(frag1);
                        return;
                    case 1:
                        ft.hide(frag2);
                        return;
    
                    }
    
                }
            }
    
            @Override
            public void onTabReselected(Tab tab, FragmentTransaction ft) {
                // TODO Auto-generated method stub
            }
        }
    }
    

    Start this activity when button clicks

    fragm1.java

        public class fragm1 extends Fragment {
    
    
    @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {
            // Inflating layout
            View v = inflater.inflate(R.layout.activity_fragment, container, false);
            // We obtain layout references
    
            return v;
    
        }
    
        @Override
        public void onViewCreated(View view, Bundle savedInstanceState) {
            super.onViewCreated(view, savedInstanceState);
    
            // Button reset=(Button)findViewById(R.id.reset);
    
    
    
    
        }
    
    
    
    
    
    }