Saturday 15 February 2014

PhoneGap HelloWorld Example in Android with Eclipse Step by Step

Hi all...Now am going to show you,How to create your Cross Plot-form (i.e Phone-gap) app with Android.


Phonegap : It is Framework. Phone Gap is an open source platform that allows you to create cross-platform mobile applications with HTML, JavaScript, and CSS. Build app once & run Any where ....

Now am going to show you step by step please follow the steps....

First we need to Download Phonegap From Official Website.You Will get a zip file ,Unzip it.You
will get like below image take Android and paste it in Folder









Create New Project: HelloWorldPhonegap :



Click on Next -->



Click on Next -->




Click on Next -->





Click on Next -->




Click on Next -->




Click on Finish-->











       Creat a Newfolder in Assets and name it as  www  .

            After Completion of this go to Phone gap Unzip Folder which is downloaded before from Official Website.

Go to android folder copy the (A) cordova-2.1.0.js & (B) cordova-2.1.0.jar and (C) XML Folder and Paste it accordingly which is shown in the fig.Below.





Then go to MainActivity and Extends the DriodGap in Place of Activity.



And replace setContentView(R.layout.activity_main) with 
Super.loadUrl("file:///android_asset/www/index.html")







Creat a index.html file and Place it in assets/www  folder

Go to Manifest File and Place all the permission which are showned below




<supports-screens
        android:largeScreens="true"
        android:normalScreens="true"
        android:smallScreens="true"
        android:xlargeScreens="true"
        android:resizeable="true"
        android:anyDensity="true"
        />

    <uses-permission android:name="android.permission.CAMERA" />
    <uses-permission android:name="android.permission.VIBRATE" />
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_LOCATION_EXTRA_COMMANDS" />
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.RECEIVE_SMS" />
    <uses-permission android:name="android.permission.RECORD_AUDIO" />
    <uses-permission android:name="android.permission.RECORD_VIDEO"/>
    <uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS" />
    <uses-permission android:name="android.permission.READ_CONTACTS" />
    <uses-permission android:name="android.permission.WRITE_CONTACTS" />   
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />   
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.GET_ACCOUNTS" />
    <uses-permission android:name="android.permission.BROADCAST_STICKY" />

    <uses-feature android:name="android.hardware.camera" />
    <uses-feature android:name="android.hardware.camera.autofocus" />











And then run the app :


Done with Phone gap Sample Hello World....



MainActivity.java :



import org.apache.cordova.DroidGap;

import android.os.Bundle;

public class MainActivity extends DroidGap {

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
super.loadUrl("file:///android_asset/www/index.html");
}

}


index.html :


<!DOCTYPE html>
<html>
<head>
PhoneGap Hello World
</head><br>
<body bgcolor="WHITE">

<a href="" >Home</a> <br>
<a href="" >ABout</a><br>
<a href="" >Services</a><br>
<a href="" >Careers</a><br>
<a href="" >ContactUs</a><br>
</body><br>
<footer>
@Copy rights apc 2014
</footer>
</html>







Screen Shots :     











Happy Programming...
Cheers....!!!







Sunday 9 February 2014

Rounded Corner Image Example in Android

Hi all ....Now am going to share how to make an bitmap image rounded corner with the help of Android Canvas and Paint.


Step 1: activity_main.xml:

<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"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity"
    android:background="#369589" >

 
 
  <ImageView
        android:id="@+id/image"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_marginTop="40dp"
        android:adjustViewBounds="true"
        android:gravity="center"/>

</RelativeLayout>



Step 2: MainActivity.java:



package com.apc.roundecornerimage;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.PorterDuff.Mode;
import android.graphics.PorterDuffXfermode;
import android.graphics.Rect;
import android.graphics.RectF;
import android.os.Bundle;
import android.widget.ImageView;

public class MainActivity extends Activity {


