1

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!

DSlomer64
  • 4,234
  • 4
  • 53
  • 88
  • 1
    Can you post your `@array/indices` and `@array/modes`? – Daniel Nugent May 17 '15 at 18:54
  • I have a feeling I'm going about this wrong... – DSlomer64 May 17 '15 at 19:00
  • You want this preference to be enabled for tablets only? – Daniel Nugent May 17 '15 at 19:07
  • Are you using the method described Here? http://stackoverflow.com/questions/9279111/determine-if-the-device-is-a-smartphone-or-tablet – Daniel Nugent May 17 '15 at 19:12
  • @DanielNugent--no, but which method? (long link) What I want is to disallow rotation if the device doesn't have great enough "landscape height" (i.e., width) to display my entire UI, which is true for typical phones. I use FIRST `isATablet` method found [here](http://stackoverflow.com/questions/5015094/how-to-determine-device-screen-size-category-small-normal-large-xlarge-usin). – DSlomer64 May 17 '15 at 19:35
  • I think that would work too. I direct linked to another way to do it in my answer! – Daniel Nugent May 17 '15 at 19:44
  • I tried the 3 xml files approach but get error: `Error:Execution failed for task ':app:mergeProDebugResources'. > C:\Users\Dov\AndroidStudioProjects\KakuroCombosBuildVariants\app\src\main\res\values\values-xlarge.xml: Error: Duplicate resources: C:\Users\Dov\AndroidStudioProjects\KakuroCombosBuildVariants\app\src\main\res\values\values-xlarge.xml:bool/isTablet, C:\Users\Dov\AndroidStudioProjects\KakuroCombosBuildVariants\app\src\main\res\values\values-sw600dp.xml:bool/isTablet` – DSlomer64 May 17 '15 at 20:00
  • Hmm, not sure what's going on there. I'd say go with your original method then, I'll remove the suggestion from the answer. Aside from that, is the Java code to enable the preference working? – Daniel Nugent May 17 '15 at 20:03
  • I have wrong directory structure for the 3 xml files. Hold off removing suggestion. – DSlomer64 May 17 '15 at 20:04
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/78041/discussion-between-dslomer64-and-daniel-nugent). – DSlomer64 May 17 '15 at 20:05

2 Answers2

2

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;
            }
        }
        //...................
Community
  • 1
  • 1
Daniel Nugent
  • 43,104
  • 15
  • 109
  • 137
  • I'm at Barnes and Noble and the connection to chat just got severed. – DSlomer64 May 17 '15 at 20:53
  • 1
    @DSlomer64 I just updated the answer, this should work. I used a variation that someone posted in order to make the `isTablet()` method a bit more readable and understandable. I modified the code from this answer: http://stackoverflow.com/questions/5015094/how-to-determine-device-screen-size-category-small-normal-large-xlarge-usin/19256468#19256468 – Daniel Nugent May 17 '15 at 20:56
  • My last chat comment was going to be that `void` isn't valid return type for `onCreateView`. – DSlomer64 May 17 '15 at 20:56
  • And 'static' is causing error, too, in `class` definition. Are you using Android Studio? 1.1.0? – DSlomer64 May 17 '15 at 21:00
  • If I change return type to `View`, then AS gripes about `protected` and changes it to `public`. Fine by me, but what does it return? Not `null`; not `viewGroup` (which is results in `IllegalStateException: Fragment com.dslomer64.kakurocombosbuildvariants.SettingsFragment did not create a view`). – DSlomer64 May 17 '15 at 21:06
0

Thanks to incredible generosity of Daniel Nugent, here's what finally worked.

preferences.xml (don't enable or disable orientation):

    <PreferenceCategory>
        <ListPreference
            android:key="orientation"
            android:title="Orientation (for tablets only)"
            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>
</PreferenceScreen>

SettingsFragment.java:

public class SettingsFragment extends PreferenceFragment implements OnSharedPreferenceChangeListener {

  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    addPreferencesFromResource(R.xml.preferences);
//  next two lines ****************************************************
    Preference p = getPreferenceManager().findPreference("orientation");
    p.setEnabled(isTablet(getActivity()));
//  Let java code enable or disable orientation ***********************
  }

  @Override
  public void onResume() {
    super.onResume();
    getPreferenceScreen().getSharedPreferences().registerOnSharedPreferenceChangeListener(this);
  }
  @Override
  public void onPause() {
    super.onPause();
    getPreferenceScreen().getSharedPreferences().unregisterOnSharedPreferenceChangeListener(this);
  }
  @Override
  public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
    SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(getActivity());

  }
}

isTablet.java:

  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;
    }
  }
DSlomer64
  • 4,234
  • 4
  • 53
  • 88