I have two activities:
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void click(View view) {
Intent intent= new Intent(this, TranslucentActivity.class);
startActivity(intent);
}
}
public class TranslucentActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
this.setTheme(android.R.style.Theme_Translucent);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_translucent);
}
}
Layout activity_main
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:android1="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:onClick="click" />
</RelativeLayout>
Layout activity_translucent
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".TranslucentActivity" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:text="Hello_world" />
</RelativeLayout>
Manifest
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.translucencytest"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.translucencytest.MainActivity" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="com.example.translucencytest.TranslucentActivity">
</activity>
</application>
</manifest>
When I click button in MainActivity it starts TranslucentActivity with transparent background. All looks fine in android emulator on platform 4.0.3 But on other platforms, e.g. 2.3.3 or 4.2, I get black background instead of transparent. What could be wrong ?
P.S. I do not want to use manifest to declare theme for activities.
In Android, we can create a transparent activity that will not be visible but your application will be running. The best part of this transparent activity is that you can create a transparent activity by just changing the resource file and we need not write the java or kotlin code for the same.
You could make an Activity float on the other Activities with a translucent window background, e.g. PicCollage app. That provides the end users with a visually-pleasing UX. PicCollage Android app.
There's a lot of discussion on setTheme() or rather why setTheme() isn't working as expected:
Quintessential to that discussion is that the setTheme doesn't work well while setting the theme in the manifest does (even Dianne Hackborn recommends to use the manifest way over the setTheme() way, see third link above). This is especially true when it comes to defining the background. Results you get in the emulator or in the graphical layout editor can unfortunately not be transferred to the real world (speak actual devices). So what you see in your 4.0.3 emulator might not be the same on a real device (as you already noticed ;-).
If there's no specific reason to use setTheme to theme your Activity then I would recommend to change your manifest like this:
<activity android:name=".TranslucentActivity"
android:theme="@android:style/Theme.Translucent"/>
You can still use the setTheme to theme other elements of your layout but I haven't found any other working solution when it comes to creating transparent or dialog like activities.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With