TimePickerDialog widget in landscape mode (PreferenceScreen)

*爱你&永不变心* 提交于 2019-12-11 01:57:29

问题


The TimePickerDialog widget in PreferenceScreen in landscape mode is too small. screenshot

I've tried out both approaches (PreferenceActivity and PreferenceFragments) according to these instructions: TimePicker in PreferenceScreen

The TimePickerDialog layout is empty in landscape mode, when using a timepicker title. So I've disabled the title. In the PreferenceActivity Version I've added setDialogTitle(""); to TimePreference. In the PreferenceFragmentCompat Version I've added the following code to TimePreference:

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
    Dialog dialog = super.onCreateDialog(savedInstanceState);
    dialog.setTitle("");
    return dialog;
}

In both versions, the clock is presented in landscape, but the widget is too small; (I have no problems with too small widgets, when using timepickerdialogs without titles outside of the PreferenceScreen)

Any ideas how to implement TimePicker in the PreferenceScreen, so that the layout is ok in landscape mode?


回答1:


Same problem here on a Motorola Moto G5 Plus phone, Android 7, in landscape mode. Portrait is ok. From the log output it looks like the preference alertdialog including the timepicker is fit vertically on the screen, thus leaving too little vertical space inside the dialog for the timepicker widget. Possible solutions:

  • In case of just a few missing dp's vertical space in the dialog, wrapping the <TimePicker> in a <ScrollView>.

  • Scaling the TimePicker down using the XML android:scaleX and android:scaleY properties (How to make the TimePicker smaller). I am currently testing this.

  • Setting the app full screen / immersive might help because it increases the 'available height'.

  • Setting the TimePicker widget to 'spinner' mode, android:timePickerMode="spinner"

Edit: The scaleX, scaleY solution is functional, but the TimePicker looks ugly. Setting the app as full screen removes on screen UI controls, which may not be the best choice for many apps. Wrapping the timepicker in a scrollview is not optimal either.

I made two XML TimePicker layouts, one with android:timePickerMode="spinner", the other with "clock". The clock layout is selected using the resource qualifier (suffix in the folder name) for available screen height: "h360dp". So that the clock is only showing when the device's available screen height, in the current orientation, excluding on-screen UI controls, is greater than 360dp. Not optimal either, but it works.

Edit 2: The approach described below is better. Tested and works on real devices from Android 4 until 7. The TimePicker dialog is properly showing in landscape orientation.

Steps are:

  • Include a general Preference item in the preferences xml file
  • Set a click listener on this Preference using onPreferenceTreeClick()
  • When this Preference is clicked, show a regular (not compatibility library) TimePickerDialog like described in the 'Pickers' guide https://developer.android.com/guide/topics/ui/controls/pickers.html
  • Save the time set on the TimePicker manually in the SharedPreferences

I put example code of all code files involved here on Stackoverflow: Appcompatactivity with custom native (not compatibility) dialogpreference containing a TimePicker



来源:https://stackoverflow.com/questions/42456966/timepickerdialog-widget-in-landscape-mode-preferencescreen

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!