     private  int color;
     private  Paint paint;
     private  Rect rect;
     private  RectF rectF;
     private  Bitmap imageOut;
     private  Canvas canvas;
     private  float roundPx;
   
     ImageView image;

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

image=(ImageView)findViewById(R.id.image);
     
                Bitmap bmp=BitmapFactory.decodeResource(getResources(), R.drawable.aps);
                 image.setImageBitmap(getRoundedRectBitmap(bmp,12));
    }

    public  Bitmap getRoundedRectBitmap(Bitmap bitmap, int pixels)
    {
        imageOut = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(),               Bitmap.Config.ARGB_8888);
        canvas = new Canvas(imageOut);

        color = 0xff424242;
        paint = new Paint();
        rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());
        rectF = new RectF(rect);
        roundPx = pixels;

        paint.setAntiAlias(true);
        canvas.drawARGB(0, 0, 0, 0);
        paint.setColor(color);
        canvas.drawRoundRect(rectF, roundPx, roundPx, paint);
     
     
        paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
        canvas.drawBitmap(bitmap, rect, rect, paint);
     
       /* paint.setXfermode(new PorterDuffXfermode(Mode.DST_IN));
        canvas.drawBitmap(bitmap, rect, rect, paint);*/

   
        return imageOut;
    }


}



Screen Shots :










                                       Note :   Place an Image in Android -drawable folder


                                     Source Code :    Click here to Download Source Code



                                                                   Happy Programming
                                                                           Cheers.....!!!






Sunday 26 January 2014

Custom GridView example in Android

Hai to all now am going to share how to creat a custom gridview......

GridView is a ViewGroup that displays items in a two-dimensional, scrollable grid.
The grid items are automatically inserted to the layout using a ListAdapter.


res/layout/

Step 1 : activity_main.xml:


<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"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

     <GridView
        android:id="@+id/gridView1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_margin="4dp"
        android:columnWidth="80dp"
        android:gravity="center"
        android:numColumns="auto_fit"
        android:stretchMode="columnWidth"
     
      />

</RelativeLayout>



Step 2: grid_row.xml:



<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:padding="5dp" >

    <ImageView
        android:id="@+id/item_image"
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:layout_marginRight="10dp"
        android:src="@drawable/home" >
    </ImageView>

    <TextView
        android:id="@+id/item_text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="5dp"
        android:textSize="15sp" >
    </TextView>

</LinearLayout>



Step 3 : MainActivity.java




package com.apc.gridSample.com;

import java.util.ArrayList;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.widget.GridView;
import android.widget.Toast;



public class MainActivity extends Activity {
protected static final Toast text = null;

GridView gridView;
ArrayList<Item> gridArray = new ArrayList<Item>();
CustomGridViewAdapter customGridAdapter;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//set grid view item
Bitmap homeIcon = BitmapFactory.decodeResource(this.getResources(), R.drawable.home);
Bitmap mediaIcon = BitmapFactory.decodeResource(this.getResources(), R.drawable.media);
Bitmap eventIcon = BitmapFactory.decodeResource(this.getResources(), R.drawable.event);
Bitmap regIcon = BitmapFactory.decodeResource(this.getResources(), R.drawable.reg);
Bitmap donateIcon = BitmapFactory.decodeResource(this.getResources(), R.drawable.donate);
Bitmap feedbackIcon = BitmapFactory.decodeResource(this.getResources(), R.drawable.feedback);
Bitmap photosIcon = BitmapFactory.decodeResource(this.getResources(), R.drawable.photos);
Bitmap videosIcon = BitmapFactory.decodeResource(this.getResources(), R.drawable.videos);
Bitmap careersIcon = BitmapFactory.decodeResource(this.getResources(), R.drawable.careers);
Bitmap contactIcon = BitmapFactory.decodeResource(this.getResources(), R.drawable.contact);
Bitmap fbIcon = BitmapFactory.decodeResource(this.getResources(), R.drawable.fb);
Bitmap twitterIcon = BitmapFactory.decodeResource(this.getResources(), R.drawable.twitter);
gridArray.add(new Item(homeIcon,"Home"));
gridArray.add(new Item(mediaIcon,"Media"));
gridArray.add(new Item(eventIcon,"Event"));
gridArray.add(new Item(regIcon,"Register"));
gridArray.add(new Item(donateIcon,"Donate"));
gridArray.add(new Item(feedbackIcon,"Feedback"));
gridArray.add(new Item(photosIcon,"Photos"));
gridArray.add(new Item(videosIcon,"Videos"));
gridArray.add(new Item(careersIcon,"Careers"));
gridArray.add(new Item(contactIcon,"Contact US"));
gridArray.add(new Item(fbIcon,"Facebook"));
gridArray.add(new Item(twitterIcon,"Twitter"));
gridView = (GridView) findViewById(R.id.gridView1);
customGridAdapter = new CustomGridViewAdapter(this, R.layout.grid_row, gridArray);
gridView.setAdapter(customGridAdapter);
}

}




