views:

538

answers:

3

My use case is writing an overlay controller activity for a landscape camera preview. I followed the instructions from a couple of tutorials for writing a transparent theme.

So my res/values/style.xml looks like this:

<resources>

  <style name="Theme" parent="android:Theme" />

  <style name="Theme.Transparent">
      <item name="android:windowBackground">@drawable/transparent_background</item>
  </style>

  <drawable name="transparent_background">#00000000</drawable>

</resources>

The activity snippet:

    <activity android:name=".CameraPreview"
       android:label="Camera"
       android:screenOrientation="landscape"
       android:theme="@android:style/Theme.NoTitleBar.Fullscreen">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

    <activity android:name=".Controlls"
              android:label="Controlls"
              android:screenOrientation="portrait"
              android:theme="@android:style/Theme.Translucent">
    </activity>

When I start this activity from my root activity, the layout gets drawn correctly, but the background stays black. I tried to use @android:style/Theme.Translucent instead, but this Theme inherits the orientation from the calling activity (landscape) and thats not what I want.

Edit:

The application holding the camera preview is set to landscape view as it does not display the preview correctly in portrait orientation. (see old google bug report)

What I wanted to do was to put an independent activity for user interaction interface in front of the camera surface holder (this activity should be set to 'portrait', or even better to 'sensor')

A: 

Try the built-in @android:style/Theme.Translucent instead of your custom one, according to this blog post. I haven't tried to do this myself, so I don't know if the technique written about there works or not.

CommonsWare
@android:style/Theme.Translucent is transparent, but fixes the view to the orientation of the underlying activity (my landscape view camera preview). This view should be portrait.
Uwe Krass
That has nothing to do with your background. If you do not want your activity to change orientation, that's handled in other ways, such as `android:screenOrientation="portrait"` in your manifest.
CommonsWare
Sorry, that I didn't mention it, but that's exactly what I did. (I will edit the post with my manifest). Thank for your advices so far.
Uwe Krass
A: 

I found out, that another important child element for the style description above is <item name="android:windowIsTranslucent">true</item> was lacking.

Problem:

That child element also causes synchronizing the activity's orientation with the calling one. (same effect as @android:style/Theme.Translucent)

Uwe Krass
A: 

Hi, I bumped into the same problem as you describe (regarding translucent background and screen orientation), but in the end I reconciled with the fact that this is just how it works. In fact it actually makes sense that it works this way. I can't think of any screen based system that supports mixing portrait and landscape views, so why should Android?

I guess the general rule is that all visible activities must have the same orientation, regardless of the attributes in the manifest-file. If all are set to "sensor" then they will all change, if one is fixed to portrait or landscape, then the others must follow (and whoever sets it last "wins").

I guess this was just so obvious to the developers that it didn't occur to them to document it :)

hermo