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!