...
 
Commits (2)
package net.dankito.utils.android.extensions
import android.app.Activity
import android.util.TypedValue
import net.dankito.utils.android.R
import net.dankito.utils.android.ui.theme.ThemeConstants
/**
* Returns current theme's name if attribute [net.dankito.utils.android.R.attr.themeName] is set on theme / style resource, null otherwise.
*/
val Activity.themeName: String?
get() {
val outValue = TypedValue()
this.theme.resolveAttribute(R.attr.themeName, outValue, true)
return outValue.string?.toString()
}
/**
* Returns true if attribute [net.dankito.utils.android.R.attr.themeName] is set on theme / style resource and
* its value equals [net.dankito.utils.android.ui.theme.ThemeConstants.LightThemeName].
*/
val Activity.hasLightTheme: Boolean
get() {
return ThemeConstants.LightThemeName == themeName
}
/**
* Returns true if attribute [net.dankito.utils.android.R.attr.themeName] is set on theme / style resource and
* its value equals [net.dankito.utils.android.ui.theme.ThemeConstants.DarkThemeName].
*/
val Activity.hasDarkTheme: Boolean
get() {
return ThemeConstants.DarkThemeName == themeName
}
\ No newline at end of file
package net.dankito.utils.android.ui.activities
import android.os.Bundle
import android.support.v7.app.AppCompatActivity
import net.dankito.utils.android.ui.theme.Theme
abstract class ThemeableActivity : AppCompatActivity() {
protected abstract fun getSelectedTheme(): Theme
override fun onCreate(savedInstanceState: Bundle?) {
setTheme(getSelectedTheme().styleResourceId)
super.onCreate(savedInstanceState)
}
protected open fun themeChanged() {
recreate()
}
}
\ No newline at end of file
package net.dankito.utils.android.ui.theme
class Theme @JvmOverloads constructor(
/**
* The internal theme name. Used e.g. for storing in preferences.
*/
val name: String,
/**
* The resource id from R.style.
*/
val styleResourceId: Int
)
\ No newline at end of file
package net.dankito.utils.android.ui.theme
class ThemeConstants {
companion object {
/**
* Attribute value of [net.dankito.utils.android.R.attr.themeName] for [net.dankito.utils.android.R.style.AppThemeLightBase].
*/
const val LightThemeName = "AppThemeLightBase"
/**
* Attribute value of [net.dankito.utils.android.R.attr.themeName] for [net.dankito.utils.android.R.style.AppThemeDarkBase].
*/
const val DarkThemeName = "AppThemeDarkBase"
}
}
\ No newline at end of file