Friday, 8 April 2016

Android Example - Frame by Frame Animations

Project Structure :


2d animation frame by frame animation project sketch

File:  src/AnimationMain.java

- Call animation class and call loadAnimation method to initialize animation images.
- Call playAnimation method to play images frame by frame.

import com.androidexample.animation.R;
import android.os.Bundle;
import android.app.Activity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
 
public class AnimationMain extends Activity {
 
     
    AnimationView anim_view;
    AnimationView anim_view1;
     
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.animatewin);
         
        // Get AnimationView reference see animation_main.xml
        anim_view = (AnimationView) this.findViewById(R.id.anim_view);
        anim_view.loadAnimation("spark", 18,0,0);
         
        // Get AnimationView reference see animation_main.xml
        anim_view1 = (AnimationView) this.findViewById(R.id.anim_view1);
         
        // You can call this method inside thread
        anim_view1.loadAnimation("spark", 18,0,0);
       
        Button play_button = (Button) this.findViewById(R.id.play_button);
         
         
        play_button.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                 
                //Play Both Animation
                anim_view.playAnimation();
                anim_view1.playAnimation();
                  
            }
        });
         
    }
     
    @Override
    protected void onDestroy() {
         
        super.onDestroy();
         
        // After activity close animation will close
        anim_view=null;
        anim_view1=null;
         
    }
     
}

File : res/layout/animation_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"
     android:background="#000000"
    >
 
     
    <TextView
         android:textColor="#ffffff"
         android:textSize="25px"
         android:layout_width="fill_parent"   
         android:layout_height="wrap_content"
         android:text="Play Two Animations"
         android:textStyle="bold"
         android:layout_gravity="center_vertical|center_horizontal"
        />
     
    <Button
        android:id="@+id/play_button"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="PLAY ANIMATION" />
     
   <LinearLayout
     android:layout_width="fill_parent"
    android:layout_height="150dip"
    > 
          
        <com.androidexample.animation.AnimationView 
            android:id="@+id/anim_view"
            android:layout_width="150dip"
            android:layout_height="150dip"  />
             
        <com.androidexample.animation.AnimationView 
            android:id="@+id/anim_view1"
            android:layout_width="150dip"
            android:layout_height="150dip" />
     
   </LinearLayout>
     
    
</LinearLayout>

File:  src/AnimationView.java


- It is the main animation class.
- Create AnimationView class object to animate image.
- Call postInvalidate() method to call onDraw() method.

import java.util.ArrayList;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.drawable.BitmapDrawable;
import android.util.AttributeSet;
import android.util.Log;
import android.widget.ImageView;
 
 
public class AnimationView extends ImageView
{
    private static final String TAG = "AnimationView";
    private Context mContext = null;
     
    private static final int DELAY = 100; //delay between frames in milliseconds
    private  int drawX = 0;
    private  int drawY = 0;
     
    private boolean mIsPlaying = false;
    private boolean mStartPlaying = false;
     
    private ArrayList<Bitmap> mBitmapList = new ArrayList<Bitmap>();
     
    private int play_frame = 0;
    private long last_tick = 0;
     
    public AnimationView(Context context, AttributeSet attrs)
    {
        super(context, attrs);
        mContext = context;
    }
     
    @Override
    protected void onDraw(Canvas c)
    {
        /******* onDraw method called first time and when postInvalidate() called *****/
         
        //Log.d(TAG, "onDraw called");
        if (mStartPlaying)
        {
            Log.d(TAG, "starting animation...");
            play_frame = 0;
            mStartPlaying = false;
            mIsPlaying = true;
             
            // Again call onDraw method
            postInvalidate();
        }
        else if (mIsPlaying)
        {
             
            if (play_frame >= mBitmapList.size())
            {
                //mIsPlaying = false;
                play_frame=0;
                 
                // Again call onDraw method
                postInvalidate();
            }
            else
            {
                long time = (System.currentTimeMillis() - last_tick);
                 
                int draw_x = drawX;
                int draw_y = drawY;
                if (time >= DELAY) //the delay time has passed. set next frame
                {
                    last_tick = System.currentTimeMillis();
                    c.drawBitmap(mBitmapList.get(play_frame), draw_x, draw_y, null);
                    play_frame++;
                     
                    // Again call onDraw method
                    postInvalidate();
                }
                else //still within delay.  redraw current frame
                {
                    c.drawBitmap(mBitmapList.get(play_frame), draw_x, draw_y, null);
                     
                    // Again call onDraw method
                    postInvalidate();
                }
            }
        }
    }
     
    /*ideally this should be in a background thread*/
    public void loadAnimation(String prefix, int nframes,int drawx,int drawy)
    {
        drawX = drawx;
        drawY = drawy;
        mBitmapList.clear();
         
        for (int x = 0; x < nframes; x++)
        {
            String name = prefix + "" + x;  // prefix = "spark" see loadAnimation call
             
            //Log.d(TAG, "loading animation frame: " + name);
             
            // Set Bitmap image
            int res_id = mContext.getResources().getIdentifier(name, "drawable", mContext.getPackageName());
            BitmapDrawable d = (BitmapDrawable) mContext.getResources().getDrawable(res_id);
            mBitmapList.add(d.getBitmap());
        }
    }
     
    public void playAnimation()
    {
        mStartPlaying = true;
         
        // Again call onDraw method
        postInvalidate();
    }
}

File : AndroidManifest.xml


<?xml version="1.0" encoding="utf-8"?>
    package="com.androidexample.animation"
    android:versionCode="1"
    android:versionName="1.0" >
 
    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="8" />
 
    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.androidexample.animation.AnimationMain"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
 
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
 
</manifest>

Android Example - PopUp

Example of using PopupWindow

