Pages

Wednesday 18 January 2012

FRAME ANIMATION


FRAME ANIMATION
/*
 A series of frames is drawn one after the other at regular intervals.
 For this create a xml which contains ImageView for showing the animation
 */

package com.animation;

import android.app.Activity;
import android.graphics.drawable.AnimationDrawable;
import android.os.Bundle;
import android.widget.ImageView;

public class FrameAnimationActivity extends Activity {

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

                                ImageView imgView = (ImageView) findViewById(R.id.Image);
                                ImageView imgView2 = (ImageView) findViewById(R.id.imageView1);

                                imgView.setVisibility(ImageView.VISIBLE);
                                imgView2.setVisibility(ImageView.VISIBLE);

                                imgView.setBackgroundResource(R.drawable.ani);
                                imgView2.setBackgroundResource(R.drawable.ani);
                               
                                frameAnimation = (AnimationDrawable) imgView.getBackground();
                                frameAnimation2 = (AnimationDrawable) imgView2.getBackground();
                                imgView.post(new Starter());
                                imgView2.post(new Starter());
                }

                public class Starter implements Runnable {

                                public void run() {
                                                // TODO Auto-generated method stub
                                                frameAnimation.start();
                                                frameAnimation2.start();
                                               
                                }

                }
}


.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" />

    <ImageView
        android:id="@+id/Image"
        android:layout_width="100px"
        android:layout_height="100px"
        android:layout_gravity="center_horizontal"
        android:background="@drawable/an2" />

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="100px"
        android:layout_height="100px"
        android:layout_gravity="center_horizontal"
        android:background="@drawable/an1" >
    </ImageView>

</LinearLayout>

Beginning Android Tablet Games Programming


Beginning Android Tablet Games Programming


Android games programmers now have the power to write games for Android tablets.Beginning Android Tablet Games Programming explains how to enhance your Android games using the new tablet interface and the additional screen estate. You'll learn how to bring your programming skills up to date and into a world where touch screens, games physics, and artificial intelligence come together in new and surprising ways.

Beginning Android Tablet Games Programming shows how to quickly and easily set up an Android development environment—in no time at all, you'll be programming away. You'll begin with some simple games using sprites and choreographed movement. Next, you'll learn how to handle user input in the modern age of touch screens and motion.

Along the way, you'll discover how to use that extra screen space on a tablet to provide more relaxed and more interesting user interactions in your games. You'll learn how to use sound and music, for instance, to make your application menus more user-friendly.

The Android operating system has recently acquired multicore functionality to meet the demands of multicore devices now entering the tablet market. With BeginningAndroid Tablet Games Programming, you'll discover how to harness that new power with your games programming through more process-demanding and fun techniques, including physics modeling, rich game world representation, artificial intelligence, and multiplayer interactions.

Throughout each chapter of Beginning Android Tablet Games Programming, you'll find code that you can add or adapt to your own games to create the components you want. You can also work up to wrapping everything together into a complete Mario-type example game.

Finally, when you have your first games ready, learn how developers have released their games and made a profit. You'll find tips on how to present your games in the Android and other application markets, and a solid approach to games marketing and monetization.
What you’ll learn
·           How to set up an Android tablet programming environment
·           How to program for touchscreens and other input devices
·           Techniques to take advantage of the extra screen size of a tablet
·           How to take advantage of multicore processing power in your games
·           How to program a multiplayer game
·           How to add artificial intelligence to in-game opponents
·           How to program game modes, game states, and game worlds
·           How to program a complete game
·           About in-app marketing and games monetization
Who this book is for
This is a book for Java programmers new to Honeycomb, tablets and games programming. An experienced games programmer can also use this book to migrate their existing expertise to the Android and the tablet environment.
Table of Contents
1.           Setting Up for Android 3.0 Java Development
2.           Creating Simple Games with Sprites and Movement
3.           Creating Gathering User Input
4.           Adding Sounds Effects, Music, and Video
5.           One-Player Game with Obstacles
6.           A Ball and Paddle Game
7.           Building a Two-Player Game
8.           A One-Player Strategy Game Part I
9.           A One-Player Strategy Game Part II
10.        Publishing the Game
11.        Testing Android Games on a Real Device

