user2456977
user2456977

Reputation: 3964

How to show/hide fragment on button click

I have 2 Fragments - ButtonFragment and ListViewFragment - in my Activity MainActivity.

ButtonFragment contains a Button, ListViewFragment contains a ListView.

Each time I click on the ButtonFragment Button I want the ListViewFragment to show/hide.

How do I code this properly?

Currently my code looks like this:

MainActivity.java

public class MainActivity extends Activity implements Communicator {

    ButtonFragment buttonFrag;
    ListViewFragment listviewFrag;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        buttonFrag= new ButtonFragment();
        listviewFrag = new ListViewFragment();

        manager = getFragmentManager();
        FragmentTransaction transaction = manager.beginTransaction();
        transaction.add(R.id.button_fragment, buttonFrag, "Fragment1");
        transaction.add(R.id.listview_fragment, listviewFrag, "Fragment2");
        transaction.commit();
    }

}

ButtonFragment.java

public class DynamicButtonsFragment extends Fragment implements View.OnClickListener {

    Button btn;

    @Override
    public View onCreateView(LayoutInflater inflater,
            @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {       
        return inflater.inflate(R.layout.button_fragment_layout, container, false);
        }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
    }

    @Override
    public void onClick(View v) {
        //?? hide listview fragment from here ??
    }

}

ListViewFragment.java

public class ListViewFragment1 extends Fragment {

    protected ArrayAdapter<String> adapter1;

    @Override
    public View onCreateView(LayoutInflater inflater,
            @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {       

        return inflater.inflate(R.layout.list_view_fragment, container, false);
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

    }

}

So my question is where do I implement the showing/hiding of ListViewFragment? I feel like I should send data to the MainActivity through the onClick method of ButtonFragment. But I do not know how to do so.

Or do I only add code in the MainActivity since the MainActivity has access to all the Fragments?

I am having trouble becase the Button is in a Fragment, not part of the MainActivity. I haven't really seen cases like this...

Can someone please help?

Upvotes: 1

Views: 10232

Answers (4)

Sjd
Sjd

Reputation: 1261

Do this ..

android.app.Fragment fragment = getActivity().getFragmentManager().findFragmentByTag("YOUR_FRAGMENT_TAG");
        getActivity().getFragmentManager().beginTransaction().hide(fragment);

inside your click event!

One more thing when you add fragments like this..

    transaction.add(R.id.button_fragment, buttonFrag, "Fragment1");
    transaction.add(R.id.listview_fragment, listviewFrag, "Fragment2");

you're expected to provide the container id instead of the id of the fragment.

Example: For MainActivity container use R.id.containerMain

Upvotes: 1

The Original Android
The Original Android

Reputation: 6215

You cannot show/hide a Fragment directly. You may show/hide a UI object like Listview. If you like, you can show/hide Fragment indirectly by using the FragmentTransaction, and you can call its method add, remove or replace. A link for sample code is Fragments

Upvotes: 1

Kirill Volkov
Kirill Volkov

Reputation: 942

If you have fragments within the same layout, you can use the following code: http://www.java2s.com/Code/Android/Core-Class/Demonstrationofhidingandshowingfragments.htm

If not, than you can use several possibilities... You can use an Intent to send data to MainActivity. You can have a singleton instance where you store pointer to your MainActivity.

You can also use Handler to send messages, but the ways discribed above are easier to implement and should be enough for you.

Upvotes: 0

Deyu瑜
Deyu瑜

Reputation: 544

If you in fragment want to do some MainActivity function , you can try

         @Override
        public void onClick(View v) {
            //?? hide listview fragment from here ??
            ((MainActivity)getActivity()).hidelistView();
            //hidelistView you should imp in your MainActivity 
        }

Upvotes: 0

Related Questions