Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Fragment Tutorial in Android

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 543
    Comment on it

    This tutorial helps you how to use Fragment Class in android Application

    the sample code give you the brief on how you can use an fragment class. In main Activity we have created four fragments. And use FragmentManager and FragmentTransaction Class.

    sample code: Create the main activity

    1. package com.example.fragmentexample3;
    2.  
    3. import android.app.Activity;
    4. import android.app.Fragment;
    5. import android.app.FragmentManager;
    6. import android.app.FragmentTransaction;
    7. import android.os.Bundle;
    8. import android.view.Menu;
    9. import android.view.View;
    10. import android.view.View.OnClickListener;
    11. import android.widget.Button;
    12.  
    13. public class MainActivity extends Activity {
    14. int fragmentNo = 1;
    15. int counter = 1;
    16. FragmentManager fm;
    17. FragmentTransaction ft;
    18.  
    19. @Override
    20. protected void onCreate(Bundle savedInstanceState) {
    21. super.onCreate(savedInstanceState);
    22. setContentView(R.layout.activity_main);
    23.  
    24. fm = getFragmentManager();
    25. ft = fm.beginTransaction();
    26. /*
    27. * MyFragment fragment1 = new MyFragment();
    28. *
    29. * MyDefaultFragment fragment2 = new MyDefaultFragment();
    30. * MyDefaultFragment fragment3 = new MyDefaultFragment();
    31. * MyDefaultFragment fragment4 = new MyDefaultFragment();
    32. */
    33. ft.replace(R.id.fragment1, new MyFragment());
    34. ft.replace(R.id.fragment2, new MyDefaultFragment());
    35. ft.replace(R.id.fragment3, new MyDefaultFragment());
    36. ft.replace(R.id.fragment4, new MyDefaultFragment());
    37. // fragment2.onCreateView(getLayoutInflater(),(FrameLayout)findViewById(R.id.fragment2),
    38. // savedInstanceState);
    39. ft.commit();
    40. Button b = (Button) findViewById(R.id.button1);
    41. b.setOnClickListener(new OnClickListener() {
    42.  
    43. @Override
    44. public void onClick(View v) {
    45. // TODO Auto-generated method stub
    46. fm = getFragmentManager();
    47. ft = fm.beginTransaction();
    48. counter++;
    49. if (counter > 4)
    50. counter = 1;
    51. System.out.println("counter" + counter);
    52. if (counter == 1) {
    53. System.out.println("in counter 1");
    54. ft.replace(R.id.fragment1, new MyFragment());
    55. ft.replace(R.id.fragment2, new MyDefaultFragment());
    56. ft.replace(R.id.fragment4, new MyDefaultFragment());
    57.  
    58. } else if (counter == 2) {
    59. System.out.println("in counter 2");
    60. ft.replace(R.id.fragment1, new MyDefaultFragment());
    61. ft.replace(R.id.fragment2, new MyFragment());
    62.  
    63. } else if (counter == 3) {
    64. System.out.println("in counter 3");
    65.  
    66. ft.replace(R.id.fragment2, new MyDefaultFragment());
    67. ft.replace(R.id.fragment3, new MyFragment());
    68.  
    69. } else if (counter == 4) {
    70. System.out.println("in counter 4");
    71. ft.replace(R.id.fragment3, new MyDefaultFragment());
    72. ft.replace(R.id.fragment4, new MyFragment());
    73. }
    74. ft.commit();
    75. }
    76. });
    77.  
    78. }
    79.  
    80. @Override
    81. public boolean onCreateOptionsMenu(Menu menu) {
    82. // Inflate the menu; this adds items to the action bar if it is present.
    83. getMenuInflater().inflate(R.menu.main, menu);
    84. return true;
    85. }
    86.  
    87. }

    Then set Layout for MainActivity main_activity.xml

    1. <Button
    2. android:id="@+id/button1"
    3. android:layout_width="wrap_content"
    4. android:layout_height="wrap_content"
    5. android:layout_alignParentTop="true"
    6. android:layout_centerHorizontal="true"
    7. android:layout_marginTop="14dp"
    8. android:text="Change" />
    9.  
    10. <LinearLayout
    11. android:layout_width="wrap_content"
    12. android:layout_height="wrap_content"
    13. android:layout_alignParentBottom="true"
    14. android:layout_alignParentLeft="true"
    15. android:layout_alignParentRight="true"
    16. android:layout_below="@+id/button1"
    17. android:orientation="vertical" >
    18.  
    19. <FrameLayout
    20. android:id="@+id/fragment1"
    21. android:layout_width="match_parent"
    22. android:layout_height="75dp"
    23. android:layout_marginTop="50dp" >
    24. </FrameLayout>
    25.  
    26. <FrameLayout
    27. android:id="@+id/fragment2"
    28. android:layout_width="match_parent"
    29. android:layout_height="75dp"
    30. android:layout_marginTop="10dp" >
    31. </FrameLayout>
    32.  
    33. <FrameLayout
    34. android:id="@+id/fragment3"
    35. android:layout_width="match_parent"
    36. android:layout_height="75dp"
    37. android:layout_marginTop="10dp" >
    38. </FrameLayout>
    39.  
    40. <FrameLayout
    41. android:id="@+id/fragment4"
    42. android:layout_width="match_parent"
    43. android:layout_height="75dp"
    44. android:layout_marginTop="10dp" >
    45. </FrameLayout>
    46. </LinearLayout>

    Create the two fragment Class:: MyFragment.java And MyDefaultFragment.java

    MyFragment.java

    1. package com.example.fragmentexample3;
    2.  
    3. import android.app.Fragment;
    4. import android.app.FragmentManager;
    5. import android.app.FragmentTransaction;
    6. import android.os.Bundle;
    7. import android.view.LayoutInflater;
    8. import android.view.View;
    9. import android.view.ViewGroup;
    10.  
    11. public class MyFragment extends Fragment {
    12. MainActivity mainAvtivity;
    13. View view;
    14.  
    15. public View onCreateView(LayoutInflater inflater, ViewGroup container,
    16. Bundle savedInstanceState) {
    17.  
    18. view = inflater.inflate(R.layout.fragmentlayout, container, false);
    19. return view;
    20.  
    21. }
    22. }

    MyDefaultFragment.java

    1. package com.example.fragmentexample3;
    2.  
    3. import android.app.Fragment;
    4. import android.os.Bundle;
    5. import android.view.LayoutInflater;
    6. import android.view.View;
    7. import android.view.ViewGroup;
    8.  
    9. public class MyDefaultFragment extends Fragment {
    10. View view;
    11.  
    12. @Override
    13. public View onCreateView(LayoutInflater inflater, ViewGroup container,
    14. Bundle savedInstanceState) {
    15. view=inflater.inflate(R.layout.defaltfragmentlayout,container,false);
    16. return view;
    17. }
    18.  
    19. }

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Reset Password
Fill out the form below and reset your password: