Get the integral and fractional parts of the given number in Corona

math.modf is used to get the integral and fractional part of a given number in corona.

Return the integral and fractional parts of the given number.

 math.modf(7)
7       0
 math.modf(7.5)
7       0.5
math.modf(-7.5)
-7      -0.5

How to change the Opacity of an image in android?

Here is a simple function to change the opacity of a view in android.
Opacity is called alpha in android. so setAlpha(int) will do the job.

 ImageView img = (ImageView)findViewById(R.id.img);
 img.setAlpha(50);

The integer value may range from 0-255.

How to get the number of Taps in Corona SDK?

Here is the sample code to get the number of taps on a view in Corona SDK.

function button:tap( event )
	print("Number of taps : "..event.numTaps)
end

button:addEventListener( "tap", button )

here button is a displayObject like an image or a UI Button and event.numTaps gives the number of Taps on that view.

How to change the textColor in Corona SDK?

This is really simple in Corona, just use textObject:setTextColor( 255,255,255 )

local textObject = display.newText( "Hello World!", 50, 50, native.systemFont, 24 )
textObject:setTextColor( 255,255,255 )

Getting started with Corona

In the previous post I told you about corona SDK and Today I will show you how to get started with Corona SDK.

1. First Go to http://www.anscamobile.com/corona/ and download the SDK for trial.
2. If you are a MAC user then download the dmg or if you are windows user then download the exe installer.
3. Open the file and install it in your system.
4. Now the Corona SDK comes with a lot of samples.
For that go to your corona installed directory and inside that a folder named samples is there.
Open a sample and locate main.lua file, right click on it and run in corona simulator.
5. Now the corona simulator will come up select continue trial and your sample code will run now.
6. Now go on and change the code as you like.
7. If you want to see the console then locate the “Corona.Debugger”( In windows) file and Run it-> it is your console.
8. Explore the rest of the sample code and have fun
.

Building games for android and iPhone

Today I will talk about building games for android and iPhone.
There are a lot of SDK’s available for building games over the internet and I am here to talk about the CORONA SDK.

Corona SDK is a software development kit created by Walter Luh, co-founder of Ansca Mobile. It allows software programmers to build mobile applications for the iPhone, iPad, and Android devices.
Corona lets developers use integrated Lua, layered on top of Objective-C, to build graphically rich applications that are also lightweight in size and quick in development time. The SDK does not charge per-app royalty or impose any branding requirement, and has a subscription-based purchase model that allows new features to be rolled out immediately to user

Check out this site for more information about corona

http://www.anscamobile.com/corona/

You can also some of our tutorials also here.
In the coming posts we will be adding more about coding in Corona.

Check out this link to get access to the corona documentation.

Steps to sign an android apk and publish it to the market

Here are the simple steps to sign an android apk and publish it to the android market if you have an android developer account.
Follow these steps exactly













1. Right click your project in eclipse and Click Android tools – > export unsigned apk.
Give it a name and save it in a directory.

2. Go to command prompt and type adb.
If you have setup the path variable then it will list the current android devices running.
If not then please set up the path variable and then continue.

3. To generate your own certificate, issue the following command:
(Copy your apk to C:Program FilesJavajdk1.6.0_14bin before issuing this command)

keytool -genkey -v -keystore mykeystore.keystore -alias mykey -keyalg RSA -validity 10000

The above command generates a certificate named mykeystore.keystore with the key alias mykeystore, generated using the RSA algorithm, and with a validity of 10,000 days (this is the minimum recommended).
You will be prompted for some information:
What is your first and last name?
[unknown]:
What is the name of your organizational unit?
[unknown]:
What is the name of your organization?
[unknown]:
What is the name of your City or Locality?
[unknown]:
What is the name of your State or Province?
[unknown]:
What is the two-letter country code for this unit?
[unknown]: e.g for USA it is US.
If you are publishing your application for the Android Market, your keystore must have a validity period that ends after 22 October 2033 (which is the reason greater than 10000 days validity is recommended).

4. Next issue this command

jarsigner -verbose -keystore mykeystore.keystore MyApp.apk mykey

When prompted for the password for the keystore, use the password that was supplied during the key generation.
To verify that the application is signed correctly, you can use the –verify option with jarsigner.exe.
To verify that your .apk is signed, you can use a command like this:

$ jarsigner -verify MyApp.apk

If the .apk is signed properly, Jarsigner prints “jar verified”. If you want more details, you can try one of these commands:

$ jarsigner -verify -verbose MyApp.apk

or

$ jarsigner -verify -verbose -certs MyApp.apk

The command above, with the –certs option added, the details of the certificate used to sign the application can be seen.

5. Now the last step (dont forget to do this).
Once you have signed the .apk with your private key, run zipalign on the file. This tool ensures that all uncompressed data starts with a particular byte alignment, relative to the start of the file. Ensuring alignment at 4-byte boundaries provides a performance optimization when installed on a device. When aligned, the Android system is able to read files with mmap(), even if they contain binary data with alignment restrictions, rather than copying all of the data from the package. The benefit is a reduction in the amount of RAM consumed by the running application.

The zipalign tool is provided with the Android SDK, inside the tools/ directory. To align your signed .apk, execute:

zipalign -v 4 MyApp.apk MyApp_new.apk

The -v flag turns on verbose output (optional). 4 is the byte-alignment (don’t use anything other than 4). The first file argument is your signed .apk (the input) and the second file is the destination .apk file (the output). If you’re overriding an existing .apk, add the -f flag.

Caution: Your input .apk must be signed with your private key before you optimize the package with zipalign. If you sign it after using zipalign, it will undo the alignment.

6. Done. Now your APK is ready for uploading to android market.

