Android Studio: creating full screen button in media controller for VideoView
I have searched the internet for quite some time how to create a full screen button in a media controller for my video. I've seen some websites showing how to do this, but most of the methods I can't figure out as I'm new to android studio. I was able to add a media controller to my application by simply not using the full screen button. So when you click on it in the media player, it goes from half the screen to full screen, and then when you click on the same button in full screen, it changes back. I know this requires a dedicated media controller, but I've tried most of the ones I've seen on the web and they don't work. Please, help
Here is my basketball shooting xml
<FrameLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:background="?colorPrimary"/>
<android.support.v7.widget.Toolbar
android:layout_marginTop="25dp"
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?actionBarSize"
android:background="@drawable/action_bar_color"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
app:theme="@style/ToolbarTheme" />
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:layout_marginTop="80dp"
android:layout_gravity="bottom">
<TabHost
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/tabHost"
android:layout_gravity="center_horizontal">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TabWidget
android:id="@android:id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/action_bar_color"
android:elevation="4dp"> </TabWidget>
<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:id="@+id/Video"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:background="@color/md_divider"
android:layout_gravity="left">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/VideoAndTitle"
android:layout_margin="7dp">
<LinearLayout
android:id="@+id/VideoHolder"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:background="#FFFFFF"
android:layout_weight="50">
<VideoView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/videoView"
android:elevation="2dp" />
</LinearLayout>
<LinearLayout
android:id="@+id/VideoTitle"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:background="#FFFFFF"
android:layout_weight="1">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textStyle="bold"
android:id="@+id/video_title"
android:text="Basketball Shooting Tutorial"
android:textSize="16sp"
android:layout_gravity="start"
android:paddingTop="5dp"
android:paddingLeft="5dp"
android:paddingBottom="5dp" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
<LinearLayout
android:id="@+id/Description"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:showDividers="none"
android:background="@color/md_divider">
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#FFFFFF"
android:layout_margin="10dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Description"
android:id="@+id/textView3"
android:textSize="17sp"
android:textStyle="bold"
android:padding="5dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="Small Text"
android:id="@+id/textView4"
android:textSize="12sp"
android:layout_marginTop="10dp"
android:padding="5dp" />
</LinearLayout>
</LinearLayout>
</FrameLayout>
</LinearLayout>
</TabHost>
</LinearLayout>
Here is my BasketballShooting.java
package com.jehan.sportstutorials;
import android.content.Intent;
import android.net.Uri;
import android.os.Build;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.util.DisplayMetrics;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.GridLayout;
import android.widget.LinearLayout;
import android.widget.MediaController;
import android.widget.TabHost;
import android.widget.VideoView;
import android.media.MediaPlayer.OnPreparedListener;
import android.app.ProgressDialog;
import android.util.Log;
import android.media.MediaPlayer;
public class BasketballShooting extends AppCompatActivity {
Toolbar toolbar;
ProgressDialog pDialog;
VideoView videoview;
String VideoURL = "https://ia801507.us.archive.org/13/items/basketball_shooting.3gp/basketball_shooting.mp4";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_basketball_shooting);
toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
TypedValue typedValueColorPrimaryDark = new TypedValue();
BasketballShooting.this.getTheme().resolveAttribute(R.attr.colorPrimaryDark, typedValueColorPrimaryDark, true);
final int colorPrimaryDark = typedValueColorPrimaryDark.data;
if (Build.VERSION.SDK_INT >= 21) {
getWindow().setStatusBarColor(colorPrimaryDark);
}
TabHost tabHost = (TabHost) findViewById(R.id.tabHost);
tabHost.setup();
TabHost.TabSpec tabSpec = tabHost.newTabSpec("video");
tabSpec.setContent(R.id.Video);
tabSpec.setIndicator("Video");
tabHost.addTab(tabSpec);
tabSpec = tabHost.newTabSpec("description");
tabSpec.setContent(R.id.Description);
tabSpec.setIndicator("Description");
tabHost.addTab(tabSpec);
//VideoView
videoview = (VideoView) findViewById(R.id.videoView);
// Create a progressbar
pDialog = new ProgressDialog(BasketballShooting.this);
// Set progressbar title
pDialog.setTitle("Basketball Shooting Tutorial");
// Set progressbar message
pDialog.setMessage("Buffering...");
pDialog.setIndeterminate(false);
pDialog.setCancelable(false);
// Show progressbar
pDialog.show();
try {
// Start the MediaController
MediaController mediacontroller = new MediaController(
BasketballShooting.this);
mediacontroller.setAnchorView(videoview);
// Get the URL from String VideoURL
Uri video = Uri.parse(VideoURL);
videoview.setMediaController(mediacontroller);
videoview.setVideoURI(video);
} catch (Exception e) {
Log.e("Error", e.getMessage());
e.printStackTrace();
}
videoview.requestFocus();
videoview.setOnPreparedListener(new OnPreparedListener() {
// Close the progress bar and play the video
public void onPrepared(MediaPlayer mp) {
pDialog.dismiss();
videoview.start();
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_basketball_shooting, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_bar) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
As I am new to android a simplified explanation would be much appreciated.
source to share
No one has answered this question yet
Check out similar questions: