fix: Add scrollable content to modern style settings dialogs (#5211)

This commit is contained in:
MarcaD
2025-06-22 14:21:00 +03:00
committed by GitHub
parent a9e9456b6b
commit 2b62fc2224
8 changed files with 152 additions and 138 deletions

View File

@@ -42,6 +42,7 @@ import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.ScrollView;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.Toolbar;
@@ -773,16 +774,15 @@ public class Utils {
Dialog dialog = new Dialog(context);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE); // Remove default title bar.
// Create main layout.
LinearLayout mainLayout = new LinearLayout(context);
mainLayout.setOrientation(LinearLayout.VERTICAL);
// Preset size constants.
final int dip4 = dipToPixels(4);
final int dip8 = dipToPixels(8);
final int dip16 = dipToPixels(16);
final int dip24 = dipToPixels(24);
// Create main layout.
LinearLayout mainLayout = new LinearLayout(context);
mainLayout.setOrientation(LinearLayout.VERTICAL);
mainLayout.setPadding(dip24, dip16, dip24, dip24);
// Set rounded rectangle background.
ShapeDrawable mainBackground = new ShapeDrawable(new RoundRectShape(
@@ -802,11 +802,36 @@ public class Utils {
ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.WRAP_CONTENT
);
layoutParams.setMargins(0, 0, 0, dip8);
layoutParams.setMargins(0, 0, 0, dip16);
titleView.setLayoutParams(layoutParams);
mainLayout.addView(titleView);
}
// Create content container (message/EditText) inside a ScrollView only if message or editText is provided.
ScrollView contentScrollView = null;
LinearLayout contentContainer = null;
if (message != null || editText != null) {
contentScrollView = new ScrollView(context);
contentScrollView.setVerticalScrollBarEnabled(false); // Disable the vertical scrollbar.
contentScrollView.setOverScrollMode(View.OVER_SCROLL_NEVER);
if (editText != null) {
ShapeDrawable scrollViewBackground = new ShapeDrawable(new RoundRectShape(
createCornerRadii(10), null, null));
scrollViewBackground.getPaint().setColor(getEditTextBackground());
contentScrollView.setPadding(dip8, dip8, dip8, dip8);
contentScrollView.setBackground(scrollViewBackground);
contentScrollView.setClipToOutline(true);
}
LinearLayout.LayoutParams contentParams = new LinearLayout.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT,
0,
1.0f // Weight to take available space.
);
contentScrollView.setLayoutParams(contentParams);
contentContainer = new LinearLayout(context);
contentContainer.setOrientation(LinearLayout.VERTICAL);
contentScrollView.addView(contentContainer);
// Message (if not replaced by EditText).
if (editText == null && message != null) {
TextView messageView = new TextView(context);
@@ -821,9 +846,8 @@ public class Utils {
ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.WRAP_CONTENT
);
messageParams.setMargins(0, dip8, 0, dip16);
messageView.setLayoutParams(messageParams);
mainLayout.addView(messageView);
contentContainer.addView(messageView);
}
// EditText (if provided).
@@ -835,22 +859,14 @@ public class Utils {
}
// Style the EditText to match the dialog theme.
editText.setTextColor(getAppForegroundColor());
editText.setBackgroundColor(isDarkModeEnabled() ? Color.BLACK : Color.WHITE);
editText.setPadding(dip8, dip8, dip8, dip8);
ShapeDrawable editTextBackground = new ShapeDrawable(new RoundRectShape(
createCornerRadii(10), null, null));
editTextBackground.getPaint().setColor(getEditTextBackground()); // Background color for EditText.
editText.setBackground(editTextBackground);
editText.setBackgroundColor(Color.TRANSPARENT);
editText.setPadding(0, 0, 0, 0);
LinearLayout.LayoutParams editTextParams = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT
);
editTextParams.setMargins(0, dip8, 0, dip16);
// Prevent buttons from moving off the screen by fixing the height of the EditText.
final int maxHeight = (int) (context.getResources().getDisplayMetrics().heightPixels * 0.6);
editText.setMaxHeight(maxHeight);
mainLayout.addView(editText, 1, editTextParams);
contentContainer.addView(editText, editTextParams);
}
}
// Button container.
@@ -861,7 +877,7 @@ public class Utils {
LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT
);
buttonContainerParams.setMargins(0, dip8, 0, 0);
buttonContainerParams.setMargins(0, dip16, 0, 0);
buttonContainer.setLayoutParams(buttonContainerParams);
// Lists to track buttons.
@@ -1036,25 +1052,29 @@ public class Utils {
}
}
// Add ScrollView to main layout only if content exist.
if (contentScrollView != null) {
mainLayout.addView(contentScrollView);
}
mainLayout.addView(buttonContainer);
dialog.setContentView(mainLayout);
// Set dialog window attributes.
Window window = dialog.getWindow();
if (window != null) {
setDialogWindowParameters(context, window);
setDialogWindowParameters(window);
}
return new Pair<>(dialog, mainLayout);
}
public static void setDialogWindowParameters(Context context, Window window) {
public static void setDialogWindowParameters(Window window) {
WindowManager.LayoutParams params = window.getAttributes();
Resources resources = context.getResources();
DisplayMetrics displayMetrics = resources.getDisplayMetrics();
DisplayMetrics displayMetrics = Resources.getSystem().getDisplayMetrics();
int portraitWidth = (int) (displayMetrics.widthPixels * 0.9);
if (resources.getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) {
if (Resources.getSystem().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) {
portraitWidth = (int) Math.min(portraitWidth, displayMetrics.heightPixels * 0.9);
}
params.width = portraitWidth;
@@ -1199,7 +1219,7 @@ public class Utils {
return darkColor == Color.BLACK
// Lighten the background a little if using AMOLED dark theme
// as the dialogs are almost invisible.
? 0xFF0D0D0D
? 0xFF080808 // 3%
: darkColor;
}
return getThemeLightColor();

View File

@@ -129,8 +129,7 @@ abstract class Check {
ImageView iconView = new ImageView(activity);
iconView.setImageResource(Utils.getResourceIdentifier("revanced_ic_dialog_alert", "drawable"));
iconView.setColorFilter(Utils.getAppForegroundColor(), PorterDuff.Mode.SRC_IN);
final int dip8 = dipToPixels(8);
iconView.setPadding(0, dip8, 0, dip8);
iconView.setPadding(0, 0, 0, 0);
LinearLayout.LayoutParams iconParams = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT

View File

@@ -24,10 +24,7 @@ import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewParent;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.*;
import androidx.annotation.ColorInt;
@@ -298,7 +295,6 @@ public class ColorPickerPreference extends EditTextPreference {
// Horizontal layout for preview and EditText.
LinearLayout inputLayout = new LinearLayout(context);
inputLayout.setOrientation(LinearLayout.HORIZONTAL);
inputLayout.setPadding(0, 0, 0, dipToPixels(10));
dialogColorPreview = new TextView(context);
LinearLayout.LayoutParams previewParams = new LinearLayout.LayoutParams(
@@ -338,11 +334,23 @@ public class ColorPickerPreference extends EditTextPreference {
paddingView.setLayoutParams(params);
inputLayout.addView(paddingView);
// Create main container for color picker and input layout.
LinearLayout container = new LinearLayout(context);
container.setOrientation(LinearLayout.VERTICAL);
container.addView(colorPicker);
container.addView(inputLayout);
// Create content container for color picker and input layout.
LinearLayout contentContainer = new LinearLayout(context);
contentContainer.setOrientation(LinearLayout.VERTICAL);
contentContainer.addView(colorPicker);
contentContainer.addView(inputLayout);
// Create ScrollView to wrap the content container.
ScrollView contentScrollView = new ScrollView(context);
contentScrollView.setVerticalScrollBarEnabled(false); // Disable vertical scrollbar.
contentScrollView.setOverScrollMode(View.OVER_SCROLL_NEVER); // Disable overscroll effect.
LinearLayout.LayoutParams scrollViewParams = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT,
0,
1.0f
);
contentScrollView.setLayoutParams(scrollViewParams);
contentScrollView.addView(contentContainer);
// Create custom dialog.
final int originalColor = currentColor & 0x00FFFFFF;
@@ -391,9 +399,9 @@ public class ColorPickerPreference extends EditTextPreference {
false // Do not dismiss dialog when onNeutralClick.
);
// Add the custom container to the dialog's main layout.
// Add the ScrollView to the dialog's main layout.
LinearLayout dialogMainLayout = dialogPair.second;
dialogMainLayout.addView(container, 1);
dialogMainLayout.addView(contentScrollView, dialogMainLayout.getChildCount() - 1);
// Set up color picker listener with debouncing.
// Add listener last to prevent callbacks from set calls above.

View File

@@ -11,11 +11,7 @@ import android.util.Pair;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.*;
import androidx.annotation.NonNull;
@@ -107,14 +103,16 @@ public class CustomDialogListPreference extends ListPreference {
@Override
protected void showDialog(Bundle state) {
Context context = getContext();
// Create ListView.
ListView listView = new ListView(getContext());
ListView listView = new ListView(context);
listView.setId(android.R.id.list);
listView.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
// Create custom adapter for the ListView.
ListPreferenceArrayAdapter adapter = new ListPreferenceArrayAdapter(
getContext(),
context,
Utils.getResourceIdentifier("revanced_custom_list_item_checked", "layout"),
getEntries(),
getEntryValues(),
@@ -137,7 +135,7 @@ public class CustomDialogListPreference extends ListPreference {
// Create the custom dialog without OK button.
Pair<Dialog, LinearLayout> dialogPair = Utils.createCustomDialog(
getContext(),
context,
getTitle() != null ? getTitle().toString() : "",
null,
null,
@@ -149,35 +147,13 @@ public class CustomDialogListPreference extends ListPreference {
true
);
Dialog dialog = dialogPair.first;
// Add the ListView to the main layout.
LinearLayout mainLayout = dialogPair.second;
// Measure content height before adding ListView to layout.
// Otherwise, the ListView will push the buttons off the screen.
int totalHeight = 0;
int widthSpec = View.MeasureSpec.makeMeasureSpec(
getContext().getResources().getDisplayMetrics().widthPixels,
View.MeasureSpec.AT_MOST
);
int heightSpec = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);
for (int i = 0; i < adapter.getCount(); i++) {
View listItem = adapter.getView(i, null, listView);
listItem.measure(widthSpec, heightSpec);
totalHeight += listItem.getMeasuredHeight();
}
// Cap the height at maxHeight.
int maxHeight = (int) (getContext().getResources().getDisplayMetrics().heightPixels * 0.6);
int finalHeight = Math.min(totalHeight, maxHeight);
// Add ListView to the main layout with calculated height.
LinearLayout.LayoutParams listViewParams = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT,
finalHeight // Use calculated height directly.
0,
1.0f
);
final int marginHorizontal = dipToPixels(8);
listViewParams.setMargins(0, marginHorizontal, 0, marginHorizontal);
mainLayout.addView(listView, mainLayout.getChildCount() - 1, listViewParams);
// Handle item click to select value and dismiss dialog.
@@ -188,10 +164,10 @@ public class CustomDialogListPreference extends ListPreference {
adapter.setSelectedValue(selectedValue);
adapter.notifyDataSetChanged();
}
dialog.dismiss();
dialogPair.first.dismiss();
});
// Show the dialog.
dialog.show();
dialogPair.first.show();
}
}

View File

@@ -17,6 +17,7 @@ import android.os.Handler;
import android.os.Looper;
import android.preference.Preference;
import android.util.AttributeSet;
import android.view.View;
import android.view.Window;
import android.webkit.WebView;
import android.webkit.WebViewClient;
@@ -216,6 +217,8 @@ class WebViewDialog extends Dialog {
// Create WebView.
WebView webView = new WebView(getContext());
webView.setVerticalScrollBarEnabled(false); // Disable the vertical scrollbar.
webView.setOverScrollMode(View.OVER_SCROLL_NEVER);
webView.getSettings().setJavaScriptEnabled(true);
webView.setWebViewClient(new OpenLinksExternallyWebClient());
webView.loadDataWithBaseURL(null, htmlContent, "text/html", "utf-8", null);
@@ -228,7 +231,7 @@ class WebViewDialog extends Dialog {
// Set dialog window attributes
Window window = getWindow();
if (window != null) {
Utils.setDialogWindowParameters(getContext(), window);
Utils.setDialogWindowParameters(window);
}
}

View File

@@ -19,6 +19,7 @@ import android.widget.SearchView;
import android.widget.TextView;
import android.widget.Toolbar;
import androidx.annotation.ColorInt;
import androidx.annotation.NonNull;
import java.util.ArrayList;
@@ -58,11 +59,7 @@ public class SearchViewController {
GradientDrawable background = new GradientDrawable();
background.setShape(GradientDrawable.RECTANGLE);
background.setCornerRadius(28 * context.getResources().getDisplayMetrics().density); // 28dp corner radius.
int baseColor = Utils.getAppBackgroundColor();
int adjustedColor = Utils.isDarkModeEnabled()
? Utils.adjustColorBrightness(baseColor, 1.11f) // Lighten for dark theme.
: Utils.adjustColorBrightness(baseColor, 0.95f); // Darken for light theme.
background.setColor(adjustedColor);
background.setColor(getSearchViewBackground());
return background;
}
@@ -72,10 +69,17 @@ public class SearchViewController {
private static GradientDrawable createSuggestionBackgroundDrawable(Context context) {
GradientDrawable background = new GradientDrawable();
background.setShape(GradientDrawable.RECTANGLE);
background.setCornerRadius(8 * context.getResources().getDisplayMetrics().density); // 8dp corner radius.
background.setColor(getSearchViewBackground());
return background;
}
@ColorInt
public static int getSearchViewBackground() {
return Utils.isDarkModeEnabled()
? Utils.adjustColorBrightness(Utils.getDialogBackgroundColor(), 1.11f)
: Utils.adjustColorBrightness(Utils.getThemeLightColor(), 0.95f);
}
/**
* Adds search view components to the activity.
*/

View File

@@ -18,12 +18,7 @@ import android.text.TextWatcher;
import android.util.Pair;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.GridLayout;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.*;
import androidx.annotation.ColorInt;
@@ -88,8 +83,6 @@ public class SegmentCategoryListPreference extends ListPreference {
// Create the main layout for the dialog content.
LinearLayout contentLayout = new LinearLayout(context);
contentLayout.setOrientation(LinearLayout.VERTICAL);
final int dip10 = dipToPixels(10);
contentLayout.setPadding(0, 0, 0, dip10);
// Add behavior selection radio buttons.
RadioGroup radioGroup = new RadioGroup(context);
@@ -103,7 +96,7 @@ public class SegmentCategoryListPreference extends ListPreference {
radioGroup.addView(radioButton);
}
radioGroup.setOnCheckedChangeListener((group, checkedId) -> selectedDialogEntryIndex = checkedId);
radioGroup.setPadding(dip10, 0, 0, 0);
radioGroup.setPadding(dipToPixels(10), 0, 0, 0);
contentLayout.addView(radioGroup);
// Inflate the color picker view.
@@ -131,7 +124,7 @@ public class SegmentCategoryListPreference extends ListPreference {
gridParams = new GridLayout.LayoutParams();
gridParams.rowSpec = GridLayout.spec(0); // First row.
gridParams.columnSpec = GridLayout.spec(1); // Second column.
gridParams.setMargins(0, 0, dip10, 0);
gridParams.setMargins(0, 0, dipToPixels(10), 0);
dialogColorDotView = new TextView(context);
dialogColorDotView.setLayoutParams(gridParams);
gridLayout.addView(dialogColorDotView);
@@ -250,20 +243,17 @@ public class SegmentCategoryListPreference extends ListPreference {
contentLayout.addView(gridLayout);
// Set up color picker listener.
// Do last to prevent listener callbacks while setting up view.
dialogColorPickerView.setOnColorChangedListener(color -> {
if (categoryColor == color) {
return;
}
categoryColor = color;
String hexColor = getColorString(color);
Logger.printDebug(() -> "onColorChanged: " + hexColor);
updateCategoryColorDot();
dialogColorEditText.setText(hexColor);
dialogColorEditText.setSelection(hexColor.length());
});
// Create ScrollView to wrap the content layout.
ScrollView contentScrollView = new ScrollView(context);
contentScrollView.setVerticalScrollBarEnabled(false); // Disable vertical scrollbar.
contentScrollView.setOverScrollMode(View.OVER_SCROLL_NEVER); // Disable overscroll effect.
LinearLayout.LayoutParams scrollViewParams = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT,
0,
1.0f
);
contentScrollView.setLayoutParams(scrollViewParams);
contentScrollView.addView(contentLayout);
// Create the custom dialog.
Pair<Dialog, LinearLayout> dialogPair = Utils.createCustomDialog(
@@ -309,13 +299,27 @@ public class SegmentCategoryListPreference extends ListPreference {
false // Do not dismiss dialog on Neutral button click.
);
dialog = dialogPair.first;
// Add the ScrollView to the dialog's main layout.
LinearLayout dialogMainLayout = dialogPair.second;
dialogMainLayout.addView(contentScrollView, dialogMainLayout.getChildCount() - 1);
// Add the custom content to the dialog's main layout.
dialogMainLayout.addView(contentLayout, 1); // Add after title, before buttons.
// Set up color picker listener.
// Do last to prevent listener callbacks while setting up view.
dialogColorPickerView.setOnColorChangedListener(color -> {
if (categoryColor == color) {
return;
}
categoryColor = color;
String hexColor = getColorString(color);
Logger.printDebug(() -> "onColorChanged: " + hexColor);
updateCategoryColorDot();
dialogColorEditText.setText(hexColor);
dialogColorEditText.setSelection(hexColor.length());
});
// Show the dialog.
dialog = dialogPair.first;
dialog.show();
} catch (Exception ex) {
Logger.printException(() -> "showDialog failure", ex);

View File

@@ -1,10 +1,10 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="48dp"
android:height="48dp"
android:viewportWidth="48"
android:viewportHeight="48">
android:width="40dp"
android:height="40dp"
android:viewportWidth="40"
android:viewportHeight="40">
<path
android:fillColor="#000000"
android:fillType="evenOdd"
android:pathData="M26.5977,6.29688 L43.8672,36.2031 C45.0195,38.2031,43.5781,40.7031,41.2695,40.7031 L6.73047,40.7031 C4.42188,40.7031,2.98047,38.2031,4.13281,36.2031 L21.4023,6.29688 C22.5547,4.29688,25.4453,4.29688,26.5977,6.29688 Z M24,30 C22.8945,30,22,30.8945,22,32 C22,33.1055,22.8945,34,24,34 C25.1055,34,26,33.1055,26,32 C26,30.8945,25.1055,30,24,30 Z M24,16 C22.9727,16,22.1289,16.7734,22.0117,17.7656 L22,18 L22,26 C22,27.1055,22.8945,28,24,28 C25.0273,28,25.8711,27.2266,25.9883,26.2344 L26,26 L26,18 C26,16.8945,25.1055,16,24,16 Z M24,16" />
android:pathData="M22.1641,5.24609 L36.5547,30.168 C37.5156,31.8359,36.3164,33.918,34.3906,33.918 L5.60938,33.918 C3.68359,33.918,2.48438,31.8359,3.44531,30.168 L17.8359,5.24609 C18.7969,3.58203,21.2031,3.58203,22.1641,5.24609 Z M20,25 C19.0781,25,18.332,25.7461,18.332,26.668 C18.332,27.5898,19.0781,28.332,20,28.332 C20.9219,28.332,21.668,27.5898,21.668,26.668 C21.668,25.7461,20.9219,25,20,25 Z M20,13.332 C19.1445,13.332,18.4414,13.9766,18.3438,14.8047 L18.332,15 L18.332,21.668 C18.332,22.5898,19.0781,23.332,20,23.332 C20.8555,23.332,21.5586,22.6875,21.6563,21.8633 L21.668,21.668 L21.668,15 C21.668,14.0781,20.9219,13.332,20,13.332 Z M20,13.332" />
</vector>