Pages

Showing posts with label time. Show all posts
Showing posts with label time. Show all posts

Sunday, May 1, 2016

Android Date and Time Controls

The Date-Time Controls in Android enable the user to pick date and time values and store them anywhere.
The date and time controls include:
1. DatePicker
2. Time Picker.
3. DatePickerDialog.
4. TimePickerDialog.
5. AnalogClock.
6. DigitalClock.
7. chronometer

DatePicker:

The DatePicker control enables the user to select a value of date only.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
android_orientation="vertical"
android_layout_width="fill_parent"
android_layout_height="fill_parent"
>
<TextView
android_layout_width="fill_parent"
android_layout_height="wrap_content"
android_id="@+id/txt"
/>
<DatePicker
android_layout_width="fill_parent"
android_layout_height="wrap_content"
android_id="@+id/datePick"
/>
<Button
android_layout_width="wrap_content"
android_layout_height="wrap_content"
android_id="@+id/btn"
android_text="Select Date"
/>
</LinearLayout>


final DatePicker dp=(DatePicker)findViewById(R.id.datePick);
Button btn=(Button)findViewById(R.id.btn);
btn.setOnClickListener(new OnClickListener()
{

public void onClick(View arg0) {
// TODO Auto-generated method stub

TextView txt=(TextView)findViewById(R.id.txt);
txt.setText("You selected "+dp.getDayOfMonth()+"/"+(dp.getMonth()+1)+"/"+dp.getYear());
}

}

);

Notice that we add 1 to the value of DatePicker.getMonth() because the months range is from 0 to 11.

If you want to set an initial selected date for the DatePicker you can use:
DatePicker.init(year, monthOfYear, dayOfMonth, onDateChangedListener);

If you want to capture the date as the date changes in the control you can make the activity implements the OnDateChangedListener interface and implements the onDateChanged method
Calendar cal=Calendar.getInstance(Locale.ENGLISH);
dp.init(cal.getTime().getYear()+1900, cal.getTime().getMonth(), cal.getTime().getDay(), this);

then

public void onDateChanged(DatePicker view, int year, int monthOfYear,
int dayOfMonth) {
// TODO Auto-generated method stub
TextView txt=(TextView)findViewById(R.id.txt);
txt.setText("You selected "+view.getDayOfMonth()+"/"+(view.getMonth()+1)+"/"+view.getYear());
}


TimePicker:
TimePicker is like DatePicker but it displays time instead of date, here’s how it looks like:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
android_orientation="vertical"
android_layout_width="fill_parent"
android_layout_height="fill_parent"
>
<TextView
android_layout_width="fill_parent"
android_layout_height="wrap_content"
android_id="@+id/txt"
/>
<TimePicker
android_layout_width="fill_parent"
android_layout_height="wrap_content"
android_id="@+id/TimePick"
/>
<Button
android_layout_width="wrap_content"
android_layout_height="wrap_content"
android_id="@+id/btn"
android_text="Select Date"
/>
</LinearLayout>


final TimePicker tp=(TimePicker)findViewById(R.id.TimePick);
Button btn=(Button)findViewById(R.id.btn);
btn.setOnClickListener(new OnClickListener()
{

public void onClick(View arg0) {
// TODO Auto-generated method stub

TextView txt=(TextView)findViewById(R.id.txt);
txt.setText("You selected "+tp.getCurrentHour()+":"+tp.getCurrentMinute());

Notice that by default the time picker displays the time in AM/PM format. If you want it to display time in 24-hours format you can use
TimePicker.setIs24HourView(boolen is24HourView);
If you want to initialize the timepicker with a certain time you can use the methods:

TimePicker.setCurrentHour(int Hour);
TimePicker.setCurrentMinute(int Minute);

You can implement the OnTimeChangedListener so that you capture any change in the time picker:
final TimePicker tp=(TimePicker)findViewById(R.id.TimePick);
tp.setCurrentHour(10);
tp.setCurrentMinute(45);

tp.setOnTimeChangedListener(new OnTimeChangedListener()
{

public void onTimeChanged(TimePicker arg0, int arg1, int arg2) {
// TODO Auto-generated method stub
TextView txt=(TextView)findViewById(R.id.txt);
txt.setText("You selected "+arg0.getCurrentHour()+":"+arg0.getCurrentMinute());
}

}
);

We checked so far the DatePicker and TimePicker widgets, but they take big space on the screen so Android provides similar controls but with different look: the DatePickerDialog and TimePickerDialog.

These widgets act the same as DatePicker and TimePicker but the appear as dialogs or popups instead of occupying a space on the screen

DatePickerDialog
DatePickerDialog is not a View, that you can’t define it in the xml layout file.
Instead you declare it from code with the following two constructors:
DatePickerDialog(Context context, DatePickerDialog.OnDateSetListener callBack, int year, int monthOfYear, int dayOfMonth);
DatePickerDialog(Context context, int theme, DatePickerDialog.OnDateSetListener callBack, int year, int monthOfYear, int dayOfMonth);

You can use it like this:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button btn=(Button)findViewById(R.id.btn);

final OnDateSetListener odsl=new OnDateSetListener()
{

public void onDateSet(DatePicker arg0, int year, int month, int dayOfMonth) {
// TODO Auto-generated method stub
TextView txt=(TextView)findViewById(R.id.txt);
txt.setText("The date is "+dayOfMonth+"/"+month+"/"+year);
}

};

btn.setOnClickListener(new OnClickListener()
{

public void onClick(View arg0) {
// TODO Auto-generated method stub


Calendar cal=Calendar.getInstance();
DatePickerDialog datePickDiag=new DatePickerDialog(DateTimeControls.this,odsl,cal.get(Calendar.YEAR),cal.get(Calendar.MONTH),cal.get(Calendar.DAY_OF_MONTH));
datePickDiag.show();
}

}

);

}

To take an action when the date is set you define an OnDateSelectedListner and implement the onDateSet method

TimePickerDialog

DatePickerDialog is similar to DatePickerDialog but used for setting time.
The constructors for TimePickerDialog are:
TimePickerDialog(Context context, TimePickerDialog.OnTimeSetListener callBack, int hourOfDay, int minute, boolean is24HourView);

TimePickerDialog(Context context, int theme, TimePickerDialog.OnTimeSetListener callBack, int hourOfDay, int minute, boolean is24HourView);


You can use it like this:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button btn=(Button)findViewById(R.id.btn);
}

final OnTimeSetListener otsl=new OnTimeSetListener()
{

public void onTimeSet(TimePicker arg0, int hourOfDay, int minute) {
// TODO Auto-generated method stub
TextView txt=(TextView)findViewById(R.id.txt);
txt.setText("The time is "+hourOfDay+":"+minute);
}
};

btn.setOnClickListener(new OnClickListener()
{

public void onClick(View arg0) {
// TODO Auto-generated method stub


Calendar cal=Calendar.getInstance();
TimePickerDialog timePickDiag=new TimePickerDialog(DateTimeControls.this,otsl,cal.get(Calendar.HOUR_OF_DAY),cal.get(Calendar.MINUTE),true);
timePickDiag.show();
}

}

);




AnalogClock

If you want to display time as in a clock you can use the AnalogClock widget. It just displays the time with no ability to edit the time.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
android_orientation="vertical"
android_layout_width="fill_parent"
android_layout_height="fill_parent"
>
<TextView
android_layout_width="fill_parent"
android_layout_height="wrap_content"
android_id="@+id/txt"
/>

<AnalogClock
android_layout_width="wrap_content"
android_layout_height="wrap_content"
android_id="@+id/clock"
/>


</LinearLayout>






DigitalClock
Same as AnalogClock but displays a digital clock
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
android_orientation="vertical"
android_layout_width="fill_parent"
android_layout_height="fill_parent"
>
<TextView
android_layout_width="fill_parent"
android_layout_height="wrap_content"
android_id="@+id/txt"
/>

<DigitalClock
android_layout_width="wrap_content"
android_layout_height="wrap_content"
android_id="@+id/digitalClock"
/>


</LinearLayout>




ChronoMeter:

The ChronoMeter acts like a timer. It has a starting point and an endpoint and you can calculate the time elapsed between these two points.
Read More..

Saturday, April 16, 2016

Episode 30 Android Design Library

This time, Tor and Chet are joined by Chris Banes (again!) to talk about the new Android Design Library, which was released at Google I/O 2015. Listen in to find out all about CoordinatorLayout, FAB, Snackbar, and more, more, more!

Subscribe to the podcast feed or download the audio file directly.

Relevant Links:
Chriss blog
Android Design Library Demo
Android Design Support Library (Android Developers Blog)

Chris: google.com/+ChrisBanes
Tor: google.com/+TorNorbye
Chet: google.com/+ChetHaase

Read More..

Saturday, April 9, 2016

Options Menu Android Developer Tutorial

Almost every application will need a menu in order to facilitate a user to perform actions on the application. In Android there are three types of menus possible.

  1. Options Menu
  2. Context Menu
  3. Sub Menu

The Options menu is the one that appears when a user touches the menu button on the mobile. This is something that is associated with an activity.  In 3.0 and later, this is available in the Action Bar itself for quick access.

In this article, I am showing how to create an Options Menu for devices having Android 2.3 or below.

The Context Menu is a floating list of menu items that appears when a user touches and holds a particular item displayed in the view, which has a menu associated with it.

The Sub Menu is a floating list of menu items that appears when the user touches a menu item that contains a nested menu.

There are two ways of creating an Options Menu in your application. One is by instantiating the Menu class and the other is by inflating a Menu from an XML menu resource.  Based on best practices it is always better to define the Menu in an XML and inflate it in your code.

Now, let us start with the example.

I am going to just define 3 menu items in the XML. Inflate it in my code. And when a user clicks on any of the menu items, I just Toast a message on what has been clicked.

NOTE: This is as usual not a practically useful piece, but sticking to my style, I want to keep it as uncluttered and as simple as possible so that the learning happens easily. And the focus is only on what concept we are trying to learn.

So, here is my options_menu.xml that is created in the res/menu folder:

<?xml version="1.0" encoding="utf-8"?>
<menu >="http://schemas.android.com/apk/res/android">
      <item android:id="@+id/next"
              android:icon="@drawable/ic_next"
              android:title="@string/next" />
      <item android:id="@+id/previous"
            android:icon="@drawable/ic_previous"
            android:title="@string/previous" />
      <item android:id="@+id/list"
            android:icon="@drawable/ic_list"
            android:title="@string/list" /> 
</menu>

You see that the Menu root node consists of 3 item leaf nodes. Each of the items consists of an idicon and title. The resource id is unique to that item and it allows the application to recognize which item has been clicked by the user. The icon is a drawable that should exist in the res/drawable folder and is the one shown in the menu item. The string is the item’s title.

The above assumes that you have three images ic_next, ic_previous and ic_list copied into the drawable folder. It goes without saying that these image sizes should be kept as small as possible.
Once this is ready, we will create a class called ViewOptionsMenu. It’s onCreate(…) method would be a simple one calling the super method and displaying the content as shown below.

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

The main.xml just shows the message: “Click on the Options Menu to view the available Menu Options”.  This message as per the norm is defined in the strings.xml file that exists in the res/values folder. Here are the contents of the main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout >="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
<TextView 
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="@string/welcome"
    android:textSize="20sp" android:textStyle="bold" android:capitalize="none" android:typeface="sans"/>
</LinearLayout>

Now, I need to override the method:  onCreateOptionsMenu(Menu menu). This method is called by Android the first time the activity is loaded. This is so for Android 2.3 and below.  Here is the code:

    public boolean onCreateOptionsMenu(Menu menu) {
      MenuInflater inflater = getMenuInflater();
      inflater.inflate(R.menu.options_menu, menu);
      return true;
    }

This method is getting a handle to the MenuInflater and using it to inflate the options menu that we have defined earlier in options_menu.xml in the res/menu folder.  That is it. The Menu is created. Isn’t is so simple?

Now, that the menu is created, how do we respond to the user when he clicks on the menu. This is done by overriding the onOptionsItemSelected(MenuItem item) method in the Activity itself as shown below:

public boolean onOptionsItemSelected(MenuItem item) {
      switch (item.getItemId()) {
      case R.id.next:
            Toast.makeText(this"You have chosen the " + getResources().getString(R.string.next) + " menu option",
Read More..

Friday, March 25, 2016

Android Eclipse LogCat showing a singe Line only at a time

The LogCat shows different device events. In Eclipse sometimes you notice that only a single line is shown and you cant scroll to see previous events. This happens because the LogCat window gets full, so clearing it will give the window a new emty space to display the log events as usual.
Read More..

Monday, March 14, 2016

Plants VS Zombies 2 Its About Time Android Full Version

Plants VS Zombies 2: It's About Time. Android Full Version




Plants VS Zombies 2 Download Free

Best" Games of 2013" Collection on Google Play

The zombies are coming… back. It’s about time! The sequel to the hit action-strategy adventure brings the fun to tablets and touchscreens. Join Crazy Dave on a crazy adventure where you’ll meet, greet and defeat legions of zombies from the dawn of time to the end of days. Amass an army of powerful new plants, supercharge them with Plant Food and power up your defenses with amazing new ways to protect your brain. And that's just the beginning! The future holds many mysteries… also zombies. Lots and lots of zombies.

Best Mobile Game at E3 – Game Informer
Best Mobile Game 2013 – Mashable
Game of the Year 2013 – Slide to Play

Game Features
• Meet new plants that will defend your lawn through time
• Go toe-to-missing-toe with dozens of new zombies
• Supercharge your floral friends with Plant Food
• Fire up amazing Finger Powers to freeze, flick and zap zombies
• Defeat challenges that will test your zombie-zapping skills
• Take on zombies from all worlds in PiƱata Party and win prizes
• Collect coins to purchase potent power-ups
• Connect to Game Services to unlock achievements and compete against friends on the leaderboards
• Grow your Zen Garden and reap sweet rewards
• Look out! Zombie chickens!

Read More..

Sunday, February 28, 2016

Date and Time Picker Views Android Developer Tutorial Part 20

Continuing my tutorials on some UI related stuff… In Part 16 & 17, I spoke about Simple ListView and Custom ListView. In Part 18, though it seems like a tutorial on Threads and Handlers, I have touched upon ProgressDialog, another view.


Here I would like to talk on the DatePicker and TimePicker that are bundled with the SDK. Both are very similar. So, I will talk only about DatePicker, but the sample code will have both.

Typically, we would want an end user to be able to set a date though a DatePickerDialog that pops-up on some user action. So, I have a button “Set Date” on the click of which a DatePickerDialog pops-up. Once the user selects a date and “sets” it, it is displayed back in a TextView field.

So, the code associated with the button is:

pickDate.setOnClickListener( new OnClickListener() {
@Override
    public void onClick(View v) {
        showDialog(DATE_DIALOG_ID);
    }
});

showDialog(…) is a method available on an Activity Itself. It just takes an integer to help decide which dialog should actually be shown. This is decided in the onCreateDialog(…) method that gets automatically invoked when showDialog(…) is called.

Here is the code for the same:

protected Dialog onCreateDialog(int id){
    switch(id) {
        case DATE_DIALOG_ID:
            return new DatePickerDialog(this, dateListener, year, month, day);
        case ….
    }
    return null;
}

In this method, when the int passed is DATE_DIALOG_ID, a new DatePickerDialog is passed along with the values for year, month and day. Where did I set values for these? In the onCreate(…) method itself, I have done this:

final Calendar cal = Calendar.getInstance();
year = cal.get(Calendar.YEAR);
month = cal.get(Calendar.MONTH);
day = cal.get(Calendar.DAY_OF_MONTH);

Also note that a handle to a listener is expected to be given to the DatePickerDialog. I will come to this listener a little later.

I have also updated the Date - TextView with these values in the updateDate() method which is invoked in the onCreate(…) method itself.

private void updateTime() {
    timeDisplay.setText(new StringBuilder().append(hours).append(:)
    .append(min));
}

So far, what I have done is shown how to create a Dialog and set the date.

Once the dialog shows up, when a user sets the date and clicks ‘set’, the listener that is invoked in on the DatePickerDialog. The method is onDateSetListener whose handle is passed during the creation of the DatePickerDialog itself.

private DatePickerDialog.OnDateSetListener dateListener =
    new DatePickerDialog.OnDateSetListener() {
        @Override
        public void onDateSet(DatePicker view, int yr, int monthOfYear, int dayOfMonth) {
            year = yr;
            month = monthOfYear;
            day = dayOfMonth;
            updateDate();
     }
};

In this method, on the click on the dialog, we are setting the day, month, year values to the new values that the user selected and also calling the updateDate() method to refresh the TextView with what the user selected.

It is this simple.

Same thing can be done with the TimePicker as well. The only difference is that you can pass a Boolean variable to say whether you want the time in 24 hour or 12 hour pattern.

Here is the complete code for the same.



Read More..

Saturday, February 13, 2016

Card Wars Adventure Time 1 5 Free MOD Full Version Unlimited Coins Download

Card Wars Adventure Time 1.5 MOD Full Version APK

 

What’s In The MOD:
Unlimited coins
Unlimited Gems
Requires Android: 2.3 and Up
Credits: RoyalGamer
MODE: OFFLINE

DOWNLOAD LINKS

Click Here For Download Links
Read More..

Wednesday, February 10, 2016

Roller Coaster Tycoon 2 Expansion Packs Wacky Worlds Time Twister Full Free for PC



















RollerCoaster Tycoon 2 (or RCT2 for short) is the second installment in theRollerCoaster Tycoon series. Like in its predecessor, the player must build successful theme parks to fulfill a set of objectives in various scenarios. It had two expansion packs, Wacky Worlds and Time Twister, which only brought more themes and theme-inspired derivatives of existing rides and were not worked on by Chris Sawyer.


RollerCoaster Tycoon 2 uses the same game engine as the first game, but somewhat optimized to sport more polished graphics, higher resolutions and a more intelligent guest AI.




Read More..

Monday, March 10, 2014

2013 Year in review giving time back to developers

2013 was a busy year for Google Cloud Platform. Watch this space: each day, a different Googler who works on Cloud Platform will be sharing his or her highlight from the past year.



My highlight this year was bringing App Engine’s managed non-relational storage service, Datastore, to developers everywhere as Google Cloud Datastore. There are many use cases and applications where developers themselves want to manage the compute-side of the equation (lucky for them, we have world class VMs as well). That said, managing large distributed storage is no easy task, often times consuming precious development hours. For me, giving time back to developers by managing the complex aspects of a scalable service and, thus, allowing them to focus on creating amazing user experiences, is definitely a highlight of the year.



-Posted by Chris Ramsdale, Product Manager
Read More..