Showing posts with label Tutorials. Show all posts

How To Make Material Design App Bar/ActionBar and Style It


In this article and most of the coming articles we would get started with material design, Material design brings lot of new features to android and In this article we would see how to implement App Bar which is a special type of ToolBar (ActionBars are now called as App Bars) and how to add actions icons to App Bar

Before we start make sure you have these requirements fulfilled.
' 1. Android Studio 1.0.1 (Latest while writing the post)
2. Appcombat v7 Support library (To Support Pre Lollipop devices)
If you have the Android Studio 1.0.1 then you don't need to worry about Appcombat v7 support library as it comes with the compiled dependency of latest Appcombat support library. Before we start coding let's look at what are we trying to make.

App bar Material design
Lets Start,

1. Open Android Studio and create a new project and select a blank activity to start with.

2. If you are on the latest version of Android Studio you don't have to add a compiled dependency of Appcombat v7 21 if not then please make sure you add the line below in your gradel build dependencies.

view raw gistfile1.bsv hosted with ❤ by GitHub
3. Even though ActionBars are replaced by App bar/Toolbar we can still use ActionBar, but in our case we are going to make a Toolbar so go to your style.xml and change the theme to "Theme.AppCompat.Light.NoActionBar, This helps us get rid of the ActionBar so we can make a App bar.

color scheme material design4. Now lets talk about the color scheme for our project, as you can see from the image alongside, there are attributes which you can set to get a basic color scheme of your App done, right now we are just dealing we App bar so we would talk about colorPrimary and colorPrimaryDark. colorPrimary as the name says is the primary color of your App and the App bar while with the colorPrimaryDark you can set the color of the status bar to a certain color.

To do that you need make a file called color.xml in your values folder and add the color attributes as shown in the below code.


<?xml version="1.0" encoding="utf-8"?>
<resources>
    <color name="ColorPrimary">#FF5722</color>
    <color name="ColorPrimaryDark">#E64A19</color>
</resources>


And this is how the style.xml looks after adding the colors.

<resources>

    <!-- Base application theme. -->
    <style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">

        <item name="colorPrimary">@color/ColorPrimary</item>
        <item name="colorPrimaryDark">@color/ColorPrimaryDark</item>
        <!-- Customize your theme here. -->
    </style>

</resources>

5. Now lets make a Toolbar, Toolbar is just like any other layout which can be placed at any place in your UI. Now as the toolbar is going to be needed on every or most of the activities instead of making it in the activity_main.xml we would make a separate file called tool_bar.xml and include it in our activity this way we can include the same file on any activity we want our toolbar to appear.

Go to res folder in your project and create a new Layout Resource File and name it tool_bar.xml with the parent layout as android.support.v7.widget.Toolbar as shown in the image below.


6. now add the background color to your tool_bar as the primary color of the app and give an elevation of 4dp for the shadow effect, this is how the tool_bar.xml looks.


<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@color/ColorPrimary"
    android:elevation="4dp"

    >

</android.support.v7.widget.Toolbar>

7. Now let's include the toolbar we just made in our main_activity file, this is how the main_activiy.xml looks.


<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"

    tools:context=".MainActivity">

    <include
        android:id="@+id/tool_bar"
        layout="@layout/tool_bar"
        ></include>

    <TextView
        android:layout_below="@+id/tool_bar"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="@dimen/TextDimTop"
        android:text="@string/hello_world" />

</RelativeLayout>



At this point this is how the app looks like as below.

App bar

8. As you can see, the tool_bar layout is added, but it doesn't quite look like a Action Bar yet, that's because we have to declare the toolBar as the ActionBar in the code, to do that add the following code to the MainActivity.java, I have put comments to help you understand what's going on.


package com.example.hp1.materialtoolbar;

import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Toast;


public class MainActivity extends ActionBarActivity { /* When using Appcombat support library
                                                         you need to extend Main Activity to
                                                         ActionBarActivity.
                                                      */


    private Toolbar toolbar;                              // Declaring the Toolbar Object


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        toolbar = (Toolbar) findViewById(R.id.tool_bar); // Attaching the layout to the toolbar object
        setSupportActionBar(toolbar);                   // Setting toolbar as the ActionBar with setSupportActionBar() call

    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}



