27
27
import android .view .KeyEvent ;
28
28
import android .view .LayoutInflater ;
29
29
import android .view .View ;
30
+ import android .view .ViewTreeObserver ;
30
31
import android .view .inputmethod .EditorInfo ;
31
32
import android .view .inputmethod .InputMethodManager ;
32
33
import android .widget .EditText ;
@@ -40,7 +41,7 @@ public class ColorPickerDialog
40
41
Dialog
41
42
implements
42
43
ColorPickerView .OnColorChangedListener ,
43
- View .OnClickListener {
44
+ View .OnClickListener , ViewTreeObserver . OnGlobalLayoutListener {
44
45
45
46
private ColorPickerView mColorPicker ;
46
47
@@ -52,6 +53,20 @@ public class ColorPickerDialog
52
53
private ColorStateList mHexDefaultTextColor ;
53
54
54
55
private OnColorChangedListener mListener ;
56
+ private int mOrientation ;
57
+ private View mLayout ;
58
+
59
+ @ Override
60
+ public void onGlobalLayout () {
61
+ if (getContext ().getResources ().getConfiguration ().orientation != mOrientation ) {
62
+ final int oldcolor = mOldColor .getColor ();
63
+ final int newcolor = mNewColor .getColor ();
64
+ mLayout .getViewTreeObserver ().removeGlobalOnLayoutListener (this );
65
+ setUp (oldcolor );
66
+ mNewColor .setColor (newcolor );
67
+ mColorPicker .setColor (newcolor );
68
+ }
69
+ }
55
70
56
71
public interface OnColorChangedListener {
57
72
public void onColorChanged (int color );
@@ -75,17 +90,19 @@ private void setUp(int color) {
75
90
76
91
LayoutInflater inflater = (LayoutInflater ) getContext ().getSystemService (Context .LAYOUT_INFLATER_SERVICE );
77
92
78
- View layout = inflater .inflate (R .layout .dialog_color_picker , null );
93
+ mLayout = inflater .inflate (R .layout .dialog_color_picker , null );
94
+ mLayout .getViewTreeObserver ().addOnGlobalLayoutListener (this );
79
95
80
- setContentView (layout );
96
+ mOrientation = getContext ().getResources ().getConfiguration ().orientation ;
97
+ setContentView (mLayout );
81
98
82
99
setTitle (R .string .dialog_color_picker );
83
100
84
- mColorPicker = (ColorPickerView ) layout .findViewById (R .id .color_picker_view );
85
- mOldColor = (ColorPickerPanelView ) layout .findViewById (R .id .old_color_panel );
86
- mNewColor = (ColorPickerPanelView ) layout .findViewById (R .id .new_color_panel );
101
+ mColorPicker = (ColorPickerView ) mLayout .findViewById (R .id .color_picker_view );
102
+ mOldColor = (ColorPickerPanelView ) mLayout .findViewById (R .id .old_color_panel );
103
+ mNewColor = (ColorPickerPanelView ) mLayout .findViewById (R .id .new_color_panel );
87
104
88
- mHexVal = (EditText ) layout .findViewById (R .id .hex_val );
105
+ mHexVal = (EditText ) mLayout .findViewById (R .id .hex_val );
89
106
mHexVal .setInputType (InputType .TYPE_TEXT_FLAG_NO_SUGGESTIONS );
90
107
mHexDefaultTextColor = mHexVal .getTextColors ();
91
108
0 commit comments