How to call a fragment inside a Fragment? - android

So I'm working in Fragments that has 2 Fragments in it. take a look at the image below:
The first fragment that shows the list of the inventory is named fragment_purchase_item_list and on the right side is the fragment_purchase_list
What I want to do is when I clicked SCAN from the toolbar on the right fragment, fragment_qrscan will replace fragment_purchase_item_list. Like this image below:
Can someone please see and check why my code is not working? Thanks!
PurchaseListFragment.java
<-- start of code snippet -->
#Override
public void onClick(View v) {
int id = v.getId();
switch (id){
case R.id.btn_paypurchasetransaction:
Fragment chargeFragment = new ChargeFragment();
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.customersales_content, chargeFragment);
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();
break;
case R.id.layout_transaction_qrcode:
// Toast.makeText(getActivity(), "Scan Item", Toast.LENGTH_SHORT).show();
Fragment scanQRCodeFragment = new QRScanFragment();
FragmentManager fragmentManager1 = getFragmentManager();
FragmentTransaction fragmentTransaction1 = fragmentManager1.beginTransaction();
fragmentTransaction1.replace(R.id.inventory_fragment, scanQRCodeFragment);
fragmentTransaction1.addToBackStack(null);
fragmentTransaction1.commit();
break;
case R.id.layout_transaction_new:
Toast.makeText(getActivity(), "New Transaction", Toast.LENGTH_SHORT).show();
break;
}
}
<-- end of code snippet>
fragment_sales.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".Fragments.SalesFragment"
android:orientation="horizontal"
android:id="#+id/customersales_content">
<fragment
android:id="#+id/inventory_fragment"
android:name="com.example.devcash.Fragments.PurchaseItemListFragment"
tools:layout="#layout/fragment_purchase_item_list"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="3">
</fragment>
<View
style="#style/Divider"
android:layout_width="1dp"
android:layout_height="wrap_content" />
<fragment
android:id="#+id/purchaselist_fragment"
android:name="com.example.devcash.Fragments.PurchaseListFragment"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="2"
tools:layout="#layout/fragment_purchase_list">
</fragment>
</LinearLayout>

Try replacing getFragmentManager() with getChildFragmentManager()

Related

Android Fragment animation on replacement doesn't work

I've got an activity in which there are 2 fragments which changes basing on a button click.
I'd like to show an animation when the replacement appens, so i searched for the code and here's what i concluded:
public void displayView(int page){
switch (page){
case 1:
FragmentHome fragmentHome = new FragmentHome();
getSupportFragmentManager().beginTransaction()
.replace(R.id.fragment, fragmentHome)
.setCustomAnimations(android.R.anim.fade_in, android.R.anim.fade_out).commit();
break;
case 2:
FragmentHomeBack fragmentHomeBack = new FragmentHomeBack();
getSupportFragmentManager().beginTransaction()
.replace(R.id.fragment, fragmentHomeBack)
.setCustomAnimations(android.R.anim.accelerate_decelerate_interpolator, android.R.anim.anticipate_interpolator).commit();
break;
}
}
I don't know why but the animations don't works, the new fragment just replace the other without animation.
Here's the activity layout code:
<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:id="#+id/activity_master"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="#dimen/space10"
tools:context="context">
<include
android:id="#+id/ll_left"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="#dimen/space5"
android:orientation="vertical"
android:layout_marginLeft="#dimen/space5"
android:layout_marginRight="#dimen/space15"
layout="#layout/menu"
android:layout_alignParentTop="true"
android:layout_above="#+id/imageView" />
<FrameLayout
android:id="#+id/fragment_holder"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_toRightOf="#id/ll_left">
</FrameLayout>
<ImageView
app:srcCompat="#drawable/xlink"
android:id="#+id/imageView"
android:layout_width="150dp"
android:layout_alignParentBottom="true"
android:layout_alignParentStart="true"
android:scaleType="fitEnd"
android:layout_height="70dp" />
The frame layout "fragment_holder" is where the Fragments are.
Thanks to everybody!
That should do the trick buddy. I have the same code and it works well.
getSupportFragmentManager()
.beginTransaction()
.setCustomAnimations(android.R.anim.fade_in,android.R.anim.fade_out)
.replace(R.id.layout_container, fragment)
.commit();
But..
I see what is the difference in our code. Try setting customAnimations before replace to see what happens.
And I have a suggestion to improve your code. In your "if" or "switch" statement set the fragment like
Fragment fragment;
if(page==0){
fragment = new MyFragment();
}else{
fragment = new MySecondFragment();
}
then you only have this code once
getChildFragmentManager()
.beginTransaction()
.setCustomAnimations(android.R.anim.fade_in,android.R.anim.fade_out)
.replace(R.id.layout_container, fragment)
.commit();

