Skip to content Skip to sidebar Skip to footer

Access View On Content_main.xml From A Fragment?

In my app I use NavController with Fragments. In some fragments I need to access views on MainActivity layout. I do that in this way: val fab: FloatingActionButton = requireActivit

Solution 1:

So, if someone should look for a solution, here is an one that worked for me. First of all, I found out that I needed to reorder my XML layouts so that put the contents of the content_main.xml and the app_bar_layout.xml directly into the activity_main.xml like this:

activity_main.xml

<?xml version="1.0" encoding="utf-8"?><androidx.drawerlayout.widget.DrawerLayoutxmlns: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:id="@+id/drawer"...><androidx.constraintlayout.widget.ConstraintLayoutandroid:layout_width="match_parent"android:layout_height="match_parent"><androidx.coordinatorlayout.widget.CoordinatorLayoutandroid:id="@+id/coordinator"...><com.google.android.material.appbar.AppBarLayout... ><com.google.android.material.appbar.CollapsingToolbarLayoutandroid:id="@+id/collapsing"... ><com.google.android.material.appbar.MaterialToolbarandroid:id="@+id/toolbar"... /></com.google.android.material.appbar.CollapsingToolbarLayout></com.google.android.material.appbar.AppBarLayout><androidx.core.widget.NestedScrollViewandroid:id="@+id/nested"... ><androidx.fragment.app.FragmentContainerViewandroid:id="@+id/host"... /></androidx.core.widget.NestedScrollView><com.google.android.material.floatingactionbutton.FloatingActionButtonandroid:id="@+id/fab"... /></androidx.coordinatorlayout.widget.CoordinatorLayout></androidx.constraintlayout.widget.ConstraintLayout><com.google.android.material.navigation.NavigationViewandroid:id="@+id/nav"... /></androidx.drawerlayout.widget.DrawerLayout>

It might be not an optimal way, but without this step, the Views on content_main.xml could not be accessed, which is really a bullshit as before it properly worked. Actually, no idea, but it seems that Google changed something in its libraries or whatever that should be.

As a next step, Views you want to access on the activity_main.xml from a certain Fragment, you have to make them public:

MainActivity.kt

classMainActivity : AppCompatActivity() {
    
    ...

    lateinitvar fab: FloatingActionButton
    lateinitvar nested: NestedScrollView

    ...

    overridefunonCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        ...

        nested = findViewById(R.id.nested)
        fab = findViewById(R.id.fab)
    
        ...
    }
}

Now, they can be used in a Fragment like this:

MyFragment.kt

classMyFragment : Fragment() {

    ...

    overridefunonCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        return inflater.inflate(R.layout.fragment_my, container, false)
    }

    overridefunonViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        
        ...

        var fab: FloatingActionButton
        var nested: NestedScrollView

        (activity as MainActivity).apply {
            fab = this.fab
            nested = this.nested
        }

        ...
    }
}

That's all guys! In this way, I could achieve my goals.

As mentioned above, this might not be an optimal way, but it worked for me.

Hopefully, this helps someone to fix this kind of problem.

Any improvements or suggestions are welcome and appreciated.

Post a Comment for "Access View On Content_main.xml From A Fragment?"