Search This Blog

Thursday, August 29, 2013

Run the same AsyncTask after completing itself.

Create an object of the task and execute the same task as following.

UpdateCoin updatecoin = new UpdateCoin();

                    if (updatecoin.getStatus() == AsyncTask.Status.PENDING) {

                    }

                    if (updatecoin.getStatus() == AsyncTask.Status.RUNNING) {

                    }

                    if (updatecoin.getStatus() == AsyncTask.Status.FINISHED) {

                        Runnable myRunner = new Runnable() {
                            public void run() {
                                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
                                    new UpdateCoin().executeOnExecutor(
                                            AsyncTask.THREAD_POOL_EXECUTOR, coin,
                                            LevelUpBonus[level - 1]);
                                } else {
                                    new UpdateCoin().executeOnExecutor(
                                            AsyncTask.THREAD_POOL_EXECUTOR, coin,
                                            LevelUpBonus[level - 1]);
                                }
                            }
                        };
                        myHandler.post(myRunner);
                    }

Tuesday, July 16, 2013

Ecxecute two Different AsyncTask parallel in android

If you want to ecxecute the two different asynctask to be ecxecute parallelly then you shoul use the second async task ecxecution like below.  UpdateCoin is the Async task.         

   if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
new UpdateCoin().executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR,
mPref.getInt("UserCoin", 0), CalculatedCoin);
} else {
new UpdateCoin().execute(mPref.getInt("UserCoin", 0), CalculatedCoin);
}

Tuesday, June 4, 2013

Sorting the ArrayList by Name and Value in android

Sorting the ArrayList by String comparison by the Following code.

Collections.sort(name, new Comparator<YOURARRAYLISTNAME>() {
    @Override
    public int compare(Invitation lhs, Invitation rhs) {
     return lhs.getName().compareToIgnoreCase(rhs.getName());
    }
   });


You can also compare Arraylist by INTEGER value by following code:


Collections.sort(value, new Comparator<YOURARRAYLISTNAME>() {
    @Override
    public int compare(Invitation lhs, Invitation rhs) {
                        return (lhs.expenditure < rhs.expenditure ? 1 : -1);
    }
   });

Thursday, May 23, 2013

Validate the String for the Email


Here is the method for checking the String is email address or not.


private boolean checkemail(String emailstr) {
// TODO Auto-generated method stub
boolean isValid = false;
   String expression = "^[\\w\\.-]+@([\\w\\-]+\\.)+[A-Z]{2,4}$";
   CharSequence inputStr = emailstr;
   Pattern pattern = Pattern.compile(expression, Pattern.CASE_INSENSITIVE);
   Matcher matcher = pattern.matcher(inputStr);
   if (matcher.matches()) {
       isValid = true;
   }
   return isValid;

}

For validate the String use following:

if(checkemail(emailstr)){
  // Here do with the Email string
}else{
  System.out.println("email String is not valid");
}

Thursday, May 9, 2013

Get the number of times app is used




 private SharedPreferences mPref;
public static final String PREFS_NAME = "YourPrefName";
int c;

in onCreate() write the following



  mPref = getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE);
c = mPref.getInt("numRun", 0);
Log.i("TAG", "Number of count of open App:" + c);
c++;
mPref.edit().putInt("numRun", c).commit();
Log.i("TAG", "Number of count after commit of open App:" + c);

Saturday, April 13, 2013

Count Down Timer in android

Android provide a android.os.CountDownTimer. By new a CountDownTimer object, and overriding the call-back function onTick() and onFinish(), you can implement a count down timer easily.

public class CountDownTimerActivity extends Activity {

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

                final TextView myCounter = (TextView)findViewById(R.id.mycounter);
                new CountDownTimer(30000, 1000) {

                                        @Override
                                        public void onFinish() {
                                                         // TODO Auto-generated method stub
                                                         myCounter.setText("completed Timer!");
                                        }

                                       @Override
                                        public void onTick(long millisUntilFinished) {
                                                    // TODO Auto-generated method stub
                                                   myCounter.setText("Millisecond Until Finished: " + String.valueOf(millisUntilFinished));
                                       }

                   }.start(); 
       } 
}

Xml file will be below.


<?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" />
<TextView
android:id="@+id/mycounter"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />

</LinearLayout>


Sunday, March 31, 2013

DialogFragment

android.app.DialogFragment, was introduced from API Level 11 (Android 3.0), displays a dialog window, floating on top of its activity's window.


public class MyDialogFragment extends DialogFragment {

            static MyDialogFragment newInstance() {

            String title = "My Fragment";

            MyDialogFragment f = new MyDialogFragment();
            Bundle args = new Bundle();
            args.putString("title", title);
            f.setArguments(args);
            return f;
            }

            @Override
            public Dialog onCreateDialog(Bundle savedInstanceState) {
                            String title = getArguments().getString("title");
                            Dialog myDialog = new AlertDialog.Builder(getActivity())
                                                   .setIcon(R.drawable.ic_launcher)
                                                   .setTitle(title)
                                                   .setPositiveButton("OK", 

                                                      new DialogInterface.OnClickListener() {
                     @Override
                     public void onClick(DialogInterface dialog, int which) {
                                      ((AndroidDialogFragmentActivity)getActivity()).okClicked();
                    }
                })
                                                  .setNegativeButton("Cancel",
new DialogInterface.OnClickListener() {

                   @Override
                   public void onClick(DialogInterface dialog, int which) {
                                      ((AndroidDialogFragmentActivity)getActivity()).cancelClicked();
                   }
               })
              .create();

return myDialog;
}

}


in main.xml modify the following code.


<?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/opendialog"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Open Dialog" />

</LinearLayout>


now in your activity just change the following code and see the dialog

public class DialogFragmentActivity extends Activity {
                          /** Called when the activity is first created. */
                         @Override
                         public void onCreate(Bundle savedInstanceState) {
                                      super.onCreate(savedInstanceState);
                                      setContentView(R.layout.main);


                                Button buttonOpenDialog = (Button)findViewById(R.id.opendialog);
                                buttonOpenDialog.setOnClickListener(new Button.OnClickListener(){

                               @Override
                               public void onClick(View arg0) {
                                                  OpenDialog();
                                }});

                  }

                  void OpenDialog(){
                         MyDialogFragment myDialogFragment = MyDialogFragment.newInstance();
                         myDialogFragment.show(getFragmentManager(), "myDialogFragment");
                  }

                 public void okClicked() {
                          Toast.makeText(AndroidDialogFragmentActivity.this,
                                           "OK Clicked!",
                                           Toast.LENGTH_LONG).show();
                 }

                 public void cancelClicked() {
                                  Toast.makeText(AndroidDialogFragmentActivity.this,
                                                 "Cancel Clicked!",  Toast.LENGTH_LONG).show();
                  }
}