7

So I've spent about two days trying to get a working SurfaceView. Tutorials I am following online aren't working even when followed to the letter. I normally get an entirely black screen.

In order to help teach myself how it works I need a working SurfaceView program.

I'm looking for a program that has the SurfaceView generated in a separate class. I would be very grateful if someone is able to post full code (XML and Java) for a SurfaceView program that simply turns the entire screen Red or White.

Thank you for any help!

(Any explanations along with the code would be amazing!)

Frenchie
  • 151
  • 1
  • 1
  • 9

2 Answers2

8

Try this link

Ansroid SurfaceView Example

I followed this tutorial example. It works fine.

Edit

Simple Code for SurfaceView

Layout xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <SurfaceView
        android:id="@+id/surfaceView"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1" />
</LinearLayout>

Java Activity Code

import android.graphics.Color;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.SurfaceView;
import android.view.ViewGroup;
import android.view.WindowManager;

public class Main2Activity extends AppCompatActivity{


  SurfaceView surfaceView;

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

    surfaceView = (SurfaceView) findViewById(R.id.surfaceView);
    //Method for making the activity full screen
    //With SurfaceView
    makeItFullScreen();
  }
  
  private void makeItFullScreen(){
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
    getSupportActionBar().hide();
    //Changing SurfaceView background color
    surfaceView.setBackgroundColor(Color.RED);

    DisplayMetrics displayMetrics = new DisplayMetrics();
    getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);

    ViewGroup.LayoutParams videoLayoutParams = surfaceView.getLayoutParams();
    videoLayoutParams.width = displayMetrics.widthPixels;
    videoLayoutParams.height = displayMetrics.heightPixels;

    ViewGroup.LayoutParams videoParams = surfaceView.getLayoutParams();
    videoParams.width = displayMetrics.widthPixels;
    videoParams.height = displayMetrics.heightPixels;
  }

}

Edit2

If you use custom SurfaceView xml will be like this..

<customClassPackageName.CustomSurfaceViewClassName
    android:id="@+id/surfaceView"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="1" />

Code inside Activity

  .......
  customClassPackageName.CustomSurfaceViewClassName surfaceView;

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

    surfaceView = (customClassPackageName.CustomSurfaceViewClassName) findViewById(R.id.surfaceView);
    .......
  • I have been through that example along with too many others. I am really just looking for some code that another user can confirm works and is extremely basic (like turning the screen red or white). I will be using this sample to learn from. – Frenchie Nov 22 '17 at 11:10
  • Thank you, I have marked this as the answer as it demonstrates the techniques I was looking for an worked. Another thing that I am very curios about is are you able to not define a SurfaceView in the XML and create/ manipulate a SurfaceView from another Java class within the project? – Frenchie Nov 22 '17 at 12:24
  • 1
    yes you can. see this http://android-er.blogspot.com/2014/03/simple-surfaceview-example.html –  Nov 22 '17 at 12:27
  • Thank you for the help however when I use that website it crashes the application. Do you have any idea why? – Frenchie Nov 22 '17 at 14:41
  • Do you check log? What is the exception? –  Nov 22 '17 at 15:37
  • 1-22 16:40:52.104 9548-9548/? E/AndroidRuntime: FATAL EXCEPTION: main Process: com.Frenchie.SurfaceView, PID: 9548 java.lang.RuntimeException: Unable to start activity ComponentInfo{com.Frenchie.SurfaceView/com.Frenchie.SurfaceView.MainActivity}: android.view.InflateException: Binary XML file line #0: Binary XML file line #0: Error inflating class com.example.androidsurfaceview.MySurfaceView – Frenchie Nov 22 '17 at 16:42
  • Sorry to keep nagging I still cant seem to get that working... In this instance what would be the customClassPackageName and customeSurfaceViewClassName. Sorry to be such a pain with this all your comments are appreciated! – Frenchie Nov 23 '17 at 11:31
  • Every class must under a package right? so that name is customClassPackageName. you will get it top of the class. like com.frenchie.surfacev​iew. and then add the Class name like com.frenchie.surfacev​iew.MySurfaceView. –  Nov 23 '17 at 11:48
  • 1-23 12:40:21.286 7926-7926/com.Frenchie.SurfaceView E/AndroidRuntime: FATAL EXCEPTION: main Process: com.Frenchie.SurfaceView, PID: 7926 java.lang.RuntimeException: Unable to start activity ComponentInfo{com.Frenchie.SurfaceView/com.Frenchie.SurfaceView.MainActivity}: android.view.InflateException: Binary XML file line #0: Binary XML file line #0: Error inflating class com.example.androidsurfaceview.MySurfaceView – Frenchie Nov 23 '17 at 12:41
  • I thought that was what you meant I was just checking, the error for this is above as it didnt work :/ – Frenchie Nov 23 '17 at 12:42
6

SurfaceView

In Android, all simple layout views are all drawn on the same GUI thread which is also used for all user interaction. So if we need to update GUI rapidly or if the rendering takes too much time and affects user experience then we should use SurfaceView.

The Android SurfaceView provides a dedicated drawing surface embedded inside of a view hierarchy. You can control the format of this surface, however, the SurfaceView takes care of placing the surface at the correct location on the screen.

Check this sample

Arnold Brown
  • 1,330
  • 13
  • 28
  • 1
    Thank you for the information, I have followed the sample previously and didn't receive any success, I am really looking for something more simple. – Frenchie Nov 22 '17 at 11:11