Please leave your valuable comments if you found this post useful.

How to create gradient for button in android through java code?

In the previous post I have showed how to create gradient buttons in android using XML.

This is how we do this in java code.

 Button b = (Button)findViewById(R.id.Button01);
 b.getBackground().setColorFilter(0xFF00FF00, PorterDuff.Mode.MULTIPLY);
 

This is another method of applying gradiant.

b.getBackground().setColorFilter(new LightingColorFilter(0xFF00FF00, 0xFFAA0000));

Please leave your valuable comments.

How to create Gradiant Buttons in android?

You can create any type of views in android and you can provide any color you want as background. It can be a single color or any number of colors as you wish.

Here is a simple example.

First create an xml inside the res/drawable folder and name it blue_button.xml and copy this code into it.

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="true" >
        <shape>
            <solid
                android:color="#449def" />
            <stroke
                android:width="1dp"
                android:color="#2f6699" />
            <corners
                android:radius="3dp" />
            <padding
                android:left="10dp"
                android:top="10dp"
                android:right="10dp"
                android:bottom="10dp" />
        </shape>
    </item>
    <item>
        <shape>
            <gradient
                android:startColor="#449def"
                android:endColor="#2f6699"
                android:angle="360" />
            <stroke
                android:width="1dp"
                android:color="#2f6699" />
            <corners
                android:radius="4dp" />
            <padding
                android:left="10dp"
                android:top="10dp"
                android:right="10dp"
                android:bottom="10dp" />
        </shape>
    </item>
    <item>
        <shape>
            <gradient
                android:startColor="#119dff"
                android:endColor="#8f6788"
                android:angle="360" />
            <stroke
                android:width="1dp"
                android:color="#2f6699" />
            <corners
                android:radius="4dp" />
            <padding
                android:left="10dp"
                android:top="10dp"
                android:right="10dp"
                android:bottom="10dp" />
        </shape>
    </item>
</selector>

Now apply this as background in a button in your xml layout.

See how it appears.

Gradiant Button Demo

Gradiant Button in android

Download this sample code from here.
Please leave your valuable comments also.

How to apply shadow to a view in android?

This is a simple example showing how to create a shadow for a textView in android.

<TextView
	    android:layout_width="fill_parent"
		android:layout_height="wrap_content"
		android:text="CoderzHeaven"
		android:textColor="#FF0000"
		android:shadowColor="#008000"
		android:shadowRadius="1.5"
		android:shadowDx="2"
		android:shadowDy="5"
		android:textSize="35sp"
		android:gravity="center"
    />
shadow in view

TextView shadow in android

Start the browser with a search query in android.

In android we can simply start a browser or start the browser with a supplied search query.
Remember that we start the browser using an intent and we can put extra parameters in an intent.
Here in the browser intent we can supply the search query as a “SearchManager.QUERY” and giving the value.

 Intent search = new Intent(Intent.ACTION_WEB_SEARCH);
 search.putExtra(SearchManager.QUERY, "ANDROID");
 startActivity(search);

How to listen to software keyboard done button in android?

Here is a simple method to listen to software keyboard done button in android while editing in an EditText.

package pack.coderzheaven;

import android.app.Activity;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.inputmethod.EditorInfo;
import android.widget.EditText;
import android.widget.TextView;

public class KeyBoardTest extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        ((EditText)findViewById(R.id.EditText01)).setOnEditorActionListener(
                new EditText.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                if (actionId == EditorInfo.IME_ACTION_SEARCH ||
                    actionId == EditorInfo.IME_ACTION_DONE ||
                    event.getAction() == KeyEvent.ACTION_DOWN &&
                    event.getKeyCode() == KeyEvent.KEYCODE_ENTER) {

                		System.out.println("DONE BUTTON PRESSED............");

                    return true;
                }
                return false;
            }
        });

    }
}

How to show progress bar while loading a webpage in android?

This is a simple example showing how to load a webpage and show a progressBar while the page is loading in android.

For that you have to set a chrome client for the webview which implements “onProgressChanged”

Here is the layout file.

<?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"
    >
  <WebView
    android:id="@+id/webview"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
  </WebView>

</LinearLayout>

Here is the java file

package pack.coderzheaven;

import android.app.Activity;
import android.os.Bundle;
import android.view.Window;
import android.webkit.WebChromeClient;
import android.webkit.WebView;

public class LoadingWebViewDemo extends Activity {

	   WebView webview;
	   @Override
	   public void onCreate(Bundle savedInstanceState)
	   {
	      super.onCreate(savedInstanceState);

	      this.getWindow().requestFeature(Window.FEATURE_PROGRESS);
	      setContentView(R.layout.main );

	      // Makes Progress bar Visible
	      getWindow().setFeatureInt( Window.FEATURE_PROGRESS, Window.PROGRESS_VISIBILITY_ON);

	       webview = (WebView) findViewById( R.id.webview );
	       webview.getSettings().setJavaScriptEnabled(true);
	       webview.getSettings().setSupportZoom(true);       //Zoom Control on web

	       webview.getSettings().setBuiltInZoomControls(true); //Enable Multitouch if supported by ROM

	       // Load URL
	       webview.loadUrl("http://www.google.com");

	       // This makes the Progress bar be updated.
	       final Activity MyActivity = this;
	       webview.setWebChromeClient(new WebChromeClient() {
	        public void onProgressChanged(WebView view, int progress)
	        {
	         MyActivity.setTitle("Loading...");
	         MyActivity.setProgress(progress * 100);

	         // Return the app name after finish loading
	            if(progress == 100)
	               MyActivity.setTitle(R.string.app_name);
	          }
	        });
	   }
	}