Step 4 : Item.java


package com.apc.gridSample.com;

import android.graphics.Bitmap;


public class Item {
Bitmap image;
String title;
String text;
public Item(Bitmap image, String title) {
super();
this.image = image;
this.title = title;
}
public Bitmap getImage() {
return image;
}
public void setImage(Bitmap image) {
this.image = image;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}

}



Step 5:  CustomGridViewAdapter.Java

package com.apc.gridSample.com;

import java.util.ArrayList;

import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;




public class CustomGridViewAdapter extends ArrayAdapter<Item> {
Context context;
int layoutResourceId;
ArrayList<Item> data = new ArrayList<Item>();

public CustomGridViewAdapter(Context context, int layoutResourceId,
ArrayList<Item> data) {
super(context, layoutResourceId, data);
this.layoutResourceId = layoutResourceId;
this.context = context;
this.data = data;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
View row = convertView;
RecordHolder holder = null;

if (row == null) {
LayoutInflater inflater = ((Activity) context).getLayoutInflater();
row = inflater.inflate(layoutResourceId, parent, false);

holder = new RecordHolder();
holder.txtTitle = (TextView) row.findViewById(R.id.item_text);
holder.imageItem = (ImageView) row.findViewById(R.id.item_image);
row.setTag(holder);
} else {
holder = (RecordHolder) row.getTag();
}

Item item = data.get(position);
holder.txtTitle.setText(item.getTitle());
holder.imageItem.setImageBitmap(item.getImage());
return row;

}

static class RecordHolder {
TextView txtTitle;
ImageView imageItem;

}

}




ScreenShot : 






Note : Please Place all appropriate images in drawable folder...





Happy Programming ....
Cheers.....!!!!!!









Tuesday 7 January 2014

Spinner Example in Android

res/layout/activity_main.xml


Step 1: spinner_sample.xml:


<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"
    android:background="@drawable/background"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity"
    >

<Spinner
    android:id="@+id/spinner1"
    android:layout_width="160px"
    android:layout_height="wrap_content"
    android:background="@drawable/spin"
    />

</RelativeLayout>


Step 2: MainActivity.java



package com.app.sample;


import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.Toast;

public class MainActivity extends Activity {
Spinner spin1 ;
String[] str= {"Sachin Tendulkar" ,"Sehwag","Sourav Ganguly" ,"Gouham Gambir" ,"Yuvi" ,"Dhoni","Rahul Dravid","Saina Nehwal","Anand","KapilDev"};

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
spin1=(Spinner)findViewById(R.id.spinner1);
ArrayAdapter<String> aa=new ArrayAdapter<String>(getBaseContext(), android.R.layout.simple_spinner_item,str);
spin1.setAdapter(aa);
spin1.setOnItemSelectedListener(
               new AdapterView.OnItemSelectedListener() {
                   @Override
                   public void onItemSelected(AdapterView<?> arg0, View arg1,
                           int arg2, long arg3) {
                       int position = spin1.getSelectedItemPosition();
                       Toast.makeText(getApplicationContext(),"You have selected "+str[+position],Toast.LENGTH_LONG).show();
                       // TODO Auto-generated method stub
                   }
                   @Override
                   public void onNothingSelected(AdapterView<?> arg0) {
                       // TODO Auto-generated method stub
                   }
               }
           );
}


}