After that, this is how the ToolBar looks

app bar

9.  Notice that the name of the app "MaterialToolbar" is black as we have set the theme parent to Theme.AppCompat.Light.NoActionBar in step 3 it gives the dark text color, So if you want to set the name as light/white text then you can just add android:theme="@style/ThemeOverlay.AppCompat.Dark" in tool_bar.xml and you would get a light text color for the toolbar text, So finally tool_bar.xml looks like this.


<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@color/ColorPrimary"
    android:theme="@style/ThemeOverlay.AppCompat.Dark"
    android:elevation="4dp"

    >

</android.support.v7.widget.Toolbar>

10. All left is to show you how to add menu items like search icon and user icon, for the icons i use icons4android.com which is free to use and I have also mentioned it in my 5 tools every android developer must know Post. I have downloaded the icons in four sizes as recommended by the official Google design guideline and added them to the drawables folder. You can see the project structure from image below.

android studio structure


11. now I need to add search icon and user icons in the menu_main.xml as shown below


<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    tools:context=".MainActivity">
    <item
        android:id="@+id/action_settings"
        android:orderInCategory="100"
        android:title="@string/action_settings"
        app:showAsAction="never" />
    <item
        android:id="@+id/action_search"
        android:orderInCategory="200"
        android:title="Search"
        android:icon="@drawable/ic_search"
        app:showAsAction="ifRoom"
        ></item>
    <item
        android:id="@+id/action_user"
        android:orderInCategory="300"
        android:title="User"
        android:icon="@drawable/ic_user"
        app:showAsAction="ifRoom"></item>

</menu>


And now everything is done our App bar looks just how we wanted it to look.

App bar

So finally we have done and our App Bar looks just how we had seen above and as we have used the appcombat support library it is compatible on pre lollipop devices as well, hope so you liked the post, if you did please share and comment.
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:appcompat-v7:21.0.3'
}

How to Quickly Set Up Less.js

Less.js (or just Less) is a CSS preprocessor that can revolutionize the way you write CSS. And it’s easy to install and set up for web development.
There are several ways to install and configure Less, but for developing in the browser, or if you’re just interested in trying it out without having to install it on a web server, the fastest way is to reference the less.js library in an HTML document. Let me show you how.
Alternatively: If you use Git, fire up the CLI, navigate to your project’s directory, and then clone the Less repo to your computer:
git clone https://github.com/less/less.js.git
There’s a lot of files and directories inside the less.js-master directory when you open it but we’re only interested in what’s inside the dist directory (which in open source lingo is short for "distribution" directory, the files for production use).
The dist folder inside less.js is where the production files are located.
Inside the dist directory you’ll find two JavaScript files: less.js andless.min.js — you can use either one.
The less.js and less.min.js files inside the "dist" directory.
less.js is the commented version, which is great if you like reading source code.less.min.js is a minified version that has a smaller file size.
Put less.js or less.min.js in your project’s directory.
With your code editor or text editor:
  1. Create an HTML document.
  2. Create a Less stylesheet. It should have a file extension of .less. Example:styles.less.
In the <head> of your HTML document, reference your Less stylesheet and the Less JS file you placed in your project’s directory:
<head>
  <link href="styles.less" type="text/css" rel="stylesheet/less"/>
  <script src="less.js" type="text/javascript"></script>
</head>

Testing the Setup

You’re now ready to use Less.
To test your setup, you can write some Less syntax inside your Less stylesheet and then see if it renders correctly in your browser.
The Less CSS below uses Less variables and the Less saturation() anddesaturation() color functions.
HTML
<!DOCTYPE html>
<html>
<head>
<title>Less.js: Quick Setup</title>
<link href="styles.less" type="text/css" rel="stylesheet/less"/>
<script src="less.js" type="text/javascript"></script>
</head>
<body>
<h1>Less.js: Quick Setup</h1>
<p><a href="http://sixrevisions.com/tutorials/set-up-less-js/">Read the tutorial</a></p>
</body>
</html>
LESS
/* Variables */
@body-bg-color: #83b692;    // green
@text-color: #fff;          // white
@button-bg-color: #f9627d;  // pink

