over 11 years ago
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
- package com.example.fragmentexample3;
- import android.app.Activity;
- import android.app.Fragment;
- import android.app.FragmentManager;
- import android.app.FragmentTransaction;
- import android.os.Bundle;
- import android.view.Menu;
- import android.view.View;
- import android.view.View.OnClickListener;
- import android.widget.Button;
- public class MainActivity extends Activity {
- int fragmentNo = 1;
- int counter = 1;
- FragmentManager fm;
- FragmentTransaction ft;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- fm = getFragmentManager();
- ft = fm.beginTransaction();
- /*
- * MyFragment fragment1 = new MyFragment();
- *
- * MyDefaultFragment fragment2 = new MyDefaultFragment();
- * MyDefaultFragment fragment3 = new MyDefaultFragment();
- * MyDefaultFragment fragment4 = new MyDefaultFragment();
- */
- ft.replace(R.id.fragment1, new MyFragment());
- ft.replace(R.id.fragment2, new MyDefaultFragment());
- ft.replace(R.id.fragment3, new MyDefaultFragment());
- ft.replace(R.id.fragment4, new MyDefaultFragment());
- // fragment2.onCreateView(getLayoutInflater(),(FrameLayout)findViewById(R.id.fragment2),
- // savedInstanceState);
- ft.commit();
- Button b = (Button) findViewById(R.id.button1);
- b.setOnClickListener(new OnClickListener() {
- @Override
- public void onClick(View v) {
- // TODO Auto-generated method stub
- fm = getFragmentManager();
- ft = fm.beginTransaction();
- counter++;
- if (counter > 4)
- counter = 1;
- System.out.println("counter" + counter);
- if (counter == 1) {
- System.out.println("in counter 1");
- ft.replace(R.id.fragment1, new MyFragment());
- ft.replace(R.id.fragment2, new MyDefaultFragment());
- ft.replace(R.id.fragment4, new MyDefaultFragment());
- } else if (counter == 2) {
- System.out.println("in counter 2");
- ft.replace(R.id.fragment1, new MyDefaultFragment());
- ft.replace(R.id.fragment2, new MyFragment());
- } else if (counter == 3) {
- System.out.println("in counter 3");
- ft.replace(R.id.fragment2, new MyDefaultFragment());
- ft.replace(R.id.fragment3, new MyFragment());
- } else if (counter == 4) {
- System.out.println("in counter 4");
- ft.replace(R.id.fragment3, new MyDefaultFragment());
- ft.replace(R.id.fragment4, new MyFragment());
- }
- ft.commit();
- }
- });
- }
- @Override
- public boolean onCreateOptionsMenu(Menu menu) {
- // Inflate the menu; this adds items to the action bar if it is present.
- getMenuInflater().inflate(R.menu.main, menu);
- return true;
- }
- }
package com.example.fragmentexample3; import android.app.Activity; import android.app.Fragment; import android.app.FragmentManager; import android.app.FragmentTransaction; import android.os.Bundle; import android.view.Menu; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; public class MainActivity extends Activity { int fragmentNo = 1; int counter = 1; FragmentManager fm; FragmentTransaction ft; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); fm = getFragmentManager(); ft = fm.beginTransaction(); /* * MyFragment fragment1 = new MyFragment(); * * MyDefaultFragment fragment2 = new MyDefaultFragment(); * MyDefaultFragment fragment3 = new MyDefaultFragment(); * MyDefaultFragment fragment4 = new MyDefaultFragment(); */ ft.replace(R.id.fragment1, new MyFragment()); ft.replace(R.id.fragment2, new MyDefaultFragment()); ft.replace(R.id.fragment3, new MyDefaultFragment()); ft.replace(R.id.fragment4, new MyDefaultFragment()); // fragment2.onCreateView(getLayoutInflater(),(FrameLayout)findViewById(R.id.fragment2), // savedInstanceState); ft.commit(); Button b = (Button) findViewById(R.id.button1); b.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub fm = getFragmentManager(); ft = fm.beginTransaction(); counter++; if (counter > 4) counter = 1; System.out.println("counter" + counter); if (counter == 1) { System.out.println("in counter 1"); ft.replace(R.id.fragment1, new MyFragment()); ft.replace(R.id.fragment2, new MyDefaultFragment()); ft.replace(R.id.fragment4, new MyDefaultFragment()); } else if (counter == 2) { System.out.println("in counter 2"); ft.replace(R.id.fragment1, new MyDefaultFragment()); ft.replace(R.id.fragment2, new MyFragment()); } else if (counter == 3) { System.out.println("in counter 3"); ft.replace(R.id.fragment2, new MyDefaultFragment()); ft.replace(R.id.fragment3, new MyFragment()); } else if (counter == 4) { System.out.println("in counter 4"); ft.replace(R.id.fragment3, new MyDefaultFragment()); ft.replace(R.id.fragment4, new MyFragment()); } ft.commit(); } }); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } }
Then set Layout for MainActivity main_activity.xml
- <Button
- android:id="@+id/button1"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_alignParentTop="true"
- android:layout_centerHorizontal="true"
- android:layout_marginTop="14dp"
- android:text="Change" />
- <LinearLayout
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_alignParentBottom="true"
- android:layout_alignParentLeft="true"
- android:layout_alignParentRight="true"
- android:layout_below="@+id/button1"
- android:orientation="vertical" >
- <FrameLayout
- android:id="@+id/fragment1"
- android:layout_width="match_parent"
- android:layout_height="75dp"
- android:layout_marginTop="50dp" >
- </FrameLayout>
- <FrameLayout
- android:id="@+id/fragment2"
- android:layout_width="match_parent"
- android:layout_height="75dp"
- android:layout_marginTop="10dp" >
- </FrameLayout>
- <FrameLayout
- android:id="@+id/fragment3"
- android:layout_width="match_parent"
- android:layout_height="75dp"
- android:layout_marginTop="10dp" >
- </FrameLayout>
- <FrameLayout
- android:id="@+id/fragment4"
- android:layout_width="match_parent"
- android:layout_height="75dp"
- android:layout_marginTop="10dp" >
- </FrameLayout>
- </LinearLayout>
<Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentTop="true" android:layout_centerHorizontal="true" android:layout_marginTop="14dp" android:text="Change" /> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:layout_alignParentLeft="true" android:layout_alignParentRight="true" android:layout_below="@+id/button1" android:orientation="vertical" > <FrameLayout android:id="@+id/fragment1" android:layout_width="match_parent" android:layout_height="75dp" android:layout_marginTop="50dp" > </FrameLayout> <FrameLayout android:id="@+id/fragment2" android:layout_width="match_parent" android:layout_height="75dp" android:layout_marginTop="10dp" > </FrameLayout> <FrameLayout android:id="@+id/fragment3" android:layout_width="match_parent" android:layout_height="75dp" android:layout_marginTop="10dp" > </FrameLayout> <FrameLayout android:id="@+id/fragment4" android:layout_width="match_parent" android:layout_height="75dp" android:layout_marginTop="10dp" > </FrameLayout> </LinearLayout>
Create the two fragment Class:: MyFragment.java And MyDefaultFragment.java
MyFragment.java
- package com.example.fragmentexample3;
- import android.app.Fragment;
- import android.app.FragmentManager;
- import android.app.FragmentTransaction;
- import android.os.Bundle;
- import android.view.LayoutInflater;
- import android.view.View;
- import android.view.ViewGroup;
- public class MyFragment extends Fragment {
- MainActivity mainAvtivity;
- View view;
- public View onCreateView(LayoutInflater inflater, ViewGroup container,
- Bundle savedInstanceState) {
- view = inflater.inflate(R.layout.fragmentlayout, container, false);
- return view;
- }
- }
package com.example.fragmentexample3; import android.app.Fragment; import android.app.FragmentManager; import android.app.FragmentTransaction; import android.os.Bundle; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; public class MyFragment extends Fragment { MainActivity mainAvtivity; View view; public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { view = inflater.inflate(R.layout.fragmentlayout, container, false); return view; } }
MyDefaultFragment.java
- package com.example.fragmentexample3;
- import android.app.Fragment;
- import android.os.Bundle;
- import android.view.LayoutInflater;
- import android.view.View;
- import android.view.ViewGroup;
- public class MyDefaultFragment extends Fragment {
- View view;
- @Override
- public View onCreateView(LayoutInflater inflater, ViewGroup container,
- Bundle savedInstanceState) {
- view=inflater.inflate(R.layout.defaltfragmentlayout,container,false);
- return view;
- }
- }
package com.example.fragmentexample3; import android.app.Fragment; import android.os.Bundle; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; public class MyDefaultFragment extends Fragment { View view; @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { view=inflater.inflate(R.layout.defaltfragmentlayout,container,false); return view; } }
0 Comment(s)