java - How can I hide a fragment on start of my MainActivity( or the application)? -
well title says all, have no idea how hide on startup. tried searching nothing happened in way want happen.
so want happen want bottomfragment hidden on start up. want show when click button in topfragment , topfragment visible @ start up.
my mainactivity.xml codes:
<?xml version="1.0" encoding="utf-8"?> <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:layout_width="match_parent" android:layout_height="match_parent" tools:context="rjj.tutorial_fragmentsthe3rd.mainactivity"> <fragment class="rjj.tutorial_fragmentsthe3rd.topfragment" android:id="@+id/topfragment" android:layout_width="match_parent" android:layout_height="300dp" /> <fragment class="rjj.tutorial_fragmentsthe3rd.bottomfragment" android:id="@+id/bottomfragment" android:layout_width="match_parent" android:layout_height="300dp" android:layout_alignparentbottom="true" android:layout_alignparentstart="true" android:layout_below="@+id/topfragment" /> </relativelayout> my topfragment.xml codes:
<?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"> <spinner android:id="@+id/origin" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignparentstart="true" android:layout_alignparenttop="true" android:entries="@array/origins"/> <spinner android:id="@+id/destination" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_below="@+id/origin" android:layout_alignparentstart="true" android:layout_margintop="41dp" android:entries="@array/destinations"/> <button android:id="@+id/button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/destination" android:layout_centerhorizontal="true" android:layout_margintop="80dp" android:text="@string/button_caption" /> </relativelayout> my bottomfragment.xml codes:
<?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"> <edittext android:id="@+id/flightqueryresult" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignparenttop="true" android:layout_centerhorizontal="true" android:layout_margintop="61dp" android:inputtype="textpersonname" android:text="name" /> </relativelayout> my mainactivity.java codes:
package rjj.tutorial_fragmentsthe3rd; import android.support.v7.app.appcompatactivity; import android.os.bundle; public class mainactivity extends appcompatactivity implements topfragment.flightsearcher{ @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); } @override public void searchforflights(string origin, string destination) { bottomfragment bottomfragment = (bottomfragment)getsupportfragmentmanager() .findfragmentbyid(r.id.bottomfragment); int randomprice = (int)(math.random()*200); string resultstring = origin + " - " + destination + " = " + randomprice; bottomfragment.displayflightqueryresult(resultstring); } } my topfragment.java codes:
package rjj.tutorial_fragmentsthe3rd; import android.content.context; import android.os.bundle; import android.support.annotation.nullable; import android.support.v4.app.fragment; import android.view.layoutinflater; import android.view.view; import android.view.viewgroup; import android.widget.button; import android.widget.spinner; public class topfragment extends fragment { private flightsearcher interfaceimplementer; public interface flightsearcher{ public void searchforflights(string origin, string destination); } @override public void onattach(context context) { super.onattach(context); this.interfaceimplementer =(flightsearcher)context; } @override public view oncreateview(layoutinflater inflater, @nullable viewgroup container, @nullable bundle savedinstancestate) { return inflater.inflate(r.layout.fragment_top, container, false); } @override public void onactivitycreated(@nullable bundle savedinstancestate) { super.onactivitycreated(savedinstancestate); button flightsearchbutton = (button)getactivity().findviewbyid(r.id.button); flightsearchbutton.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { spinner originspinner = (spinner)getactivity().findviewbyid(r.id.origin); spinner destinationspinner = (spinner)getactivity().findviewbyid(r.id.destination); interfaceimplementer.searchforflights(originspinner.getselecteditem().tostring(),destinationspinner.getselecteditem().tostring()); } }); } } my bottomfragment codes:
package rjj.tutorial_fragmentsthe3rd; import android.os.bundle; import android.support.annotation.nullable; import android.support.v4.app.fragment; import android.view.layoutinflater; import android.view.view; import android.view.viewgroup; import android.widget.edittext; public class bottomfragment extends fragment { @nullable @override public view oncreateview(layoutinflater inflater, @nullable viewgroup container, @nullable bundle savedinstancestate) { return inflater.inflate(r.layout.fragment_bottom, container, false); } public void displayflightqueryresult(string result){ edittext resultfield = (edittext)getactivity().findviewbyid(r.id.flightqueryresult); resultfield.settext(result); } }
on start hide fragment this:
fragmentmanager manager = getsupportfragmentmanager(); fragmenttransaction ft = manager.begintransaction(); fragment bottomfragment = manager.findfragmentbyid(r.id.bottomfragment); ft.hide(bottom); ft.commit(); then can again show it:
fragmentmanager manager = getsupportfragmentmanager(); fragmenttransaction ft = manager.begintransaction(); fragment bottomfragment = manager.findfragmentbyid(r.id.bottomfragment); ft.show(bottom); ft.commit();
Comments
Post a Comment