Browse Author: rohit.gujar

Drawable Animation

Drawable animation lets you load a series of Drawable resources one after another to create an animation. This is a traditional animation in the sense that it is created with a sequence of different images, played in order, like a roll of film. The AnimationDrawable class is the basis for Drawable animations.

While you can define the frames of an animation in your code, using the AnimationDrawable class API, it’s more simply accomplished with a single XML file that lists the frames that compose the animation. The XML file for this kind of animation belongs in the res/drawable/ directory of your Android project. In this case, the instructions are the order and duration for each frame of the animation.

The XML file consists of an <animation-list> element as the root node and a series of child <item> nodes that each define a frame: a drawable resource for the frame and the frame duration. Here’s an example XML file for a Drawable animation:

<animation-list xmlns:android=””
<item android:drawable=”@drawable/rocket_thrust1″ android:duration=”200″ />
<item android:drawable=”@drawable/rocket_thrust2″ android:duration=”200″ />
<item android:drawable=”@drawable/rocket_thrust3″ android:duration=”200″ />

This animation runs for just three frames. By setting the android:oneshot attribute of the list to true, it will cycle just once then stop and hold on the last frame. If it is set false then the animation will loop. With this XML saved as rocket_thrust.xml in the res/drawable/ directory of the project, it can be added as the background image to a View and then called to play. Here’s an example Activity, in which the animation is added to an ImageView and then animated when the screen is touched:

AnimationDrawable rocketAnimation;

public void onCreate(Bundle savedInstanceState) {

ImageView rocketImage = (ImageView) findViewById(;
rocketAnimation = (AnimationDrawable) rocketImage.getBackground();

public boolean onTouchEvent(MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN) {
return true;
return super.onTouchEvent(event);
It’s important to note that the start() method called on the AnimationDrawable cannot be called during the onCreate() method of your Activity, because the AnimationDrawable is not yet fully attached to the window. If you want to play the animation immediately, without requiring interaction, then you might want to call it from the onWindowFocusChanged() method in your Activity, which will get called when Android brings your window into focus.

Abstract class to get a address from current location in Android

public abstract class ReverseGeocoderTask extends AsyncTask<Void, Void, String> {
    private static final String TAG = "ReverseGeocoder";
    private String value = "Checking your location...";

    public abstract void onAddressFound(String address);

    private float mLat;
    private float mLng;
    private Activity mContext;

    public ReverseGeocoderTask(Activity context, Location location) {
        mContext = context;
        mLat = (float) location.getLatitude();
        mLng = (float) location.getLongitude();

    protected void onPreExecute() {
        value = "Searching for address...";

    protected String doInBackground(Void... params) {
        try {
            List<Address> addressList =
                    getFromLocation(mLat, mLng, 1);
            StringBuilder sb = new StringBuilder();
            for (Address addr : addressList) {
                int index = addr.getMaxAddressLineIndex();

            value = sb.toString();
//          Logger.logger("Address Address    " + value);
        } catch (Exception ex) {

            Utilities.showToast(mContext,"Oops! There was an error.Please try to search your location again.");
            value = "Type your location here...";
//            Log.e(TAG, "Geocoder exception: ", ex);
        return value;

    protected void onPostExecute(String location) {

    public static List<Address> getFromLocation(double lat, double lng, int maxResult) {

        List<Address> retList = null;
        String address = String.format(Locale.getDefault(), "$f,%2$f&language=" + Locale.getDefault(), lat, lng);
        URL myurl;
        try {
            myurl = new URL(address);
//            Logger.logger("myurl.......URl...=  ", myurl.toString());
            HttpURLConnection urlConnection = (HttpURLConnection) myurl.openConnection();
            InputStream is = urlConnection.getInputStream();
            if (is != null) {
                int responseCode = urlConnection.getResponseCode();
                if (responseCode == HttpURLConnection.HTTP_OK) {
                    BufferedReader responseReader = new BufferedReader(new InputStreamReader(is));
                    String responseLine;
                    StringBuilder stringBuilder = new StringBuilder();
                    BufferedReader reader = new BufferedReader(
                            new InputStreamReader(is));
                    while ((responseLine = responseReader.readLine()) != null) {

//                    Logger.logger("stringBuilder.......stringBuilder...=  ", stringBuilder.toString());
                    JSONObject jsonObject = new JSONObject();
                    jsonObject = new JSONObject(stringBuilder.toString());

                    retList = new ArrayList<>();

                    Address addr = new Address(Locale.getDefault());
                    if ("OK".equalsIgnoreCase(jsonObject.getString("status"))) {
                        JSONArray results = jsonObject.getJSONArray("results");
//                for (int i = 0; i < results.length(); i++) {
                        JSONObject result = results.getJSONObject(0);

                        String indiStr = result.getString("formatted_address");
//                        Logger.logger("indiStr   " + indiStr);

                        addr.setAddressLine(0, indiStr);

//                }
//                        Logger.logger("retList   " + retList);
                    } else {
//                        throw new IllegalStateException("Method failed: " + response.getStatusLine());
        } catch (IOException | JSONException e) {
        return retList;


Communication between Bluetooth devices

I had used this communication to get the surrounding temperature, but we can also use this to develop chat room which will be offline.

BluetoothSPP bt; //Bluetooth Socket

bt = new BluetoothSPP(this); //initilisation

// check weather Bluetooth is on/off


if (!bt.isBluetoothEnabled()) {

Intent intent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);

startActivityForResult(intent, BluetoothState.REQUEST_ENABLE_BT);


else {











Toast.makeText(getApplicationContext() , “Bluetooth is not available” , Toast.LENGTH_SHORT).show();



bt.setBluetoothConnectionListener(new BluetoothConnectionListener() {

public void onDeviceDisconnected() {

//do something if device get disconnected


public void onDeviceConnectionFailed() {

//do something if device connection get failed


public void onDeviceConnected(String name, String address) {

//  do something if device get connected

// name is the name of connected  device  and address is the unique is of bluetooth



bt.setOnDataReceivedListener(new OnDataReceivedListener()


public void onDataReceived(byte[] data,String message)


// when any message is get received