Tuesday 21 July 2015

Simple way to add animation on Text View


Here is the way to add animation on text view.

create "anim" folder inside res folder.

add text_anim.xml file in it and paste below code -

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
   android:interpolator="@android:anim/linear_interpolator">
   <scale
       android:fromXScale="1.0"
       android:toXScale="0.8"
       android:fromYScale="1.0"
       android:toYScale="1.2"
       android:pivotX="50%"
       android:pivotY="50%"
       android:duration="100"
       android:repeatCount="4"
       android:repeatMode="reverse" />
</set>

now here in your activity class -

TextView tv = (TextView)findViewById(R.id.text);

Animation a = AnimationUtils.loadAnimation(context, R.anim.text_anim);;
tv.startAnimation(a);

Simple Enjoy this cool animation.

Thursday 28 May 2015

Good way to Cancel AsynTask


First Lets have a class like LoginTask

class LoginTask extends AsyncTask<String, Void, String> {

private LoginTask loginTask;

@Override
        protected Void doInBackground(String... unused) {

              if (isCancelled())
                {
                    //retun from here
                    return null
                }

// Do your task you want just add this line
}


  @Override
        protected void onPostExecute(String result) {

        }  


}


// Here let say Your main Activity from where you want to cancel your lask

public class LoginActivity extends Activity {

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

// Start login task here ok


loginTask = new LoginTask();
            loginTask.execute();
}

//Cancell task here
public void cancelTask(){
//Just check the condition like its Running,Finished or Pending
//import Status as like AsynkTask.Status and cancel the task if it is running than it will call cancel and //you will return to post execute with null value
if (loginTask != null && loginTask.getStatus() != Status.FINISHED)
            loginTask.cancel(true);
}

@Override
onBackPressed(){// Let say you want to cancel task on back press

cancelTask();

}


}