Author Topic: Android Tips  (Read 4272 times)

Offline ampos

  • Prof. Inline
  • *****
  • Posts: 1592
    • View Profile
    • AMpostata Website
Android Tips
« on: 2011-Oct-12 »
Some tips/things I have learned from Android

NATIVE RESOLUTION

On Android, screens can be landscape (horizontal) or portrait (vertical) natively (hardware coded). Mostly, all phones are vertical and tablets are horizontal.

If you try to open a horizontal screen on a vertical device, you will get a mess, as acelerators (joyx & joyy) are coded for the native screen orientation.

You need to know the native resolution of the android device. The best idea is to set your GLB project for Android as 9000x9000, and the device will open its native reolution. Then, check it on your device and use setorientation to rotate it to your program desiree.

You are making a vertical app. Run the app at 9000x9000, it will be open at 800x480. It is a horizontal device. SETORIENTATION 3 to turn -90ยบ left. Pseudocode:

Code: GLBasic [Select]
setscreen 9000,9000,0
getscreensize xres,yres
if xres>yres
   setorientation 3
   swap xres,yres
   turn=true
endif

if turn=true or webos_touchpad=true  //gravity!
  dx=-getjoyy(0)
  dy=-getjoyx(0)
else
   dx=getjoyx(0)
   dy=-getjoy(0)
endif

(notice that WebOS touchpad has the acelerometers also rotated and not the screen, but this thread is about Android)

MANIFIEST and SCREEN ROTATION

You have to disable screen rotation automatically by Android OS, or your app will crash when the device is tilt. It is being done throught the manifiest.xml

I have removed the line android:configChanges="orientation|keyboardHidden" to avoid autochanges from Android. Also the line android:screenOrientation="portrait" has to be changed to avoid messing more with Android screens. So, I ended with

Quote
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.diniplay.magicbox"
      android:installLocation="preferExternal"
      android:versionCode="01200"
      android:versionName="1.200">
    <uses-sdk android:minSdkVersion="4" android:targetSdkVersion="8" />
   <supports-screens android:resizeable="false"
                 android:smallScreens="true"
                 android:normalScreens="true"
                 android:largeScreens="true"
                 android:anyDensity="true" />
    <application android:label="@string/app_name" android:icon="@drawable/icon" android:debuggable="false">
        <activity   android:name="org.libsdl.app.SDLActivity"
               android:label="MagicCoinBox"
               android:screenOrientation="nosensor" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
</manifest>

This will make your app compatible with 690 devices, acording to Android Markt.

If you don't remove the line configChanges..., it will be compatible with "just" 689 devices, but this one is MY device, so I am asuming this extra device is a really generic one, as my tablet did not come with Google Market and had to be installed "by hand".

Also anyDensity="true" to make it even more compatible.

PLACEHOLDER FOR HARDWARE BUTTONS info :)
check my web and/or my blog :D
http://diniplay.blogspot.com (devblog)
http://www.ampostata.org
http://ampostata.blogspot.com
I own PC-Win, MacBook 13", iPhone 3G/3GS/4G and iPAC-WinCE

Offline r0ber7

  • Dr. Type
  • ****
  • Posts: 469
    • View Profile
Re: Android Tips
« Reply #1 on: 2011-Dec-15 »
Thanks for the info, I'll be sure to return to this thread once I get around to compiling for Android. I'm wondering though, why 9000x9000? Any specific reason or does it just need to be big?

Offline ampos

  • Prof. Inline
  • *****
  • Posts: 1592
    • View Profile
    • AMpostata Website
Re: Android Tips
« Reply #2 on: 2011-Dec-15 »
To be sure it will work on future devices with 8000x5000 pixels resolution  :nana:
check my web and/or my blog :D
http://diniplay.blogspot.com (devblog)
http://www.ampostata.org
http://ampostata.blogspot.com
I own PC-Win, MacBook 13", iPhone 3G/3GS/4G and iPAC-WinCE

Offline planetm

  • Mr. Drawsprite
  • **
  • Posts: 56
    • View Profile
Re: Android Tips
« Reply #3 on: 2012-Jul-23 »
Sorry to dig up an old topic. I've been working on an android based game for the past few weeks, and things seem to be going pretty well. The final hurdle is getting to to run on devices other than my own phone! I've made the screen scale (based on another ampos post - thank you!), and it works fine on a friends phone with a higher resolution than mine. The problem is tablets, I have two tablets one running android honeycomb and a new nexus 7. To try and work out what was happening I wrote this quick code based on that posted by ampos above. It displays the current xres and yres, then draws a rectangle to a virtual screen before scaling it to display on the real screen.

Code: GLBasic [Select]
SETSCREEN 9000,9000,0

GLOBAL xres,yres

GETSCREENSIZE xres,yres

GLOBAL device$=PLATFORMINFO$("DEVICE")

PRINT "xres="+xres,10,10
PRINT "yres="+yres,10,30
PRINT "device="+device$,10,50

IF xres>yres
        PRINT "Landscape",10,70
ELSEIF yres>xres
        PRINT "Portrait",10,70
ELSE
        PRINT "Square?",10,70
ENDIF

SHOWSCREEN

MOUSEWAIT

CREATESCREEN 1,100,800,480

USESCREEN 1
CLEARSCREEN RGB(0,0,255)
DRAWRECT 100,100,600,280,RGB(255,0,0)
PRINT "xres="+xres,10,10
PRINT "yres="+yres,10,30
PRINT "device="+device$,10,50

IF xres>yres
        PRINT "Landscape",10,70
ELSEIF yres>xres
        PRINT "Portrait",10,70
ELSE
        PRINT "Square?",10,70
ENDIF

USESCREEN -1
CLEARSCREEN RGB(0,0,0)
STRETCHSPRITE 100,0,0,xres,yres

SHOWSCREEN

MOUSEWAIT

END

Both the tablets report landscape resolutions (i.e. xres > yres), but they fail completely to display the scaled rectangles. It works perfectly on my phone.  In my game I get a display on the tablets, but it is rotated through 90 degrees (SETORIENTATION 3 made no difference).

Any ideas?

Thank you.

Offline erico

  • Community Developer
  • Prof. Inline
  • ******
  • Posts: 4057
    • View Profile
    • Portfolio
Re: Android Tips
« Reply #4 on: 2012-Jul-23 »
GREAT AMPOS! :O

This will sure help me out, million thanks! :nw:

Offline Wampus

  • Prof. Inline
  • *****
  • Posts: 1004
    • View Profile
Re: Android Tips
« Reply #5 on: 2012-Jul-23 »
planetm I tried your code on a Nook Color running a Gingerbread build (2.3.7) with the default AndroidManifest.xml and Ampos's version. Everything seemed OK there with the rectangle drawing and orientation. Also tried a Ice Cream Sandwich build (4.0.3) and all seemed well. So, might be something to do with the device hardware and Android configuration. What was the Honeycomb tablet you mentioned?

If this is happening I'm bothered. I want my stuff to work on the Nexus 7. Looks like I have a good excuse to get one.

Offline planetm

  • Mr. Drawsprite
  • **
  • Posts: 56
    • View Profile
Re: Android Tips
« Reply #6 on: 2012-Jul-25 »
Thanks for the reply Wampus. The honeycomb tablet is an Advent Vega running a version of Android 3.2. I've spent some time on it again this morning but can't work out what's happening. I've tried scaling using stretchsprite or ampos' routine based on startpoly , both give the same result.

It seems that the scaled image appears at right hand end of the tablets landscape display, rotated 90 degrees anticlockwise. The touchscreen responds as if the buttons are in the correct places however.

Offline Kitty Hello

  • code monkey
  • Administrator
  • Prof. Inline
  • *******
  • Posts: 10649
  • here on my island the sea says 'hello'
    • View Profile
    • http://www.glbasic.com
Re: Android Tips
« Reply #7 on: 2012-Aug-09 »
It seems setting <uses-sdk android:minSdkVersion="8" android:targetSdkVersion="14" /> allows you to hide the system menu on Android 4.0 and higher.

Offline sf-in-sf

  • Mr. Drawsprite
  • **
  • Posts: 93
  • Artist F.P. Brixey
    • View Profile
    • My computed art project
Re: Android Tips
« Reply #8 on: 2013-Oct-26 »
Thanks a lot ampos!
Planetm, an easy way to impose your orientation to android is to do something like

SETSCREEN 9000,9099,0 // if you want a portrait

or

SETSCREEN 9099,9000,0 // if you want a landscape.

It works well for me in the emulator (small screen)+ large tablet G9 10.1 inch Archos.
Good luck!
On the day the atom is a cube I will start believing in the square pixel.