15

I have used this code as @gnobal posted on https://stackoverflow.com/a/2700683/1556329 and it works great. But my issue is that I have found that when I apply Theme.Transparent the activity does not goes to landscape mode when I rotate the mobile phone.

Theme:

 <style name="Theme.Transparent" parent="android:Theme.Dialog">
    <item name="android:windowIsTranslucent">true</item>
    <item name="android:windowBackground">@android:color/transparent</item>
    <item name="android:windowContentOverlay">@null</item>
    <item name="android:windowNoTitle">true</item>
    <item name="android:windowIsFloating">true</item>
    <item name="android:backgroundDimEnabled">false</item>
  </style>

Layout:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/layoutImagen"
    android:layout_width="match_parent"
    android:layout_height="match_parent"

    android:orientation="vertical"
    android:windowContentOverlay="@null"
    android:windowNoTitle="true"
    tools:context=".ImagenExamen_activity" >

<!--     android:background="#CC000000" -->
<!--     android:backgroundDimEnabled="true" -->

    <ImageView
        android:id="@+id/imageViewImagen"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center" />

</LinearLayout>

Activity:

import android.app.Activity;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;

import com.meapp.Utilities;

public class Imagen_activity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_imagen_examen);

        Bundle b = getIntent().getExtras();
        String imagen = b.getString("imagen_name");
        int id = getResources().getIdentifier(imagen, "drawable", getPackageName());

        ImageView imageView = (ImageView) findViewById(R.id.imageViewImagen);


        // Determinacion tamaño fuente
        BitmapFactory.Options bitmapOpt = new BitmapFactory.Options();
        bitmapOpt.inJustDecodeBounds = true;
        BitmapFactory.decodeResource(getResources(), id, bitmapOpt);

        int[] screen_dim = Utilites.verDimensionesPantalla(this);


                // más eficiente si se usa una potencia de 2
        imageView.setImageBitmap(Utilities
                .decodeSampledBitmapFromResource(getResources(), id,
                        screen_dim[0], screen_dim[1], true));



        imageView.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                cerrarImagen();
            }
        }); 


    }

    public void cerrarImagen() {
        ((Activity) this).finish();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.imagen_examen_, menu);
        return true;
    }

}

Why can't it be rotating? I have tried to change windowIsFloating to false, and change other parameters without luck.

Community
  • 1
  • 1
Chelu Martín
  • 578
  • 5
  • 17
  • Luis Martib Romera: Did you find the reason why orientation stop working with above defined theme? I am also facing same issue, but want to know the reason – Sagar Trehan Jan 19 '16 at 12:27

4 Answers4

27

you may check out this

             android:screenOrientation="sensor"
or 
             android:screenOrientation="user" 

add one of them to your manifest <activity............../>. and don't forget to add this to your application in manifest as well if you wish to avoid recreating your activity android:configChanges="keyboardHidden|orientation|screenSize"

Kosh
  • 6,140
  • 3
  • 36
  • 67
  • Thank you K0sh, that resolved the problem. I don't understand why in a normal activity you don't have to activate that attribute but using that new theme it does. Anyways thank you again! – Chelu Martín Jul 04 '13 at 18:45
  • This isn't working for me :( Not sure why, it's a tablet hardware I'm using with Jellybean and I was delighted when I found this answer, but for some reason the tablet still does not use the sensor's orientation when I tell it too (I also have `android:windowIsTranslucent` and `android:windowIsFloating` set to true) – Daniel Wilson Jun 06 '15 at 22:40
15

If you set android:windowIsTranslucent or android:windowIsFloating to true rotation notifications are disabled (as long as you don't force them to be enabled with android:screenOrientation="sensor").

Sufian
  • 6,405
  • 16
  • 66
  • 120
David Gräff
  • 151
  • 2
  • 3
0

I have theme with android:windowIsTransluent and rotation stops. To turn it ON i have experiment by adding screenOrientation="sensor" for application, theme and first Activity. Adding rotation only for first Activity turn rotation on for all activities in application.

Adrian Cid Almaguer
  • 7,815
  • 13
  • 41
  • 63
0

I had the same problem. Before doing any solutions, first be sure that rotation feature is enabled and then check your home screen that can be rotate, if no, Reboot your phone and the problem will be solved!

Ayub
  • 2,345
  • 27
  • 29