Screen Shots :



















Source Code :  Click here to Download Source Code



                                                       Happy Programming.....
                                                               Cheers....!!!!!










Thursday 2 January 2014

ANDROID SDK Error : Unable to resolve target " android-X "

This error because of android Could not find the proper SDK Version ..so install appropriate version so you can resolve the problem

Just go click on the Android Projects

Properties-> Android -> Project Build Target -> Check Android 4.1.2(Version) 

or else 

in the Eclipse Click on SDK Manager 







Just check the box of the SDK you want to Use to test against your project and click install  and remove unused API's



Happy New Year to All

A Year (2013) is ended a few Hours ago.............

Now its the time to forget every bad memory of the past year and start your life afresh.

So, get up and hug a friend with whom you had a fight lately, get up and hug you parents and promise them you will not hurt them anymore, give a self hug and promise to yourself that come what may you would remain truthful to yourself, hug a poor kid and tell her that she is not alone, Help the needy and abolish the greedy from your life. So, just forget all your grudges and send these wonderful new year wishes to your loved one........................Happy New Year to all...........Cheers.....!!!!!


Thanks

Lokesh B

Tuesday 16 April 2013

SetError Example In Android



res/layout/seterror.xml


Step 1: seterror.xml :




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

    <TextView
        android:id="@+id/textViewPhoneNo"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="To"
        android:textAppearance="?android:attr/textAppearanceLarge" />

    <EditText
        android:id="@+id/editTextTo"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        >

        <requestFocus />
    </EditText>

    <TextView
        android:id="@+id/textViewSubject"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Subject"
        android:textAppearance="?android:attr/textAppearanceLarge" />

    <EditText
        android:id="@+id/editTextSubject"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content">
    </EditText>

    <TextView
        android:id="@+id/textViewMessage"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Message"
        android:textAppearance="?android:attr/textAppearanceLarge" />

    <EditText
        android:id="@+id/editTextMessage"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:gravity="top"
        android:inputType="textMultiLine"
        android:lines="5" />

    <Button
        android:id="@+id/buttonSend"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="Send" />
</LinearLayout>



Step 2: SetError.java :



public class SetError extends Activity implements OnClickListener{


Button buttonSend;
    
EditText txtTo;

    
EditText txtSubject;

    
EditText txtMessage;




@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.contactus);
 
buttonSend = (Button) findViewById(R.id.buttonSend);


txtTo = (EditText) findViewById(R.id.editTextTo);




txtSubject = (EditText) findViewById(R.id.editTextSubject);

txtMessage = (EditText) findViewById(R.id.editTextMessage);
        

buttonSend.setOnClickListener(new OnClickListener() {

           @Override
           public void onClick(View v) {


String to = txtTo.getText().toString();
                 

String subject = txtSubject.getText().toString();


             

String message = txtMessage.getText().toString();


                  

if (to.equals("")) {

txtTo.setError("You forgot to enter the To");

}
else if (subject.equals("")){



 txtTo.setError(null);

                
                txtSubject.setError("You forgot to enter the Subject");
                





}

else if (message.equals("")) {

txtSubject.setError(null);
                

               txtMessage.setError("You forgot to enter the Message");


}

 else{


Toast.makeText(getApplicationContext(),"
Message Send Succusefully",

Toast.LENGTH_SHORT).show();

}
}

});
}


@Override
public void onClick(View v) {
// TODO Auto-generated method stub

}



}

Screen Shot:









    Happy Programming ....

Cheers