Monday, April 16, 2012

How to integrate AdMob ads in android application.

In this post I show how to integrate ads in your android application.

 Step 1:

 Open admob.com in your browser.



Step 2:

 Log in to admob site using admob account or google account.




Step 3:

Fill the details.





step 4:

 Click on Add your first App. Below Monotize Your mobile apps







Step 5:

 Fill your Bank details. (For Bank SWIFT code visit your bank website.)




 Step 6:

Select Android App icon from Add Site/App. Fill the details below about your app. Click continue.









Step 7:

Click on Admob Android SDK to download the sdk zip file and extact it.

Step 8:

We can integerate ads in our application in two ways.

1. Using XML file

 2. Using java code

In this post I show how to integrate ads using XML code. Open your project in Eclipse IDE.

Step 9:

 Right click on your project and select properties

Step 10:

select java build path > Libraries > Add External jar. browse the jar file (GoogleAdMobAdsSdk-4.1.0 or other versions) click ok.




Step 11:

open your project mainfest file. Add the following lines of code. before application tag.
To load ads we need internet permission.

<uses-permission android:name="android.permission.INTERNET">
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE">


Step 12:

 Add an activity to your manifest file.

<android:name="com.google.ads.AdActivity"
activity android:configchanges="keyboard|keyboardHidden|orientation" >

 Step 13:

 Open your main.xml file or any other xml file where you want to place the ads. Add the following code in your layout tag after "xmlns:android="...." "

 xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads"

or

 Replace your layout with the following code

<linearlayout
xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads" xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="fill_parent"
android:layout_width="fill_parent"
android:orientation="vertical"
>

replace your_admob_id with your admob publisher id.

To get admob publisher id click on your application name in admob site and select manage settings.
Copy your id and paste it in adview tag.

Note:
publisher id changes from one application to another application. Make sure that your package name in eclipse and your package name in admob is same. Other wise it won't work.


Step 14:

 Run your application 
You will see the ads in your Emulator..

Note:

Don't click on your ads in Emulator.. If you click your account will be closed for clicking self ads ..
 

How to publish Android application in the Android Market/ Google Play

 In this post I show how to publish your android app in market. Before publishing an application you have to build an apk file for your project. To build an apk file and to publish it in the market follow the steps below.

 Step 1:

Open Eclipse IDE > Right click on your project > Select Export An Export window will open. Select "Export Android Application" under "Android" folder. Click Next.



Step 2:


It opens an "Export Android Application" window. Click Next. Select the location of your already created keystore by clicking browse button. Enter the password for your keystore. Click Next










Step 3:

 Select the Alias and password and click Next.







Step 4:

Select the destination folder to save your apk. Click on Finish.

Now the apk file for your project is created. You are ready to publish your application.


Steps for publishing an application.

 Step 1:

 Open http://developer.android.com/ in your web browser. Click the link "Learnmore>>" under "Publish."







 Step 2:

 Sign in with your email and password.

 Step 3:

It will open Android developer Console. Fill the details under Listing Details. Click Continue.






Step 4:

 Read Android Market Developer Distribution Agreement. Click the check box below, click I agree, Continue.






Step 5:

 To Register as a developer you have to pay $25. If you are ready to pay click Continue.







Step 6:


 Fill the details. Enter the card number , CVC (Card Verification Code--- available on the back side of your card.). Enter valid expiration date... etc.. Click on Agree and Continue.







Step 7:

 After your payment received it will opens a webpage where you can find "Upload Application".. 
Click it.

 Step8:

 It opens a window.. select your apk file and click on upload.. Fill the product Details.. etc.. Open APK files tab.. click on Publish..






 Step 9: Now your application is published successfully..

 Note:

 It will take few minutes to upload in all servers.. so you cannot find your application in the market immediately...

Wednesday, March 21, 2012

Simple ListView Example

In this post I show how to create a Simple ListView. Create a xml file listview_row.xml which contains a TextView for displaying the values in ListView.

listview_row.xml

<?xml version="1.0" encoding="utf-8" ?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
 android:id="@+id/textView1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="10dp"
android:textSize="16sp">
</TextView>

main.xml

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



    <ListView
        android:id="@+id/listView1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" >

        </ListView>


</LinearLayout>

SimpleListViewActivity.java

public class SimpleListViewActivity extends Activity
{
 private ListView listview;
private ArrayAdapter<String> listAdapter;

@Overridepublic void onCreate(Bundle icicle)
{
super.onCreate(icicle);
setContentView(R.layout.main);

listView=(ListView)findViewById(R.id.listView1);

String[] actress=new String[]{"Anushka","Bhavana","Charmy","Deepika","Genelia","Hansika","Kajal Agarwal","Nayanatara","Namitha"};


ArrayList<String> actressList=new ArrayList<String >();
actressList.addAll(Arrays.asList(actress));


listAdapter=new ArrayAdapter<String>(this,R.layout.listview_row,actressList);
listView.setAdapter(listAdapter);


}
}


Now Run the program.

 

Splitting a URL/ Getting the file name from URL string

In this post I show how to split a URL  and get the file name. Here my URL is http://file1.hpage.com/008142/64/bilder/kajalagarwal1.jpeg now  i want to get the file name from URL.

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" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="TextView" />

</LinearLayout>

SplitActivity.java

public class SplitActivity extends Activity
{

String URL="http://file1.hpage.com/008142/64/bilder/kajalagarwal1.jpeg";

String[] values;

String filename;

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

values=URL.split("/");
filename=values[values.length-1];

TextView textView=(TextView)findViewById(R.id.textView1);
textView.setText("file name:");
textView.append(filename);

}
}

Sunday, December 25, 2011

Screen Capture Using java

In this post I show how to capture screen with java.
Capture.java


import java.awt.AWTException;
import java.awt.Rectangle;
import java.awt.Robot;
import java.awt.Toolkit;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;

public class MyScreenCapture {
    public static void main(String args[]) throws AWTException
    {
        Robot robot = new Robot();
        int width,height;
        width=(int) Toolkit.getDefaultToolkit().getScreenSize().getWidth(); // to get the screen width
        height=(int) Toolkit.getDefaultToolkit().getScreenSize().getHeight(); // to get the screen size
       
        BufferedImage bi=robot.createScreenCapture(new Rectangle(width, height));
        try {
            ImageIO.write(bi, "jpeg", new File(System.currentTimeMillis()+".jpeg"));
        } catch (IOException e) {
           
            e.printStackTrace();
        } 
    }   
}

In the above code createScreenCapture() method creates an image containing pixels read from the screen.
ImageIO.write() method writes an image using an arbitrary ImageWriter that supports the given format to a File.
Click here to know about Robot class.

Tuesday, December 20, 2011

Creating a Paint Brush Application in java - 1

In this post, I show how to create a Paint Brush application in java swings. In this application if you click and drag the mouse it will paint a line on the window.
Create a class PaintBrush.java extend it from JFrame class. Because a frame represents a window with a title bar and borders. Frame becomes the basis for creating the screens for an application because all the components go into the frame.

To create a frame, we have to create an object to JFrame class in swing.

PaintBrush.java 

import javax.swing.*;
public class PaintBrush extends JFrame
{

      // initializing the frame
 public PaintBrush()
{
     setVisible(true);
     setTitle("Paint Brush");
     setSize(400,400);
     setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
 public static void main(String args[])
{
       PaintBrush pbrush=new PaintBrush();
}
}

If you compile and run the above program it will display a window with 400px width ,400px height and with a title "Paint Brush".







Now create a class MyPanel.java which extends JPanel class.

MyPanel.java

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class MyPanel extends JPanel implements MouseMotionListener
{  
    int x1,y1;
    public MyPanel()
    {
// to listen the mouse events (mouse dragging and mouse moving)
    addMouseMotionListener(this);
  
    }
  
    public void paint(Graphics g)
    {

        g.setColor(Color.black);          
        g.fillOval(x1,y1,15,15);
    }

// this method is invoked when we drag the mouse
    public void mouseDragged(MouseEvent me)
    {  
      
        x1=me.getX(); // to save the mouse pointer x-coordinate while dragging.
        y1=me.getY(); // to save the mouse pointer y-coordinate while dragging.
      
        repaint();
    }
    public void mouseMoved(MouseEvent me)
    {

    }
}
Compile and run the above classes and start painting ..




Android Examples

Java Examples

Drawing 2D graphics in java

In this post, i show how to create 2D graphics in java step-by-step.

Create a class MyPaint.java and extend it from JFrame class.

Paint.java
import javax.swing.*;
public class MyPaint extends JFrame
{
 public static void main(String args[])
{

}
}

Create an object for MyPaint class

Paint.java

import javax.swing.*;
public class MyPaint extends JFrame
{
 public static void main(String args[])
{
      MyPaint paint=new MyPaint();
}

}

Now initialize the class by creating constructor .


 MyPaint.java

public class MyPaint extends JFrame
{

public MyPaint()
{
      // to visible the frame
      setVisible(true);

        //to set the size        
        setSize(400,400);

        //to set the title
        setTitle("My Paint");
       
        //close the window when click on the close button
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
 public static void main(String args[])
{
      MyPaint paint=new MyPaint();

}

}

If you compile and run the program, you will see the output as follows.



Now create a class MyPanel.java extends JPanel class. We draw all the graphics in this class.

MyPanel.java

import javax.swing.*; //for JPanel class
import java.awt.*;   // for Color class
public class MyPanel extends JPanel
{
 public MyPanel()
{
 // set the Panel's background color
setBackground(Color.white);
}
}

Now add this class to our MyPaint class.

MyPaint.java


public class MyPaint extends JFrame
{

public MyPaint()
{
      // to visible the frame
      setVisible(true);


        //to set the size        
        setSize(400,400);

        //to set the title
        setTitle("My Paint");
       
        //close the window when click on the close button
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

}

