Skip to content

Commit 3335097

Browse files
committed
Added codes for dynamic fragment
1 parent dde01c1 commit 3335097

File tree

8 files changed

+134
-1
lines changed

8 files changed

+134
-1
lines changed

Images/S40.png

72.2 KB
Loading

Kotlin/P40.kt

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package com.example.four
2+
3+
import androidx.appcompat.app.AppCompatActivity
4+
import android.os.Bundle
5+
import androidx.fragment.app.FragmentManager
6+
import androidx.fragment.app.FragmentTransaction
7+
8+
class MainActivity : AppCompatActivity() {
9+
override fun onCreate(savedInstanceState: Bundle?) {
10+
super.onCreate(savedInstanceState)
11+
setContentView(R.layout.activity_main)
12+
13+
val fm: FragmentManager = supportFragmentManager
14+
val fragmentTransaction: FragmentTransaction = fm.beginTransaction()
15+
val displaymode = resources.configuration.orientation
16+
if (displaymode == 1) {
17+
val f1 = FragmentOne()
18+
fragmentTransaction.replace(android.R.id.content, f1)
19+
} else {
20+
val f2 = FragmentTwo()
21+
fragmentTransaction.replace(android.R.id.content, f2)
22+
}
23+
fragmentTransaction.commit()
24+
}
25+
}

Kotlin/P40_a.kt

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package com.example.four
2+
3+
import android.os.Bundle
4+
import androidx.fragment.app.Fragment
5+
import android.view.LayoutInflater
6+
import android.view.View
7+
import android.view.ViewGroup
8+
9+
10+
class FragmentOne : Fragment() {
11+
12+
13+
override fun onCreateView(
14+
inflater: LayoutInflater, container: ViewGroup?,
15+
savedInstanceState: Bundle?
16+
): View? {
17+
// Inflate the layout for this fragment
18+
return inflater.inflate(R.layout.fragment_one, container, false)
19+
}
20+
21+
22+
}

Kotlin/P40_b.kt

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package com.example.four
2+
3+
import android.os.Bundle
4+
import androidx.fragment.app.Fragment
5+
import android.view.LayoutInflater
6+
import android.view.View
7+
import android.view.ViewGroup
8+
9+
class FragmentTwo : Fragment() {
10+
11+
12+
override fun onCreateView(
13+
inflater: LayoutInflater, container: ViewGroup?,
14+
savedInstanceState: Bundle?
15+
): View? {
16+
// Inflate the layout for this fragment
17+
return inflater.inflate(R.layout.fragment_two, container, false)
18+
}
19+
20+
}

Xml/P40.xml

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
3+
xmlns:app="http://schemas.android.com/apk/res-auto"
4+
xmlns:tools="http://schemas.android.com/tools"
5+
android:layout_width="match_parent"
6+
android:orientation="horizontal"
7+
android:layout_height="match_parent"
8+
tools:context=".MainActivity">
9+
10+
11+
12+
</androidx.constraintlayout.widget.ConstraintLayout>

Xml/P40_a.xml

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
3+
xmlns:tools="http://schemas.android.com/tools"
4+
android:layout_width="match_parent"
5+
android:layout_height="match_parent"
6+
android:background="@color/white"
7+
tools:context=".FragmentOne">
8+
9+
<TextView
10+
android:id="@+id/textView2"
11+
android:layout_width="match_parent"
12+
android:layout_height="match_parent"
13+
android:gravity="center"
14+
android:textSize="30sp"
15+
android:textColor="@color/black"
16+
android:text="I'M FRAGMENT ONE" />
17+
18+
</FrameLayout>

Xml/P40_b.xml

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
3+
xmlns:tools="http://schemas.android.com/tools"
4+
android:layout_width="match_parent"
5+
android:layout_height="match_parent"
6+
android:background="#909090"
7+
tools:context=".FragmentTwo">
8+
9+
<TextView
10+
android:id="@+id/textView"
11+
android:layout_width="match_parent"
12+
android:layout_height="match_parent"
13+
android:gravity="center"
14+
android:textColor="@color/black"
15+
android:text="I'M FRAGMENT TWO"
16+
android:textSize="30sp" />
17+
</FrameLayout>

developingApp.md

+20-1
Original file line numberDiff line numberDiff line change
@@ -243,7 +243,7 @@ Add a service with the name same as class name of the JobService in the manifest
243243

244244
### <u>12. Fragment with ListView:</u>
245245

246-
> App with two fragments in a single activity. The first fragment is having a list of items. When we click on any item, the second fragment is displayed with the name of the item.
246+
> App with two fragments in a single activity. The first fragment is having a list of items. When we click on any item, the second fragment is displayed with the name of the item. It also shows how to pass data from one fragment to another fragment.
247247
- Fragment
248248
- FragmentManager
249249
- ListView
@@ -258,4 +258,23 @@ Add a service with the name same as class name of the JobService in the manifest
258258
| [2'nd fragment](Xml/P39_b.xml) | [2'nd fragment](Kotlin/P39_b.kt) | |
259259

260260

261+
---
262+
263+
### <u>13. Dynamic Fragment Example App:</u>
264+
265+
> App with two fragments. The fragments are displayed depending on the orientation of the device. When the device is in portrait mode, the first fragment is displayed. When the device is in landscape mode, the second fragment is displayed.
266+
- Fragment
267+
- FragmentManager
268+
- FragmentTransaction
269+
- Orientation
270+
- Button
271+
- TextView
272+
273+
| XML Codes | Kotlin Codes| Output |
274+
|-----------|:-----------:| :-----------:|
275+
| [Main activity](Xml/P40.xml) | [Main activity](Kotlin/P40.kt) | |
276+
| [1'st fragment](Xml/P40_a.xml) | [1'st fragment](Kotlin/P40_a.kt) | [Output Screenshot](Images/S40.png) |
277+
| [2'nd fragment](Xml/P40_b.xml) | [2'nd fragment](Kotlin/P40_b.kt) | |
278+
279+
261280
---

0 commit comments

Comments
 (0)