Thursday, June 27, 2013

Android AnimationDrawable - Loading Example

0 comments

Android Drawable Animation 


In this tutorial you will learn how to animate Image set by using Android AnimationDrawable 




loading.xml

res/anim/loading.xml


<?xml version="1.0" encoding="utf-8"?>
<animation-list xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/voipemLoading"
    android:oneshot="false" >

    <item
        android:drawable="@drawable/loading_01"
        android:duration="300"/> 
    <item
        android:drawable="@drawable/loading_02"
        android:duration="300"/>
    <item
        android:drawable="@drawable/loading_03"
        android:duration="300"/>
    <item
        android:drawable="@drawable/loading_04"
        android:duration="300"/>

</animation-list>


DravableAnimation


package com.javasrilankansupport.drawableanimation;

import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.app.Activity;
import android.graphics.drawable.AnimationDrawable;

public class DravableAnimation extends Activity implements OnClickListener {

 private AnimationDrawable mAnimation;
 private ImageView mAnimLogo;
 private Button mbtnStart;
 private boolean mStart;

 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.dravable_animation);

  mAnimLogo = (ImageView) findViewById(R.id.loading_image);
  mAnimation = (AnimationDrawable) mAnimLogo.getDrawable();
  mbtnStart = (Button) findViewById(R.id.btn_start);

  mbtnStart.setOnClickListener(this);
 }

 @Override
 public void onClick(View v) {
  if (!mStart) {
    // start animation 
   mAnimLogo.post(new Runnable() {
    @Override
    public void run() {
     mAnimLogo.setVisibility(View.VISIBLE);
     mAnimation.start();
     mStart = true;
     mbtnStart.setText("Stop");
    }
   });
  } else {     
   // stop animation            
   mAnimation.stop();
   mbtnStart.setText("Start");
   mStart = false;
  }
 }
}


dravable_animation.xml


<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".DravableAnimation"
    android:background="@drawable/bg_login" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="44dp"
        android:text="@string/hello_world"
        android:textColor="@color/black"
        android:textSize="24sp" />

    <ImageView
        android:id="@+id/loading_image"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/textView1"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="37dp"
        android:src="@anim/loading"
        android:visibility="invisible" />

    <Button
        android:id="@+id/btn_start"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/loading_image"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="48dp"
        android:text="@string/btn_start" />

</RelativeLayout>

Download AnimationDrawable complete source code 




Read more...

How to move an image from left to right and right to left in android

2 comments

Android Animation with TranslateAnimation

In this tutorial you will learn how to animate an image from left to right and right to left in android using TranslateAnimation


Android Animation TranslateAnimation



     ImageView img_animation = (ImageView) findViewById(R.id.img_animation);

        TranslateAnimation animation = new TranslateAnimation(0.0f, 400.0f,
                0.0f, 0.0f);          //  new TranslateAnimation(xFrom,xTo, yFrom,yTo)
        animation.setDuration(5000);  // animation duration 
        animation.setRepeatCount(5);  // animation repeat count
        animation.setRepeatMode(2);   // repeat animation (left to right, right to left )
//      animation.setFillAfter(true);      

        img_animation.startAnimation(animation);  // start animation 


Animation 


package com.javasrilankansupport.animation;

import android.os.Bundle;
import android.app.Activity;
import android.view.animation.TranslateAnimation;
import android.widget.ImageView;

public class Animation extends Activity {

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

  ImageView img_animation = (ImageView) findViewById(R.id.img_animation);

  TranslateAnimation animation = new TranslateAnimation(0.0f, 400.0f,
    0.0f, 0.0f);
  animation.setDuration(5000);
  animation.setRepeatCount(5);
  animation.setRepeatMode(2);
  animation.setFillAfter(true);
  img_animation.startAnimation(animation);

 }
}


activity_animation.xml


 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".Animation" >
    
    <ImageView
        android:id="@+id/img_animation"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="30dp"
        android:layout_marginTop="42dp"
        android:src="@drawable/emo_im_crying" />

</RelativeLayout>



Read more...

Tuesday, June 25, 2013

Android OnKeyListener Example

0 comments
In this post you will learn how to implement Android OnKeyListener with EditText

android onkeylistener javasrilankansupport.com

EditTextKeyListener

EditTextKeyListener class implement the Android OnKeyListener interface





package com.javasrilankansupport.androidkeylistner;

import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnKeyListener;
import android.widget.EditText;
import android.widget.Toast;
import android.app.Activity;

public class EditTextKeyListener extends Activity implements OnKeyListener {

 private EditText userName;
 private EditText password;

 private String mUserName;
 private String mPassword;

 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_keylistner);

  userName = (EditText) findViewById(R.id.txt_name);
  password = (EditText) findViewById(R.id.password);

  // set onkeyListner
  userName.setOnKeyListener(this);
  password.setOnKeyListener(this);

 }

 @Override
 public boolean onKey(View v, int keyCode, KeyEvent event) {

  switch (v.getId()) {

  case R.id.txt_name:
   // keyup and "ENTER"
   if ((event.getAction() == KeyEvent.ACTION_DOWN)
     && (keyCode == KeyEvent.KEYCODE_ENTER)) {
    mUserName = userName.getText().toString();
    if (mUserName.equals("")) {
     Toast.makeText(this, "Enter user name...",
       Toast.LENGTH_SHORT).show();

    } else
     password.requestFocus();
   }
   break;

  case R.id.password:
   // keydown and "ENTER"
   if ((event.getAction() == KeyEvent.ACTION_DOWN)
     && (keyCode == KeyEvent.KEYCODE_ENTER)) {
    mPassword = password.getText().toString();
    if (mPassword.equals(""))
     Toast.makeText(this, "Enter password...",
       Toast.LENGTH_SHORT).show();

    else
     // show Toast Long time
     Toast.makeText(
       this,
       "User name : " + mUserName + "  Password : "
         + mPassword, Toast.LENGTH_LONG).show();
   }
   break;

  }
  return false;
 }

}

activity_keylistener.xml


<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".EditTextKeyListener" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello_world"
        android:textSize="20sp" />

    <EditText
        android:id="@+id/txt_name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/textView1"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="48dp"
        android:ems="10"
        android:inputType="textPersonName" 
        android:hint="@string/username"
       >

        <requestFocus />
    </EditText>

    <EditText
        android:id="@+id/password"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignRight="@+id/txt_name"
        android:layout_below="@+id/txt_name"
        android:layout_marginTop="24dp"
        android:ems="10"
        android:inputType="textPassword"
        android:hint="@string/psass" />

</RelativeLayout>


Download Android KeyListener project

Read more...