0% found this document useful (0 votes)
65 views8 pages

Ferdinandus Vito Janu P

The document contains code for an Android application that uses fragments. It includes code for a MainActivity class that handles fragment transactions, a HomeFragment class that displays a text view and button, layout files for the fragment and activity, and strings for text display. The HomeFragment listens for a button click to open a CategoryFragment but this code is not yet implemented.

Uploaded by

Vito Janu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
65 views8 pages

Ferdinandus Vito Janu P

The document contains code for an Android application that uses fragments. It includes code for a MainActivity class that handles fragment transactions, a HomeFragment class that displays a text view and button, layout files for the fragment and activity, and strings for text display. The HomeFragment listens for a button click to open a CategoryFragment but this code is not yet implemented.

Uploaded by

Vito Janu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 8

Nama : Ferdinandus Vito Janu Priantoro

NIM : 1103170020
MAIN ACTIVITY

package com.example.myflexiblefragment

import androidx.appcompat.app.AppCompatActivity

import android.os.Bundle

import android.util.Log

import com.example.myflexiblefragment.ui.HomeFragment

class MainActivity : AppCompatActivity() {

override fun onCreate(savedInstanceState: Bundle?) {

super.onCreate(savedInstanceState)

setContentView(R.layout.activity_main)

val mFragmentManager = supportFragmentManager

val mHomeFragment = HomeFragment()

val fragment = mFragmentManager.findFragmentByTag(HomeFragment::class.java.simpleName)

if (fragment !is HomeFragment) {

Log.d("MyFlexibleFragment", "Fragment Name :" + HomeFragment::class.java.simpleName)

mFragmentManager

.beginTransaction()

.add(R.id.frame_container, mHomeFragment, HomeFragment::class.java.simpleName)

.commit()

}
HomeFragment.kt

package com.example.myflexiblefragment.ui

import android.os.Bundle

import androidx.fragment.app.Fragment

import android.view.LayoutInflater

import android.view.View

import android.view.ViewGroup

import android.widget.Button

import com.example.myflexiblefragment.R

// TODO: Rename parameter arguments, choose names that match

// the fragment initialization parameters, e.g. ARG_ITEM_NUMBER

private const val ARG_PARAM1 = "param1"

private const val ARG_PARAM2 = "param2"

/**

* A simple [Fragment] subclass.

* Use the [HomeFragment.newInstance] factory method to

* create an instance of this fragment.

*/

class HomeFragment : Fragment(), View.OnClickListener {

// TODO: Rename and change types of parameters

private var param1: String? = null

private var param2: String? = null


override fun onCreate(savedInstanceState: Bundle?) {

super.onCreate(savedInstanceState)

arguments?.let {

param1 = it.getString(ARG_PARAM1)

param2 = it.getString(ARG_PARAM2)

override fun onCreateView(

inflater: LayoutInflater, container: ViewGroup?,

savedInstanceState: Bundle?

): View? {

// Inflate the layout for this fragment

return inflater.inflate(R.layout.fragment_home, container, false)

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {

super.onViewCreated(view, savedInstanceState)

val btnCategory: Button = view.findViewById(R.id.btn_category)

btnCategory.setOnClickListener(this) }

override fun onClick(v: View) {

}
companion object {

/**

* Use this factory method to create a new instance of

* this fragment using the provided parameters.

* @param param1 Parameter 1.

* @param param2 Parameter 2.

* @return A new instance of fragment HomeFragment.

*/

// TODO: Rename and change types and number of parameters

@JvmStatic

fun newInstance(param1: String, param2: String) =

HomeFragment().apply {

arguments = Bundle().apply {

putString(ARG_PARAM1, param1)

putString(ARG_PARAM2, param2)

}
Strings.xml

<resources>

<string name="app_name">MyFlexibleFragment</string>

<string name="hello_blank_fragment">Hello blank fragment</string>

<string name="this_profile">Ini activity Profile</string>

<string name="this_category">Ini fragment Category</string>

<string name="category_lifestyle">Ke fragment Lifestyle</string>

<string name="category_name">Category Name</string>

<string name="category_description">Category Description</string>

<string name="to_profile">Ke Halaman Profile Activity</string>

<string name="show_dialog">Tampilkan sebuah dialog</string>

<string name="hello_home_fragment">Hello Ini Home Fragment</string>

<string name="to_category">Ke fragment Category</string>

<string name="question_coach">Siapa pelatih tersukses Machester United?</string>

<string name="sir_alex_ferguson">Sir Alex Ferguson</string>

<string name="jose_mourinho">Jose Mourinho</string>

<string name="louis_van_gaal">Louis Van Gaal</string>

<string name="david_moyes">David Moyes</string>

<string name="choose">Pilih</string>

<string name="close">Tutup</string>

</resources>
Fragment_home.xml

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

xmlns:tools="http://schemas.android.com/tools"

android:layout_width="match_parent"

android:layout_height="match_parent"

android:orientation="vertical">

<TextView

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:layout_margin="16dp"

android:text="@string/hello_home_fragment" />

<Button

android:id="@+id/btn_category"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:layout_marginLeft="16dp"

android:layout_marginRight="16dp"

android:text="@string/to_category" />

</LinearLayout>
Activity_main.xml

<?xml version="1.0" encoding="utf-8"?>

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"

xmlns:tools="http://schemas.android.com/tools"

android:id="@+id/frame_container"

android:layout_width="match_parent"

android:layout_height="match_parent"/>

You might also like