Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Custom view not filling screen?

When I execute my android application it works fine, except the view will not fill up the screen. I'm using the following xml file:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent"
    android:orientation="vertical"
    > 
    <com.project.name.SplashView
          android:id="@+id/splashView" 
          android:layout_width="fill_parent" 
          android:layout_height="fill_parent" 
    /> 
</LinearLayout>

I have a custom view defined that extends the regular view and I'm overwriting the onDraw method to display a splash screen. In code I'm setting the layout with a simple call:

setContentView(R.layout.splash);

I'm not going to post my onDraw method because it has a bunch of animation code. However, I've tested the view bounds by simply whiting out the background of the canvas using this function (so I know its not filling the screen):

private void drawBackground(Canvas c) 
{
    Paint p = new Paint();
    p.setColor(Color.WHITE);
    c.drawRect(0, 0, c.getWidth(), c.getHeight(), p);
}

Anyone with any ideas?

like image 441
onit Avatar asked Oct 16 '25 21:10

onit


1 Answers

Strange, I took your code and was able to make it work without issue. I compiled my test against 2.2, and ran it on a 2.3.3 emulator. Code provided inline below, and as a zip here:

ExampleCustomViewFull.zip

Which area of your screen is not getting painted properly? If it is the title bar and notification area, then refer to my comment in the manifest below:

AndroidManifest.xml:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.koderutils.example.customviewfull"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk android:minSdkVersion="9" />

    <application
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name" >
    <!--
    NOTE: If you want the Activity to take up the entire screen, add this attribute. 
    android:theme="@android:style/Theme.NoTitleBar.Fullscreen" 
    -->
    <activity
        android:label="@string/app_name"
        android:name=".ExampleCustomViewFullActivity">
        <intent-filter >
            <action android:name="android.intent.action.MAIN" />

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

</manifest>

ExampleCustomViewFull.java:

package com.koderutils.example.customviewfull;

import android.app.Activity;
import android.os.Bundle;

public class ExampleCustomViewFullActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    }
}

CustomView.java:

package com.koderutils.example.customviewfull;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.View;

public class CustomView extends View {

    public CustomView(Context context) {
    super(context);
    }

    public CustomView(Context context, AttributeSet attrs) {
    super(context, attrs);
    }

    public CustomView(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
    }

    @Override
    protected void onDraw(Canvas c) {
    super.onDraw(c);
    Paint p = new Paint();
    p.setColor(Color.WHITE);
    c.drawRect(0, 0, c.getWidth(), c.getHeight(), p);
    }
}

main.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <com.koderutils.example.customviewfull.CustomView
    android:id="@+id/custom_view"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"/>

</LinearLayout>
like image 86
Eric Schlenz Avatar answered Oct 18 '25 10:10

Eric Schlenz



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!