Reputation: 183
I'm trying to play a short introductory video for my application. Since it is a game, first I created a theme to hide the action bar and go full-screen.
styles.xml:
<style name="AppTheme.NoTitleBar" parent="AppTheme">
<item name="android:windowNoTitle">true</item>
</style>
<style name="AppTheme.NoTitleBar.Fullscreen" parent="AppTheme.NoTitleBar">
<item name="android:windowFullscreen">true</item>
</style>
Then, I created a layout with only a VideoView
:
activity_main.xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@android:color/black"
tools:context=".MainActivity" >
<VideoView
android:id="@+id/videoView_intro"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="0dp"
android:layout_marginTop="0dp" />
</RelativeLayout>
Lastly, I played the video by the following code:
MainActivity.java:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
VideoView v = (VideoView) findViewById(R.id.videoView_intro);
v.setVideoURI(Uri.parse("android.resource://"+getPackageName()+"/"+R.raw.intro));
/* This line had a code snippet for event handling, unrelated to this question */
v.start();
}
I succeeded at playing the video, but the VideoView had a small margin at the bottom. I assume it matches the height of the notification bar. How can I get rid of that margin?
EDIT: I don't know whether this information is needed, but the activity is forced as landscape. Also, I target all android devices from API level 8, so the solution must be compatible with API Level 8.
AndroidManifest.xml:
<activity
android:name="org.package.name.MainActivity"
android:label="@string/app_name"
android:screenOrientation="landscape"
android:theme="@style/AppTheme.NoTitleBar.Fullscreen" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
Upvotes: 2
Views: 7429
Reputation: 11
VideoView v = (VideoView) findViewById(R.id.videoView_intro);
v.setVideoURI(Uri.parse("android.resource://"+getPackageName()+"/"+R.raw.intro));
DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
v.setLayoutParams(new LayoutParams(metrics.widthPixels, metrics.heightPixels));
v.start();`enter code here`
this code worked correctly
Upvotes: 1
Reputation: 183
OK, I think I became a fool when I came up with this mind.
The problem was not about the notification bar. Instead, it was about the aspect ratio of the video.
The video was sized 480×272. It had the aspect ratio of approx. 1.7647. On the other hand, the usable screen dimension of Galaxy Nexus was 1184×720, which had the aspect ratio of approx. 1.6444. So, basically the video was "longer" than the screen.
I didn't find out the cause because: 1) Curiously the height of the margin nearly matched the height of the notification bar, and 2) Since the video was a recording of a system which had narrower screen than the phone, I didn't believe that would be the cause. (Encoder broke the supposed aspect ratio.)
To conclude, what I should look into is this article: Center VideoView in landscape mode
The lesson is, don't get into your hypothesis too much.
Upvotes: 2
Reputation: 2290
Your best bet might be to create your own VideoView subclass, override the onMeasure(int, int) method and have that grab the screen dimensions and modify the VideoView height/width to match the correct height/width for your device.
If you don't wish to do that, then your other options that could be easier would be to launch the video in a fullscreen Dialog or you can set the LayoutParams to a static size generated at runtime:
VideoView v = (VideoView) findViewById(R.id.videoView_intro);
v.setVideoURI(Uri.parse("android.resource://"+getPackageName()+"/"+R.raw.intro));
DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
v.setLayoutParams(new LayoutParams(metrics.widthPixels, metrics.heightPixels));
v.start();
Upvotes: 6