Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I hide a fragment on start of my MainActivity( or the application)?

Well the title says it all, I have no idea how to hide it on startup. I tried searching but nothing happened in the way that I want it to happen.

So what I want to happen is that I want the BottomFragment to be hidden on start up. I want it to show up when I click this button in the TopFragment and the TopFragment is visible at start up.

My MainActivity.xml codes:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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="rjj.tutorial_fragmentsthe3rd.MainActivity">

    <fragment
        class="rjj.tutorial_fragmentsthe3rd.TopFragment"
        android:id="@+id/topFragment"
        android:layout_width="match_parent"
        android:layout_height="300dp" />
    <fragment
        class="rjj.tutorial_fragmentsthe3rd.BottomFragment"
        android:id="@+id/bottomFragment"
        android:layout_width="match_parent"
        android:layout_height="300dp"
        android:layout_alignParentBottom="true"
        android:layout_alignParentStart="true"
        android:layout_below="@+id/topFragment" />

</RelativeLayout>

My TopFragment.xml codes:

<?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">

    <Spinner
        android:id="@+id/origin"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentStart="true"
        android:layout_alignParentTop="true"
        android:entries="@array/origins"/>

    <Spinner
        android:id="@+id/destination"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/origin"
        android:layout_alignParentStart="true"
        android:layout_marginTop="41dp"
        android:entries="@array/destinations"/>

    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/destination"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="80dp"
        android:text="@string/button_caption" />
</RelativeLayout>

My BottomFragment.xml Codes:

<?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">

    <EditText
        android:id="@+id/flightQueryResult"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="61dp"
        android:inputType="textPersonName"
        android:text="Name" />
</RelativeLayout>

My MainActivity.java codes:

package rjj.tutorial_fragmentsthe3rd;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

public class MainActivity extends AppCompatActivity implements TopFragment.FlightSearcher{

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

    }

    @Override
    public void searchForFlights(String origin, String destination) {
        BottomFragment bottomFragment = (BottomFragment)getSupportFragmentManager()
                .findFragmentById(R.id.bottomFragment);
        int randomPrice = (int)(Math.random()*200);
        String resultString = origin + " - " + destination + " = " + randomPrice;
        bottomFragment.displayFlightQueryResult(resultString);

    }
}

My TopFragment.java codes:

package rjj.tutorial_fragmentsthe3rd;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.Spinner;

public class TopFragment extends Fragment {

    private FlightSearcher interfaceImplementer;
    public interface FlightSearcher{
        public void searchForFlights(String origin, String destination);
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        this.interfaceImplementer =(FlightSearcher)context;
    }

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

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        Button flightSearchButton = (Button)getActivity().findViewById(R.id.button);
        flightSearchButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Spinner originSpinner = (Spinner)getActivity().findViewById(R.id.origin);
                Spinner destinationSpinner = (Spinner)getActivity().findViewById(R.id.destination);

                interfaceImplementer.searchForFlights(originSpinner.getSelectedItem().toString(),destinationSpinner.getSelectedItem().toString());
            }
        });
    }
}

My BottomFragment codes:

package rjj.tutorial_fragmentsthe3rd;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;


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

    public void displayFlightQueryResult(String result){
        EditText resultField = (EditText)getActivity().findViewById(R.id.flightQueryResult);
        resultField.setText(result);
    }
}
like image 647
Yahj Rodriguez Avatar asked Aug 18 '17 15:08

Yahj Rodriguez


People also ask

Can we hide the menu on the toolbar in one fragment?

How do we hide the menu on toolbar in one fragment? When you click the show button to open a fragment, you can see the fragment menu items ordered before activity menu items. This is because of the menu item's android:orderInCategory attribute value. When you click the hide button to hide the fragment.

How add and remove fragments?

Use replace() to replace an existing fragment in a container with an instance of a new fragment class that you provide. Calling replace() is equivalent to calling remove() with a fragment in a container and adding a new fragment to that same container. transaction. commit();

How do I show fragments in activity XML?

You can add your fragment to the activity's view hierarchy either by defining the fragment in your activity's layout file or by defining a fragment container in your activity's layout file and then programmatically adding the fragment from within your activity.

Why you should use fragments on your Android application?

Fragments introduce modularity and reusability into your activity's UI by allowing you to divide the UI into discrete chunks. Activities are an ideal place to put global elements around your app's user interface, such as a navigation drawer.


1 Answers

On start hide fragment like this:

FragmentManager manager = getSupportFragmentManager();
FragmentTransaction ft = manager.beginTransaction();
Fragment bottomFragment = manager.findFragmentById(R.id.bottomFragment);
ft.hide(bottomFragment);
ft.commit();

Then you can again show it:

FragmentManager manager = getSupportFragmentManager();
FragmentTransaction ft = manager.beginTransaction();
Fragment bottomFragment = manager.findFragmentById(R.id.bottomFragment);
ft.show(bottom);
ft.commit();
like image 94
Fori Avatar answered Oct 10 '22 21:10

Fori