Error Checking in Corona

The biggest annoyance and pitfall is basic errors caused by typos. In Lua, unlike other languages, you don’t need to declare variables.
If you use a variable without declaring it, it becomes a global. This can cause uncaught errors if you have a typo. Consider the following:
local levelComplete = true
if LevelComplete == true then

print(“you won”)

end
Notice the typo, ‘LevelComplete’ with a capital ‘L’. Lua/Corona won’t catch this and instead make ‘LevelComplete’ a new global, and of course it will never be set to true, and so you could never win this game!
To get around the problem you can add the following code to the beginning of your main.lua to catch these issues. This will prevent ALL globals from being declared, and so would catch the above by displaying an error.
function declare (name, initval)

rawset(_G, name, initval or false)?

end
setmetatable(_G, {

__newindex = function(_ENV, var, val)

if var ~= “tableDict” then

error((“attempt to set undeclared global\”%s\””):format(tostring(var)), 2)

else?

rawset(_ENV, var, val)

end

end,
__index = function(_ENV, var, val)

if var ~= “tableDict” then?

error((“attempt to read undeclared global\”%s\””):format(tostring(var)), 2) else

rawset(_ENV, var, val)?

end?

end,

})

 

If you do want to use a Global then you need to use declare as below:

declare(“Levels”)

declare(“BonusLevels”)

Sencha Datastore connection to PHP

Create a Table “Employee” with following fields in Mysql:

ID, FirstName, LastName, Salary

After that you need to create a php file “data-file.php” with script for listing table contents.

Then we need to create a Datastore to connect our php file data_file.php:

EmployeeDataStore = new Ext.data.Store({
id: 'EmployeeDataStore',
proxy: new Ext.data.HttpProxy({
url: 'data_file.php',      // PHP file to connect to database or an array with data
method: 'POST'
}),
baseParams:{task: "LIST-ALL"}, // this parameter asks for listing
reader: new Ext.data.JsonReader({
// we tell the datastore where to get his data from
root: 'results',
totalProperty: 'total',
id: 'id'
},[
{name: 'ID', type: 'int', mapping: 'ID'},
{name: 'FirstName', type: 'string', mapping: 'firstname'},
{name: 'LastName', type: 'string', mapping: 'lastname'},
{name: 'Salary', type: 'float', mapping: 'salary'}
]),
sortInfo:{field: 'ID', direction: "ASC"}
});

 

Creating Resources from IDs Dynamically

To Make Bitmap from the Resource

 

Bitmap b = BitmapFactory.decodeResource(getResources(), R.drawable.icon);

 

When a new image is taken through a camera, you can add that image to the array/list and update the adapter using notifyDataSetChanged.

 

Bitmap[] images = { BitmapFactory.decodeResource(getResources
(),R.drawable.hm1),BitmapFactory.decodeResource(getResources
(),R.drawable.hm2),BitmapFactory.decodeResource(getResources(),R.drawable.hm3)};

 

Useful methods to handle exceptions in PHP

getMessage() 

gets the exception’s message

 
 

getCode(

returns a numerical code that represents the exception

 
 

getFile()

returns the file where the exception was thrown

 
 

getLine() 

returns the line number in the file where the exception was thrown

 
 

getTrace()

returns the an array of the backtrace() before the exception was thrown

 
 

getPrevious() 

returns the exception thrown before the current one, if any

 
 

getTraceAsString()

returns the backtrace() of an exception as a string instead of an array

 
 

__toString() 

returns the whole exception as a string. Is overrideable.

 
 

 
 

Lauching the Browser by supplying search criteria in Android

Often, you want to supply some criteria to search on. You can do this by supplying this information as part of the Intent’s extras. The ACTION_WEB_SEARCH Intent specifically uses the SearchManager.QUERY extra field for the search criteria. For example, to perform the Google search on pygmy goats, you configure the SearchManager.QUERY extra and launch the Browser as follows:

 

Intent search = new Intent(Intent.ACTION_WEB_SEARCH);  

search.putExtra(SearchManager.QUERY, “iPhone 4S”);  

startActivity(search);  

 
 

Launching the Browser From Android ApplicationsĀ 

Android uses Uri (Uniform Resource Identifier) objects to identify the unique location of a piece of data. Uri objects are often used to specify the data that an Intent is supposed to use. In this case, we will create a Uri object from a web URL using the parse() method:

 
 

Uri uriUrl = Uri.parse(“http://www.schogini.com/”);  

 
 

You can view HTML content using the following Intent: android.content.Intent.ACTION_VIEW. Begin by creating an Intent of this type and specifying the URI you created above, as follows, within your Button click handler:

 
 

Intent launchBrowser = new Intent(Intent.ACTION_VIEW, uriUrl);  

 
 

When you launch this Intent, any applications that can display web will be able to handle this request. Once you have set the type data for the Intent, you can call the startActivity() method, passing in your Intent:

startActivity(launchBrowser);  

 
 

When you click on this button, the Browser application (which generally handles HTML content display) is launched to the website you provided.

Sending http request to a remote server in sencha

Sending http request to a remote server in sencha:

Ext.Ajax.request({
url: 'ajax_data/simple.json',
success: function(response, opts) {
console.dir(Ext.decode(response.responseText););
},
failure: function(response, opts) {
console.log('request failed : ' + response.status);
}
});

where,

url: The URL to which to send the request, or a function to call which returns a URL string

params:  parameters to the request

 

callback :  The function to be called upon receipt of the HTTP response. The callback is called regardless of success or failure and is passed the following parameters:

options: The parameter to the request call.

success: True if the request succeeded.

response: The XMLHttpRequest object containing the response data.

 

Ext.Ajax.request({ url:'ajax_demo/sample.json', success:function(response, opts){var obj =Ext.decode(response.responseText); console.dir(obj);}, failure:function(response, opts){ console.log('server-side failure with status code '+ response.status);}});

Customizing Android Fonts – Typeface

 

There are three different default typefaces which are known as the Droid family of fonts: sans, monospace and serif. You can specify any one of them as the value for the android:typeface attribute in the XML declaration of a component that supports text styling, such as TextView. Here’s an example of all three typefaces in action:

<?xml version="1.0" encoding="utf-8"?>  
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
              android:orientation="vertical"  
             android:layout_width="fill_parent"  
             android:layout_height="fill_parent"  
        >  
   <TextView  
           android:layout_width="fill_parent"  
          android:layout_height="wrap_content"  
           android:text="This is a 'sans' demo!"  
            android:typeface="sans"  
          />  
     <TextView  
            android:layout_width="fill_parent"  
           android:layout_height="wrap_content"  
           android:text="This is a 'serif' demo!"  
           android:typeface="serif"  
         />  
    <TextView  
            android:layout_width="fill_parent"  
           android:layout_height="wrap_content"  
           android:text="This is a 'monospace' demo!"  
           android:typeface="monospace"  
           />  
</LinearLayout>  

Setting IdleTimer in Corona

Here is the code that helps you set the IdleTimer in Corona

system.setIdleTimer( false )  -- This will disable the IdleTimer of the system

system.setIdleTimer( true )  -- This will enable the IdleTimer of the system

By default, the IdleTimer of a system will be set to true. If the IdleTimer is active then it dims the screen and eventually will make the device to sleep mode.