1

Basically, I have an app with two Activities.

#1 - MainActivity
This has a solid black background and a button.
When the button is pressed TransparentActivity should be presented.

#2 - TransparentActivity
I want this to be transparent (so the phones normal UI can be seen through).

I've tried using the following code:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setTheme(android.R.style.Theme_Translucent);
    setContentView(R.layout.activity_trick);
}

But it causes the app to crash with an NullPointerException.

Jakob Halskov
  • 428
  • 6
  • 22
  • Please have a look at http://stackoverflow.com/questions/2176922/how-to-create-transparent-activity-in-android I hope this helps Regards – Androidme Jan 16 '14 at 02:39
  • @Androidme I just tried that and it causes an crash too. – Jakob Halskov Jan 16 '14 at 02:41
  • If I apply the Theme to MainActivity it gets transparent, but if I set the TransparentActivity's theme to be transparent it crashes. – Jakob Halskov Jan 16 '14 at 02:51
  • From a user called Max - AppCompatActivity hasn't the Theme_Translucent(maybe the Theme_Translucent is null),you should create your own style – WhatsThePoint Nov 29 '17 at 08:36

2 Answers2

2

Try1:

Make super.onCreate(savedInstanceState); call after setTheme(android.R.style.Theme_Translucent);. So do as:

setTheme(android.R.style.Theme_Translucent);
super.onCreate(savedInstanceState);

Try 2:

If that doesn't work, I find the following way easiest to make my activity transparent:

<activity android:name=".your.activity.declaration.here" 
android:theme="@android:style/Theme.Translucent.NoTitleBar" />

Basically add android:theme="@android:style/Theme.Translucent.NoTitleBar" to your activity declaration in manifest. I can see that you are trying to do a similar thing programatically but by specifying it in manifest never crashed for me. If it does, then there might be other reasons.

Hope it helps.

Shobhit Puri
  • 25,769
  • 11
  • 95
  • 124
  • Thanks for your suggestions; Try 1 did the same and Try 2 simply made the app unresponsive when tapping the button. I have a lot of iOS experience but is very new to Android Development. – Jakob Halskov Jan 16 '14 at 03:11
  • For 1) Can you post the stacktrace/log please? For 2) By unresponsive you mean that nothing happen when you click the button or it hangs? – Shobhit Puri Jan 16 '14 at 03:16
  • Update: 2) almost works - the second activity is shown with a transparent background. But it is on top of the first. The goal is to see the 'home screen' of the Android Phone through the TransparentActivity (having MainActivity being 100% invisible so to speak) – Jakob Halskov Jan 16 '14 at 03:17
  • Is your second `TransparentActivity` are you using `android:width="match_parent` and `android:height="match_parent"` in the outermost layout of the layout xml file? If yes change them to `wrap_content`. This will only cover the necessary space. Rest of the `MainActivity` will be visible then. – Shobhit Puri Jan 16 '14 at 03:24
  • I want to do the opposite :) Making the TransparentActivity transparent and MainActivity should not be seen at all. So the user will see the content of TransparentActivity and under that the normal Android UI. – Jakob Halskov Jan 16 '14 at 03:26
  • You must be using intents to start the second activity. So before you start the `TransparentActivity` from the `MainActivity`, call `MainActivity.finish()`( after calling `startActivity(..)` function). Your MainActivity will end and a new activity will start which will be transparent. – Shobhit Puri Jan 16 '14 at 03:29
  • Great it works, apart from the fact I can't go back to the MainActivity since I've finished it? – Jakob Halskov Jan 16 '14 at 03:42
  • It depends when do you want to go back to the `MainActivity`. If you want to go to the main activity when you press the `back` button, then override the `onBackPressed` function from inside `Activity` and again start the `MainActivity` using intent. Otherwise you can again start it from any button press etc. Glad it helped. Feel free to upvote/accept the answer :) – Shobhit Puri Jan 16 '14 at 03:45
0

AppCompatActivity hasn't the Theme_Translucent(maybe the Theme_Translucent is null),you should create your own style.

user4261201
  • 2,324
  • 19
  • 26
Max
  • 1
  • This does not provide an answer to the question it is more of a comment, when you have hit 50 reputation you can [comment everywhere](https://stackoverflow.com/help/privileges/comment) for now i have added the comment for you – WhatsThePoint Nov 29 '17 at 08:35