Question

I've created an Android game based on this tutorial. I have a lot of development experience, but these are my first steps into Java and a mobile platform.

The game is created as a single activity with everything drawn on a SurfaceView and 'Screens' specified that break everything up. I have no xml layout. The onCreate for the main class is the best place to see how it's set up. The AndroidFastRenderView extends SurfaceView.

    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    requestWindowFeature(Window.FEATURE_NO_TITLE);
    getWindow().setFlags(
            WindowManager.LayoutParams.FLAG_FULLSCREEN
                    | WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON,
            WindowManager.LayoutParams.FLAG_FULLSCREEN
                    | WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);

    boolean isPortrait = getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT;
    int frameBufferWidth = isPortrait ? 480 : 800;
    int frameBufferHeight = isPortrait ? 800 : 480;
    Bitmap frameBuffer = Bitmap.createBitmap(frameBufferWidth,
            frameBufferHeight, Config.RGB_565);

    float scaleX = (float) frameBufferWidth
            / getWindowManager().getDefaultDisplay().getWidth();
    float scaleY = (float) frameBufferHeight
            / getWindowManager().getDefaultDisplay().getHeight();

    renderView = new AndroidFastRenderView(this, frameBuffer);
    graphics = new AndroidGraphics(getAssets(), frameBuffer);
    fileIO = new AndroidFileIO(this);
    audio = new AndroidAudio(this);
    input = new AndroidInput(this, renderView, scaleX, scaleY);
    screen = getInitScreen();
    setContentView(renderView);
}

I'm looking for a way to enable to user to enter a name for the high scores table - which I thought would be simple. I'm really struggling with the best way to receive keyboard input. What's my best option here? I'm assuming there's some way I can create a custom edit that I can draw on my the surface view myself and receive keyboard input that way. However, I'm guessing displaying an EditText over the top of the surface view will be easier - but I can't for the life of me work out how to do it. I've tried numerous methods that I've found through Google and SO but can't seem to get any of them to work.

Advice and sample code would be much appreciated.

Many thanks

Was it helpful?

Solution 2

Ok, after a fair amount of research I found this which gave me some good ideas. I went a step further than my original question. I added an XML layout that contained a AndroidFastRenderView. I then set this as my content, used findViewById to get the AndroidFastRenderView and set it up the as I had done previously.

    setContentView(R.layout.gamelayout);
    renderView = (AndroidFastRenderView)findViewById(R.id.androidFastRenderView);
    renderView.initialise(this, frameBuffer);

I created another xml layout for my high scores screen, this in the constructor my HighScores screen added code to include this in my current layout.

    final AndroidGame agame = (AndroidGame) game;

    agame.runOnUiThread(new Runnable() {

        @Override
        public void run() {
            View currentView = agame.findViewById(R.id.gameLayout);
            ViewGroup parent = (ViewGroup) currentView.getParent();
            currentView = agame.getLayoutInflater().inflate(R.layout.highscorescontent, parent, false);
            parent.addView(currentView);
        }
    });

I then added code to remove the high scores layout again when we leave the high scores screen.

    View currentView = agame.findViewById(R.id.highScoresLayout);
    ViewGroup parent = (ViewGroup) currentView.getParent();
    parent.removeView(currentView);

If anyone knows a more elegant solution then let me know - but this'll do me nicely for now.

OTHER TIPS

The best way for you is using internal storage. You can save files directly on the device's internal storage. By default, files saved to the internal storage are private to your application and other applications cannot access them (nor can the user). When the user uninstalls your application, these files are removed.

To create and write a private file to the internal storage:

Call openFileOutput() with the name of the file and the operating mode. This returns a FileOutputStream. Write to the file with write(). Close the stream with close().

`String FILENAME = "hello_file";
String string = "hello world!";
FileOutputStream fos = openFileOutput(FILENAME, Context.MODE_PRIVATE);
fos.write(string.getBytes());
fos.close();

MODE_PRIVATE will create the file (or replace a file of the same name) and make it private to your application. Other modes available are: MODE_APPEND, MODE_WORLD_READABLE, and MODE_WORLD_WRITEABLE.

To read a file from internal storage:

Call openFileInput() and pass it the name of the file to read. This returns a FileInputStream. Read bytes from the file with read(). Then close the stream with close().

Saving cache files

If you'd like to cache some data, rather than store it persistently, you should use getCacheDir() to open a File that represents the internal directory where your application should save temporary cache files.

When the device is low on internal storage space, Android may delete these cache files to recover space. However, you should not rely on the system to clean up these files for you. You should always maintain the cache files yourself and stay within a reasonable limit of space consumed, such as 1MB. When the user uninstalls your application, these files are removed.

Or use SQLite databases more detail here

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top