Last active
October 6, 2022 09:17
-
-
Save mikkipastel/86f62ece0f100aedb5342f12460b09c6 to your computer and use it in GitHub Desktop.
add second fragment
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
<?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"> | |
<FrameLayout | |
android:id="@+id/container" | |
android:layout_width="match_parent" | |
android:layout_height="match_parent"/> | |
<EditText | |
android:id="@+id/edittext" | |
android:layout_width="match_parent" | |
android:layout_height="wrap_content" | |
android:layout_marginHorizontal="24dp" | |
android:hint="type me" | |
app:layout_constraintStart_toStartOf="parent" | |
app:layout_constraintEnd_toEndOf="parent" | |
app:layout_constraintTop_toTopOf="parent" | |
app:layout_constraintBottom_toBottomOf="parent" /> | |
<Button | |
android:id="@+id/button" | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:layout_marginTop="16dp" | |
android:text="Click Me" | |
app:layout_constraintTop_toBottomOf="@+id/edittext" | |
app:layout_constraintStart_toStartOf="@id/edittext" | |
app:layout_constraintEnd_toEndOf="@id/edittext" /> | |
</androidx.constraintlayout.widget.ConstraintLayout> |
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
package com.ascedncorp.androidbasic | |
import android.os.Bundle | |
import android.view.LayoutInflater | |
import android.view.View | |
import android.view.ViewGroup | |
import androidx.fragment.app.Fragment | |
import com.ascedncorp.androidbasic.databinding.FragmentMainBinding | |
class MainFragment: Fragment() { | |
private lateinit var binding: FragmentMainBinding | |
companion object { | |
fun newInstance() = MainFragment() | |
} | |
override fun onCreateView( | |
inflater: LayoutInflater, | |
container: ViewGroup?, | |
savedInstanceState: Bundle?, | |
): View { | |
binding = FragmentMainBinding.inflate(inflater, container, false) | |
return binding.root | |
} | |
override fun onViewCreated(view: View, savedInstanceState: Bundle?) { | |
super.onViewCreated(view, savedInstanceState) | |
val text = binding.edittext.text.toString() | |
binding.button.setOnClickListener { | |
requireActivity().supportFragmentManager.beginTransaction() | |
.add(R.id.rootview, SecondFragment.newInstance(text)) | |
.addToBackStack(null) | |
.commit() | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment