Created
July 7, 2021 12:13
-
-
Save AkaashSaini/f1c85efdb603afa6caba2315d99b8d79 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Fragments | |
| A Fragment is a piece of an activity which enable more modular activity design. It will not be wrong if we say, a fragment is a kind of sub-activity. | |
| • A fragment has its own layout and its own behaviour with its own life cycle callbacks. | |
| • You can add or remove fragments in an activity while the activity is running. | |
| • You can combine multiple fragments in a single activity to build a multi-plane UI. | |
| • A fragment can be used in multiple activities. | |
| • Fragment life cycle is closely related to the life cycle of its host activity which means when the activity is paused, all the fragments available in the activity will also be stopped. | |
| • A fragment can implement a behaviour that has no user interface component. | |
| • Fragments were added to the Android API in Honeycomb version of Android which API version 11. | |
| Steps | |
| 1. Add fragment classes and create their layouts | |
| public class first extends Fragment { | |
| public first() { | |
| // Required empty public constructor | |
| } | |
| @Override | |
| public View onCreateView(LayoutInflater inflater, ViewGroup container, | |
| Bundle savedInstanceState) { | |
| // Inflate the layout for this fragment | |
| return inflater.inflate(R.layout.fragment_first, container, false); | |
| } | |
| } | |
| Here fragment_first is the layout | |
| 2. Write following code to use fragment in activity | |
| first f1=new first(); //User defined fragment class | |
| FragmentTransaction ft=getSupportFragmentManager().beginTransaction(); | |
| ft.replace(R.id.fragment_container,f1); | |
| ft.addToBackStack(null); | |
| ft.commit(); | |
| Full Code | |
| activity_main.xml | |
| <?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:id="@+id/content_main" | |
| android:layout_width="match_parent" | |
| android:layout_height="match_parent" | |
| app:layout_behavior="@string/appbar_scrolling_view_behavior" | |
| tools:context="com.example.vgt.myapplication.MainActivity" | |
| tools:showIn="@layout/activity_main" android:background="#abcdef"> | |
| <LinearLayout | |
| android:orientation="vertical" | |
| android:layout_width="match_parent" | |
| android:layout_height="match_parent" | |
| android:layout_alignParentTop="true" | |
| android:layout_marginTop="17dp" | |
| android:layout_alignParentRight="true" | |
| android:layout_alignParentEnd="true"> | |
| <LinearLayout | |
| android:orientation="horizontal" | |
| android:layout_width="match_parent" | |
| android:layout_height="wrap_content"> | |
| <Button | |
| android:text="First" | |
| android:layout_width="wrap_content" | |
| android:layout_height="wrap_content" | |
| android:id="@+id/button" | |
| android:layout_weight="1" /> | |
| <Button | |
| android:text="Second" | |
| android:layout_width="wrap_content" | |
| android:layout_height="wrap_content" | |
| android:id="@+id/button1" | |
| android:layout_weight="1" /> | |
| </LinearLayout> | |
| <FrameLayout | |
| android:layout_width="match_parent" | |
| android:layout_height="400dp" | |
| android:id="@+id/fragment_container" android:background="#d5d5ff" | |
| > | |
| </FrameLayout> | |
| </LinearLayout> | |
| </RelativeLayout> | |
| fragment_first.xml | |
| <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" | |
| xmlns:tools="http://schemas.android.com/tools" | |
| android:layout_width="match_parent" | |
| android:layout_height="400dp" | |
| tools:context="com.example.vgt.myapplication.first" android:background="#999999"> | |
| <LinearLayout | |
| android:orientation="vertical" | |
| android:layout_width="match_parent" | |
| android:layout_height="match_parent"> | |
| <TextView | |
| android:text="TextView" | |
| android:layout_width="match_parent" | |
| android:layout_height="wrap_content" | |
| android:id="@+id/textView" /> | |
| <Button | |
| android:text="Button" | |
| android:layout_width="match_parent" | |
| android:layout_height="wrap_content" | |
| android:id="@+id/button3" /> | |
| </LinearLayout> | |
| </FrameLayout> | |
| fragment_second.xml | |
| <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" | |
| xmlns:tools="http://schemas.android.com/tools" | |
| android:layout_width="match_parent" | |
| android:layout_height="400dp" | |
| tools:context="com.example.vgt.myapplication.second" android:background="#ffd5d5"> | |
| <LinearLayout | |
| android:orientation="vertical" | |
| android:layout_width="match_parent" | |
| android:layout_height="match_parent"> | |
| <Button | |
| android:text="Button" | |
| android:layout_width="match_parent" | |
| android:layout_height="wrap_content" | |
| android:id="@+id/button4" /> | |
| <Button | |
| android:text="Button" | |
| android:layout_width="match_parent" | |
| android:layout_height="wrap_content" | |
| android:id="@+id/button5" /> | |
| </LinearLayout> | |
| </FrameLayout> | |
| MainActivity.java | |
| public class MainActivity extends FragmentActivity { | |
| Button b1,b2; | |
| @Override | |
| protected void onCreate(Bundle savedInstanceState) { | |
| super.onCreate(savedInstanceState); | |
| setContentView(R.layout.activity_main); | |
| b1= (Button) findViewById(R.id.button); | |
| b2=(Button) findViewById(R.id.button1); | |
| b1.setOnClickListener(new View.OnClickListener() { | |
| @Override | |
| public void onClick(View v) { | |
| //create a object of fragment class | |
| first f1=new first(); | |
| FragmentTransaction ft = getSupportFragmentManager(). beginTransaction(); | |
| ft.replace(R.id.fragment_container,f1); | |
| ft.addToBackStack(null); | |
| ft.commit(); | |
| } | |
| }); | |
| b2.setOnClickListener(new View.OnClickListener() { | |
| @Override | |
| public void onClick(View v) { | |
| second f2=new second(); | |
| FragmentTransaction ft = getSupportFragmentManager(). beginTransaction(); | |
| ft.replace(R.id.fragment_container,f2); | |
| ft.addToBackStack(null); | |
| ft.commit(); | |
| } | |
| }); | |
| } | |
| } | |
| First.java | |
| public class first extends Fragment { | |
| public second() { | |
| // Required empty public constructor | |
| } | |
| @Override | |
| public View onCreateView(LayoutInflater inflater, ViewGroup container, | |
| Bundle savedInstanceState) { | |
| // Inflate the layout for this fragment | |
| return inflater.inflate(R.layout.fragment_first, container, false); | |
| } | |
| } | |
| Second.java | |
| public class second extends Fragment { | |
| public second() { | |
| // Required empty public constructor | |
| } | |
| @Override | |
| public View onCreateView(LayoutInflater inflater, ViewGroup container, | |
| Bundle savedInstanceState) { | |
| // Inflate the layout for this fragment | |
| return inflater.inflate(R.layout.fragment_second, container, false); | |
| } | |
| } | |
| To access the components of UI of fragment, override following method in fragment class. | |
| @Override | |
| public void onViewCreated(View view, @Nullable final Bundle savedInstanceState) { | |
| super.onViewCreated(view, savedInstanceState); | |
| b1=(Button) view.findViewById(R.id.button3); | |
| b1.setOnClickListener(new View.OnClickListener() { | |
| @Override | |
| public void onClick(View v) { | |
| Toast.makeText(getActivity(),"Hello",Toast.LENGTH_LONG).show(); | |
| } | |
| }); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment