Search code examples
androidandroid-scrollviewoncreate

Scrollview is not showing any content in emulator or phone even though the preview is looking fine SOLVED


Android studio scrollview is not showing any content in emulator or phone even though the Android studio preview is fine. Even the background colour is missing. Although, I'am a noob with Android studio but I think that everything should work in this code and thereby I'm confused.

I have read multiple threads about this problem but non of them answered this question adequately. As instructed to by these answers, I have tried to adjust the width and height settings, and used linear layout as a root, and other tricks, but with little success. However, I think that the linear layout is not blocking the scrollview and the problem is either in this xml file or somewhere else. I have tried to solve this now at least two hours by myself, but I'm turning to more advanced developers to ask for some advice.

The problem was that I had no onCreate method in the Java file.

<?xml version="1.0" encoding="utf-8"?>
<!--
<LinearLayout
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="50dp"
android:orientation="vertical"
tools:context=".StartGame">
-->



<ScrollView
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_height="fill_parent"
android:layout_width="fill_parent"
android:background="@drawable/background"
tools:context=".StartGame">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="center_vertical"
            android:text="@string/eka_teksti"
            android:textAlignment="center"
            android:textColor="#000"
            android:layout_marginTop="2dp"/>

        <ImageView
            android:id="@+id/imageView"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:srcCompat="@mipmap/ic_launcher" />


    </LinearLayout>
</ScrollView>

<!--
</LinearLayout>
-->

Here is the relevant Java code


    package com.jussitamminen.pplpankki;

    import android.os.Bundle;
    import android.support.v7.app.AppCompatActivity;
    import android.content.Intent;
    import android.view.View;

    public class StartGame extends AppCompatActivity {


    }

And there is the Manifest code also


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

        <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=".StartGame"
                android:label="Second Activity"
                android:parentActivityName=".MainActivity">

            </activity>
        </application>

    </manifest>

I expect that the scrollview has a background colour and other content as I defined.

here


Solution

  • According to your question, you do not have an onCreate() method. There is no linking between your Java class and the XML file.That is why you are seeing nothing.

    Add the onCreate to your StartGame class like this.

    public class StartGame extends AppCompatActivity {
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.your_xml);
        }
    }