how to set application theme in code rather than in manifest

126
August 16, 2019, at 7:00 PM

I want my application to apply Leanback-theme on Android-TV, and AppCompat-theme on Android phones.

originally manifest.xml

<application
            android:allowBackup="true"
            android:icon="@mipmap/ic_launcher"
            android:label="@string/app_name"
            android:supportsRtl="true"
            android:theme="@style/tv"
            android:usesCleartextTraffic="true"
        >

Now manifest.xml

<application
            android:allowBackup="true"
            android:icon="@mipmap/ic_launcher"
            android:label="@string/app_name"
            android:supportsRtl="true"
            android:usesCleartextTraffic="true"
        >

I have tried several versions listed below, but none of them work. All of these gives a white background.

styles.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <style name="tv" parent="@style/Theme.Leanback"/>
    <style name="phone" parent="@style/Theme.AppCompat"/>
</resources>

MainActivity.kt

class MainActivity : Activity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        application.setTheme(R.style.tv)
        super.onCreate(savedInstanceState)
        showLayout()
    }
    private fun showLayout() {
        setContentView(R.layout.layout_tv)
    }
}

MainActivity.kt

class MainActivity : Activity() {
        override fun onCreate(savedInstanceState: Bundle?) {
            super.onCreate(savedInstanceState)
            showLayout()
        }
        private fun showLayout() {
            application.setTheme(R.style.tv)
            setContentView(R.layout.layout_tv)
    }
}

MainActivity.kt

class MainActivity : Activity() {
        override fun onCreate(savedInstanceState: Bundle?) {
            super.onCreate(savedInstanceState)
            showLayout()
        }
        private fun showLayout() {
            val theme = super.getTheme()
            theme.applyStyle(R.style.tv, true)
            setContentView(R.layout.layout_tv)
    }
}
Rent Charter Buses Company
READ ALSO
Duplicate class found in modules with R8 enabled

Duplicate class found in modules with R8 enabled

I created two aar-libraries: comexample:test1:1

164
How to decrypt signal protocol message sent from node in android - java

How to decrypt signal protocol message sent from node in android - java

I'm setting up a connection between a laptop and an android phone with socketIOI wanted the messages to be encrypted with signal protocol, and so far I've been able to start a session and send a message from the phone to the laptop and decrypt it

137
Deside the host before connecting with WiFi Direct

Deside the host before connecting with WiFi Direct

How can you decide which phone would be the Group Owner of the Direct Network before connecting to it in Android Studio?

129
Java reference to activity

Java reference to activity

i have non-Activity class QtAdMobBanner that store reference to main Activity (simplified code below), why reference that set once at initialization still valid after activity created/destroyed (ActivityLifecycleCallbacks still work)?

172