Alex Burdusel
Alex Burdusel

Reputation: 3184

Android Studio not identifying xml file as layout file

I accidentally created drawer_list_item.xml in the screenshot using New -> File menu, instead of New -> Layout resource file and now I can't open it as a regular layout file in design mode. It opens it as a plain text file.

enter image description here

Is there any way I can change its type. I couldn't find anything related to this through Android Studio's settings

File content:

  <?xml version="1.0" encoding="utf-8"?>
  <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:orientation="vertical"
              android:layout_width="match_parent"
              android:layout_height="match_parent">

              <TextView
              android:layout_width="match_parent"
              android:layout_height="match_parent" />

  </LinearLayout>

Edit for more things I tried:

Upvotes: 12

Views: 8765

Answers (5)

Rohith K N
Rohith K N

Reputation: 871

I had a similar issue because of importandroid.R in one of the activity file. Just removed it and it is all good now.

Upvotes: 1

Mehdi
Mehdi

Reputation: 3783

It happens sometimes when wrong indexes/caches are created by Android Studio.

  1. Go to File menu
  2. Click on Invalidate Caches / Restart
  3. Restart your IDE and wait for IDE to reindex the files

It should be okay after doing those ...

Upvotes: 2

Gary McGowan
Gary McGowan

Reputation: 1681

Preferences -> File Types and remove the Registered PatternAndroid Studio Preferences

Upvotes: 11

marchinram
marchinram

Reputation: 5888

I simply had to right click on the file in the Project view and select 'Mark as XML' in Android Studio 2.2.x

enter image description here

Upvotes: 2

Alex Burdusel
Alex Burdusel

Reputation: 3184

Found the source of the problem - it was in the config files Android Studio creates in your home directory: ~/.AndroidStudioPreview2.0. In my case, it was ~/.AndroidStudioPreview2.0/config/options/filetypes.xml

    <application>
      <component name="FileTypeManager" version="16">
        <ignoreFiles list="*.hprof;*.pyc;*.pyo;*.rbc;*~;.DS_Store;.git;.hg;.svn;CVS;RCS;SCCS;__pycache__;_svn;rcs;" />
        <extensionMap>
          <mapping pattern="drawer_list_item.xml" type="PLAIN_TEXT" />
        </extensionMap>
      </component>
    </application>

Therefore, it seems like I somehow made Android Studio index that file name pattern drawer_list_item.xml as plain text... Removing that mapping entry from the file solves the problem: <mapping pattern="drawer_list_item.xml" type="PLAIN_TEXT" />

Upvotes: 13

Related Questions