I have created a fragment, and used this inside another Fragment layout.
This is the XML of my layout.
<WebView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/wv_facebook"
>
</WebView>
<fragment
class="com.loading.LoadingAnimation"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/wv_facebook_loadingAnimation"
/>
Now i want to get the ID of this fragment. I used following procedure but i am getting incompatible type error message.
Loading Animation loadingAnimation;
loadingAnimation = (LoadingAnimation) getFragmentManager().findFragmentById(R.id.wv_facebook_loadingAnimation);
Required LoadingAnimation Found android.support.v4.app.fragment
You need to call getChildFragmentManager() instead of getFragmentManager().
You may try below sample too:
DashboardFragment innerFragment = new DashboardFragment();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
innerFragment .setEnterTransition(new Slide(Gravity.RIGHT));
innerFragment .setExitTransition(new Slide(Gravity.LEFT));
}
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
folderName = mFileList.get(position).getName();
fragmentTransaction.add(R.id.content_frame, innerFragment, folderName);
fragmentTransaction.addToBackStack(folderName);
fragmentTransaction.commit();
Related
I have created an Activity containing a FrameLayout with the ID "container", though I cannot refer to this view, I have tested it in a simple reference:
activity layout:
<FrameLayout 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"
android:gravity="center_horizontal"
android:orientation="vertical"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context="app.chrono.name.LoginActivity">
<FrameLayout
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent">
</FrameLayout>
</FrameLayout>
Activity:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
FragmentManager fragmentManager = activity.getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.remove(current);
fragmentTransaction.add(R.id.container, new LoginFragment()); //container is written in red
}
I do not understand why this container cannot be refered to. It should be accessible. It is written in red and seems to not exist.
Your code is right,
Click on the line that is below your code findviewbyid and one suggestion comes press alt+enter that's all.
Also try to invalidate cache and restart :
Go to file and invalidate cache and restart.
Container is a view but in specific it is a layout view i.e FrameLayout.
So change the type to FrameLayout instead of View.
FrameLayout container = (FrameLayout)findViewById(R.id.container);
EDIT:
Also if you find first solution not working add the id directly in the fragment transition like this.
fragmentTransaction.add(R.id.container, new LoginFragment());
You should commit(); your fragment transaction.
FragmentManager fragmentManager = activity.getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.remove(current);
fragmentTransaction.add(R.id.container, new LoginFragment());
fragmentTransaction.commit();
First remove this line from your code
container = findViewById(R.id.container);
Use this function for opening fragment
public void loadFragment(Fragment fragment) {
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.remove(current);
fragmentTransaction.add(R.id.container, new LoginFragment());
}
and call in onCreate like this
loadFragment(new LoginFragment());
And make sure you import these
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
I am trying to add multiple fragments to a FrameLayout. I need to add them one below another. But they are overlapping.
main_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/fragment_container"
android:layout_width="match_parent"
android:layout_height="match_parent">
</FrameLayout>
MainActivity.java
public class MainActivity extends FragmentActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_layout);
// Create instance of fragments
FragmentPrimaryList firstFrag = new FragmentPrimaryList();
FragmentSecondaryList secFrag = new FragmentSecondaryList();
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
// add fragment to the fragment container layout
fragmentTransaction.add(R.id.fragment_container,firstFrag);
fragmentTransaction.add(R.id.fragment_container,secFrag);
fragmentTransaction.commit();
}
}
They looks like this. How can I solve this?
Use the replace method instead of the add method.
The replace method hides the current fragment before adding new one, the add method simply adds the new fragment without disposing off the older fragment.
In order to retain the back stack, use the fragment transaction with backstack management.
I need to display both fragments at same time, one below another
You can try the following layout:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/fragment_container"
android:layout_width="match_parent"
android:layout_height="match_parent">
</FrameLayout>
</LinearLayout>
Now add fragmments using:
fragmentTransaction.add(R.id.fragment_container1,firstFrag);
make sure you Fragment's layout_height = "wrap_content"
Add background to your fragments and make "android:clickable="true" to parent layout.
Do this way:
First fragment
FragmentPrimaryList firstFrag = new FragmentPrimaryList();
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
// add fragment to the fragment container layout
fragmentTransaction.add(R.id.fragment_container,firstFrag);
fragmentTransaction.commit();
second fragment
FragmentSecondaryList secFrag = new FragmentSecondaryList();
fragmentManager = getSupportFragmentManager();
fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.add(R.id.fragment_container,secFrag);
fragmentTransaction.commit();
Fragment doesn't work when using fragment tag in XML layout. But, after replace fragment tag to Framelayout it works fine without making any change in my code.
When i use the code below fragment doesn't change/work.
<fragment
android:id="#+id/fragment_container"
android:name="com.example.user.example.MyFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#+id/btnPressMe"
/>
But, when i replace the above code with the code below it changes/works.
<FrameLayout
android:id="#+id/fragment_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#+id/btnPressMe">
</FrameLayout>
Java code:
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
Fragment2 fragment2 = new Fragment2();
fragmentTransaction.replace(R.id.fragment_container, fragment2);
fragmentTransaction.commit();
if you want to infalte to another fragment you must use
getChildFragmentManager() instead of getFragmentManager()
FragmentManager fobject = getChildFragmentManager();
FragmentTransaction transaction = fobject .beginTransaction();
fragment_container= fobject .findFragmentById(R.id.fragment_container);
if (fragment_container!= null) {
transaction.replace(R.id.fragment_container, yournewfragment);
transaction.commit();
}
transaction.commit();
I have code like this to take my currently shown fragment in my activity and show a different fragment. Sometimes the first view is displayed on top of the other view.
What could cause that? Is there a better way to do this?
android.app.FragmentTransaction fragmentTransaction = getFragmentManager().beginTransaction();
fragmentTransaction.remove(from);
fragmentTransaction.commit();
getFragmentManager().executePendingTransactions();
fragmentTransaction = getFragmentManager().beginTransaction();
fragmentTransaction.add(R.id.container, to);
if(showBackButton) {
fragmentTransaction.addToBackStack(to.toString());
}
fragmentTransaction.commit();
You might try the replace command instead. Something like:
FragmentTransaction transaction = fm.beginTransaction();
transaction.replace(R.id.fragment_container, iFrag);
transaction.commit();
The fragment_container is just a FrameLayout inside its own XML file:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/fragment_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="?android:attr/actionBarSize"
android:background="#888888" />
iFrag is an instantiated class that extends Fragment.
i am using the following layout for fragment transaction. i am replacing the fragment1
with new fragments by using the following code.i am attaching the layout file also for your reference. i am executing the below code for every listitem click.
Code:
Fragment newFragment = new Grades();
android.app.FragmentTransaction transaction = getFragmentManager().beginTransaction();
// Replace whatever is in the fragment_container view with this fragment
transaction.replace(R.id.fragment1, newFragment);
// Commit the transaction
transaction.commit();
Fragment newFragment = new Teachers();
android.app.FragmentTransaction transaction = getFragmentManager().beginTransaction();
// Replace whatever is in the fragment_container view with this fragment
transaction.replace(R.id.fragment1, newFragment);
// Commit the transaction
transaction.commit();
<LinearLayout
android:id="#+id/slidingPanel"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="left"
android:orientation="vertical"
android:background="#android:color/white"
>
<fragment
android:id="#+id/fragment1"
android:name="com.example.slideoutmenu.Item3"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</Linearlayout>
my doubt is should i replace the fragment1 everytime when i show a new fragment or should i replace the existing fragment if so how can i replace the existing fragment.
To replace the fragment:-
FragmentTransaction fragmentTransaction = getFragmentManager().beginTransaction();
YourFragment yourFragment = new YourFragment();
fragmentTransaction.replace(R.id.top_layout, yourFragment ); // top_layout is parent layout / viewgroup where you want to place your new fragment
fragmentTransaction.commit();