getSupportActionBar with YouTubeBaseActivity
Asked Answered
E

2

12

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 my toolbar.

mActionBar = (Toolbar) findViewById(R.id.toolbar);
        if (mActionBar != null)
        {
            setSupportActionBar(mActionBar);
        }

        getSupportActionBar().setTitle(getResources().getString(R.string.youtubetitle));
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        getSupportActionBar().setHomeButtonEnabled(true);

I am getting getSupportActionbar could not be resolved. Is there an easy way to get access to toolbar by extending activity YouTubeBaseActivity.

Am I doing anything wrong?

Thanks!

Emotionality answered 23/2, 2016 at 17:20 Comment(4)
Just tell me which type activity you want to extend ?appcompat activity ?Postrider
You can simply use a custom xml layout as header instead of actionbar.Postrider
Can you give me an idea?Emotionality
I have updated my answer. please do checkPostrider
N
36

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();
        }
    }    

}
Norward answered 23/2, 2016 at 17:31 Comment(9)
Have you seen any sample Android App (Example) that can use YouTubePlayerSupportFragment - If so can you give me some link. I couldn't find any... as I don't know how to call them.Emotionality
@SanjanaNair It's my understanding you would replace a YouTubePlayerView in your layout file with a static Fragment, and interact with it as you would any other static fragment. There are a few demos on GitHub, and I've edited my answer above (untested).Norward
Would you have to have a working build for this? I have something very similar, but the app crashes whenever I attempt to load a Youtube Support Fragment. I just asked a question regarding the AppCompatActivity with YTSupportFragment.Ethylene
@Ethylene I could try and put a sample app together later tonight, but I wont be able to for a few hours yet. What's the link to your question?Norward
@Norward #37099397 Thanks!Ethylene
@Norward i have implemented your code, its working fine, thanks for your referenceYoulandayoulton
you saved my life.Incise
This works... Just Replace this fragment in your layout instead of Youtube Player View and its Done.. Thanks @NorwardBendigo
That was easier than I thought!Chattanooga
R
1

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 :)

Actionbar using Textview

Resist answered 31/12, 2019 at 19:32 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.