Google release Android Design Site


Google release Android Design Site
https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgCr_3UHxHTz0ow-Ys1behC1EsvBH0aJ8vB3oxOsXMTt6elJQULEkAOoLD_b_GAGU2SjZXWs5McBr95DNPVTavCzTs6TOSatnZM7RMWxj-XikbM3PV55-hqgbKPPZ9QhUpSjo6vfzP6R-tf/s200/Android+Design.png
Google are introducing
 Android Design for developers to learn about principles, building blocks, and patterns for creating world-class Android user interfaces. Whether you’re a UI professional or a developer playing that role, these docs show you how to make good design decisions, big and small. In the coming months, it will be expanded with more in-depth content.

-
 Android Design

Multi-Language Translate


Multi-Language Translate
Extends from previous exercise, AndroidTranslate, using Google Translate API in Android application. It's a multi-language translation application. There are two spinner in the application, user can select the language to be translated.



------------------------------------------
Please note:
- google-api-
translate-java have to be downloaded and build path have to be set, refer the articlegoogle-api-translate-java.
- "android.permission.INTERNET" have to be set in AndroidMainfest.xml, refer to
 the articleAndroidTranslate, using Google Translate API in Android application.
------------------------------------------

Modify main.xml to add two
 Spinner to select the language to be translated from and to.

<?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="@string/hello"
  />
<EditText
 android:id="@+id/InputText"
  android:layout_width="fill_parent"
  android:layout_height="wrap_content"
 />
<TextView
 android:layout_width="fill_parent"
  android:layout_height="wrap_content"
  android:text="from:"
  />
<Spinner
 android:id = "@+id/spinner_InputLanguage"
 android:layout_width="fill_parent"
 android:layout_height="wrap_content"
 />
<TextView
 android:layout_width="fill_parent"
  android:layout_height="wrap_content"
  android:text="to:"
  />
<Spinner
 android:id = "@+id/spinner_OutputLanguage"
 android:layout_width="fill_parent"
 android:layout_height="wrap_content"
 />
<Button
 android:id="@+id/TranslateButton"
  android:layout_width="fill_parent"
  android:layout_height="wrap_content"
  android:text="Translate"
 />
<TextView
 android:id="@+id/OutputText"
  android:layout_width="fill_parent"
  android:layout_height="wrap_content"
  />
</LinearLayout>

main.xml can be
 downloaded here.

Implement a file arrayLanguage.xml in the folder /res/values/


<?xml version="1.0" encoding="utf-8"?>
<resources>
<string-array name="language">
<item>ARABIC</item>
<item>BULGARIAN</item>
<item>CATALAN</item>
<item>CHINESE</item>
<item>CHINESE_SIMPLIFIED</item>
<item>CHINESE_TRADITIONAL</item>
<item>CROATIAN</item>
<item>CZECH</item>
<item>DANISH</item>
<item>DUTCH</item>
<item>ENGLISH</item>
<item>FILIPINO</item>
<item>FINNISH</item>
<item>FRENCH</item>
<item>GERMAN</item>
<item>GREEK</item>
<item>HEBREW</item>
<item>HINDI</item>
<item>INDONESIAN</item>
<item>ITALIAN</item>
<item>JAPANESE</item>
<item>KOREAN</item>
<item>LATVIAN</item>
<item>LITHUANIAN</item>
<item>NORWEGIAN</item>
<item>POLISH</item>
<item>PORTUGESE</item>
<item>ROMANIAN</item>
<item>RUSSIAN</item>
<item>SERBIAN</item>
<item>SLOVAK</item>
<item>SLOVENIAN</item>
<item>SPANISH</item>
<item>SWEDISH</item>
<item>UKRANIAN</item>
<item>VIETNAMESE</item>
</string-array>
</resources>

arrayLanguage.xml can be
 downloaded here.

Modify AndroidTranslate.java

package com.exercise.AndroidTranslate;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;

