Reputation: 169
I have a fragment that I would like to display on top of an activity. This is the code that I'm trying right now...
MainActivity.class
FragmentManager fragmentManager = getSupportFragmentManager();
DirectionsFragment directionsFragment = new DirectionsFragment();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.setCustomAnimations(android.R.anim.fade_in, android.R.anim.fade_out);
fragmentTransaction.add(R.id.directions_fragment_framelayout, directionsFragment);
fragmentTransaction.commit();
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
<!-- irrelevant parameters -->
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<FrameLayout
android:id="@+id/directions_fragment_framelayout"
android:layout_width="match_parent"
android:layout_height="match_parent">
</FrameLayout>
<com.mapbox.mapboxsdk.maps.MapView>
<!-- A BUNCH OF CHILDREN -->
</com.mapbox.mapboxsdk.maps.MapView>
</RelativeLayout>
DirectionsFragment.class
public class DirectionsFragment extends Fragment {
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
ViewGroup viewGroup = (ViewGroup) inflater.inflate(R.layout.fragment_directions, container, false);
return viewGroup;
}
}
fragment_directions.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
<!-- irrelevant params -->
android:layout_width="match_parent"
android:layout_height="match_parent">
<ListView
android:id="@+id/directions_listview"
android:layout_width="match_parent"
android:layout_height="match_parent"
<!-- irrelevant params -->
/>
</android.support.constraint.ConstraintLayout>
The activity itself runs fine; when I trigger the fragment to show, it runs through the FragmentTransaction and fragment view inflation fine (confirmed using logs and breakpoints). Since the fragment is being added to a FrameLayout, why isn't the fragment showing up? Thanks in advance!
Upvotes: 0
Views: 182
Reputation: 9395
I think it will be helpfull
Try to swap your FrameLayout and MapView in activity_main.xml
Your code is working fine but its behind the map view thats why its not showing.
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
<!-- irrelevant parameters -->
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<com.mapbox.mapboxsdk.maps.MapView>
<!-- A BUNCH OF CHILDREN -->
</com.mapbox.mapboxsdk.maps.MapView>
<FrameLayout
android:id="@+id/directions_fragment_framelayout"
android:layout_width="match_parent"
android:layout_height="match_parent">
</FrameLayout>
</RelativeLayout>
Add background color in your fragment layout because fragment loaded but transparent layout.
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
android:layout_width="match_parent"
android:background="@android:color/white"
android:layout_height="match_parent">
<ListView
android:id="@+id/directions_listview"
android:layout_width="match_parent"
android:layout_height="match_parent"
<!-- irrelevant params -->
/>
</android.support.constraint.ConstraintLayout>
Upvotes: 1