Plugin Settings
Learn how to create a settings screen for your plugin.
You can create a settings screen for your plugin by implementing the create_settings method. This method should return a list of setting control objects, which are Python dataclasses imported from the ui.settings module.
General Example
Here is a general example that demonstrates how to use all available setting controls.
Accessing and Modifying Settings
To access settings from your code, use the self.get_setting("KEY", DEFAULT_VALUE) method:
To save or update a setting's value programmatically, use the self.set_setting() method:
The set_setting method will persist the new value. If reload_settings is set to True, the settings UI will be completely rebuilt.
You can also export all settings for a plugin to a dictionary or import them from a dictionary. This can be useful for backup/restore functionality.
Supported Controls
Here is a summary of the available setting controls and their parameters.
| Control | key | text | default | Other Important Parameters |
|---|---|---|---|---|
| Header | - | Required | - | text: The title of the section. |
| Divider | - | - | - | text: (Optional) A note displayed on the divider line. |
| Switch | Required | Required | Required (bool) | subtext: str, icon: str, on_change(bool), on_long_click(View), link_alias: str |
| Selector | Required | Required | Required (int index) | items: List[str], icon: str, on_change(int), on_long_click(View), link_alias: str |
| Input | Required | Required | (Optional) str | subtext: str, icon: str, on_change(str), on_long_click(View), link_alias: str |
| Text | - | Required | - | icon: str, accent: bool, red: bool, on_click(View), create_sub_fragment() -> List, on_long_click(View), link_alias: str |
| EditText | Required | - | (Optional) str | hint: str, multiline: bool, max_length: int, mask: str (regex), on_change(str) |
Parameter Details
| Parameter | Type | Description |
|---|---|---|
key | str | Required for stateful controls. A unique string to identify the setting. This key is used with get_setting() and set_setting() to manage its value. |
text | str | Required for most controls. The main display text or label for the setting item. |
default | Any | The initial value of the setting if no value has been saved yet. The type depends on the control (bool for Switch, int for Selector, str for Input/EditText). |
subtext | str | Optional. Additional text displayed below the main text for more context or explanation. |
icon | str | Optional. The name of a drawable resource to use as an icon (e.g., "msg_settings"). You can find icon names in the Telegram app's source code. |
on_change | Callable | Optional. A function that is called immediately when the user changes the setting's value. The function receives the new value as an argument (e.g., Callable[[bool]] for Switch, Callable[[int]] for Selector). |
on_click | Callable | Optional. A function that is called when the user clicks on the item. It receives the Android View object as an argument. Primarily used with the Text control. |
on_long_click | Callable | Optional. A function that is called when the user long-presses the setting item. It receives the Android View object as an argument. |
link_alias | str | Optional. A unique alias for this setting. If provided, a "Copy Link" option will appear on long-press, allowing users to get a direct deeplink to this specific setting. |
items | List[str] | Required for Selector. A list of strings representing the options the user can choose from. |
create_sub_fragment | Callable | Optional. Used with Text. A function that returns a new list of setting items. Clicking the Text item will navigate to a new sub-page with these settings. |
accent | bool | Optional. Used with Text. If True, the text is styled with the theme's accent color. |
red | bool | Optional. Used with Text. If True, the text is styled in red, typically for warnings or destructive actions. |
hint | str | Required for EditText. Placeholder text displayed inside the text field when it's empty. |
multiline | bool | Optional. Used with EditText. If True, allows the text field to have multiple lines. |
max_length | int | Optional. Used with EditText. The maximum number of characters allowed in the input. |
mask | str | Optional. Used with EditText. A regex pattern to filter input characters (e.g., "[0-9]" would only allow digits). |