3

I'm creating android app to play liveStream, I added the videoView in my layout.xml and added the folder of layout-land

I want to make the app shows video full screen in only layout-land not portrait

so I added the following code in onCreate():

public void onConfigurationChanged(Configuration newConfig){
if(getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE
   {
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
   } else 
    if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT) {
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN,    WindowManager.LayoutParams.FLAG_FULLSCREEN);
    }
}

but my problem is when I rotate the device it restarts the activity and this lead the stream of video to stop and I want to prevent this. So. I added to manifest this line

 android:configChanges="keyboard|keyboardHidden|orientation|screenSize"

but this leads the code of onCreate() to be executed once only.

How to make my app plays the video stream continuously and when I rotate device to make full screen in layout land then when back to portrait make it normal?

Hope that anyone got my mean. Thanks in advance.

1
  • how u resolved your issue ? below api level19
    – Erum
    Sep 16, 2015 at 12:05

2 Answers 2

12

You should add your videoview (or content Parent) as last element in your layout file. and use the next code:

private RelativeLayout.LayoutParams paramsNotFullscreen; //if you're using RelativeLatout           

@Override
public void onConfigurationChanged(Configuration newConfig) 
{
    super.onConfigurationChanged(newConfig);


    if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) //To fullscreen
    {
        paramsNotFullscreen=(RelativeLayout.LayoutParams)mVideoView.getLayoutParams();
        RelativeLayout.LayoutParams params=new LayoutParams(paramsNotFullscreen);
        params.setMargins(0, 0, 0, 0);
        params.height=ViewGroup.LayoutParams.MATCH_PARENT;
        params.width=ViewGroup.LayoutParams.MATCH_PARENT;
        params.addRule(RelativeLayout.CENTER_IN_PARENT);
        mVideoView.setLayoutParams(params);

    } 
    else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT) 
    {
        mVideoView.setLayoutParams(paramsNotFullscreen);
    }
}

It gets a copy of videoview layoutparams and saves it in a global variable. then creates a new layoutparams object with the previous values but setting now the limits to match_parent and sets it in your videoview. You videoview now is in fullscreen. When you put your device in portrait, paramsNotFullscreen restores previous values.

UPDATE:

In your manifest file you must add into Activity declaration the following code to avoid the activity restart:

android:configChanges="screenLayout|screenSize|orientation"
5
  • 1
    Call requires API level 19
    – Omid Omidi
    Jan 26, 2015 at 21:17
  • @OmidOmidi I tested this code in 2.3+. Anyway I published this answer in 2012 when KitKat was not released yet
    – ClarkXP
    Jan 27, 2015 at 21:15
  • @ClarkXP uing this code i will not be in need of layout-land file ? how will i do this my xm layout for video is here attached pastie.org/10423424
    – Erum
    Sep 16, 2015 at 11:56
  • 1
    You should mention that it's necessary to add android:configChanges="orientation|screenSize" to the activity in your Manifest to avoid activity to be destroyed and recreated on rotate
    – Helder AC
    Dec 9, 2015 at 10:03
  • added ;) @gekod, actually you must add also screenLayout option
    – ClarkXP
    Dec 9, 2015 at 14:19
1

try this...

@Override
    public void onConfigurationChanged(Configuration newConfig) 
    {
        super.onConfigurationChanged(newConfig);


        if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) 
        {
            setContentView(R.layout.create_job_l);

        } 
        else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT)
        {
            setContentView(R.layout.create_job_p);

        }
    }
    @Override
    public void onCreate(Bundle savedInstanceState)
    {

        super.onCreate(savedInstanceState);
        if (getResources().getConfiguration().orientation == 1)
        {
            setContentView(R.layout.create_job_p);
        }
        if (getResources().getConfiguration().orientation == 2)
        {
            setContentView(R.layout.create_job_l);
        }


    }
3
  • this make the app permanently landScape and this is not my target, I want both landScape and portrait but do full Screen in landScape only. Oct 22, 2012 at 13:04
  • after you edited the code, is this will affect the stream of the video? as I prevented the app from restarting the activity when rotate Oct 22, 2012 at 13:24
  • you have to define static videoView1 in another class,when change orientation of then you have to set current videoView object in to static videoView1......when restarting the activity at time you have to assign static videoView1 in to current videoView.. Oct 22, 2012 at 13:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.