Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use tools:context to associtate Fragment files?

Recently I came across the use-case of using tools:context in my layout files for activities. I learned that I can use it to associate an Activity class file to my layout file.

But how can I use it to associate a Fragment class file to any layout?

Bacuase as I have tried, it is only showing Activity list in autocomplete suggestion.

like image 795
Dhaval Avatar asked Jan 26 '23 19:01

Dhaval


2 Answers

There is a bug in Android Studio, you have to write it manually, it will appear red(indicating it's unresolved) until you finish typing, then it will appear in green

like image 98
Mohammad Elsayed Avatar answered Jan 28 '23 08:01

Mohammad Elsayed


Have you tried it doing like this:

Let's say Relative layout is your parent layout in your fragment's layout:

<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=".Folder.Fragment"  >

Now this .xml file will be associated with your .Fragment class which is a fragment.

like image 37
Umair Avatar answered Jan 28 '23 08:01

Umair