/* LESS CSS */
body {
  background: @body-bg-color;
  color: @text-color;
  font-family: sans-serif;
  text-align: center;
}
a:link, a:visited {
  background: @button-bg-color;
  color: @text-color;
  display: inline-block;
  padding: 10px 10px;
  text-decoration: none;
}
a:hover {
  background-color: desaturate(@button-bg-color, 50%);
}
a:active {
  background-color: saturate(@button-bg-color, 50%);
}
Result
A Less.js browser setup test page

In-browser Error Hints

By default, Less will warn you whenever it encounters errors in the web page. This is useful during web development.
Less errors

Compile Less CSS Before Deployment

Once development is complete, compile .less files into regular .css files. If you want to do this quick-and-dirty, you can use an online Less compiler.
The Less CSS above was compiled to the following by using LESSTESTER:
/* Variables */
/* LESS CSS */
body {
  background: #83b692;
  color: #ffffff;
  font-family: sans-serif;
  text-align: center;
}
a:link,
a:visited {
  background: #f9627d;
  color: #ffffff;
  display: inline-block;
  padding: 10px 10px;
  text-decoration: none;
}
a:hover {
  background-color: #d08b97;
}
a:active {
  background-color: #ff5c79;
}

Moving Forward with Less

Though the method described in this tutorial is the fastest way to get up and running with Less, it’s best used only for exploring, testing, and development because having the JavaScript library process your CSS every time a visitor requests your web page is bad for performance.
Once you’re ready to commit to Less and use it in your web development projects, the best options would be to install and set it up on the web server or to remove the less.js library and compile your Less CSS to normal CSS.

Designing the Perfect Hyperlink — It’s Not as Simple as You Think

Designing the Perfect Hyperlink — It's Not as Simple as You Think
Hyperlinks are the glue that holds the Web together. Without links, the Web would be a very different place, that’s if it would exist at all. Using a simple HTML element — the <a> element –you can create a bond with any other web page on the Internet. Hyperlinks are magical.
Hyperlinks are fundamental to the Web. They are always just there. Maybe that’s why many site owners and web designers don’t pay them the attention they deserve.
The design of the HTML <a> element is crucial in the user’s reading experience; we should take enough time to design them well.
I’m about to share with you some hyperlink design tips that will lead to a better user experience, enhanced web accessibility, and maybe even bring improvements to your search engine rankings.

Hyperlinks Need to Look Like Hyperlinks

All your hyperlinks need to stand out and clearly say to your readers, "Hey I’m a link. You can click on me."
Hyperlinks should appear interactable.
As web designers, we like to innovate and experiment with different navigation techniques, but sticking with certain design conventions is important.
One of the things that need to remain conventional is our hyperlinks.
According to a study in link readability, the regular Web user sees blue-and-underlined text as links.
Blue and underlined is a good standard to stick to, for no other reason than it’s what we Internet users have been acclimatized ourselves to.

Examples of Hyperlink Designs

Below you will see 3 different hyperlink designs. They are from top newspaper websites.
On the surface, these are all good hyperlink designs. They are some shade of blue. They stand out amongst the surrounding body of text.
But why is The New York Times hyperlink design better than the other two examples?
Allow me to explain.

A Simple Way to Test Your Hyperlink Design

Let me show you an easy method of testing if your hyperlinks clearly stand out from its surroundings.
If you blur and remove the color from the design, you will see what stands out if someone was quickly skimming or reading the page or if someone has particular problems with their vision such as low-vision or color blindness. (Read more aboutcolor testing tools.)
You can do this by:
Modifying your CSS property values for <a> and <p> elements to blur them and remove their colors
Taking a screenshot and editing it in Photoshop
  1. Image > Adjustments > Desaturate
  2. And then Filter > Blur > Gaussian Blur
Let’s look back to our earlier examples, but this time we are going to view them when they are blurred and in black and white.
Here is The Guardian’s; you can see that the hyperlink is hard to spot:
BBC uses a bold font weight to create emphasis on their hyperlinks, which is marginally better than The Guardian’s hyperlink design because it at least stands out a bit more.
With The NY Times, it’s still possible to work out where the link is.

The Problem with Underlining Links

Now here’s where it gets tricky.
Here is where hyperlink design gets a bit unsimple.
Here is where our convention of underlining links fail.
There is a study that shows that readability decreases when we underline the text in our hyperlinks.
The study says that underlined links have "seriously underestimated effects on the usability of Web pages."
The study reports that our current convention of underlining hyperlinks "can significantly reduce the readability of the text."
The researchers go as far as saying, "alternatives should be carefully considered for the design of future Web browsers."
Essentially, the researchers are saying that our current conventions for hyperlinks — underlined text — should be changed systemically.
The reason why underlined hyperlinks reduces legibility is that certain characters that go below the base line — characters with descenders extending below the underline such as p, g,  j, and q — are getting affected by the text-decoration: underline CSS property value.
Here is the default style of hyperlinks in the Google Chrome web browser (version 28):

What’s the Solution to This Readability Issue?

We can fix this readability issue ourselves. We don’t have to wait for a change in the way web browsers render underlined text by default.
How? We can use the CSS border-bottom property instead of the CSS text-decoration property to underline our hyperlink elements.
Using the border-bottom property can place the underline a few pixels below the affected characters, making the hyperlink easier to read.
Here is the CSS used for the image above:
a {
 text-decoration: none;
 padding-bottom: 3px;
 border-bottom: 1px solid blue;
}
Even more powerful than just fixing a readability issue, we can also control the underline’s style independently from the hyperlink text color, thereby decoupling these two components of a hyperlink.
For example, we can reduce the hyperlink underline’s distinctiveness to make the text more legible, or we can make it more distinctive to make the entire hyperlink design really stand out.
For the purpose of illustration, I changed the underline color just a little bit, making it a lighter shade of blue:
CSS:
a {
 text-decoration: none;
 padding-bottom: 1px;
 border-bottom: 1px solid #8d8df3;
}

Make Hyperlink Text Longer

This next concept I’m going to discuss goes a bit into content strategy territory (which is a big part of web design process).
Some of you might dislike this suggestion because it deals with the content creation process, and some of you might not have control over that part of the web development process.
The basis for this next tip I’m going to share is Fitts’s Law.
The concept of Fitts’s Law is simple. The law states that the larger something is, the easier it is to see and interact with.
That makes sense, especially in the context of touchscreen devices where the size of your elements matter, where the input device (our fingers) is less precise than a mouse pointer.
Using a finger to click on a hyperlink can be a pain; often times you will have to zoom in for small links, adding an additional barrier towards users getting the action they desire (which is to interact with the hyperlink).
But there is only so much we can do with the style of our links.
We can bold them, underline them, change their color.
How about making them bigger by changing their font size?
If we change the <a> element’s font-size property, it affects the reading flow, and can affect the consistency of our line-heights.
Look at how the continuity of the reading experience is disrupted by increasing the font size of hyperlinks:
So we can’t expand them vertically. We will need to expand them horizontally.

User-friendly SEO Benefits

Having longer anchor text is a user-friendly SEO tactic. That is, hyperlinks with longer link titles is better for users according to Fitts’s Law, but it also has the nice side benefit of being better for search engine rankings.
Anchor text should be descriptive and should tell the user and search engines what the page you are linking to is about, according to Google’s Search Engine Starter Guide.
Say you were writing about walls.
Compare the two ways a hyperlink is used in these sentences below:
"I would like to talk about advanced wall-building techniques. Click hereto learn how to build a basic wall because what I will talk about is beyond the basics."
Versus:
"I would like to talk about advanced wall-building techniques. You will need to learn how to build a basic wall because what I will talk about is beyond the basics."
Not only is the second version better for our user, but it is additionally better for search engines too because there is more context than the anchor text that just says "here".

Should Hyperlinks be Blue?

According to a study by Google blue links got more clicks than greenish-blue links.
The study I referenced earlier about underlined text readability likewise affirms that Web users immediately recognize links when they are blue and underlined.
However, in my opinion, not all hyperlinks absolutely need to be blue.
The important thing about hyperlink design is that your links are obviously links.
If you can achieve that with a different color other than the conventional blue color, go for it.
Microsoft Development Network (MSDN) supports this concept.
The fundamental guideline about designing hyperlinks "is users must be able torecognize links by visual inspection alone—they shouldn’t have to hover over an object or click it to determine if it is a link," according to their link design pattern guideline. They didn’t say anything about links needing to be blue.
There are some cases where blue-colored links aren’t the best option.
For example, if the background color makes it hard to read blue links, then usability and readability triumphs over the standard blue link convention.
Always do what is best for the user, even if that means breaking conventions.

Summary

Here are the big ideas:
  1. Designing hyperlinks should be well-thought-out.
  2. Blurring and removing color from the design is a quick way of demonstrating how well your links stand out.
  3. Underlined text is a strong and familiar convention. The problem with underlining text, though, is that readability decreases. The solution is to use CSS to remedy the issue.
  4. Using longer descriptive anchor text can improve usability (Fitts’s Law), with the added benefit of being better for search engines.
  5. The one thing that is important in the design of hyperlinks is this: hyperlinks should obviously look like hyperlinks.

Speed Up Your Web Development Workflow with Grunt

I’m going to help you get started with Grunt, an open source JavaScript task runner that will help automate some of your web development tasks. Grunt will speed up and improve your build process.
My goal with this Grunt tutorial is to get you to experience the same efficiency improvements I’ve gained through this awesome task runner.

What is Grunt?

When I started my job as a front-end web developer at CDNify (we’re a content delivery network aimed at web developers, startups, and digital agencies) I had absolutely no idea what Grunt was or how it could significantly improve my development workflow.
Months down the road, and I’m now using Grunt every single day.
I can’t imagine my front-end web development workflow without it now.
In short, Grunt is an open source JavaScript project that automates repetitive tasks you often have to do as a web developer.
With Grunt, you can automate tasks like minification, unit testing, and preparing your files and website assets for production use. Grunt does this by wrapping these processes up into tasks.
A few examples of things you can automate with Grunt:
  • Optimize your web images for speed and performance
  • Analyze your code for potential errors (often referred to as linting)
  • Combine your external resources for faster page load times
  • Enforce your coding style guides for uniformity and readability throughout your project’s code base
  • Compile your CSS from your preprocessor of choice (e.g. Sass and Less)
Anything you are doing over and over again is a candidate for Grunt.
You can configure Grunt to watch certain files for changes, and then build the results on the fly.

Using Grunt in Your Web Development Team

When used in a team environment, Grunt can help every person in that team write code that adheres to the same standards/style guides. Details such as controlling the indentation of code can now be a strict process, as a whole build will fail if any part throws an error.
For example, when indenting code, you could automatically enforce the use of spaces instead of tabs (or vice versa, depending on your preferences), which will ensure that the whole team has the same configuration. When a team member pushes code to the repository, it’s in the expected format.
Grunt will help you catch sloppy code such as missing semicolons, braceless control statements, unused variables, and trailing whitespace when used in conjunction with your favorite code-quality tools (e.g. JSHint) . This is excellent for discovering human errors as well as disallowing valid — but badly written — JavaScript.

What You Need to Know

To get the most out of Grunt, you should first know (or learn about) the following.

Command-line Interface

In order to use Grunt effectively, you will need to have a basic understanding of the command prompt/terminal. At the very least, you should know how to navigate to a directory on your system and run commands on through a CLI.
If you’re not comfortable with CLIs, read this tutorial first: Getting Started with Command-Line Interfaces.
Don’t be put off; you’ll see that a CLI is very simple to use once you actually start using it. The command line is a very powerful tool. Knowing how to use it effectively can speed up your development workflow in ways beyond just being able to use Grunt. It’s a worthwhile investment to learn about CLIs.

Optional: Version Control System

Although not technically required in order to use Grunt, Grunt works best in an environment where version control is being used.
Using Grunt for a small static website that is likely to be updated infrequently may be considered overkill. Any web development project that’s larger than that should be version-controlled any ways. Large-scale websites are where Grunt becomes critical and extremely useful.
Read this list of Git tutorials for beginners or this introductory guide to Git to help you get started with Git, a popular version control system.

Get Started with Grunt

