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();
Related
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()
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.
I am changing fragments inside framelayout.This is my main activity layout:
<?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"
android:orientation="vertical" xmlns:app="http://schemas.android.com/apk/res/com.impact.ribony">
<com.astuetz.PagerSlidingTabStrip
android:id="#+id/tabs"
android:layout_width="match_parent"
android:layout_height="48dip"
android:visibility="gone"
app:pstsIndicatorColor ="#0B0B16"
app:pstsShouldExpand="true" />"
<FrameLayout
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
<android.support.v4.view.ViewPager
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/pager"
android:visibility="gone"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/tabs">
</android.support.v4.view.ViewPager>
</RelativeLayout>
Firstly I am showing login fragment then user logins I am hiding framelayout.Because I am using viewpager.If user wants go to settings I am loading settings fragment then I am showing framelayout again.
But I have a problem,you can see it here: http://youtu.be/H6AO7yYusjM
You can see when user goes to settings fragment,firstly login fragment showing for 1 second then settings fragment is loading.How can I prevent this ? (When I try to change fragment always showing last fragment for 1 second not just login fragment)
If user logins successfully I am removing login fragment with following lines:
pager.setVisibility(View.VISIBLE);
tabs.setVisibility(View.VISIBLE);
FrameLayout layout = (FrameLayout)findViewById(R.id.container);
layout.setVisibility(View.GONE);
I am loading login fragment with following code:
FrameLayout layout = (FrameLayout)findViewById(R.id.container);
layout.setVisibility(View.VISIBLE);
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
LoginFragment conv = new LoginFragment();
loginObj=LoginFragment.newInstance("asd");
fragmentTransaction.replace(R.id.container, loginObj,"LoginFragment");
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();
actionBar.hide();
pager.setVisibility(View.GONE);
I am loading settings fragment with following code:
FrameLayout layout=(FrameLayout)findViewById(R.id.container);
pager.setVisibility(View.GONE);
tabs.setVisibility(View.GONE);
FragmentManager fragmentManager=getSupportFragmentManager();
FragmentTransaction fragmentTransaction=fragmentManager.beginTransaction();
SettingsFragment conv=new SettingsFragment();
SettingsFragment.newInstance(LOGGED_USERNAME);
fragmentTransaction.replace(R.id.container,conv);
fragmentTransaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();
layout.setVisibility(View.VISIBLE);
I've got a ListFragment with this:
public void fragmentSelection(Integer count) {
Fragment fr;
if (count == 0) {
fr = new Fragment1();
} else {
fr = new Fragment2();
}
FragmentManager fm = getFragmentManager();
FragmentTransaction fragmentTransaction = fm.beginTransaction();
fragmentTransaction.replace(R.id.content, fr, fr.getClass().getName());
fragmentTransaction.commit();
}
here I am trying to replace the fragment content with fr (depends on count value)..
the part from XML looks like this
<fragment
android:id="#+id/content"
android:name="com.test.app.DefaultContent"
android:layout_width="match_parent"
android:layout_height="match_parent">
</fragment>
Now when I want to replace fragment "content" with "fr" it do not replace it 1:1 - it adds fr down to the already current fragment (here DefaultContent) - so - why it does not replace it? What I have to do, that it replaces it and not puts the fr in addition below the current fragment
The View ID you pass to replace(), in your case R.id.content, has to be the ID of the Fragment's parent View, not the original Fragment itself.
That means you have to set android:id="#+id/content" to the Fragment's parent, like this:
<FragmentLayout
android:id="#+id/content"
android:layout_width="match_parent"
android:layout_height="match_parent">
<fragment
android:name="com.test.app.DefaultContent"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</FragmentLayout>
Or, if that does not work for you, set a new ID to the parent and use it for replace().
I have such layout:
frame_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/items"
android:layout_width="match_parent"
android:layout_height="match_parent">
</FrameLayout>
Next, in FragmentActivity I do:
setContentView(R.layout.fragment_layout);
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
String tag = MyListFragment.class.getName();
MyListFragment fragment = (MyListFragment)
getSupportFragmentManager().findFragmentByTag(tag);
if (fragment == null) {
fragment = new MyListFragment();
ft.add(R.id.items, fragment, tag);
}
else {
ft.replace(R.id.items, fragment, tag);
}
ft.addToBackStack(null);
ft.commit();
When I call this code once, fragment shown perfectly! But, when it called twice I see no content!
After some investigation, I found that problem's caused by this:
setContentView(R.layout.fragment_layout);
E.g., when it is called once, fragment content is perfectly shown! But I need to call setContentView a lot of times to show another fragments.
Where's the mistake?
P.S. It's possible to make MyListFragment hardcoded into XML, but this does not fit to me, because I need to replace layout contents with other fragments.
As nobody answers, I found another solution: keep layout for every frame and show/hide them.
That's how I did it.... Main layout:
<?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">
<FrameLayout
android:id="#+id/container1"
android:visibility="gone"
android:layout_width="match_parent"
android:layout_height="match_parent">
</FrameLayout>
<FrameLayout
android:id="#+id/container2"
android:visibility="gone"
android:layout_width="match_parent"
android:layout_height="match_parent">
</FrameLayout>
That's how I show fragment2 (belongs to container2) in fragment activity:
findViewById(R.id.container1).setVisibility(View.GONE);
findViewById(R.id.container2).setVisibility(View.VISIBLE);
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
String tag = MyFragment.class.getName();
Fragment fragment = (Fragment)getSupportFragmentManager().findFragmentByTag(tag);
if (fragment == null) {
fragment = new MyFragment();
ft.add(R.id.container2, fragment, tag);
}
ft.addToBackStack(null);
ft.commit();