Monday, June 4, 2012

Long Countdown Timer for Android

I will be showing you how to create a long countdown timer for Google Android development.

Place this in your onCreate method for the activity with the countdown. In your layout, create a TextView and give it an id of 'txtTimer'.
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Date date = new Date(115,4,26,12,0,0);
        long dtMili = System.currentTimeMillis();
        Date dateNow = new Date(dtMili);
        long remain = date.getTime() - dateNow.getTime();

        new CountDownTimer(remain, 1000)
        {
            @Override
            public void onFinish()
            {
                //Action for when the timer has finished.
                TextView tv = (TextView) findViewById(R.id.txtTimer);
                tv.setText("Timer has Finished");
            }

            @Override
            public void onTick(long millisUntilFinished)
            {
                //Action for every tick of the countdown.
                TextView tv = (TextView)findViewById(R.id.txtTimer);
                tv.setText(timeCalculate(millisUntilFinished/1000) + " Countdown");
            }
        }.start();
    }  


First off we need to implement Androids CountDownTimer. The first parameter is the amount of milliseconds it will take to finish and the second parameter is the interval to countdown at (1000 represents 1 second).


Set the variable 'date' to the Date you want the countdown to end at. Keep in mind Java starts at year 1900, the month January starts at 0 and the hours are military time. In the following example I'm making a countdown to May 26, 2012 at 12PM.




Next, I created a separate method that converts long milliseconds into a string output. Credit to the Stack Overflow question for the following code. http://stackoverflow.com/questions/3749297/countdown-timer-in-android
   public String timeCalculate(long ttime)   
   {  
     long days, hours, minutes, seconds;  
     String daysT = "", restT = "";  
   
     days = (Math.round(ttime) / 86400);  
     hours = (Math.round(ttime) / 3600) - (days * 24);  
     minutes = (Math.round(ttime) / 60) - (days * 1440) - (hours * 60);  
     seconds = Math.round(ttime) % 60;  
   
     if(days==1) daysT = String.format("%d day ", days);  
     if(days>1) daysT = String.format("%d days ", days);  
   
     restT = String.format("%02d:%02d:%02d", hours, minutes, seconds);  
   
     return daysT + restT;  
   }  

Friday, April 27, 2012

Running Python from Command Prompt

Want to run Python on Windows from command prompt? This is a tutorial to set it up. Basically what we will be doing is adding in the Path of the install location of Python to the Windows Environment for command prompt to locate it.

Step 1:

Download Python, As of this writing, version 2.7 is the stable version of Python, so I would recommend downloading that based whether your computer is 32x or 64x.

Once the file has been downloaded, run the installer. Once complete, move to Step 2.

Step 2:

Now to set the PATH in the Windows environment.

    Windows XP:

    - Right click "My Computer" and select Properties.


      - Click on the "Advanced" tab at the top and select "Environment Variables" button.


        - In the list titled "System Variables" scroll down until you see the variable "Path". Select the line and press the "Edit" button underneath. In the pop-up window, click on the second textbox called "Variable value:"  and go to the end of the text. Add the install path directory, if its the default path add this text to it: ";C:\Python27". (Don't forget the delimiter ';' before the path.) Then click "OK"


 

    Windows Vista/7:

     - Click the Start button and right click on Computer. Now click properties in the dropdown as seen                                                       below.


- In the System Settings, press the "Advanced System Settings" link to the left. In the following pop-up press the "Environment Variables" button. Next, in the Environment pop-up, use the second list to find the variable called "Path", select that and press the edit button. Now as seen below, Edit the Variable value by adding the install path directory, if its the default path add this text to it: ";C:\Python27" to the end of the textbox. (Don't forget the delimiter ';' before the path.) Click "Ok" when complete.





Step 3:
Now we are able to run Python from command prompt.
First, open up command prompt:
Windows XP: Click Start and use "Run", Type "cmd" and hit enter.
Windows Vista/7: Click start and type "cmd" and hit enter.

Type in "python" into the window and you should get the following meaning its setup correctly. Now you can run python.