Skip to content

Custom Theme

Create and apply a custom theme in Fyne by overriding colors, fonts, and icons to match your app’s branding.

Show Code
package main
import (
"image/color"
"fyne.io/fyne/v2"
"fyne.io/fyne/v2/app"
"fyne.io/fyne/v2/container"
"fyne.io/fyne/v2/theme"
"fyne.io/fyne/v2/widget"
)
// MyTheme is a custom theme implementing fyne.Theme
type MyTheme struct{}
// -------- COLORS ----------
func (m MyTheme) Color(name fyne.ThemeColorName, variant fyne.ThemeVariant) color.Color {
switch name {
case theme.ColorNameBackground:
// Set custom background (light gray)
return color.NRGBA{R: 240, G: 240, B: 240, A: 255}
case theme.ColorNameButton:
// Custom button color (blue)
return color.NRGBA{R: 52, G: 152, B: 219, A: 255}
case theme.ColorNameDisabledButton:
return color.NRGBA{R: 180, G: 180, B: 180, A: 255}
case theme.ColorNameFocus:
// Dark text
return color.NRGBA{R: 33, G: 33, B: 33, A: 255}
case theme.ColorNameDisabled:
return color.NRGBA{R: 120, G: 120, B: 120, A: 255}
}
// Fallback → use default theme colors
return theme.DefaultTheme().Color(name, variant)
}
// -------- ICONS ----------
func (m MyTheme) Icon(name fyne.ThemeIconName) fyne.Resource {
switch name {
case theme.IconNameConfirm:
return theme.CancelIcon()
}
return theme.DefaultTheme().Icon(name)
}
// -------- FONTS ----------
func (m MyTheme) Font(style fyne.TextStyle) fyne.Resource {
return theme.DefaultTheme().Font(style)
}
// -------- SIZES ----------
func (m MyTheme) Size(name fyne.ThemeSizeName) float32 {
switch name {
case theme.SizeNameText:
return 18 // larger text
case theme.SizeNamePadding:
return 12 // more padding
}
return theme.DefaultTheme().Size(name)
}
func main() {
a := app.New()
w := a.NewWindow("Custom Theme Demo")
// Apply our theme
a.Settings().SetTheme(&MyTheme{})
btn := widget.NewButton("Custom Themed Button", func() {})
label := widget.NewLabel("Hello from custom theme!")
entry := widget.NewEntry()
entry.SetPlaceHolder("Type here...")
w.SetContent(
container.NewVBox(
label,
btn,
entry,
widget.NewButtonWithIcon("Confirm", theme.ConfirmIcon(), func() {}),
),
)
w.Resize(fyne.NewSize(400, 250))
w.ShowAndRun()
}