Search code examples
androidandroid-studioandroid-nestedscrollview

Nested scroll view hides action bar in the emulator


My DescriptionActivity.java uses NestedScrollView as the main container. In design view, I can normally see the action bar but when I turn on the emulator, action bar is hidden and the activity is full screen. How can i show action bar also in the emulator?

This is what I see in design view:

Screenshot of design tool

This is what I see in the emulator:

Screenshot of emulator

Here is my xml code:

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.NestedScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fillViewport="true"
    android:background="@drawable/background"
    tools:context="przemo.me.recommend.recommendme.DescriptionActivity">

    <android.support.constraint.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <TextView
            android:id="@+id/textView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginEnd="8dp"
            android:layout_marginStart="16dp"
            android:layout_marginTop="16dp"
            android:text="Trailer"
            android:textColor="@android:color/black"
            android:textSize="18sp"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintHorizontal_bias="0.0"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />

        <FrameLayout
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:layout_marginBottom="8dp"
            android:layout_marginEnd="8dp"
            android:layout_marginStart="8dp"
            android:layout_marginTop="8dp"
            app:layout_constraintBottom_toTopOf="@+id/textView3"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/textView">

            <com.google.android.youtube.player.YouTubePlayerView
                android:id="@+id/youtubePlayerView"
                android:layout_width="match_parent"
                android:layout_height="match_parent">

            </com.google.android.youtube.player.YouTubePlayerView>

        </FrameLayout>

    </android.support.constraint.ConstraintLayout>
</android.support.v4.widget.NestedScrollView>

Manifest:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="przemo.me.recommend.recommendme">

<uses-permission android:name="android.permission.INTERNET" />

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:roundIcon="@mipmap/ic_launcher_round"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    <activity android:name=".MainActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity android:name=".CreatorsActivity" />
    <activity android:name=".DescriptionActivity"></activity>
</application>

Styles:

<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
    <!-- Customize your theme here. -->
    <item name="colorPrimary">#a8a8a8</item>
    <item name="colorPrimaryDark">#000000</item>
    <item name="colorAccent">#000000</item>
</style>

DescriptionActvity.java

public class DescriptionActivity extends YouTubeBaseActivity
                        implements YouTubePlayer.OnInitializedListener {

TextView filmDesTextView;
YouTubePlayerView youTubePlayerView;
String GOOGLE_API_KEY = "AIzaSyAniT";
String videoId;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_description);

    setTitle("Description");

    filmDesTextView = (TextView) findViewById(R.id.filmDesTextView);
    youTubePlayerView = (YouTubePlayerView) findViewById(R.id.youtubePlayerView);

    AsyncHttpClient client = new AsyncHttpClient();

    Intent intent = getIntent();
    String title = intent.getStringExtra("title");

    client.get("https://tastedive.com/api/similar?q=" + title + "&k=movie&info=1", new JsonHttpResponseHandler() {
        @Override
        public void onSuccess(int statusCode, Header[] headers, JSONObject response) {

            try {

                String description = response.getJSONObject("Similar").getJSONArray("Info").getJSONObject(0).getString("wTeaser");
                final String url = response.getJSONObject("Similar").getJSONArray("Info").getJSONObject(0).getString("yUrl");
                final int REQUEST_CODE = 1;

                Pattern pattern = Pattern.compile("(?<=youtu.be/|watch\\?v=|/videos/|embed\\/)[^#\\&\\?]*");
                Matcher matcher = pattern.matcher(url);

                if (matcher.find()) {
                    videoId = matcher.group(0);
                }
                filmDesTextView.setText(description);

                youTubePlayerView.initialize(GOOGLE_API_KEY, new YouTubePlayer.OnInitializedListener() {
                    @Override
                    public void onInitializationSuccess(YouTubePlayer.Provider provider, YouTubePlayer youTubePlayer, boolean wasRestored) {

                        if(!wasRestored) {
                            youTubePlayer.cueVideo(videoId, REQUEST_CODE);
                        }
                    }

                    @Override
                    public void onInitializationFailure(YouTubePlayer.Provider provider, YouTubeInitializationResult youTubeInitializationResult) {

                    }
                });
            } catch (JSONException e) {

                e.printStackTrace();
            }
        }

        @Override
        public void onFailure(int statusCode, Header[] headers, Throwable e, JSONObject response) {

            Toast.makeText(DescriptionActivity.this, "Data loading failed!", Toast.LENGTH_SHORT).show();
        }
    });

}

@Override
public void onInitializationSuccess(YouTubePlayer.Provider provider, YouTubePlayer youTubePlayer, boolean wasRestored) {


}

@Override
public void onInitializationFailure(YouTubePlayer.Provider provider, YouTubeInitializationResult youTubeInitializationResult) {

}
}

Solution

  • Your problem is that YouTubeBaseActivity extends Activity and not AppCompat, which is a beautiful decision by Google’s clusterfuck of framework.

    Unfortunately, short of making your own activity for YouTube, the alternative is to use YouTubePlayerFragment (see here) that won’t care about what Activity it lives on, so your activity can now be a normal AppCompat activity, and you do your YT stuff inside the Fragment.