Tuesday 10 February 2015

How to create the Customized Splash Screen in Android


1.      Create a customized splash screen layout

      a.       Layout having the TextView with the splash screen title
      b.      ImageView having the Loading image


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
Layout : main.xml
<? xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:background="#fff"
    android:gravity="center"
    android:layout_height="fill_parent">
   
 <TextView
android:id="@+id/Title"
               android:layout_width="wrap_content"
               android:layout_height="wrap_content"
               android:padding="10dp"
               android:textSize="30sp"
               android:textColor="#cd6000"
               android:textStyle="bold"
               android:text="My Splash Page" />
              
 <ImageView
android:id="@+id/Loading"
               android:layout_width="wrap_content"
               android:layout_height="wrap_content"
               android:src="@drawable/loading" />
</LinearLayout>

2.      Create a rotate.xml in a drawable folder for need of  loading animation

<?xml version="1.0" encoding="UTF-8"?>
<rotate
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:fromDegrees="0"
    android:toDegrees="360"
    android:pivotX="50%"
    android:pivotY="50%"
    android:repeatCount="infinite"
    android:interpolator="@android:anim/linear_interpolator"   
    android:duration="1000" />

3.      Source SplashScreen.java

       a.  Referencing the xml widgets in to code.
 b.  Create a Animation and start load the animation to the ImageView.
       c.     Using the handler class and runnable thread make postdelay for 1000 milliseconds   and clear the animation then start the second activity.


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
package com.app;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;
import android.widget.TextView;

/**
 * @author Rajendhiran. E
 * Feb 10, 2013 11:10:52 PM
 */

public class SplashScreen extends Activity
{
   TextView Title;
   ImageView Loading;
   Animation anim;
   Handler h;
  
   protected void onCreate(Bundle savedInstanceState)
   {    
         super.onCreate(savedInstanceState);
         setContentView(R.layout.main);
         init();
         process();
   }
  
/* @author Rajendhiran. E, Feb 10, 2013 11:14:24 PM  */ 

   private void init()
   {
         Title = (TextView) findViewById(R.id.Title);
         Loading = (ImageView) findViewById(R.id.Loading);
         anim = AnimationUtils.loadAnimation(SplashScreen.this, 
R.drawable.rotate);
         Loading.startAnimation(anim);
         h = new Handler();
   }    

   /* @author Rajendhiran. E, Feb 10, 2013 11:19:35 PM */        
   private void process()
   {
         h.postDelayed(new  Runnable()
         {
               public void run()
               {                      
                     Loading.clearAnimation();
                     finish();
startActivity(new Intent(SplashScreen.this,SecondActivity.class));
               }
         }, 1000);        
   }    
}
Source Code: SplashScreen.zip

Tuesday 3 February 2015

Customized Count down timer


      CountDownTimer is a predefined class, which help us in the timer related activities, such as showing up the  reducing of time seconds, that we have seen while playing games or doing some timer related stuffs to our apps etc… its easy to develop just passing the milliseconds as parameters and it throws as our customized output as (HH:MM:SS) etc… 

1.     Design a layout with a counter start Button and TextView to display the time

1:  <?xml version="1.0" encoding="utf-8"?>  
2:  <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
3:    android:orientation="vertical"  
4:    android:layout_width="fill_parent"  
5:    android:layout_height="fill_parent"  
6:    >  
7:  <TextView   
8:    android:id="@+id/countdown"  
9:    android:layout_width="fill_parent"   
10:    android:layout_height="wrap_content"   
11:    android:text="00:00:00"  
12:    />  
13:   <Button  
14:     android:id="@+id/TimeBtn"  
15:     android:layout_width="fill_parent"  
16:     android:layout_height="wrap_content"  
17:     android:text="Count down Start!" />  
18:  </LinearLayout>  

2.      POCApp.java with counter down timer

1:  package com.app.poc;  
2:  import java.text.SimpleDateFormat;  
3:  import java.util.TimeZone;  
4:  import android.app.Activity;  
5:  import android.os.Bundle;  
6:  import android.os.CountDownTimer;  
7:  import android.util.Log;  
8:  import android.view.View;  
9:  import android.widget.Button;  
10:  import android.widget.TextView;  
11:  public class POCApp extends Activity   
12:  {  
13:    public Button TimeBtn;  
14:    public TextView Time;  
15:    MyCounter timer;  
16:    public void onCreate(Bundle savedInstanceState)   
17:    public void onCreate(Bundle savedInstanceState)   
18:    {  
19:      super.onCreate(savedInstanceState);  
20:      setContentView(R.layout.main);  
21:      init();  
22:      process();  
23:    }  
24:    private void process()   
25:    {  
26:      TimeBtn.setOnClickListener(new View.OnClickListener()  
27:      {  
28:        public void onClick(View arg0)   
29:        {      
30:          if(timer!=null)  
31:           timer.cancel();  
32:          timer = new MyCounter(3800*1000,1000);  
33:          timer.start();                          
34:          //Toast.makeText(POCApp.this, disHour+":"+disMinu+":"+disSec, Toast.LENGTH_LONG).show();  
35:        }  
36:      });  
37:    }  
38:    /**  
39:     *  @author Rajendhiran. E  
40:     *  Jan 16, 20134:46:24 PM  
41:     */  
42:    private void init()   
43:    {  
44:    TimeBtn = (Button) findViewById(R.id.TimeBtn);  
45:      Time = (TextView) findViewById(R.id.countdown);  
46:    }  
47:    /**  
48:     *  @author Rajendhiran. E  
49:     *  Jan 16, 20134:46:25 PM  
50:     */  
51:    class MyCounter extends CountDownTimer  
52:    {  
53:      SimpleDateFormat mSimpleDateFormat;  
54:      public MyCounter(long millisInFuture, long countDownInterval)   
55:      {  
56:        super(millisInFuture, countDownInterval);  
57:        mSimpleDateFormat= new SimpleDateFormat("HH:mm:ss");  
58:        mSimpleDateFormat.setTimeZone(TimeZone.getTimeZone("UTC"));  
59:      }  
60:      @Override  
61:      public void onFinish()   
62:      {  
63:       Log.d("Timer Completed: ","Completed!");  
64:        Time.setText("00:00:00");        
65:      }  
66:      @Override  
67:      public void onTick(long millisUntilFinished)  
68:      {  
69:       Time.setText(mSimpleDateFormat.format(millisUntilFinished));      
70:      }  
71:     }