I’ll run through the general steps of installing Grunt, which is a process that relies on Node.js (an open source development platform for network applications).

Install Node.js

Node.js home page
In order to install Grunt, Node.js must first be installed or available in your dev environment, which could your personal computer or web server.
If you need help with this, read this: How to Install Node.js.
Installing Node.js allows us to install Grunt using Node.js’s package manager, called npm.

Install Grunt

Next, you need to install Grunt and its dependencies. Just run this command:
npm install -g grunt

Using Grunt in a Web Development Project

Now that you have Grunt installed, let’s go over the basics of how to use it in a web development project.
To use Grunt in a web development project, we need two files: package.json and aGruntfile (e.g. Gruntfile.js).

package.json

package.json is a JSON file. This file should be located in your project’s root directory.
Project information and settings are specified in package.json, such as the project name, version, author, and if it’s a private project or not.
package.json also contains what are known in the Node.js nomenclature as devDependenciesdevDependencies are items you need for your project. In this sense, Grunt will be an item listed under devDependencies, along with the Grunt plugins you want to use for the project (I’ll talk about this later).
Here’s an example template for a package.json file:
{
 "name" : "Project Name",
 "version" : "version number",
 "author" : "Your Name",
 "private" : true,
"devDependencies" : { "grunt" : "~0.4.0" } }
By specifying the project’s dependencies in package.json, we can use npm to install them for us automatically simply by running the following command in our project’s directory:
npm install
Running that command will give us an output like this:
npm http GET https://registry.npmjs.org/grunt
 npm http 304 https://registry.npmjs.org/grunt
 npm http GET https://registry.npmjs.org/async
 npm http GET https://registry.npmjs.org/dateformat/1.0.2-1.2.3
 npm http GET https://registry.npmjs.org/colors
 npm http GET https://registry.npmjs.org/coffee-script
 npm http GET https://registry.npmjs.org/glob
 npm http GET https://registry.npmjs.org/iconv-lite
 npm http GET https://registry.npmjs.org/findup-sync
 npm http GET https://registry.npmjs.org/lodash
 npm http GET https://registry.npmjs.org/js-yaml
 npm http GET https://registry.npmjs.org/hooker
 npm http GET https://registry.npmjs.org/minimatch
 npm http GET https://registry.npmjs.org/nopt
 npm http GET https://registry.npmjs.org/which
 npm http GET https://registry.npmjs.org/rimraf
 npm http GET https://registry.npmjs.org/underscore.string
 npm http GET https://registry.npmjs.org/eventemitter2
 npm http 304 https://registry.npmjs.org/async
 npm http 304 https://registry.npmjs.org/dateformat/1.0.2-1.2.3
 npm http GET https://registry.npmjs.org/dateformat/-/dateformat-1.0.2-1.2.3.tgz
 npm http 304 https://registry.npmjs.org/glob
 npm http 304 https://registry.npmjs.org/colors
 npm http 304 https://registry.npmjs.org/iconv-lite
 npm http 304 https://registry.npmjs.org/findup-sync
 npm http 304 https://registry.npmjs.org/lodash
 npm http 304 https://registry.npmjs.org/js-yaml
 npm http 304 https://registry.npmjs.org/hooker
 npm http 304 https://registry.npmjs.org/minimatch
 npm http 304 https://registry.npmjs.org/which
 npm http 304 https://registry.npmjs.org/rimraf
 npm http 304 https://registry.npmjs.org/underscore.string
 npm http 200 https://registry.npmjs.org/coffee-script
 npm http 304 https://registry.npmjs.org/eventemitter2
 npm http 200 https://registry.npmjs.org/nopt
 npm http 200 https://registry.npmjs.org/dateformat/-/dateformat-1.0.2-1.2.3.tgz
 npm http GET https://registry.npmjs.org/graceful-fs
 npm http GET https://registry.npmjs.org/abbrev
 npm http GET https://registry.npmjs.org/sigmund
 npm http GET https://registry.npmjs.org/lru-cache
 npm http GET https://registry.npmjs.org/graceful-fs
 npm http GET https://registry.npmjs.org/inherits
 npm http GET https://registry.npmjs.org/argparse
 npm http GET https://registry.npmjs.org/esprima
 npm http 304 https://registry.npmjs.org/graceful-fs
 npm http 304 https://registry.npmjs.org/abbrev
 npm http 304 https://registry.npmjs.org/lru-cache
 npm http 304 https://registry.npmjs.org/graceful-fs
 npm http 304 https://registry.npmjs.org/sigmund
 npm http 304 https://registry.npmjs.org/inherits
 npm http 304 https://registry.npmjs.org/argparse
 npm http 304 https://registry.npmjs.org/esprima
 npm http GET https://registry.npmjs.org/underscore
 npm http 304 https://registry.npmjs.org/underscore
 grunt@0.4.1 node_modules/grunt
 ├── which@1.0.5
 ├── dateformat@1.0.2-1.2.3
 ├── colors@0.6.0-1
 ├── hooker@0.2.3
 ├── async@0.1.22
 ├── eventemitter2@0.4.12
 ├── coffee-script@1.3.3
 ├── underscore.string@2.2.1
 ├── iconv-lite@0.2.11
 ├── lodash@0.9.2
 ├── findup-sync@0.1.2 (lodash@1.0.1)
 ├── rimraf@2.0.3 (graceful-fs@1.1.14)
 ├── nopt@1.0.10 (abbrev@1.0.4)
 ├── minimatch@0.2.12 (sigmund@1.0.0, lru-cache@2.3.0)
 ├── glob@3.1.21 (inherits@1.0.0, graceful-fs@1.2.3)
 └── js-yaml@2.0.5 (esprima@1.0.3, argparse@0.1.15)

