Search This Blog

Tuesday 29 May 2012

How to update a UI in an Android Activity

The activity creates the UI View on the OnCreate method.

  /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    }

In this function you can set your view using the setContentView API.

setContentView API sets an explicit view to the UI.

Now you can update the UI by implementing a simple handler.

final Handler mHandler = new Handler()
     {
          public void handleMessage(Message msg)
          {
                  if(msg.what == 1)
                        textview.setText(msg.obj.toString());
                  else
                        textview.setText("Default Message");
          }
}

Now you can send the message to the handler to update the UI elements

   Message msg = Message.obtain(mHandler);
  msg.what = 1;  
  msg.obj = "Hello";
  mHandler.sendMessage(msg);

msg.what is used to define what type of message you are sending so you can implement the handler function for that message.

When a process is created for application, its main thread is dedicated to running a message queue that takes care of managing the all activities and any windows they create. You can create your own threads, and communicate back with the main application thread through a Handler.

This can be done by posting or sending a message to the message queue. The message is picked by message queue and the action will be taken as implemented in handler function. 



Monday 28 May 2012

How to set the Background of the Main View of Activity?

You can set the background of the activity main view using the following code

   //get the root view of the activity
 View view = this.getWindow().getDecorView();
   //set the background color to BLUE
 view.setBackgroundColor(Color.BLUE);

getDecorView() retrieve the top-level window decor view (containing the standard window frame/decorations and the client's content inside of that).

if you want to draw a image on the background then use

view.setBackgroundDrawable(getResources().getDrawable(R.drawable.bgimage));

The bgimage need to add in res->drawable-mdpi.

drawable-mdpi stores medium density pixel independence on your phone screen.

Friday 25 May 2012

How to sign your app before deploying to the Android Phone?

This post focuses on the quick steps you need before you deploy your android app on your phone.

Before deploying your application to the android phone you need to sign your application.

To sign your application right click on the project and select Android Tools

Now click on the Export Signed Application Package


Browse the project and click Next


Now select option create new keystore

Browse the key file and create a password and click Next.

Now select the option Create New Key and provide the following information.

Enter the unique Alias Name and enter the password and confirm password.

Enter the validity years (not max then 1000).

Now Enter First and Last Name and keep rest as blank.

Click Next and Now select the destination file for your export.

This will export the .apk file for your application.

Now your app is ready to move on your phone.









Thursday 24 May 2012

Create Android "Hello Creative Android" Program

This post will quickly explain to develop a "Hello Creative Android" Program for beginners.

See how to setup your Android Development environment

http://creativeandroidapps.blogspot.in/2012/05/setup-android-development-environment.html

Open Eclipse and go to File->New->Others and select the Android folder. Expand the Android Folder and select the Android Project.
Click Next and give name as "HelloCreativeWorld".
Now Click Next and select the build target as Android [4.0.3].
Now Click Next and give a package name (unique package name is required here)

For example :com.creativeandroidapp.example

Keep rest of the items as default and finish the project. See the Project is created in Package explorer.

Expand the project src directory and open the HelloCreativeWorldActivity.java

This file is created by wizard and is the main activity file for your android application.
(Activity is the application component which is responsible to draw the UI and interact with user. There could be multiple activities in one application.)

The main Activity is defined in AndroidMainfest.xml file for the project

<activity
            android:name=".HelloCreativeWorldActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
  </activity>

In HelloCreativeWorld example the activity will show you a message on the screen by following statement
setContentView(R.layout.main);


This method is called in onCreate method;this method is called when system creates the activity.


R.layout.main is an XML view setup in the UI.


R stands for Resources and the map file R.java is automatically generated for all the resources you defined in layout XML files.

R.java is created in gen directory of your project.


Expand the res->layout of your project and open the main.xml. 


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >
    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/hello" />
</LinearLayout>



The Text view will display the text message on the UI screen. The android:text property of the TextView is set to a string defined in res->string.xml.


<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="hello">Hello World, HelloCreativeWorldActivity!</string>
    <string name="app_name">HelloCreativeWorld</string>
</resources>



 Change the highlighted text to Hello Creative World.


 Now you are ready to run your app. Right Click on project and click on Run As -> Android Application.


 The android AVD (Android Virtual Device) will be launched. See the following screen shot showing the output of your first app.




Enjoy the first app as Android Developer.

Setup the Android Development Environment on Windows


This post explain how to quickly setup the development environment for the android application development on windows

Welcome Creative App Developers you can start your creative world of apps here


You should have following operating system versions
Windows XP (32-bit), Vista (32- or 64-bit), or Windows 7 (32- or 64-bit),Windows 2003 (32 bit)

The system must have JDK 6 installed.


  • For development environment you can use the Eclipse 3.6.2+ version. The Eclipse could be download 

  • Now you can download and install the Android SDK manager. you can download from http://developer.android.com/sdk/index.html
  • Once you setup the Android SDK Manager you can choose the installation packages from the manager window and SDK manager will automatically download and install the various packages.
  • Now you need an Eclipse Plugin for Android Development.
        Open Eclipse, then go to Help > Install New Software. and click Add, in the top-right corner.
        Now In Add Repository Dialog enter the "ADT Plugin" for the Name and put the following URL 
        for the Location  http://dl-ssl.google.com/android/eclipse/.
        Now the available software dialog will appear where you can check for Developer Tools and click
        Next to download the tools.
        Once the installation complete restart the Eclipse.

       Now you need a small configuration for ADT Plugin. In Eclipse go to Window->Preferences and open
       the preferences panel. select Android from left panel and choose the option to send usage data to 
       Google. (If you wish to send)
      
      Now in the main panel point your installed SDK to the SDK Location.

Done!. Now you are ready to develop your creative apps for Android.
     

Tuesday 22 May 2012

Parser Error in installing the android app

The parser error comes during the installation of app due to the target and SDK platform version differences.

You can fix this by installing the android support SDK for older versions and configure your AndroidMainfest.xml.

Refer the following link for more details

http://creativeandroidapps.blogspot.in/2012/05/how-to-support-backward-compatibility.html

How to support the backward compatibility in Android app deployment?


The code developed with new versions of the android SDK can be run on the older version using the following specification in the AndroidMainfest.xml

<uses-sdk android:minSdkVersion="8" android:targetSdkVersion="15"/>

In addition to this you need to create a directory in your project as libs (parallel to src) and copy the android-support-v4.jar from the android -support package installed.

After copy the lib file you need to add that into the build path by right click on the lib file in eclipse and click on Build Path -> Add Build Path

Monday 21 May 2012

Creative Android Apps

Android is a platform to create the creative applications for today's emerging gadget markets. creative android apps is a helping forum for all creative android developers to build the quick apps using the JAVA technology.

In today's post you will get an overview of the android platform and the applications you can create on android.

Android is an operating system developed by Google to provide a standard platform to create the applications for end users. The Google provides an Android platform SDK in JAVA to develop the applications quickly.

The various versions of the Android OS and Platform SDK are compatible so you can build a app which can run on all OS versions.

You can create the applications using your imagination and create the next future app.

Here is the list of some of the popular areas in which you can start your application development.

  • Android Game Apps
  • Android Eduction Apps
  • Android Content Management Apps
  • Android Security Apps
  • Android Social Apps