Navigation drawer fragments overlap even with replace()

I have a navigation drawer in an activity. The activity's layout has a relative layout for a fragment. The id of relativelayout is mainContent in which I load the fragment(call it fragA) in the onCreate() of the activity. I have this same fragment also in the navigation drawer along with other navigation items. It is loaded oncreate of activity and can also be loaded from navigation drawer.
On selecting the navigation items, I replace the fragment in the mainContent which is the relativelayout in the activity with replace(). In the navigation drawer, the 0th item is the "fragB"(the same as fragA but in nav drawer). Second is another fragment(fragC). When I select fragB "more than once" and then select fragA and press back button, fragB and fragC overlap.
And also I have to keep pressing back button to go to initial screen because every time I select an item from nav drawer, new fragments fragBs and fragCs are created. Not replaced. I replace them in mainContent(first relative layout) in the activity's layout. I'm using FrameLayout for the fragments.
This is the activity's layout in which I place fragments:
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/drawerLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.prematixsofs.taxiapp.DateVehiclePicker">
<!-- The main content view -->
<RelativeLayout
android:id="#+id/mainContent"
android:layout_width="match_parent"
android:layout_height="match_parent">
<include
layout="#layout/footer"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_gravity="center_horizontal|bottom"></include>
</RelativeLayout>
<!-- The navigation drawer -->
<RelativeLayout
android:id="#+id/drawerPane"
android:layout_width="230dp"
android:layout_height="match_parent"
android:layout_gravity="start">
<!-- Profile Box -->
<RelativeLayout
android:id="#+id/profileBox"
android:layout_width="match_parent"
android:layout_height="80dp"
android:background="#ff02c7c4"
android:padding="8dp">
<ImageView
android:id="#+id/avatar"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_marginTop="15dp"
android:src="#drawable/user" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="42dp"
android:layout_centerVertical="true"
android:layout_marginLeft="15dp"
android:layout_toRightOf="#+id/avatar"
android:orientation="vertical">
<TextView
android:id="#+id/userName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:clickable="true"
android:textColor="#fff"
android:textSize="16sp"
android:textStyle="bold" />
<TextView
android:id="#+id/viewProfile"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:layout_marginTop="4dp"
android:text="View Profile"
android:textColor="#fff"
android:textSize="12sp" />
</LinearLayout>
</RelativeLayout>
<!-- List of Actions (pages) -->
<ListView
android:id="#+id/navList"
android:layout_width="280dp"
android:layout_height="match_parent"
android:layout_below="#+id/profileBox"
android:background="#ffffffff"
android:choiceMode="singleChoice" />
<TextView
android:id="#+id/invisibleTextViewId"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="gone" />
</RelativeLayout>
This is the Activity's code where I load the fragA:
android.support.v4.app.FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.add(R.id.mainContent, new DateTimePicker(),"tags").commit();
This is nav drawer onSelect:
private void selectItemFromDrawer(int position) {
Fragment fragment = null;
switch (position){
case 0: fragment = new DateTimePicker();
break;
case 1: fragment = new PreferencesFragment();
break;
case 2:
mDrawerLayout.closeDrawer(mDrawerPane);
AlertDialog.Builder alertDialog = new AlertDialog.Builder(DateVehiclePicker.this);
alertDialog.setTitle("Logout");
// Setting Dialog Message
alertDialog.setMessage("Do you want to Logout?");
alertDialog.setPositiveButton("YES", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
sessionManager.logoutUser();
}
});
alertDialog.setNegativeButton("NO", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
Intent intent = new Intent(getApplicationContext(), MainActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.putExtra("EXIT", true);
startActivity(intent);
dialog.cancel();
}
});
alertDialog.show();
}
if(fragment!=null)
{
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.addToBackStack(null).replace(R.id.mainContent, fragment);
fragmentTransaction.commit();
mDrawerLayout.closeDrawer(mDrawerPane);
}
This is where I set listener for nav drawer:
mDrawerList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
selectItemFromDrawer(position);
}
});
I call the method selectItemFromDrawer(position);
You must handle BackStack for this you must clear all fragment which is on stack when you added or replace with main fragment
#Override
public void onBackPressed() {
Logg.e(TAG, getFragmentManager().getBackStackEntryCount());
try {
if (mNavigationDrawerFragment.isDrawerOpen())
{
mNavigationDrawerFragment.closeDrawer();
}
if (getFragmentManager().getBackStackEntryCount() > 1) {
getFragmentManager().popBackStack();
} else
{ Exit();
} catch (Exception e) {
e.printStackTrace();
}
}
Problem is you are creating new object for fragment transaction everytime you are trying to change fragment. Make it global in your code and use same instance everywhere.
Here is the code with switch case instead of if else
private void displayView(int position) {
Fragment fragment = null;
String title = getString(R.string.app_name);
switch (position) {
case 0:
fragment=new FragmentHome();
title="Home";
break;
case 1:
fragment=new FragmentAddress();
title="Update Address";
break;
case 2:
fragment=new FragmentHelpList();
title="My Helps";
break;
case 3:
fragment=new FragmentCompletedHelps();
title="History";
break;
case 4:
fragment=new FragmentSupport();
title="Account & Support";
break;
case 5:
fragment=new FragmentAbout();
title="About";
break;
case 6:
appSharedPreferences.setSession(false);
finish();
break;
default:
break;
}
if (fragment != null) {
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.container_body, fragment);
fragmentTransaction.commit();
// set the toolbar title
getSupportActionBar().setTitle(title);
}
Make the fragment variable global too
Try changing R.id.mainContent to R.id.container. I have similar setup but it is working fine with R.id.container.
fragmentManager.beginTransaction().replace(R.id.container,
new Registration_fragment()).commit();
and you have to replace FragA (not add) also initially during load.
fragmentTransaction.replace(R.id.mainContent, new DateTimePicker(),"tags").commit();

change class in fragment throw activity

i have three fragments, and one activity.
i have put the two fragments side by side in the activity and i would like when click on the first fragment, to change it to the third one.
activity :
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:baselineAligned="false"
android:orientation="horizontal"
android:id="#+id/parent">
<fragment
android:id="#+id/mainPage"
android:layout_width="0dp"
android:layout_weight="0.7"
android:layout_height="wrap_content"
class="com.example.user1.lotus.MainMenu" >
</fragment>
<fragment
android:id="#+id/side_bar"
android:layout_width="0dp"
android:layout_weight="0.3"
android:layout_height="wrap_content"
class="com.example.user1.lotus.SideBar" >
</fragment>
</LinearLayout>
now i would like to change class="com.example.user1.lotus.MainMenu"
to class="com.example.user1.lotus.Starter" through my activity
Is this possible?
in activity
public void onFragmentInteraction(String s){
switch (s){
case "menu":
break;
case "starters":
break;
case "mainDish":
MainDish newFragment = new MainDish();
FragmentTransaction transaction = getFragmentManager().beginTransaction();
transaction.replace(R.id.mainPage, newFragment);
transaction.addToBackStack(null);
transaction.commit();
and in the fragment
btnMainDish.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v)
{
mListener.onFragmentInteraction("mainDish");
}
});
Just do this in the onClick listener method of the button your fragment will get replaced by the third fragment:
ThirdFragment newFragment = new ThirdFragment();
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.layout_replace, newFragment);
transaction.addToBackStack(null);
// Commit the transaction
transaction.commit();
Hope this helps.

