Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android: how to load fragment into FrameLayout

I'm starting with Android and in my project I'm using this BottomBar. Love it, works pretty well. The code of my application is almost identical on to the one he uses in the tutorial, the only different thing is that my MainActivity extends from AppCompatActivity.

Now what I'm trying to do is to load fragments in the FrameLayout:

<!-- This could be your fragment container, or something -->
<FrameLayout
    android:id="@+id/contentContainer"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_above="@+id/bottomBar"
    />

To do so I'm trying this piece of code, which I found googling for the answer of my issue:

// Create new fragment and transaction
QrCodeFragment newFragment = new QrCodeFragment();
FragmentTransaction transaction = getFragmentManager().beginTransaction();

// Replace whatever is in the fragment_container view with this fragment,
// and add the transaction to the back stack if needed
transaction.replace(R.id.bottomBar, newFragment);
transaction.addToBackStack(null);

// Commit the transaction
transaction.commit();

The line transaction.replace(R.id.bottomBar, newFragment); is complaining about the type of newFragment, which should be android.app.Fragment. My QrCode class: public class QrCodeFragment extends Fragment

As I'm starting with Android today I'm confused if I'm doing the right thing. If I should really load fragments inside the FrameLayout and if so, what am I doing wrong that I can't load it. I know it's the type of my fragment, but I don't know how to create it with the required type. I created it using Android Studio > New > Fragment > Fragment (Blank)

Thanks for any help


UPDATE: I found the solution to my error in this post but even not having the error I still can't see the fragment.

like image 606
André Luiz Avatar asked May 31 '17 18:05

André Luiz


People also ask

How do I add a fragment to FrameLayout?

and then you can use the FragmentManager to create a FragmentTransaction which allows us to add fragments to the FrameLayout at runtime: // Begin the transaction FragmentTransaction ft = getSupportFragmentManager(). beginTransaction(); // Replace the contents of the container with the new fragment ft. replace(R.

How do I attach a fragment to an activity?

Add a fragment to an activity 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.

What is FrameLayout in fragment?

FrameLayout is designed to block out an area on the screen to display a single item. Generally, FrameLayout should be used to hold a single child view, because it can be difficult to organize child views in a way that's scalable to different screen sizes without the children overlapping each other.


2 Answers

First you have a mistake in your Fragment transaction line, according with your layout should be:

transaction.replace(R.id.contentContainer, newFragment); // not R.id.bottomBar

Second, you should use supportFragmentManager instead of fragmentManager to work with support fragments, so implement the following way:

final FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
transaction.replace(R.id.contentContainer, newFragment);
transaction.addToBackStack(null);
transaction.commit();
like image 182
AlexTa Avatar answered Oct 06 '22 01:10

AlexTa


in my case i solve it by using

androidx.fragment.app.FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();

replace your

FragmentTransaction

like image 32
Shadiqur Avatar answered Oct 06 '22 01:10

Shadiqur