Skip to main content

Fragments

  • A Fragment represents a behavior or a portion of user interface in an Activity.
  • A fragment must always be embedded in an activity and the fragment's life cycle is directly affected by the host activity's life cycle. 
  • You can insert a fragment into your activity layout by declaring the fragment in the activity's layout file, as a <fragment> element, or from your application code by adding it to an existing ViewGroup
  • We can also use a fragment without its own UI as an invisible worker for the activity. 
  • Fragments can be added, removed, replaced, and animated inside an Activity dynamically. 

Fragment API Level

Fragment introduced in Honeycomb (Android 3.0) API level 11

Purpose 

  • support more dynamic and flexible UI designs on large screens 
  • promote re usability . 

key classes


Fragment
FragmentManager
FragmentTransaction

Step of Creating Fragment

Step-1

Create a subclass of fragment



Step-2

Adding a user interface

Create a  layout  for fragment




Implement the onCreateView() callback method to provide a user interface for a fragment




The inflate() method takes three arguments:
  • The resource ID of the layout you want to inflate.
  • The ViewGroup to be the parent of the inflated layout. Passing the container is important in order for the system to apply layout parameters to the root view of the inflated layout, specified by the parent view in which it's going.
  • A boolean indicating whether the inflated layout should be attached to the ViewGroup (the second parameter) during inflation. (In this case, this is false because the system is already inserting the inflated layout into the container—passing true would create a redundant view group in the final layout.)

Step- 3
Adding fragment into activity


There are two way to add fragment into activity

1.   Declare the fragment inside the activity's layout file.

Include fragment as a view inside your activity layout.




2.   programmatically add the fragment to an existing ViewGroup


Use to add the fragment run time.




Reference:http://developer.android.com/guide/components/fragments.html

Comments

Popular posts from this blog

Adding fragment without UI

The fragment without UI can only be add within the activity programmatically. For this we have different variation of add method available.Use public abstract  FragmentTransaction  add ( Fragment  frag,  String  tag) frag:  fragment we want  to add in  activity tag: unique ID for identification usually the name of fragment. This method add the fragment in activity .As there is no UI so onCreateView () implementation is not required. Code snippet:             FragmentManager fragmentManager = getFragmentManager();             FragmentTransaction fragmentTransaction = fragmentManager                         .beginTransaction();             // creating a fragment object             Fragmentone fragmentone = new Fragmentone();             fragmentTransaction .add( fragmentone , "fragmentone" );

Target "Unknown" in android Device Chooser ..Eclipse

Problem Eclipse not recognize my device target Solution A Rest the ADB Open the DDMS perspective  Reset the ADB  Solution B Restart the eclipse Solution C Restart the device Note: if there is any other solution ,Please share with me . Thanks 

Exclamation Mark with Android Project Name in Eclipse

Solution A Check errors window. Try to solve the error there Solution B Right click the project,  select Android and make sure that a build target is checked. sometimes you need to unchecked it, select a different API, apply settings, and then again select the API that was originally selected. Solution C Restart the eclipse Note :If none of the above solution work then find new one  and tell me  about the new solution in comments .thanks