Gruntfile

The Gruntfile is the main configuration file for the project, and specifies what tasks Grunt should run and what files in the project they affect.
Your project’s Gruntfile can be a JavaScript file (Gruntfile.js) or CoffeeScript file (Gruntfile.coffee).
In this tutorial, we’ll be using JavaScript.
At its most basic form, the Gruntfile should contain the following:
module.exports = function(grunt){
 grunt.initConfig({
  pkg: grunt.file.readJSON('package.json')
 });
 
 grunt.registerTask('default', []);
};
initConfig is where the dependency options are specified. Each Grunt plugin is configured using a JSON object (with some exceptions).
Some plugins allow more than one configuration to be loaded. For example, there may be a specific set of experimental JavaScript that uses a different library to jQuery, and so that library must be predefined instead of jQuery in the JSHint configuration. (We will look at this in more detail in the third part of this Grunt tutorial series.)
registerTask can be specified more than once. The default task is run when Grunt is executed in the command line, and so this should contain common setup tasks.
At this point, if you run Grunt successfully, it will generate the following output:
Done, without errors.
Now we have a project skeleton which will become useful once we use some plugins.

Using Grunt Plugins

A key feature of Grunt is the use of Grunt plugins. Grunt plugins are referred to asgruntplugins in the Grunt nomenclature.
gruntplugins are user-contributed modules that will help you automate tasks without having to write your own task scripts.
For example, you can use the grunt-contrib-compress gruntplugin to compress and optimize the file sizes of your project files.
To use the plugin in a project, these are the steps to take:
  1. List it as a devDependency object in package.json
  2. Load it using the loadNpmTasks function in the project’s Gruntfile
  3. Register the task by using the registerTask function in the project’s Gruntfile
  4. Run npm install to install Grunt and the plugin
Here’s the sample source code for using the grunt-contrib-compress gruntplugin in your project.
package.json
{
 "name" : "My Sample Project",
 "version" : "1.0",
 "author" : " Ben Briggs",
 "private" : true,

 "devDependencies" : {
  "grunt" : "~0.4.0",
  "grunt-contrib-compress" : "~0.5.2"
 }
}
Gruntfile
module.exports = function(grunt){
 grunt.initConfig({
  pkg: grunt.file.readJSON('package.json')
 });

 grunt.loadNpmTasks('grunt-contrib-compress');

 grunt.registerTask('default', [compress]);
};
Grunt plugins are actually just npm modules that follow the gruntplugin template. You can find gruntplugins on the npm registry by browsing modules tagged with "gruntplugin" or at the official Grunt Plugins page. There are currently over 300 listed gruntplugins.