 public static void main(String args[])
{
      MyPaint paint=new MyPaint();

// creating MyPanel class object
    MyPanel panel=new MyPanel();


// adding panel to MyPaint class
paint.setContentPane(panel);



}

}

Now we are ready to draw graphics. To draw graphics we use paint() method in our MyPanel class.

MyPanel.java


import javax.swing.*; //for JPanel class

import java.awt.*;   // for Color class

public class MyPanel extends JPanel
{
 public MyPanel()
{
 // set the Panel's background color
setBackground(Color.white);

}

public void paint(Graphics g)
{
       // to set the brush color.
        g.setColor(Color.green);
      // to draw a Rectangle
       g.drawRect(10,50,70,50);
      
        g.setColor(Color.red);
       // to draw an Oval or circle
        g.drawOval(100,80,50,50);

        g.setColor(Color.blue);
        //to draw a filled Rectangle
        g.fillRect(200,150,50,50);

        g.setColor(Color.yellow);
        // to draw a filled Oval
        g.fillOval(160,80,70,70);
      
        g.setColor(Color.black);
        // to draw a filled Arc
        g.fillArc(90,190,150,150,360,160);

}
}

Now compile and run the program,you can see the output as follows.

 
 

Thursday, November 24, 2011

Animation Part 1

In this post I show how to create Animation in android.
Create a folder "anim" in your project "res" folder. Create a new xml (translate.xml) file in anim folder, select the root element as translate.
Write the following code in your translate.xml file

translate.xml

<?xml version="1.0" encoding="utf-8"?>
<translate
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:fromXDelta="200%"
    android:toXDelta="0%"
    android:fromYDelta="200%"
    android:toYDelta="0%"
    android:duration="3000"
    android:zAdjustment="top">
</translate>

Open your main.xml file, add a TextView.

main.xml 

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical"
    android:gravity="center_horizontal" >

    <TextView
        android:id="@+id/t1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="Welcome"
        android:gravity="center_horizontal"
        android:textSize="30sp" />

</LinearLayout>

Open your Activity file and write the following code.

AnimationActivity.java

import android.app.Activity;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.TextView;

public class AnimationActivity extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    }
 private void RunAnimation()
    {
        Animation a=AnimationUtils.loadAnimation(this, R.anim.translate);
        a.reset();
        TextView tv=(TextView)findViewById(R.id.t1);
        tv.clearAnimation();
        tv.startAnimation(a);
   }
    @Override
    public boolean onTouchEvent(MotionEvent event) {
        if(event.getAction()==MotionEvent.ACTION_DOWN)
        {
            RunAnimation();
        }
        return true;
    }
}

Run your project.

Wednesday, November 23, 2011

Using Google Maps in Android

In this post I show how to create a Google Maps in your application.


Create a New project




















Select BuildTarget as GoogleAPIS 2.3.3 version























 Enter Package name and click Finish.






To use google maps in our application we need a MD5 finger print provided by
the google. To get the MD5 finger print follow the steps.

Right click on your project and click "Android Tools> Export Signed Application
Package..."

  



It opens a window "Export Android Application". Click Next. Choose "Create new keystore"
and browse for the location where you can store the generated key.

Note: Remember the location.
Enter password and confirm. Click Next.




















Enter Alias, password,confirm,validity and First and Last Name
Click Next




















Browse for the location to save the .apk file.
Click Finish.





















Open command prompt and change your directory to Java's bin directory.












Enter the following command
bin> keytool -list -alias Aliaskey(enter your alias key name here) -keystore "enter your
keystore location in double quotes" -storepass yourpassword -keypass yourpassword

Enter









It generates a MD5 finger print.









copy the generated finger print without any spaces.

Open the following link in browser.

http://code.google.com/android/maps-api-signup.html

paste your fingerprint code and click Generate API key.
















I
It generates a code for mapview as follows
<com.google.android.maps.MapView
                android:id="@+id/map_view"
                 android:layout_width="fill_parent"
                 android:layout_height="fill_parent"
                 android:enabled="true"
                 android:clickable="true"
                 android:apiKey="enter your key here"
                 />
 copy this and paste in main.xml file.


main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >
    <com.google.android.maps.MapView
                android:id="@+id/map_view"
                 android:layout_width="fill_parent"
                 android:layout_height="fill_parent"
                 android:enabled="true"
                 android:clickable="true"
                 android:apiKey="your key here"
                 />

</LinearLayout>


To run this application we need to add the permission in AndroidManifest.xml file. And also include the library file.


AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.arun.nov.googlemaps"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk android:minSdkVersion="10" />
    <uses-permission android:name="android.permission.INTERNET"/>
    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
        <uses-library android:name="com.google.android.maps" />
        <activity
            android:label="@string/app_name"
            android:name=".GoogleMapsActivity" >
            <intent-filter >
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>


GoogleMapsActivity.java


import android.os.Bundle;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;

public class GoogleMapsActivity extends MapActivity {
    private MapView mapview;
    private MapController controller;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
       
        mapview=(MapView)findViewById(R.id.map_view);
        mapview.setSatellite(true);
       
    }
    @Override
    protected boolean isRouteDisplayed() {
        // TODO Auto-generated method stub
        return false;
    }
}

Before run your project we need to create a virtual device for running google maps.





















To start the emulator select the vertual device and click start button.



















Now run your project and you will get the following out put.