I am trying out youtube video to be played on my app. I understand this can be achieve by extending activity as YouTubeBaseActivity by doing this I don\'t have access to you
I was not interested in moving my already completed code to fragment for just adding a toolbar with a back button. So I figured out an easy way to do it.
Inside the layout xml I added a TextView which looks like a toolbar. Here's the code for the Textview. Also, you need to add a vector asset (back icon) in the drawable folder
<TextView
android:layout_width="match_parent"
android:layout_height="56dp"
android:id="@+id/actionbartitle"
android:background="@color/colorPrimary"
android:maxLines="1"
android:textSize="16sp"
android:textStyle="bold"
android:paddingStart="@dimen/activity_horizontal_margin"
android:paddingEnd="@dimen/activity_horizontal_margin"
android:text="Go Back"
android:gravity="center_vertical"
android:drawablePadding="@dimen/padding_8"
android:drawableLeft="@drawable/ic_back"
android:textColor="@android:color/white"
android:ellipsize="end"/>
And Simply adding onclicklistener on the textview for back press
val actionbarTitle = findViewById<TextView>(R.id.actionbartitle)
actionbarTitle.setOnClickListener {
super.onBackPressed()
}
Here's how the final result look likes! Have your cup of coffee :)
YouTubeBaseActivity extends Activity, (as opposed to, for example AppCompatActivity
), so the getSupportActionBar()
method doesn't exist.
You could try to make your class extend AppCompatActivity
, and use a YouTubePlayerSupportFragment instead wherever you would normally use a YouTubePlayerView
.
Edit:
Add the following to your layout
file, in place of a YouTubePlayerView
<fragment
android:name="com.google.android.youtube.player.YouTubePlayerSupportFragment"
android:id="@+id/youtube_fragment"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
Access it in onCreate()
of your Activity
in the same way you would any other static Fragment
public class CustomYouTubeActivity extends AppCompatActivity implements YouTubePlayer.OnInitialisedListener {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fragments_demo);
YouTubePlayerSupportFragment frag =
(YouTubePlayerSupportFragment) getSupportFragmentManager().findFragmentById(R.id.youtube_fragment);
frag.initialize(DeveloperKey.DEVELOPER_KEY, this);
}
@Override
public void onInitializationSuccess(YouTubePlayer.Provider provider, YouTubePlayer player,
boolean wasRestored) {
if (!wasRestored) {
//I assume the below String value is your video id
player.cueVideo("nCgQDjiotG0");
}
@Override
public void onInitializationFailure (YouTubePlayer.Provider provider, YouTubeInitializationResult error) {
if (errorReason.isUserRecoverableError()) {
errorReason.getErrorDialog(this, RECOVERY_DIALOG_REQUEST).show();
} else {
String errorMessage = String.format(getString(R.string.error_player), errorReason.toString());
Toast.makeText(this, errorMessage, Toast.LENGTH_LONG).show();
}
}
}