I am trying to access an TextView element on a wrapped Relative layout by id but it doesnt shows up in the reference’s Id, So how could I possibly get the element’s view by id with the findViewById method?
I have added the extension that allows me to reference the element directly but it is not exactly what I am looking for
I am using a navigation view to switch between fragments and the main activity has a fragment that displays the selected item in the menu
so how can I possibly access the own’s element in the view layout inside the fragment definition???
fragment definition
activity main
<?xml version="1.0" encoding="utf-8"?>
<androidx.drawerlayout.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
tools:context=".MainActivity">
<androidx.constraintlayout.widget.ConstraintLayout
android:id="@+id/mainLayout">
<include layout="@layout/action_bar"></include>
<fragment
android:layout_width="match_parent"
android:layout_height="0dp"
android:id="@+id/navHostFragment"
android:name="androidx.navigation.fragment.NavHostFragment"
app:defaultNavHost="true"
app:navGraph="@navigation/main_navigation">
</fragment>
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.drawerlayout.widget.DrawerLayout>
galleryNumber in R.id is not showing up
package com.example.applicationtest
import android.os.Bundle
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import kotlinx.android.synthetic.main.gallery_fragment.view.*
import android.R
import kotlinx.android.synthetic.main.gallery_fragment.*
class gallery_fragment : Fragment() {
private var param1: String? = null
private var param2: String? = null
lateinit var recyclerView: RecyclerView
lateinit var galleryAdapter:GalleryAdapter;
lateinit var imageList:MutableList<String>
lateinit var galleryNumberText:TextView
fun GetGalleryFragment(inflater: LayoutInflater) :ViewGroup = inflater.inflate(this.id, null) as ViewGroup
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
var _galleryFragment = GetGalleryFragment(inflater)
val but = _galleryFragment.findViewById<View>(R.id.galleryNumber) as TextView
galleryNumberText = galleryNumber //this works only because the extension has been added but it is not really what I want
// Inflate the layout for this fragment
return inflater.inflate(this.id, container, false)
}
fragment view
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
tools:context=".gallery_fragment"
android:id="@+id/gallery_fragment"
android:background="#333333"
android:orientation="vertical">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="56dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/galleryNumber"
android:layout_centerVertical="true"
android:layout_marginLeft="16dp"
android:textColor="@color/white"
android:textStyle="bold"
android:textSize="18dp"
/>
</RelativeLayout>
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recyclerViewImageItems"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</androidx.recyclerview.widget.RecyclerView>
</RelativeLayout>```
2
Answers
Delete your
GetGalleryFragment
function because it is completely wrong. It doesn’t make sense to inflate extra copies of your layout.Also, class names should always start with a capital letter to fit convention and so you can tell the difference between constructor calls and regular function calls, and the difference between type references and variable names. (Conversely and for the same reasons, function names should not start with a capital letter in Kotlin.)
It’s no longer necessary to override
onCreateView()
because there is now a Fragment constructor that can do it for you just by passing the layout name. This is much simpler. You should overrideonViewCreated()
instead and you can find your views inside this function by searching on the provided rootview
.Also, I don’t recommend combining
var
andMutable
-anything. And you shouldn’t ever have to uselateinit
for a List that starts empty. It’s cleaner to initialize variables at the declaration site when possible. Probably this list should be moved to a ViewModel, but I’m leaving it below.You might want to switch to View Binding so you don’t have to deal with finding and creating properties for each individual view.
if you are new to kotlin you can use android kotlin extention plugin to remove dependency on findviewby id. you can directly access the id of the view in your kotlin file without using find viewby id .
here is the link for the reference to how to use plugin and how can you access it:
https://antonioleiva.com/kotlin-android-extensions/
you can add plugin in your build.gradle app file like this
now you can directly access the view defined in the XML by using the id name you have provided.
Hence it is deprecated by google so you can also use viewbinding to achive the same.
Here is the link to use ViewBinding in your app.
https://developer.android.com/topic/libraries/view-binding