Fragments are ways by which you can create multiple views in a single Activity, to avoiding to create multiple activities. You can add / remove fragments while the activity is running.
You can create a fragment in 2 ways: - static using XML and dynamic using programmatically
XML:
- Create 2 fragment classes using the wizard. This will create a class that extends Fragment and create the layout xml accordingly.
- headerFragment
- ArticleFragment.
- Now add the 2 fragments in the main activity XML file as below.
<fragment
android:layout_width="match_parent"
android:layout_height="0dp"
android:id="@+id/headline_fragment"
android:layout_weight="1"
android:name="io.ctrlspace.fragmentexample.HeadlineFragment"> </fragment>
<fragment
android:layout_width="match_parent"
android:layout_height="0dp"
android:id="@+id/article_fragment"
android:layout_weight="1"
android:name="io.ctrlspace.fragmentexample.ArticleFragment"> </fragment>
Now this is not of much use since we add these in static xml, often we may need to add dynamically fragments based on runtime logic.
Programmatically:
- Create a simple Empty Activity - say MainActivity.
- In the main Activity layout file, add a layout to hold the fragments, say a frame layout
- Now create a Fragment using Android studio - call it as HomeFragment. This will create a Java class that extends Fragment and a fragment layout file.
- Add some content for the fragment in the layout design to identify that its a fragment content, say add a text box with a text inside first fragment.
- In the main Activity add the following code.
- Get the FragmentManager instance
- Start the transaction of the fragment manager.
- Add or replace the fragment using the add / replace method in the fragment transaction, pass the fragment container and the fragment to be added or replaced to this method.
- Commit the transaction.
public static FragmentManager fragmentManager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
fragmentManager = getSupportFragmentManager();
if(findViewById(R.id.fragment_container) != null) {
if (savedInstanceState != null) {
return;
}
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
HomeFragment homeFragment = new HomeFragment();
fragmentTransaction.add(R.id.fragment_container, homeFragment, null);
fragmentTransaction.commit();
}
}
Fragment to Activity communication is handled in a different way - you can view the source code for this here.
For more information on Fragment to Activity communication visit this github link:
ReplyDeletehttps://github.com/deiveehan/android-samples-refresh/tree/master/fragmentactivitycommunication
This comment has been removed by the author.
ReplyDeleteIt would have been the happiest moment for you,I mean if we have been waiting for something to happen and when it happens we forgot all hardwork and wait for getting that happened.
ReplyDeleteData Science Training in Chennai | Data Science course in anna nagar
Data Science course in chennai | Data science course in Bangalore
Data Science course in marathahalli | Data Science course in btm
Thank you a lot for providing individuals with a very spectacular possibility to read critical reviews from this site.
ReplyDeletepython training in pune | python training institute in chennai | python training in Bangalore
Whoa! I’m enjoying the template/theme of this website. It’s simple, yet effective. A lot of times it’s very hard to get that “perfect balance” between superb usability and visual appeal. I must say you’ve done a very good job with this.
ReplyDeleteOracle Training in Chennai | Best Oracle Training Institute in Chennai
Web Design Training in Chennai
Web Design Training in Chennai|Best Web Design Training in Chennai
AngularJS Training in Chennai |Advanced SAS Training in Chennai | Best SAS Training in Chennai
wonderful post!!helpful with us...Thanks keep to more important message share to future!!
ReplyDeleteAndroid Training in Chennai | Certification | Mobile App Development Training Online | Android Training in Bangalore | Certification | Mobile App Development Training Online | Android Training in Hyderabad | Certification | Mobile App Development Training Online | Android Training in Coimbatore | Certification | Mobile App Development Training Online | Android Training in Online | Certification | Mobile App Development Training Online
ReplyDeleteIt is amazing and wonderful to visit your site.Thanks for sharing this information,this is useful to me
angular js training in chennai
angular js training in annanagar
full stack training in chennai
full stack training in annanagar
php training in chennai
php training in annanagar
photoshop training in chennai
photoshop training in annanagar
Thank you so much for sharing this pretty post, it was so good to read and useful to improve my knowledge as updated one, keep blogging.
ReplyDeletejava training in chennai
java training in velachery
aws training in chennai
aws training in velachery
python training in chennai
python training in velachery
selenium training in chennai
selenium training in velachery
I think you have a long story to share and i am glad after long time finally you cam and shared your experience.
ReplyDeletehardware and networking training in chennai
hardware and networking training in tambaram
xamarin training in chennai
xamarin training in tambaram
ios training in chennai
ios training in tambaram
iot training in chennai
iot training in tambaram
Really very nice blog information for this one and more technical skills are improve,i like that kind of post.
ReplyDeleteweb designing training in chennai
web designing training in omr
digital marketing training in chennai
digital marketing training in omr
rpa training in chennai
rpa training in omr
tally training in chennai
tally training in omr
ReplyDeleteThis post is so interactive and informative.keep update more information...
RPA Training in Velachery
RPA Training in Chennai
Great post. keep sharing such a worthy information.
ReplyDeletecontent writing course in chennai
online content writing course