Android Studio - Kotlin - open fragment from optionsmenu | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

Android Studio - Kotlin - open fragment from optionsmenu

Hello community I am new at kotlin and want to ask, if someone could help me to write some functions which allow me to open a fragment when the item in the optionsmenu is clicked. I can't get any info about it online and I think, I am a too beginner-minded person to go for it alone. I hava a optionsmenu.xml file which acts like the main_activity. In there, there are 5 items with the ids optionsmenuitem1-5 When I click in the optionsmenu on "Backups" it should open the BackupFragment. Does anyone have an idea how I could handle this, or what I do wrong? Basically I realised, that I don't have that much understanding to solve it. MainActivity.kt: // open BackupFragment from optionsmenu override fun onClick(view: View?) { when(item?.optm_backupitem){ R.id.backupfragment->{ return true } Thank you very much for trying to help me. Kind regards Pascal

29th Dec 2021, 7:27 PM
Pascal
9 Answers
+ 1
Please put your onOptionsItemSelected method below the onCreate method. This might solve a few problems. And as for the R.id.ConstraintLayout error, go to your backup fragment's layout file and click on the Design tab. Inside the Component Tree, select the first item. Then in the Attributes section, the first item would be "id". In id, write an id for the layout. Copy that id and paste it instead of ConstraintLayout in R.id.ConstraintLayout.
9th Jan 2022, 4:52 AM
Rivan
0
You should use the onOptionsItemSelected method to achieve it. For example: override fun onOptionsItemSelected(item : MenuItem?) : Boolean { val id = item!!.itemId when(id) { "R.id.backupFragment" -> { supportFragmentManager.Commit { setReorderingAllowed(true)   add<BackupFragment>(R.id.fragment_container_view)             } } } } Note that you need to use the AndroidX Fragment Library to use this method. Hope it helps.
4th Jan 2022, 4:52 AM
Rivan
0
Thank you for your help Rivan it looks like something is still not quite right in my Code. The id of the options menu item Backup can't be recognised by the system. And the problems are starting with an "Incompatible types: String and Int" Error. The others in red are unresolved reference errors. Could you help me? https://www.sololearn.com/post/1461266/?ref=app
5th Jan 2022, 3:31 AM
Pascal
0
The "R.id.backupFragment" is your menu item's id that you have written in your optionsmenu.xml file. So replace it with your own id and remove the double inverted commas. Add this to your build.gradle file if it isn't already there: For Groovy add this code: dependencies {     def fragment_version = "1.4.0"     implementation "androidx.fragment:fragment-ktx:$fragment_version" } And if it is Kotlin then add this: dependencies {     val fragment_version = "1.4.0"     implementation("androidx.fragment:fragment-ktx:$fragment_version") } And also add { after supportFragmentManager.Commit in your MainActivity.kt. Instead of R.id.fragment_container_view, you have to add your own layout's view id, i.e., if it's a ConstraintLayout then give it some id like backupFragmentLayout and use it in your MainActivity.
5th Jan 2022, 4:24 AM
Rivan
0
Thank you Rivan for your answer. Now, there are some unresolved reference errors left: https://www.sololearn.com/post/1466771/?ref=app
8th Jan 2022, 10:31 AM
Pascal
0
Rivan I think the id here should be the name "unnamed" right? But how can I change it now? If not, I also searched in the whole attributes. Nothing with the name ID. Hmmm... In the attributes options menu I also selected everything in there, so I can see and search all attributes. Oh my God, I am such an beginner! ☹️😩 https://www.sololearn.com/post/1468651/?ref=app
9th Jan 2022, 1:30 PM
Pascal
0
Now I saw, that there is an import problem. Maybe this helps? https://www.sololearn.com/post/1468704/?ref=app
9th Jan 2022, 2:18 PM
Pascal
0
In your layout file, go to Code tab. In there you would find something like this: <?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:id="@+id/your_id> Simply add this line: android:id="@+id/your_id" as shown above. Replace your_id with whatever id you want.
10th Jan 2022, 5:07 AM
Rivan
0
And as for the import error, first go to Build tab and click on Clean Project. Then Rebuild your project.
10th Jan 2022, 5:11 AM
Rivan