import com.google.api.translate.Language;
import com.google.api.translate.Translate;



public
class AndroidTranslate extends Activity {
 
 EditText MyInputText;
 Button MyTranslateButton;
 TextView MyOutputText;
 Spinner spinner_InputLanguage, spinner_OutputLanguage;
 
  /** Called when the activity is first created. */
  @Override
  public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.main);
   
      MyInputText = (EditText)findViewById(R.id.InputText);
      MyTranslateButton = (Button)findViewById(R.id.TranslateButton);
      MyOutputText = (TextView)findViewById(R.id.OutputText);
   
      MyTranslateButton.setOnClickListener(MyTranslateButtonOnClickListener);
   
      ArrayAdapter<CharSequence> adapter
      = ArrayAdapter.createFromResource(this,
        R.array.language, android.R.layout.simple_spinner_item);
   
      spinner_InputLanguage = (Spinner) findViewById(R.id.spinner_InputLanguage);
      adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
      spinner_InputLanguage.setAdapter(adapter);
   
      spinner_OutputLanguage = (Spinner) findViewById(R.id.spinner_OutputLanguage);
      adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
      spinner_OutputLanguage.setAdapter(adapter);
  }

  private Button.OnClickListener MyTranslateButtonOnClickListener
    = new Button.OnClickListener(){

  @Override
  public void onClick(View v) {
   // TODO Auto-generated method stub
   String InputString;
   String OutputString = null;
   InputString = MyInputText.getText().toString();
  
   Language fromLanguage =
    Language.valueOf((String)spinner_InputLanguage
    .getItemAtPosition((int) spinner_InputLanguage
    .getSelectedItemId()));
   Language toLanguage =
    Language.valueOf((String)spinner_OutputLanguage
    .getItemAtPosition((int) spinner_OutputLanguage
    .getSelectedItemId()));
  
   try {
    Translate.setHttpReferrer("http://android-er.blogspot.com/");
    OutputString = Translate.execute(InputString,
      fromLanguage, toLanguage);
   
   } catch (Exception ex) {
    ex.printStackTrace();
    OutputString = "Error";
      }
  
   MyOutputText.setText(OutputString);
  }
  };
}

Turn On/Off Action Bar


Turn On/Off Action Bar
ActionBar on Android 3.0 (API level 11) is a window feature that identifies the application and user location, and provides user actions and navigation modes.

To turn it on/off programmatically, call ActionBar.show() and ActionBar.hide().

Example:
Action Bar On
Action Bar Off


package com.exercise.AndroidActionBar;

import android.app.ActionBar;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class AndroidActionBarActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
       
        Button showActionBar = (Button)findViewById(R.id.showactionbar);
        Button hideActionBar = (Button)findViewById(R.id.hideactionbar);
       
        showActionBar.setOnClickListener(new Button.OnClickListener(){

   @Override
   public void onClick(View arg0) {
    ActionBar actionBar = getActionBar();
    actionBar.show();
   }});
       
        hideActionBar.setOnClickListener(new Button.OnClickListener(){

   @Override
   public void onClick(View arg0) {
    ActionBar actionBar = getActionBar();
    actionBar.hide();
   }});
    }
}



<?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" />
    <Button
        android:id="@+id/showactionbar"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="Show Action Bar" />
    <Button
        android:id="@+id/hideactionbar"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="Hide Action Bar" />

</LinearLayout>

Autostart our own application when android device bootup compeleted


Autostart  own application at bootup.

when you power on the device open our application below the codes.
through receiver we can achive this one .

Androidmenifest.xml

 <receiver android:enabled="true" android:name=".OpenOurAppReceiver"
        android:permission="android.permission.RECEIVE_BOOT_COMPLETED">

        <intent-filter>
                <action android:name="android.intent.action.BOOT_COMPLETED" />
                <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>
OpenOurAppReceiver.class
public class
 OpenOurAppReceiver extends BroadcastReceiver{
    @Override
    public void onReceive(Context context, Intent intent) {
          
  Intent i = new Intent(context, MainActivity.class);
            i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
               context.startActivity(i);  

    }

}