Fragment Default View Wont Change

i really need help.
This is how my application looks like. I got two(2) fragments where the left side is for the buttons and for the right side is for my details.
The bigger fragment has a default display which i called splash. An image is viewed here, a bigger image that would take the height of the device.
So my case is when i click/press button 1, it would display the fragment for that but the image is still present.
My question now is, how to i get rid of the 'splash display'?
Here is my code
/**
* on main listener
*/
#Override
public void onMainListener(String whatPress){
Fragment fr = null;
if(whatPress.equals("button1"))
fr = new FragmentOne();
if(whatPress.equals("button2"))
fr = new FragmentTwo();
if(whatPress.equals("button3"))
fr = new FragmentThree();
FragmentManager fm = getFragmentManager();
FragmentTransaction fragmentTransaction = fm.beginTransaction();
fragmentTransaction.replace(R.id.detailFragment, fr, whatPress);
fragmentTransaction.commit();
}
The code above is the one handles which button is pressed.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<fragment
android:id="#+id/menuFragment"
android:name="FragmentMenu"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="match_parent"
class="com.mizsh.Fragment_Menu" />
<fragment
android:id="#+id/detailFragment"
android:name="com.mizsh.Fragment_Splash"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="match_parent" />
</LinearLayout>
The code above is my main.xml
You should replace the detailFragment with FrameLayout
<FrameLayout android:id="#+id/detailFragment" android:layout_weight="1"
android:layout_width="0px" android:layout_height="match_parent" />
and replace it with Fragment_Splash in the onCreate of the activity
FragmentTransaction fragmentTransaction = fm.beginTransaction();
fragmentTransaction.replace(R.id.detailFragment, fragmentSplash, null);
fragmentTransaction.commit();
then replace with chosen fragment on click
FragmentTransaction fragmentTransaction = fm.beginTransaction();
fragmentTransaction.replace(R.id.detailFragment, chosenFragment, null);
fragmentTransaction.commit();

