Reputation: 4069
I have a VideoView and am streaming mp4 files, which is working fine. However I'm trying to set the video to centered on screen when in portrait, and set it to full screen when in landscape. The code I am trying is below, but it is not working. If I switch to landscape, it doesn't change anything until I switch back to portrait, then it's full screen and is too large for the portrait view of the phone. How can I make my videoview adjust to full screen when in landscape, and switch back to standard centered in parent when in portrait? I have added the configChanges="orientation" to my activity in the manifest.
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
// Checks the orientation of the screen
if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
RelativeLayout.LayoutParams lp = (RelativeLayout.LayoutParams) videoView.getLayoutParams();
lp.addRule(RelativeLayout.CENTER_IN_PARENT, 1);
lp.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM, 1);
lp.addRule(RelativeLayout.ALIGN_PARENT_LEFT, 1);
lp.addRule(RelativeLayout.ALIGN_PARENT_RIGHT, 1);
videoView.setLayoutParams(lp);
} else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT){
RelativeLayout.LayoutParams lp = (RelativeLayout.LayoutParams) videoView.getLayoutParams();
lp.addRule(RelativeLayout.CENTER_IN_PARENT, 1);
lp.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM, 0);
lp.addRule(RelativeLayout.ALIGN_PARENT_LEFT, 0);
lp.addRule(RelativeLayout.ALIGN_PARENT_RIGHT, 0);
videoView.setLayoutParams(lp);
}
}
Also, I am under the impression that setting a 0,1 (false, true) after the layout param will either add the rule or remove it. Is this correct?
thank you in advance for any help!
Upvotes: 1
Views: 2944
Reputation: 93561
No, the second parameter is the id of the view that the rule is relative to. YOu need to use removeRule to remove it.
Truthfully I think its easier to just build a new LayoutParam object and replace the old one. Also, you may need to call requestLayout on the parent after changing it, I'm unsure whether it automatically does it or not.
Upvotes: 1