Top Menu

Drop Down MenusCSS Drop Down MenuPure CSS Dropdown Menu

Friday 30 January 2015

Rotate Animation sample using XML in Android

I would like to share this example of Rotation Animation as this may be very helpful to the needy one.

In this example, I’ll try to introduce you doing simple animations with Android. It is simple enough that on clicking a button, an images starts rotating from 0 degree to 360 degree in N seconds. Lets begin with the animation XML. Once you have created a new Android project, create a folder named "anim" in res and a file named 'rotator.xml' inside res/anim. 

res/anim/rotator.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:interpolator="@android:anim/linear_interpolator">
  <rotate
      android:fromDegrees="0"
      android:toDegrees="360"
      android:pivotX="50%"
      android:pivotY="50%"
      android:duration="2000"
      android:startOffset="0"/>
</set>

Then open res/main.xml, after removing the default textView in the layout, add an ImageView and Button into the layout. Set the src property of the ImageView as your filename of the added image, for example android:src="@drawable/myimg" 

res/main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
    <ImageView
        android:id="@+id/img_View"
        android:layout_width="
wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:src="@drawable/myimg" />
    <Button
        android:id="@+id/btn"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"  />
</RelativeLayout>


OK, lets edit the main class. In the onClick() for the button, add the necessary code for running the animation. Check the following code. 

MainActivity.java
package com.example.ImageRotation;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.ImageView;

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        Button btn = (Button) findViewById(R.id.btn);
      
        btn.setOnClickListener(new OnClickListener()    {
            @Override
            public void onClick(View arg0) {
                final ImageView myImage = (ImageView)findViewById(R.id.img_View);
                final Animation myRotation = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.rotator);
                myImage.startAnimation(myRotation);
            }
        });

    }
}



 
Read more...

Monday 19 January 2015

Get a device ID for Android

This example explains how to get android device unique ID. The device unique id is needed when we want user registration for specific device. This way we can achieve security.

Android Activity Layout  (activity_main.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=".MainActivity" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:textColor="#cc0000"
        android:textSize="32sp" />

</RelativeLayout>
Android Activity (AndroidActivity.java)
package com.example.getdeviceid;

import android.app.Activity;
import android.os.Bundle;
import android.provider.Settings.Secure;
import android.widget.TextView;

public class MainActivity extends Activity {

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

  //getting unique id for device
  String id = Secure.getString(getContentResolver(), Secure.ANDROID_ID);

  //displaying id in textview
  TextView tv = (TextView) findViewById(R.id.textView1);
  tv.setText(id);

 }

}
Output
Get Device ID Example in Android





Read more...

Play YouTubePlayerVideo in VideoView using YouTube API

Table of Contents

  1. Downloading YouTubePlayer API
  2. Register your app in Google Developer Console
  3. Create a new Android application project
  4. Define your activity layout
  5. Initialize YouTubePlayerView in your acitvity
  6. Output
  7. Reference
In this tutorial we'll see how to use YouTubePlayer API in Android. YouTube player API provides an embedded view that supports with various playback controls.

1. Downloading YouTubePlayer API
Before you start writing some code, you'll have to first download the YouTube Android Player API. Once downloaded import the jar into your project lib folder and add the jar to your build path.

YouTube Android Player API
2. Register your app in Google Developer Console
  • Before you start using the Youtube player API for Android, you have to register your application with Google Developer Console and get the API key. The API key is a unique value for your application. Do not disclose it to others.
  • Visit Google APIs Console and login with your Google account and create a new project.
  • Once your project is created, go to API's and enable "YouTube data API V3"
  • And the select  "API Access" in your API Console. You'll find API key. This API key will be used to access YouTube Android Player APIs.
YouTube Android Player API2
3. Create a new Android Application project

Now you are ready to create a new Android project to play Youtube videos using YouTube player API. You may use IDE of your choice, here I am using Eclipse.

You need INTERNET permission to play video over internet. Add below permission in your application AndroidManifest.xml file.

<uses-permission android:name="android.permission.INTERNET" />
4. Define your activity layout

In this example, we will create an sample layout that uses YouTubePlayerView inside an LinearLayout.
<LinearLayout 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"
    android:orientation="vertical" >

    <com.google.android.youtube.player.YouTubePlayerView
        android:id="@+id/youtube_player"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#fff"
        android:padding="5dp" />

</LinearLayout>
5. Initialize YouTubePlayerView in you Activity

Initializing YouTubePlayerView by calling initialize method. Once the initialize in successful, you will get the onInitializationSuccess() callback with YouTubePlayer instance, You can add additional listenersto handle the playback controls.
package com.example.youtubeplayer;

import android.os.Bundle;
import android.widget.Toast;
import com.google.android.youtube.player.YouTubeBaseActivity;
import com.google.android.youtube.player.YouTubeInitializationResult;
import com.google.android.youtube.player.YouTubePlayer;
import com.google.android.youtube.player.YouTubePlayer.ErrorReason;
import com.google.android.youtube.player.YouTubePlayer.PlaybackEventListener;
import com.google.android.youtube.player.YouTubePlayer.PlayerStateChangeListener;
import com.google.android.youtube.player.YouTubePlayer.Provider;
import com.google.android.youtube.player.YouTubePlayerView;
import com.javatechig.youtubeandroid.R;

public class MainActivity extends YouTubeBaseActivity implements YouTubePlayer.OnInitializedListener {

 public static final String API_KEY = "YOUR API KEY";

 //http://youtu.be/<VIDEO_ID>
 public static final String VIDEO_ID = "dKLftgvYsVU";

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

  /** attaching layout xml **/
  setContentView(R.layout.activity_main);

  /** Initializing YouTube player view **/
  YouTubePlayerView youTubePlayerView = (YouTubePlayerView) findViewById(R.id.youtube_player);
  youTubePlayerView.initialize(API_KEY, this);

 }

 @Override
 public void onInitializationFailure(Provider provider, YouTubeInitializationResult result) {
  Toast.makeText(this, "Failured to Initialize!", Toast.LENGTH_LONG).show();
 }

 @Override
 public void onInitializationSuccess(Provider provider, YouTubePlayer player, boolean wasRestored) {

  /** add listeners to YouTubePlayer instance **/
  player.setPlayerStateChangeListener(playerStateChangeListener);
  player.setPlaybackEventListener(playbackEventListener);

  /** Start buffering **/
  if (!wasRestored) {
   player.cueVideo(VIDEO_ID);
  }
 }

 private PlaybackEventListener playbackEventListener = new PlaybackEventListener() {

  @Override
  public void onBuffering(boolean arg0) {

  }

  @Override
  public void onPaused() {

  }

  @Override
  public void onPlaying() {

  }

  @Override
  public void onSeekTo(int arg0) {

  }

  @Override
  public void onStopped() {

  }

 };

 private PlayerStateChangeListener playerStateChangeListener = new PlayerStateChangeListener() {

  @Override
  public void onAdStarted() {

  }

  @Override
  public void onError(ErrorReason arg0) {

  }

  @Override
  public void onLoaded(String arg0) {

  }

  @Override
  public void onLoading() {
  }

  @Override
  public void onVideoEnded() {

  }

  @Override
  public void onVideoStarted() {

  }
 };
}
6. Output

YouTube Android Player API Example - Javatechig

7. References
Read more...