half or quarter black screen in android

Posted by Mike McKeown on Stack Overflow See other posts from Stack Overflow or by Mike McKeown
Published on 2012-09-26T19:58:31Z Indexed on 2012/09/26 21:37 UTC
Read the original article Hit count: 970

Filed under:
|

I have an android activity that when I launch sometimes (about 1 in 4 times) it only draws quarter or half the screen before showing it. When I change the orientation or press a button the screen draws properly.

I'm just using TextViews, Buttons, Fonts - no drawing or anything like that.

All of my code for initialising is in the onCreate(). In this method I'm loading a text file, of about 40 lines long, and also getting a shared preference. Could this cause a delay so that it can't draw the intent?

Thanks in advance if anyone has seen anything similar.

EDIT - I tried commenting out the loading of the word list, but it didn't fix the problem.

Here is the activity

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/mainRelativeLayout"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@drawable/blue_abstract_background" >

    <RelativeLayout
        android:id="@+id/relativeScore"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:gravity="center_horizontal" >

        <TextView
            android:id="@+id/ScoreLabel"
            style="@style/yellowShadowText"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_centerVertical="true"
            android:padding="10dip"
            android:text="SCORE:"
            android:textSize="18dp" />
    />

        <TextView
            android:id="@+id/ScoreText"
            style="@style/yellowShadowText"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerVertical="true"
            android:layout_toRightOf="@id/ScoreLabel"
            android:padding="5dip"
            android:text="0"
            android:textSize="18dp" />

        <TextView
            android:id="@+id/GameTimerText"
            style="@style/yellowShadowText"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:layout_centerVertical="true"
            android:layout_marginLeft="5dp"
            android:layout_marginRight="5dp"
            android:minWidth="25dp"
            android:text="0"
            android:textSize="18dp" />

        <TextView
            android:id="@+id/GameTimerLabel"
            style="@style/yellowShadowText"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerVertical="true"
            android:layout_toLeftOf="@id/GameTimerText"
            android:padding="10dip"
            android:text="TIMER:"
            android:textSize="18dp" />
             />
    </RelativeLayout>

    <RelativeLayout
        android:id="@+id/relativeHighScore"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/relativeScore" >

        <TextView
            android:id="@+id/HighScoreLabel"
            style="@style/yellowShadowText"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:padding="10dip"
            android:text="HIGH SCORE:"
            android:textSize="16dp" />

        <TextView
            android:id="@+id/HighScoreText"
            style="@style/yellowShadowText"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_toRightOf="@id/HighScoreLabel"
            android:padding="10dip"
            android:text="0"
            android:textSize="16dp" />
    </RelativeLayout>

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/relativeHighScore"
        android:orientation="vertical" >

        <LinearLayout
            android:id="@+id/linearMissing"
            android:layout_width="fill_parent"
            android:layout_height="80dp"
            android:layout_gravity="center"
            android:orientation="vertical" >

            <View
                android:layout_width="match_parent"
                android:layout_height="2dp"
                android:background="@color/yellow_text" />

            <TextView
                android:id="@+id/MissingWordText"
                style="@style/yellowShadowText"
                android:layout_width="wrap_content"
                android:layout_height="fill_parent"
                android:layout_gravity="center_horizontal"
                android:layout_marginTop="25dp"
                android:text="MSSNG WRD"
                android:textSize="22dp"
                android:typeface="normal" />
        </LinearLayout>

        <View
            android:layout_width="match_parent"
            android:layout_height="2dp"
            android:background="@color/yellow_text" />

        <TableLayout
            android:id="@+id/buttonsTableLayout"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content" >

            <TableRow
                android:id="@+id/tableRow3"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:gravity="center_horizontal"
                android:paddingTop="5dp" >

                <Button
                    android:id="@+id/aVowelButton"
                    android:layout_width="66dp"
                    android:layout_height="66dp"
                    android:layout_gravity="center_horizontal"
                    android:layout_marginBottom="10dp"
                    android:layout_marginRight="5dp"
                    android:background="@drawable/blue_vowel_button"
                    android:text="@string/a"
                    android:textColor="@color/yellow_text"
                    android:textSize="30dp" />

                <Button
                    android:id="@+id/eVowelButton"
                    android:layout_width="66dp"
                    android:layout_height="66dp"
                    android:layout_gravity="center_horizontal"
                    android:layout_marginBottom="10dp"
                    android:layout_marginRight="5dp"
                    android:background="@drawable/blue_vowel_button"
                    android:text="@string/e"
                    android:textColor="@color/yellow_text"
                    android:textSize="30dp" />
            </TableRow>

            <TableRow
                android:id="@+id/tableRow4"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:gravity="center_horizontal" >

                <Button
                    android:id="@+id/iVowelButton"
                    android:layout_width="66dp"
                    android:layout_height="66dp"
                    android:layout_gravity="center_horizontal"
                    android:layout_margin="5dp"
                    android:background="@drawable/blue_vowel_buttoni"
                    android:text="@string/i"
                    android:textColor="@color/yellow_text"
                    android:textSize="30dp" />

                <Button
                    android:id="@+id/oVowelButton"
                    android:layout_width="66dp"
                    android:layout_height="66dp"
                    android:layout_gravity="center_horizontal"
                    android:layout_margin="5dp"
                    android:background="@drawable/blue_vowel_button"
                    android:text="@string/o"
                    android:textColor="@color/yellow_text"
                    android:textSize="30dp" />

                <Button
                    android:id="@+id/uVowelButton"
                    android:layout_width="66dp"
                    android:layout_height="66dp"
                    android:layout_gravity="center_horizontal"
                    android:layout_margin="5dp"
                    android:background="@drawable/blue_vowel_button"
                    android:text="@string/u"
                    android:textColor="@color/yellow_text"
                    android:textSize="30dp" />
            </TableRow>
        </TableLayout>

        <TextView
            android:id="@+id/TimeToStartText"
            style="@style/yellowShadowText"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:textSize="24dp" />

        <Button
            android:id="@+id/startButton"
            style="@style/yellowShadowText"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal"
            android:layout_marginTop="10dp"
            android:background="@drawable/blue_button_menu"
            android:gravity="center"
            android:padding="10dip"
            android:text="START!"
            android:textSize="28dp" />
    </LinearLayout>

