Search code examples
javaandroidxmllistpreference

How do I enable a disabled (in xml) ListPreference object in .java code?


EDIT 3

Here's how I determine if the device is a tablet in onCreate for main Activity:

  public static boolean isTablet(Context ctx){
    return (ctx.getResources().getConfiguration().screenLayout
        & Configuration.SCREENLAYOUT_SIZE_MASK
    )
        >= Configuration.SCREENLAYOUT_SIZE_LARGE;
  }

I don't know if that's a hack and not reliable, but in any event, I don't know how to use it in onCreate in the Fragment (below) because I don't know what Context to pass to isATablet.

  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    addPreferencesFromResource(R.xml.preferences);Preference p = getPreferenceManager().findPreference("orientation");
    p.setEnabled(isTablet(????????????????));

EDIT showing arrays as defined in strings.xml as requested:

<string-array name="modes">
    <item>0</item>
    <item>1</item>
    <item>2</item>
</string-array>
<string-array name="indices">
    <item>Portrait</item>
    <item>Landscape</item>
    <item>Rotate</item>
</string-array>

* END EDIT *

How do I enable this ListPreference object in .java code?

<PreferenceCategory>
    <ListPreference
        android:key="orientation"
        android:title="Orientation (for tablets only)"
        android:enabled="false"
        android:summary="Lock in portrait or landscape mode or neither (allow rotation to determine)?"
        android:defaultValue="0"
        android:entries="@array/indices"
        android:entryValues="@array/modes"
        >
    </ListPreference>
</PreferenceCategory>

I can't make Android Studio 1.1.0 let me give it an id; but if there's a way, there's my solution--how do I do it?

EDIT

Workaround:

android:enabled="true"

if(isATablet) { // ***************************
  switch (preferences.getString(ORIENTATION, "0")) {
    case "0":
      setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
      break;
    case "1":
      setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
      break;
    case "2":
      setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR);
      break;
    default:
      Toast.makeText(getApplicationContext(), "Bad orientation", Toast.LENGTH_SHORT).show();
  }
}
else{ // ***************************
  editor = preferences.edit(); // ***************************
  editor.putString(ORIENTATION,"0"); // ***************************
  editor.commit(); // ***************************
  setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);

}

Very lame. Please help!


Solution

  • It looks like you can just use the PreferenceManager and get the Preference based on the key. See documentation here and here.

    You could use the method described here to determine if it's a tablet or not.

    Then, it's just as simple as getting the preference, and enabling it if it's a tablet!

    Edit:

    In order to use your method and give it a valid Context, you could do it like this. Note that I used a modified version of the code posted here for the isTablet() method:

     public class SettingsFragment extends PreferenceFragment implements OnSharedPreferenceChangeListener {
            @Override
            public void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                addPreferencesFromResource(R.xml.preferences);
    
                Preference p = getPreferenceManager().findPreference("orientation");
                p.setEnabled(isTablet(getActivity()));
            }
    
            private boolean isTablet(Context context) {
                int screenLayout = context.getResources().getConfiguration().screenLayout;
                screenLayout &= Configuration.SCREENLAYOUT_SIZE_MASK;
    
                switch (screenLayout) {
                    case Configuration.SCREENLAYOUT_SIZE_SMALL:
                        return false;
                    case Configuration.SCREENLAYOUT_SIZE_NORMAL:
                        return false;
                    case Configuration.SCREENLAYOUT_SIZE_LARGE:
                        return true;
                    case 4: // Configuration.SCREENLAYOUT_SIZE_XLARGE is API >= 9
                        return true;
                    default:
                        return false;
                }
            }
            //...................