android.widget.PopupWindow can be used to display an arbitrary view. The popup windows is a floating container that appears on top of the current activity.

Example of PopupWindow

Create /res/layout/popup.xml to define the view of the PopupWindow.
<?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" 
    android:background="@android:color/background_light">
 <LinearLayout 
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:orientation="vertical" 
     android:layout_margin="1dp"
     android:background="@android:color/darker_gray">
     >
     <LinearLayout 
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:orientation="vertical" 
      android:layout_margin="20dp">
      <TextView
          android:layout_width="fill_parent"
          android:layout_height="wrap_content"
          android:text="It's a PopupWindow" />
      <ImageView
          android:layout_width="fill_parent"
          android:layout_height="wrap_content"
          android:src="@drawable/ic_launcher" />
      <Button
          android:id="@+id/dismiss"
          android:layout_width="fill_parent"
          android:layout_height="wrap_content"
          android:text="Dismiss" />
      </LinearLayout>
 </LinearLayout>
</LinearLayout>


Main activity Java code to handle the PopupWindow
package com.exercise.AndroidPopupWindow;

import android.app.Activity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.PopupWindow;

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

   @Override
   public void onClick(View arg0) {
    LayoutInflater layoutInflater 
     = (LayoutInflater)getBaseContext()
      .getSystemService(LAYOUT_INFLATER_SERVICE);  
    View popupView = layoutInflater.inflate(R.layout.popup, null);  
             final PopupWindow popupWindow = new PopupWindow(
               popupView, 
               LayoutParams.WRAP_CONTENT,  
                     LayoutParams.WRAP_CONTENT);  
             
             Button btnDismiss = (Button)popupView.findViewById(R.id.dismiss);
             btnDismiss.setOnClickListener(new Button.OnClickListener(){

     @Override
     public void onClick(View v) {
      // TODO Auto-generated method stub
      popupWindow.dismiss();
     }});
               
             popupWindow.showAsDropDown(btnOpenPopup, 50, -30);
         
   }});
    }
}


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" />
    <Button
        android:id="@+id/openpopup"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="Open Popup Window" />
    
</LinearLayout>

video on youtube

Android Studio 2.0 and Emulator 25.1.1 are official now

Android Studio 2.0 and Emulator 25.1.1 are officially now


Android Studio 2.0 and Emulator 25.1.1 are moved to stable release channels.
This release is focused on development productivity by introducing instant run and a much faster emulator. Please see our blog post for more details.

announcement: Android Tools Project Site - Android Studio 2.0 and Emulator 25.1.1 are Available in the Stable, Beta, and Dev Channels

Upgrade to Android Studio 2.0 from 1.5:

Android Example - Alert Box

In this tutorial, we show you how to display an alert box in Android. See flowing Steps :
  1. First, use the AlertDialog.Builder to create the alert box interface, like title, message to display, buttons, and button onclick function
  2. Later attach above builder to AlertDialog and display it.
  3. Done.
P.S This project is developed in Eclipse 3.7, and tested with Android 2.3.3.

1 Android Layout Files

Simpel layout file, display a button on screen.
File : res/layout/main.xml
Markup
<?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" >
 
    <Button
        android:id="@+id/buttonAlert"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Show Alert Box" />
            
</LinearLayout>

2. Activity

When user click on this button, display the alert box, with your pre-defined alert dialog interface.
File : MainActivity.java
Java
package com.mkyong.android;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class MainActivity extends Activity {

 final Context context = this;
 private Button button;

 public void onCreate(Bundle savedInstanceState) {

  super.onCreate(savedInstanceState);
  setContentView(R.layout.main);

  button = (Button) findViewById(R.id.buttonAlert);

  // add button listener
  button.setOnClickListener(new OnClickListener() {

  @Override
  public void onClick(View arg0) {

   AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
    context);

   // set title
   alertDialogBuilder.setTitle("Your Title");

   // set dialog message
   alertDialogBuilder
    .setMessage("Click yes to exit!")
    .setCancelable(false)
    .setPositiveButton("Yes",new DialogInterface.OnClickListener() {
     public void onClick(DialogInterface dialog,int id) {
      // if this button is clicked, close
      // current activity
      MainActivity.this.finish();
     }
      })
    .setNegativeButton("No",new DialogInterface.OnClickListener() {
     public void onClick(DialogInterface dialog,int id) {
      // if this button is clicked, just close
      // the dialog box and do nothing
      dialog.cancel();
     }
    });

    // create alert dialog
    AlertDialog alertDialog = alertDialogBuilder.create();

    // show it
    alertDialog.show();
   }
  });
 }
}

3. Demo

Start it, display a button.
android alert box example
When button is clicked, display the alert box
android alert box example
If “Yes” button is clicked, close the activity and return back to your Android main screen.
android alert box example
Follow me on twitter - @tweet_naman

Malaria Defender - By Naman Tiwari


I have developed an application known as Malaria Defender .
It is an only application through which a person can check malaria in seconds with just few clicks .

It have a nice design and some nice graphics . 
Download the app here - Click here

Steps to use app .

1. Create a profile .


2. Browse the content

3. Check malaria.


4. Gather Report


5. Gather profile


6. Edit Profile


7. Take doctors advice


And a lot more .

Developer - Naman Tiwari
Age 13 .

follow me on twitter - @tweet_naman








Tuesday, 12 January 2016

"Programming IQ" Version 2.0

"Programming IQ" - Version 2.0

 Programming IQ has launched Version 2.0
. Get it now on play store . Link of the app



An app for people those who want to clear out programming interviews.

App contain more than 700 Questions of 12 languages .

App is developed by a 12 years boy Naman Tiwari

Contact me for help - namantiwari2002@gmail.com
Twitter - @tweet_naman

Please download and rate the application.