</RelativeLayout>

And the OnCreate() and a few methods:

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_game);
    isNewWord = true; 
    m_gameScore = 0; 
    m_category = getCategoryFromExtras();           
    //   loadWordList(m_category); 
    initialiseTextViewField(); 
    loadHighScoreAndDifficulty(); 
    setFonts();
    setButtons(); 


    setStartButton(); 

    enableDisableButtons(false); 


}

private void loadHighScoreAndDifficulty() {
    //setting preferences
    SharedPreferences prefs = this.getSharedPreferences(m_category, Context.MODE_PRIVATE);
    int score = prefs.getInt(m_category, 0); //0 is the default value
    m_highScore = score; 
    m_highScoreText.setText(String.valueOf(m_highScore)); 

    prefs = this.getSharedPreferences(DIFFICULTY, Context.MODE_PRIVATE);
    m_difficulty = prefs.getString(DIFFICULTY, NRML_DIFFICULTY); 

}

private void initialiseTextViewField() {
    m_startButton = (Button) findViewById(R.id.startButton);
    m_missingWordText = (TextView) findViewById(R.id.MissingWordText); 
    m_gameScoreText = (TextView) findViewById(R.id.ScoreText); 
    m_gameScoreLabel = (TextView) findViewById(R.id.ScoreLabel); 
    m_gameTimerText = (TextView) findViewById(R.id.GameTimerText); 
    m_gameTimerLabel = (TextView) findViewById(R.id.GameTimerLabel); 
    m_timeToStartText = (TextView) findViewById(R.id.TimeToStartText);
    m_highScoreLabel = (TextView) findViewById(R.id.HighScoreLabel); 
    m_highScoreText = (TextView) findViewById(R.id.HighScoreText);
}

private String getCategoryFromExtras() {
    String category = ""; 
    Bundle extras = getIntent().getExtras();
    if (extras != null) {
        category = extras.getString("category");
    }
    return category;
}

private void enableDisableButtons(boolean enable) {
    Button button = (Button) findViewById(R.id.aVowelButton);
    button.setEnabled(enable);
    button = (Button) findViewById(R.id.eVowelButton);
    button.setEnabled(enable);
    button = (Button) findViewById(R.id.iVowelButton);
    button.setEnabled(enable);
    button = (Button) findViewById(R.id.oVowelButton);
    button.setEnabled(enable);
    button = (Button) findViewById(R.id.uVowelButton);
    button.setEnabled(enable);
}

private void setStartButton() {
    m_startButton.setOnClickListener(new View.OnClickListener() {
        public void onClick(View view) {

            m_gameScore = 0; 
            updateScore(); 
            m_startButton.setVisibility(View.GONE);
            m_timeToStartText.setVisibility(View.VISIBLE);
            startPreTimer(); 
        }

    });

}

© Stack Overflow or respective owner

Related posts about android

Related posts about android-activity