0

This problem does not occur in regular layout XML files, but it does occur in PreferenceScreen xml files. Is this expected behavior? When I define a custom view and add it to a PreferenceScreen directly it's XML file, that custom view has no auto-complete or refactoring capabilities for its attributes.

To clarify what I mean by custom Preferences: I use this method to implement custom Preferences.

So let's go by the example in that link. What I want to do is have all the benefits of auto-complete (e.g. IntelliSense). Suppose I want to create a custom SeekBar that I defined/created, but instead of typing manually the package name and class name, I press CTRL+SPACE to bring up the auto-complete list. But it doesn't show up!

Suppose I want this to be my final PreferenceScreen xml:

<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" >
    
    <com.example.SeekbarPreference
        android:key="pref_volume"
        android:title="@string/volume" />
    
</PreferenceScreen>

I want to CTRL+SPACE to autocomplete my custom Preference:

<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" >
    
    <com.ex
    
</PreferenceScreen>

But it doesn't work! It does work for Custom Views under normal layout files that are inflated by Activities. But if it's a PreferenceScreen, I have to type that all out but I am prone to make errors.

Furthermore, I want to refactor my resources that the custom Preference's attributes reference. Suppose I go into my "strings.xml" resources file and change "@string/volume" to "@string/volume_label"

<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" >
    
    <com.example.SeekbarPreference
        android:key="pref_volume"
        android:title="@string/volume" />
    
</PreferenceScreen>

When I am in my "strings.xml" file, if I right click and refactor "volume" to "volume_label", these resources are renamed in the entire projects including in layout xml files and java class files. However, the "@string/volume" in the PreferenceScreen's file stays unchanged. And when I compile and install the app I get an error from Android Studio that the String resource called "volume" does not exist.

If I am in my PreferenceScreen's XML file, if I right click on: android:title="@string/volume", the refactor > rename button is missing.

poetryrocksalot
  • 697
  • 2
  • 10
  • 19

1 Answers1

0

I solved it.

The problem arose from the access modifier specified for the custom Preference class.

If you want to be able to refactor and have autocomplete enabled for your custom Preferences, Android studio still needs access to the class.

Here is what it looks like if autocomplete and refactoring does not work:

class MySeekBarPreference extends SeekBarPreference {
    // Autocomplete and refactoring DISABLED in Android Studio
}

Here is what it needs to look like if you want autocomplete and refactoring to work:

public class MySeekBarPreference extends SeekBarPreference {
    // Autocomplete and refactoring ENABLED in Android Studio
}

With this new info in mind, I don't think the original problem was relevant to whether it was due to it being a PreferenceScreen or a regular inflated layout. My other custom Views that worked in regular layouts were public, but my custom Preference in PreferenceScreen was not public.

poetryrocksalot
  • 697
  • 2
  • 10
  • 19