Saturday, 27 September 2014

Progress Dialog in Android AsyncTask

I will show how to use ProgressDialog class for showing progress dialog to indicate page is loading in android.
Please try it your self.

package com.j4android;

import android.app.Activity;
import android.os.Bundle;

public class MainActicity extends Activity{
ProgressDialog progressBar;
@Override
public void onCreate(Bundle bundle){
super.onCreate(bundle);
setContentView(R.layout.main);

            progressBar = ProgressDialog.show(MainActicity.this, "","Loading. Please wait...", true);
            new CallAsynClass().execute();
        }

       private class CallAsynClass extends AsyncTask<URL, Integer, Long> {
          protected Long doInBackground(URL... urls) {
            //do something
          }
          protected void onProgressUpdate(Integer... progress) {
            //do something
          }
          protected void onPostExecute(Long result) {
                progressBar.dismiss();
            //do something
          }
          protected void onPreExecute(){
                progressBar.show();

ProgressLoadingDelayTime();
    //do something
          }
       }
       public void ProgressLoadingDelayTime(){
      Handler handler = new Handler();
      handler.postDelayed(new Runnable() {
           @Override
    public void run() {
                 progressBar.dismiss();
            }
              }, 25000); 

       }
}

Wednesday, 24 September 2014

Split a String in Android

Simple example of how to split a string in android using Split() method. This method need Regular expression as it's parameter, Let see how to construct the code.

Please try it your self for your own practices .

package com.myandroidtutorial;

import android.app.Activity;
import android.os.Bundle;

public class SplitDemoActivity extends Activity{
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

String lang = "Java | C | C++ | DotNet | PHP";
String[] seperateLanguage = lang.toString().trim().split("\\|");
for (int i = 0; i < seperateLanguage.length; i++) {
System.out.println("Index["+i+"] = " + seperateLanguage[i].trim());
}
}
}

Thanks.

Friday, 19 September 2014

Android - Animation

Animation

Please try it  yourself

shake.xml:

<translate xmlns:android="http://schemas.android.com/apk/res/android"
    android:duration="1000"
    android:fromXDelta="0"
    android:interpolator="@anim/cycle_7"
    android:toXDelta="10" />

zoom_in.xml:

<scale  xmlns:android="http://schemas.android.com/apk/res/android"
  android:fromXScale="1" 
  android:toXScale="2" 
  android:fromYScale="1" 
  android:toYScale="2" 
  android:pivotX="50%" 
  android:pivotY="50%" 
  android:duration="1500" 
  android:fillAfter="true">
</scale>

rotate.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:duration="500"
        android:fromDegrees="0"
        android:pivotX="30%"
        android:pivotY="30%"
        android:repeatMode="restart"
        android:repeatCount="5"
        android:startOffset="0"
        android:toDegrees="360" 
        android:fillAfter="true"
        android:fillEnabled="true"
        android:interpolator="@android:anim/bounce_interpolator"/>

</set>

main.xml:



<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

            android:layout_width="match_parent"

            android:layout_height="match_parent"           
            android:orientation="vertical" >

            <EditText
                android:id="@+id/edtxt"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:hint="Android" >
            </EditText>

            <TextView
                android:id="@+id/txt"
                android:layout_width="wrap_content"

                android:layout_height="wrap_content"

                android:layout_gravity="center"

                android:gravity="center" />

          
        </LinearLayout>


Activity:

package com.myandroidtutorial;

public class MainActivity extends Activity {


TextView txt;

EditText edtxt;


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

                txt = (TextView)findViewById(R.id.txt);
edtxt = (EditText)findViewById(R.id.edtxt);

                Animation animation = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.shake);
                txt.startAnimation(animation);
                edtxt.startAnimation(animation);
        }
}



Friday, 12 September 2014

Android Toast Example

Please try it your self

//display in short period of time
Toast.makeText(getApplicationContext(), "msg msg", Toast.LENGTH_SHORT).show();
//display in long period of time
Toast.makeText(getApplicationContext(), "msg msg", Toast.LENGTH_LONG).show();

Android alert dialog example

Please try it your self

      AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(context);
         // set title
      alertDialogBuilder.setTitle("Alert!");
         // set dialog message
      alertDialogBuilder.setMessage("Type the Text")
      .setCancelable(false)
      .setPositiveButton("Yes"new DialogInterface.OnClickListener() {
          public void onClick(DialogInterface dialog,int id) {
             //do something
          }
      })
      .setNegativeButton("No",new DialogInterface.OnClickListener() {
          public void onClick(DialogInterface dialog,int id) {
              dialog.cancel();
          }
      });
       // create alert dialog
      AlertDialog alertDialog = alertDialogBuilder.create();
       // show it
      alertDialog.show();

     //alertDialog.setCanceledOnTouchOutside(true);

How to implement AsyncTask in Android applications?

Please try it your self.

How to implement AsyncTask in Android applications?

package com.j4android;

import android.app.Activity;
import android.os.Bundle;

public class MainActicity extends Activity{
@Override
public void onCreate(Bundle bundle){
super.onCreate(bundle);
setContentView(R.layout.main);
                new CallAsynClass().execute();
        }

       private class CallAsynClass extends AsyncTask<URL, Integer, Long> {
          protected Long doInBackground(URL... urls) {
            //do something
          }
          protected void onProgressUpdate(Integer... progress) {
            //do something
          }
          protected void onPostExecute(Long result) {
            //do something
          }
          protected void onPreExecute(){
    //do something
          }
       }
}