42
42
import android .widget .FrameLayout ;
43
43
import android .widget .LinearLayout ;
44
44
import android .widget .RelativeLayout ;
45
+ import android .widget .ScrollView ;
45
46
import android .widget .TextView ;
46
47
import android .widget .Toast ;
47
48
import android .widget .Toolbar ;
@@ -773,16 +774,15 @@ public static Pair<Dialog, LinearLayout> createCustomDialog(
773
774
Dialog dialog = new Dialog (context );
774
775
dialog .requestWindowFeature (Window .FEATURE_NO_TITLE ); // Remove default title bar.
775
776
776
- // Create main layout.
777
- LinearLayout mainLayout = new LinearLayout (context );
778
- mainLayout .setOrientation (LinearLayout .VERTICAL );
779
-
780
777
// Preset size constants.
781
778
final int dip4 = dipToPixels (4 );
782
779
final int dip8 = dipToPixels (8 );
783
780
final int dip16 = dipToPixels (16 );
784
781
final int dip24 = dipToPixels (24 );
785
782
783
+ // Create main layout.
784
+ LinearLayout mainLayout = new LinearLayout (context );
785
+ mainLayout .setOrientation (LinearLayout .VERTICAL );
786
786
mainLayout .setPadding (dip24 , dip16 , dip24 , dip24 );
787
787
// Set rounded rectangle background.
788
788
ShapeDrawable mainBackground = new ShapeDrawable (new RoundRectShape (
@@ -802,55 +802,71 @@ public static Pair<Dialog, LinearLayout> createCustomDialog(
802
802
ViewGroup .LayoutParams .MATCH_PARENT ,
803
803
ViewGroup .LayoutParams .WRAP_CONTENT
804
804
);
805
- layoutParams .setMargins (0 , 0 , 0 , dip8 );
805
+ layoutParams .setMargins (0 , 0 , 0 , dip16 );
806
806
titleView .setLayoutParams (layoutParams );
807
807
mainLayout .addView (titleView );
808
808
}
809
809
810
- // Message (if not replaced by EditText).
811
- if (editText == null && message != null ) {
812
- TextView messageView = new TextView (context );
813
- messageView .setText (message ); // Supports Spanned (HTML).
814
- messageView .setTextSize (16 );
815
- messageView .setTextColor (getAppForegroundColor ());
816
- // Enable HTML link clicking if the message contains links.
817
- if (message instanceof Spanned ) {
818
- messageView .setMovementMethod (LinkMovementMethod .getInstance ());
810
+ // Create content container (message/EditText) inside a ScrollView only if message or editText is provided.
811
+ ScrollView contentScrollView = null ;
812
+ LinearLayout contentContainer = null ;
813
+ if (message != null || editText != null ) {
814
+ contentScrollView = new ScrollView (context );
815
+ contentScrollView .setVerticalScrollBarEnabled (false ); // Disable the vertical scrollbar.
816
+ contentScrollView .setOverScrollMode (View .OVER_SCROLL_NEVER );
817
+ if (editText != null ) {
818
+ ShapeDrawable scrollViewBackground = new ShapeDrawable (new RoundRectShape (
819
+ createCornerRadii (10 ), null , null ));
820
+ scrollViewBackground .getPaint ().setColor (getEditTextBackground ());
821
+ contentScrollView .setPadding (dip8 , dip8 , dip8 , dip8 );
822
+ contentScrollView .setBackground (scrollViewBackground );
823
+ contentScrollView .setClipToOutline (true );
819
824
}
820
- LinearLayout .LayoutParams messageParams = new LinearLayout .LayoutParams (
825
+ LinearLayout .LayoutParams contentParams = new LinearLayout .LayoutParams (
821
826
ViewGroup .LayoutParams .MATCH_PARENT ,
822
- ViewGroup .LayoutParams .WRAP_CONTENT
827
+ 0 ,
828
+ 1.0f // Weight to take available space.
823
829
);
824
- messageParams .setMargins (0 , dip8 , 0 , dip16 );
825
- messageView .setLayoutParams (messageParams );
826
- mainLayout .addView (messageView );
827
- }
830
+ contentScrollView .setLayoutParams (contentParams );
831
+ contentContainer = new LinearLayout (context );
832
+ contentContainer .setOrientation (LinearLayout .VERTICAL );
833
+ contentScrollView .addView (contentContainer );
834
+
835
+ // Message (if not replaced by EditText).
836
+ if (editText == null && message != null ) {
837
+ TextView messageView = new TextView (context );
838
+ messageView .setText (message ); // Supports Spanned (HTML).
839
+ messageView .setTextSize (16 );
840
+ messageView .setTextColor (getAppForegroundColor ());
841
+ // Enable HTML link clicking if the message contains links.
842
+ if (message instanceof Spanned ) {
843
+ messageView .setMovementMethod (LinkMovementMethod .getInstance ());
844
+ }
845
+ LinearLayout .LayoutParams messageParams = new LinearLayout .LayoutParams (
846
+ ViewGroup .LayoutParams .MATCH_PARENT ,
847
+ ViewGroup .LayoutParams .WRAP_CONTENT
848
+ );
849
+ messageView .setLayoutParams (messageParams );
850
+ contentContainer .addView (messageView );
851
+ }
828
852
829
- // EditText (if provided).
830
- if (editText != null ) {
831
- // Remove EditText from its current parent, if any.
832
- ViewGroup parent = (ViewGroup ) editText .getParent ();
833
- if (parent != null ) {
834
- parent .removeView (editText );
853
+ // EditText (if provided).
854
+ if (editText != null ) {
855
+ // Remove EditText from its current parent, if any.
856
+ ViewGroup parent = (ViewGroup ) editText .getParent ();
857
+ if (parent != null ) {
858
+ parent .removeView (editText );
859
+ }
860
+ // Style the EditText to match the dialog theme.
861
+ editText .setTextColor (getAppForegroundColor ());
862
+ editText .setBackgroundColor (Color .TRANSPARENT );
863
+ editText .setPadding (0 , 0 , 0 , 0 );
864
+ LinearLayout .LayoutParams editTextParams = new LinearLayout .LayoutParams (
865
+ LinearLayout .LayoutParams .MATCH_PARENT ,
866
+ LinearLayout .LayoutParams .WRAP_CONTENT
867
+ );
868
+ contentContainer .addView (editText , editTextParams );
835
869
}
836
- // Style the EditText to match the dialog theme.
837
- editText .setTextColor (getAppForegroundColor ());
838
- editText .setBackgroundColor (isDarkModeEnabled () ? Color .BLACK : Color .WHITE );
839
- editText .setPadding (dip8 , dip8 , dip8 , dip8 );
840
- ShapeDrawable editTextBackground = new ShapeDrawable (new RoundRectShape (
841
- createCornerRadii (10 ), null , null ));
842
- editTextBackground .getPaint ().setColor (getEditTextBackground ()); // Background color for EditText.
843
- editText .setBackground (editTextBackground );
844
-
845
- LinearLayout .LayoutParams editTextParams = new LinearLayout .LayoutParams (
846
- LinearLayout .LayoutParams .MATCH_PARENT ,
847
- LinearLayout .LayoutParams .WRAP_CONTENT
848
- );
849
- editTextParams .setMargins (0 , dip8 , 0 , dip16 );
850
- // Prevent buttons from moving off the screen by fixing the height of the EditText.
851
- final int maxHeight = (int ) (context .getResources ().getDisplayMetrics ().heightPixels * 0.6 );
852
- editText .setMaxHeight (maxHeight );
853
- mainLayout .addView (editText , 1 , editTextParams );
854
870
}
855
871
856
872
// Button container.
@@ -861,7 +877,7 @@ public static Pair<Dialog, LinearLayout> createCustomDialog(
861
877
LinearLayout .LayoutParams .MATCH_PARENT ,
862
878
LinearLayout .LayoutParams .WRAP_CONTENT
863
879
);
864
- buttonContainerParams .setMargins (0 , dip8 , 0 , 0 );
880
+ buttonContainerParams .setMargins (0 , dip16 , 0 , 0 );
865
881
buttonContainer .setLayoutParams (buttonContainerParams );
866
882
867
883
// Lists to track buttons.
@@ -1036,25 +1052,29 @@ public static Pair<Dialog, LinearLayout> createCustomDialog(
1036
1052
}
1037
1053
}
1038
1054
1055
+ // Add ScrollView to main layout only if content exist.
1056
+ if (contentScrollView != null ) {
1057
+ mainLayout .addView (contentScrollView );
1058
+ }
1039
1059
mainLayout .addView (buttonContainer );
1040
1060
dialog .setContentView (mainLayout );
1041
1061
1042
1062
// Set dialog window attributes.
1043
1063
Window window = dialog .getWindow ();
1044
1064
if (window != null ) {
1045
- setDialogWindowParameters (context , window );
1065
+ setDialogWindowParameters (window );
1046
1066
}
1047
1067
1048
1068
return new Pair <>(dialog , mainLayout );
1049
1069
}
1050
1070
1051
- public static void setDialogWindowParameters (Context context , Window window ) {
1071
+ public static void setDialogWindowParameters (Window window ) {
1052
1072
WindowManager .LayoutParams params = window .getAttributes ();
1053
1073
1054
- Resources resources = context .getResources ();
1055
- DisplayMetrics displayMetrics = resources .getDisplayMetrics ();
1074
+ DisplayMetrics displayMetrics = Resources .getSystem ().getDisplayMetrics ();
1056
1075
int portraitWidth = (int ) (displayMetrics .widthPixels * 0.9 );
1057
- if (resources .getConfiguration ().orientation == Configuration .ORIENTATION_LANDSCAPE ) {
1076
+
1077
+ if (Resources .getSystem ().getConfiguration ().orientation == Configuration .ORIENTATION_LANDSCAPE ) {
1058
1078
portraitWidth = (int ) Math .min (portraitWidth , displayMetrics .heightPixels * 0.9 );
1059
1079
}
1060
1080
params .width = portraitWidth ;
@@ -1199,7 +1219,7 @@ public static int getDialogBackgroundColor() {
1199
1219
return darkColor == Color .BLACK
1200
1220
// Lighten the background a little if using AMOLED dark theme
1201
1221
// as the dialogs are almost invisible.
1202
- ? 0xFF0D0D0D
1222
+ ? 0xFF080808 // 3%
1203
1223
: darkColor ;
1204
1224
}
1205
1225
return getThemeLightColor ();
0 commit comments