I need to change theme of the activity programmatically. Theme can be "classic" - regular Android app window or transparent window (similar to Theme.Dialog).
If I set the theme in manifest, it works as expected.
<activity android:theme="@style/Dialog" ...
If I set it programatically (the very same theme), background is black.
@Override
protected void onCreate(Bundle savedInstanceState) {
setTheme(R.style.Dialog);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//getWindow().setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));
}
// Now in manifest the theme isn't set
The issue is that background is black. Is there a solution how to do set Dialog theme programmatically ? (I tried to set background of layout, etc. explicitly in XML and code but it didn't help.)
Whole code
manifest
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.jc.myapplication">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/MainTheme">
<activity
android:name=".MainActivity"
>
<!--android:theme="@style/Dialog"-->
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
styles.xml
resources>
<style name="MainTheme" parent="Theme.AppCompat.Light.DarkActionBar"/>
<style name="Dialog" parent="Theme.AppCompat.Light.DarkActionBar">
<item name="android:windowFrame">@null</item>
<item name="android:windowBackground">@android:color/transparent</item>
<item name="android:windowIsFloating">true</item>
<item name="android:windowContentOverlay">@null</item>
<item name="android:windowTitleStyle">@null</item>
<item name="android:windowAnimationStyle">@android:style/Animation.Dialog</item>
<item name="android:windowSoftInputMode">stateUnspecified|adjustPan</item>
<item name="android:backgroundDimEnabled">false</item>
<item name="android:background">@android:color/transparent</item>
<item name="windowNoTitle">true</item>
<item name="android:colorBackgroundCacheHint">@null</item>
<item name="android:windowIsTranslucent">true</item>
</style>
</resources>
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/mainLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#00ff0000">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Hello World!"
android:textSize="100dp"
android:textColor="#ffff00"/>
</LinearLayout>
MainActivity.java
package com.example.jc.myapplication;
import android.graphics.drawable.ColorDrawable;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.LinearLayout;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
setTheme(R.style.Dialog);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//((LinearLayout)findViewById(R.id.mainLayout)).setBackgroundColor(android.graphics.Color.TRANSPARENT);
//getWindow().setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));
}
}