What I have done :
- I'm trying to add .XML with a relative layout to a fragment named as 'home_frag'.
- I have first created the home_frag.XML in layout as mentioned below and then created the fragment java class which is mentioned below.
- But, when I tried to inflate home_frag.XML inside fragment class, it says 'Cannot resolve symbol 'home_frag' and it is in red as mentioned below.
- Seems like my .XML is not accessible inside fragment class.
- I tried the 3 options suggested by Android studio
- *rename - not useful since it suggests other layouts.
- *create layout resource value - try to create the string 'home_frag' inside strings.xml but it didn't solve the issue.
- *create layout resource file - I deleted my 'home_frag' and created again using this option. But results were the same when I tried to inflate it was in red meaning unable to access the layout file.
What I'm expecting:
This is kind of strange which I cannot figure out what is going on. I can see 'home_frag' inside layout files too.
What I'm looking for is simply to connect my 'home_frag.xml' with 'HomeFrag.java' fragment class which always appear in red.
HomeFrag.java :
package com.example.myapplication.fragments;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import com.example.myapplication.R;
public class HomeFrag extends Fragment {
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
return inflater.inflate(R.layout.home_frag,container,false);
}
}
home_frag.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#8BC34A">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/title"
android:text="@string/notifications"
android:textSize="30sp"
android:layout_centerInParent="true"
/>
</RelativeLayout>
I would appreciate any suggestion on this. I'm using Android Studio 3.6.1. Thank you.
Answer :
I was able to solve my issue just by doing File > Invalidate Chaches / Restart.
Both HomeFrag and home_frag.xml look correct. I would suspect it's either Android studio not indexing, you could try clicking run and see if it works or gives you an error in the build output. Try File -> Invalidate Caches / Restart is another good one to try if this is the problem. If that doesn't work then it could be that the string "notifications" doesn't exist in your res/values (most likely strings.xml) or your layout isn't located in res/layout/home_frag.xml