Home > Blockchain >  Can't find views from not activity_main.xml
Can't find views from not activity_main.xml

Time:10-23

I'm making app with custom dialog fragment. I created a separate xml file test_parameters.xml for this dialog fragment, but when I try to refer to views there by their id, Android Studio doesn't see them. Only views from activity_main are available. What's the reason and how can I fix it?

enter image description here

MainActivity.kt:

package com.example.meltflowratecalculator

import...

class MainActivity : AppCompatActivity() {

    private lateinit var binding: ActivityMainBinding
    lateinit var methodsSpinner: Spinner
    lateinit var timeSpinner: Spinner
    lateinit var result: TextView


    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        binding = ActivityMainBinding.inflate(layoutInflater)
        setContentView(binding.root)

        binding.settingsButton.setOnClickListener {
            var dialog = CustomDialogFragment()
            dialog.show(supportFragmentManager, "customDialog")
        }
    }
}

CustomDialogFragment.kt:

package com.example.meltflowratecalculator

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.DialogFragment

class CustomDialogFragment : DialogFragment() {
    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        var rootView: View = inflater.inflate(R.layout.test_parameters, container, false)

        return rootView
    }
}

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"
    tools:context=".MainActivity">

    <com.google.android.material.floatingactionbutton.FloatingActionButton
        android:id="@ id/settings_button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="end|bottom"
        android:layout_margin="16dp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintBottom_toBottomOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

test_parameters.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">

    <TextView
        android:id="@ id/result"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/testing_method_title"
        android:layout_marginTop="16dp"
        android:layout_marginStart="16dp"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <Spinner
        android:id="@ id/methods"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="16dp"
        android:layout_marginTop="16dp"
        android:entries="@array/methods"
        android:minHeight="48dp"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@id/result" />

</androidx.constraintlayout.widget.ConstraintLayout>

CodePudding user response:

You will have to create a class extending DialogFragment. There you need to set the id.

CodePudding user response:

You're getting that binding object by calling ActivityMainBinding#inflate - ActivityMainBinding is automatically generated from activity_main.xml (hence the name). It only contains bindings for the views in that layout file (and anything it includes but that's more advanced) - right now you only have one view with an id, settings_button. That's why settingsButton is the only property you can see in the binding file.

You can use findViewById to search the view hierarchy for Fragments' views - but that's generally a bad idea, an Activity shouldn't know anything about the Fragment's internal workings or layout. The Fragment should handle all that itself, and either pass data to its host activity directly, or put the data somewhere the Activity can access it (like a ViewModel, or in a Navigation backstack entry)

Basically put all your functionality inside the DialogFragment - you can use TestParametersBinding to inflate the dialog's layout in the same way you did in your activity, and you can access all its views in there, and handle whatever it is the dialog does. The activity should just show it, and do something with the result it sends back

  • Related