Changing to DrawerLayout in Portrait Mode in Android

I've got a question.
I have a fragment, which inflates two other fragments (Fragment A is left, Fragment B is right)
I want to hide Fragment A when the user changes the mode to portrait-mode.
I added in the layout-port/xml a DrawerLayout and added the code for the NavigationDrawer in my Fragment, but it doesn't work.
Does anyone have an idea, how to realize it?
Thanks :)
I've solved the first problem with this code:
#Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
orientation = newConfig.orientation;
switch (orientation) {
case Configuration.ORIENTATION_LANDSCAPE:
FragmentA fragmentleft = new FragmentA();
FragmentB fragmentright = new FragmentB();
FragmentTransaction transaction1 = getSupportFragmentManager()
.beginTransaction();
FragmentTransaction transaction2 = getSupportFragmentManager()
.beginTransaction();
transaction1.replace(R.id.framelayout_left, fragmentleft);
transaction2.replace(R.id.framelayout_right, fragmentright);
transaction1.commit();
transaction2.commit();
break;
case Configuration.ORIENTATION_PORTRAIT:
createNavigationDrawer();
Log.d("TEST", "Portrait");
break;
}
}
But now I have the problem, that i have to inflate FragmentA in my ListView of the DrawerLayout:
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<FrameLayout
android:id="#+id/framelayout_right"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<ListView
android:id="#+id/framelayout_left_drawer"
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:background="#111"
android:choiceMode="singleChoice"
android:divider="#android:color/transparent"
android:dividerHeight="0dp" />
But how can I do this?
It doesn't work properly :(
I had also tried to change the ListView to a FrameLayout and inflate the FragmentA within it, but it didn't work too:(
I would be very thankful, if somebody could help me :)

Categories

Resources