2022 Oct. 23.
出典
[Android & Kotlin] Fragment をActivityに追加
[Android] Fragment コードで記述する
build.gradle (Module: app)
"implementation 'androidx.fragment:fragment-ktx:1.5.3'"を記述する
plugins { id 'com.android.application' id 'org.jetbrains.kotlin.android' } android { compileSdk 33 defaultConfig { applicationId "net.sytes.rokkosan.myfragment" minSdk 30 targetSdk 33 versionCode 1 versionName "1.0" testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" } buildTypes { release { minifyEnabled false proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro' } } compileOptions { sourceCompatibility JavaVersion.VERSION_1_8 targetCompatibility JavaVersion.VERSION_1_8 } kotlinOptions { jvmTarget = '1.8' } } dependencies { implementation 'androidx.core:core-ktx:1.9.0' implementation 'androidx.appcompat:appcompat:1.5.1' implementation 'com.google.android.material:material:1.7.0' implementation 'androidx.constraintlayout:constraintlayout:2.1.4' testImplementation 'junit:junit:4.13.2' androidTestImplementation 'androidx.test.ext:junit:1.1.3' androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0' implementation 'androidx.fragment:fragment-ktx:1.5.3' }
MainActivity.kt
/* 2022 Oct. 23. This application is copy of https://akira-watson.com/android/kotlin/fragment-programmatically.html. But modified with https://akira-watson.com/android/fragment-code.html. */ package net.sytes.rokkosan.myfragment import android.os.Bundle import androidx.appcompat.app.AppCompatActivity import androidx.fragment.app.FragmentManager import androidx.fragment.app.FragmentTransaction class MainActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) if (savedInstanceState == null) { // FragmentManagerのインスタンス生成 val fragmentManager: FragmentManager = supportFragmentManager // FragmentTransactionのインスタンスを取得 val fragmentTransaction: FragmentTransaction = fragmentManager.beginTransaction() // インスタンスに対して張り付け方を指定する fragmentTransaction.replace(R.id.fragment, SampleFragment()) // 張り付けを実行 fragmentTransaction.commit() } } }
SampleFragment.kt
package net.sytes.rokkosan.myfragment import android.os.Bundle import android.view.LayoutInflater import android.view.View import android.view.ViewGroup import androidx.fragment.app.Fragment class SampleFragment: Fragment() { override fun onCreateView( inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle? ): View { // Inflate the layout for this fragment return inflater.inflate(R.layout.fragment_main, container, false) } }
activity_main.xml
<?xml version="1.0" encoding="utf-8"?> <androidx.constraintlayout.widget.ConstraintLayout 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:layout_width="match_parent" android:layout_height="match_parent" android:padding="20dp" android:background="#def" tools:context=".MainActivity"> <androidx.fragment.app.FragmentContainerView android:id="@+id/fragment" android:layout_width="wrap_content" android:layout_height="300dp" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintRight_toRightOf="parent" app:layout_constraintTop_toTopOf="parent" app:layout_constraintVertical_bias="0.5" /> </androidx.constraintlayout.widget.ConstraintLayout>
fragment_main.xml
<?xml version="1.0" encoding="utf-8"?> <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#bcd" android:orientation="vertical" android:gravity="center" > <TextView android:text="@string/hello_fragment" android:layout_width="wrap_content" android:textSize="40sp" android:textColor="#03f" android:layout_height="wrap_content" android:id="@+id/textView" app:layout_constraintTop_toTopOf="parent" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintRight_toRightOf="parent" /> </androidx.constraintlayout.widget.ConstraintLayout>
strings.xml
<resources> <string name="app_name">MyFragment</string> <string name="hello_fragment">Hello Fragment</string> </resources>