Friday, April 15, 2011

SUPPORTING FULL SCREEN IN ANROID

In order to run the application in full screen mode you should add the following code to your activity.
getWindow().setFlags(WindowManager.LayoutParams.NO_STATUS_BAR_FLAG,
                   WindowManager.LayoutParams.NO_STATUS_BAR_FLAG); 

Tuesday, April 12, 2011

ANDROID MENUS


Android supports 3 types of Application Menus. Those are
1. Options Menu
2. Context Menu
3. Sub Menu
Options Menu :
      It is a collection of Menu items which will appear when the user invokes "Menu" button on the Android device.
                                                           Example for Options Menu
If the item count is <= 6 then all those are displayed properly.
If the item count is >6 then more button will appear default. If we press on the more button then remaining items will appear as shown below.
  
Context Menu :
    Context Menu is the menu which is similar to right click on your desktop computer. It means it will raise when the user tap or click on a list item it will display a menu.
Sub Menu :
    It is also a list of menu item that will appear if the user selects item in the menu item contains a nested list of menu.
   For example item 3 is having a Sub Menu.
 
Sub Menu
If you want more detail about Menus go through the following link

Monday, April 11, 2011

DIALOG IN ANDROID / CUSTOM DIALOG

cdialog.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="200dip"
  android:orientation="vertical"
  android:padding="5dip"
  android:layout_height="fill_parent">
 
  <TextView
      android:layout_width="fill_parent"
      android:text="Dialog Title"
      android:gravity="center_vertical"
      android:layout_height="20dip">
  </TextView>
 
  <TextView
      android:layout_width="fill_parent"
      android:layout_marginTop="5dip"
      android:background="#FFFFFF"
      android:layout_height="0.5sp">
  </TextView>
 
  <TextView
      android:layout_width="fill_parent"
      android:text="Dialog Message"
      android:gravity="center_vertical"
      android:layout_height="50dip">
  </TextView>
 
  <Button
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="OK"
      android:id="@+id/btnOk"
      android:layout_gravity="center">
  </Button>
 
</LinearLayout>
 

main.xml:

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

</LinearLayout>



CustomDialog.java:
package com.customdialog.home;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.view.Window;

public class CustomDialog extends Dialog
{

    public CustomDialog(Context context)
    {
        super(context);
    }
   
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.cdialog);
    }
}
Home.java:

package com.customdialog.home;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class Home extends Activity
{
    /** Called when the activity is first created. */
    Button btnClick;
    CustomDialog cd;
    @Override   
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
       
        btnClick = (Button)findViewById(R.id.btnDialog);
        btnClick.setOnClickListener(new OnClickListener()
        {           
            @Override
            public void onClick(View arg0)
            {
                cd = new CustomDialog(Home.this);
                cd.show();
               
                Button btnDimiss = (Button)cd.findViewById(R.id.btnOk);
                btnDimiss.setOnClickListener(new OnClickListener()
                {                   
                    @Override
                    public void onClick(View arg0)
                    {
                        cd.dismiss();   
                    }
                });

            }
        });
    }
}


TABS IN ANDROID

In order to create the tabs we have to use the following
TabHost
TabWidget
The TabHost is the root node for the layout contain TabWidget and Frame Layout.
The TabWidget is to maintain the tabs.
The Frame Layout contains the visible view for that Tab.

tabs.xml
<?xml version="1.0" encoding="utf-8"?>
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@android:id/tabhost"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
   
    <LinearLayout
        android:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent">
       
        <!-- Contain Tabs -->
        <TabWidget
            android:id="@android:id/tabs"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content" />
       
        <!-- Contain Content of the Tab -->
        <FrameLayout
            android:id="@android:id/tabcontent"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"/>
           
    </LinearLayout>
</TabHost>

main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="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/hello"
    />
</LinearLayout>




Home.java
package com.tabsdemo.activities;
import android.app.TabActivity;
import android.content.ComponentName;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TabHost;


public class Home extends TabActivity
{
    /** Called when the activity is first created. */
    TabHost objTabHost;
    public String TAB_NAMES[] = {"tab1","tab2","tab3"};
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.tabs);
       
        //Getting the TabHost Object to add TabSpec
        objTabHost = getTabHost();
       
        for(int i=0 ; i
        {
            //Creation of a Tab
            TabHost.TabSpec tab = objTabHost.newTabSpec(TAB_NAMES[i]);
           
            //Associating Intent to Tab
            ComponentName oneActivity = new ComponentName("com.tabsdemo.activities", "com.tabsdemo.activities." + TAB_NAMES[i]);
            Intent intent = new Intent().setComponent(oneActivity);                       
            tab.setContent(intent);
           
            //Adding Name and Drawable to Tab
            tab.setIndicator(TAB_NAMES[i], getResources().getDrawable(R.drawable.more_tab));//("TAB"+i, R.drawable.more_tab);
           
            objTabHost.addTab(tab);
        }
       
        objTabHost.setCurrentTab(0);
    }
}


tab1.iava:
 package com.tabsdemo.activities;

import android.app.Activity;
import android.os.Bundle;

public class tab1 extends Activity
{
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        //Content of the Tab1
    }
}

tab2.java:
 package com.tabsdemo.activities;

import android.app.Activity;
import android.os.Bundle;

public class tab2 extends Activity
{
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    }
}
tab3.java:
package com.tabsdemo.activities;

import android.app.Activity;
import android.os.Bundle;


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

If you want  more detail go to the following link:
http://developer.android.com/resources/tutorials/views/hello-tabwidget.html

PUBLISING THE APPLICATION TO ANDROID MARKET

In order to submit Android Application to Market Place we should follow some steps :

Step 1: 

We Have to generate own keystore with keystore name, password, Alias Name , Alias Password and Validity as follows
 
 
 
Open Command Prompt
Go to the drive in which JAVA is installed.
Change the directory to jdk. 
change the directory to bin.
Execute  keytool cmd.

Then execute the following command.
$ keytool -genkey -v -keystore yourkeystorename.keystore -alias alias_name_foryourkeystore  
  -keyalg RSA -keysize 2048 -validity 10000
  
   Here 10000 is the minimum validity period. If you want increase the period based 
on your requirement.
 
   on execution of this command keystore will created in your bin folder.
 
Step 2:
 
versioning the Application. 
 
-> Setting the application version  
  
   An integer value that represents the version of the application code. 
   In your application if you specify the android:versionCode as 2
   It means your application has 2 release.  
 
-> Setting the application version name.
 
     A value that represents the version of the application code, as it should be shown 
   to users. 
 
-> Specifying the Applications system api requirements. 
 
     To specify api level requirements we have to use  in our manifase.xml
 
     android:minSdkVersion :
         The minimum version of the android platform on which the application will run.
     android:targerSdkversion : 
          Specifies the API level on which the applicaion is designed to run.
     android:maxSdkVersion :
          The maximum version of the Android platform on which the application is designed to run.  
 
Step 3: 

Signing the application with the keysotre.
 
NOTE: 
Before signing test the application no of times. Check for android:debuggable in your
manifast.xml because you do not release for public in debug mode. So we have to remove 
that.
 
Go to Ecclipsc
Right Click on Your Application.
We found the option Android Tools -> Export Signed Application Package
Then follow the windows that will appear.  
 
 
 
 
  
 
The prescribed link is as follows. Here you find complete documentaion.
 
http://developer.android.com/guide/publishing/app-signing.html 

Saturday, April 9, 2011

HIDING THE TITLE BAR THROUGHOUT APPLICATION

  To hide the title bar through out application we have to include android:theme attribute to application tag in the Andriod Manifast.xml

 <application android:name="TestApplication" android:theme="@android:style/Theme.NoTitleBar">


Then in entire application title bar was not visible.

GETTING DEVICE ID

TelephonyManager is the predefined class that provides information about telephony services on the device. This class contain several methods. You can found complete documentation about this class using the following url

http://developer.android.com/reference/android/telephony/TelephonyManager.html


TelephonyManager tm = (TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);
//Getting Device Id
 tm.getDeviceId();

//Getting the Phone No
tm.getLine1Number();

Friday, April 8, 2011

METHOD TO CHECK WIFI IS AVAILABLE OR NOT

 The following method Return whether Wi-Fi is enabled or disabled.
 True if enabled otherwise false.
 
 public boolean checkWIFI()
 {
        WifiManager wm = (WifiManager) getSystemService(Context.WIFI_SERVICE);
        return wm.isWifiEnabled();
 }

METHOD TO CHECK INTERNET IS AVAILABLE OR NOT

The following method returns true if internet is available and false if not available.

public boolean checkInternetAvailablity() 
{
         ConnectivityManager cm = (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);                                         
          return cm.getActiveNetworkInfo().isConnected();

}

REMOVING DULL BACKGROUND WHILE RUNNING PROGRESS DIALOG

While running progress dialog the screen becomes dull. In order  to avoid that blur the following code will helpful.

Here i am applying properties to our progress dialog. In this example "lp.dimAmount:" is the attribute to set the dim amount .

pdlg = ProgressDialog.show(getParent(), "", "Loading..");
 //Setting the properties
WindowManager.LayoutParams lp = pdlg.getWindow().getAttributes();
 lp.dimAmount=0.0f;
//Applying to the screen
 pdlg.getWindow().setAttributes(lp);

SHOWING PROGRESS DIALOG WHILE DOWNLOADING USING THREAD

hi ...

In your application u want to get the data from the server. In this example i am putting all that parsing logic in a thread and showing progress dialog while loading the data. After getting all the data i am dismissing the dialog.
The following example illustrates the same ...


public void parsing()
{
        ProgressDialog pdlg ;
      
       //SHOWING DIALOG
        pdlg = ProgressDialog.show(getParent(), "", "Loading..");
        Thread thr_parsing = new Thread()
        {
            public void run()
            {
               //do the parsing
                runOnUiThread(new Runnable()
                {                   
                    @Override
                    public void run()
                    {   
                            //DISMISSING THE DIALOG
                            pdlg.dismiss();
                    }
                });
            }
        };
        thr_parsing.start();
       
    }

Thursday, April 7, 2011

INTERCEPTING ONE APP FROM ANOTHER IN ANDROID

hi, if u want to open one application from another using intent the following code is usuful.

Intent intent = new Intent(Intent.ACTION_MAIN);

intent.setComponent(new ComponentName("com.example.android.apis","com.example.android.apis.ApiDemos")); //Here we have to specify package name and corresponding activity in that package are parameters.

startActivity(intent);

      In this example i am opening api demos from my application. So "com.example.android.apis" is the main package and "com.example.android.apis.ApiDemos" is the main activity.

DOWNLOADING IMAGE FROM INTERNET IN ANDROID

public Bitmap downloadImageFromWeb(String url)
{
        Bitmap bitmap = null;
        InputStream in;
        try       
        {
            //Creation of url
            URL url_img = new URL(url);
          
            //Establishing Connection
            in = url_img.openStream();
          
            //Converting input stream to bitmap
            bitmap = BitmapFactory.decodeStream(in);
          
            //Closing the input stream
            in.close();
        }
        catch (MalformedURLException e)
        {
            e.printStackTrace();
        }
        catch (IOException e)
        {
            e.printStackTrace();
        }
        return bitmap;
}

DEVICE MODEL OR VISIBLE NAME IN ANDROID


Using Build  Class we will get the details of the mobile like device name, device info...

Build . MODEL ---- The visible device name.

Build . PRODUCT --- The name of the overall product.

Build . MANUFACTURER -- The manufacturer of the product.


For More Details about this class go through the link.
http://developer.android.com/reference/android/os/Build.html

Wednesday, April 6, 2011

Listview in Android

The listview is the most important view of the Android. For the mobile applications memory is the main concern. So we will use listview to optimize memory. For example we want to show a list of records at that there are two possibilities are there.

1. using for loop we will add the views to a LINEAR LAYOUT.

2. using a LISTVIEW.

In both cases we will get the same output. But the second way is the optimized way.

If we implement first way it will create all views at a time only. So the memory will waste.

If we implement the second thing then listitems will create dynamically. It mean we are having 30 records. At a time we have to see only 5 items according to screen resolution. Then listview will create only 5 items at the first time and if we scroll down to view the other items at that time it will create the other views. It mean it will not create 30 records views at a time.

Example :

main.xml:



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

</LinearLayout>

header.xml:

Xml for the listview header.
<?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">

  <TextView
  android:layout_width="fill_parent"
  android:text="Header"
  android:layout_height="wrap_content">
  </TextView>

</LinearLayout>
listlayout.xml
Xml for the listitem view.

<?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="wrap_content">

  <TextView
  android:layout_width="fill_parent"
  android:id="@+id/tvTitle"
  android:text="listitem"
  android:gravity="center_vertical"
  android:layout_height="30dip">
  </TextView>

</LinearLayout>

Home.Java:
This is the java file . Here we are using listview with custom list item. In the list item at present i am using on textview. In that you have to put what ever you want.

package com.exlistview.activities;
import android.app.Activity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListView;

public class Home extends Activity 
{   
ListView lvList;
    @Override
    public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        //GETTING THE ID OF THE LISTVIEW
        lvList = (ListView)findViewById(R.id.lvList);
       
        //Adding Header to ListView
        
        //INFLATING THE HEADER VIEW FROM THE XML
        LayoutInflater inflater = getLayoutInflater();
        View header = (View)inflater.inflate(R.layout.header, lvList, false);
        
        //ADDING TO LISTVIEW
        lvList.addHeaderView(header, null, false);
        
        //SETTING THE ADAPTER
        lvList.setAdapter(new listAdapter());      
        
    }
    
    public class listAdapter extends BaseAdapter
    {

@Override
public int getCount() 
{
//ITEM SIZE
return 20;
}

@Override
public Object getItem(int arg0) 
{
return arg0;
}

@Override
public long getItemId(int arg0) 
{
return arg0;
}

@Override
public View getView(int arg0, View arg1, ViewGroup arg2) 
{
//INFLATING THE ITEM FOR THE LIST
if(arg1 == null)
arg1 = getLayoutInflater().inflate(R.layout.listlayout, null);
return arg1;
}
   
    }
}





UPDATING THE SDK




In Eclipsc Window > Select Android Sdk and Avd manager

Then a window will open



In that select Available Packages then it will list all the available packages select the things what ever you want and click on install selected then sdk will updated.

Note :  

For updating sdk also we need INTERNET.

INSTALLING ANDROID SDK ON YOUR SYSTEM

Step 1:

Downloading the Eclipsc from the following link.


The "Eclipse Classic" version is recommended.

Step 2:

Downloading the "Software Development Kit"

The SDK starter package is not a full development environment—it includes only the core SDK Tools, which you can use to download the rest of the SDK components (such as the latest Android platform).


Step3:

Installing ADT (Android Development Tool) for the Eclipsc.



Step 4:

Adding ADT to Eclipsc.

 Help > Install New Software.... > add the downloaded ADT and click "OK".

Step 5:

Adding the Downloaded SDK

Window  > Preferences >  Then a window will open...




In that select Android. Then click on browse and target the downloaded sdk and click on ok.


That's it. Installation completed.

Note :  

For the Step 5 we need INTERNET.



Sax Parsing


try
{
        //url for parsing
        URL url = new URL("Your URL");        

          //reading the data at the specified url
        
          //For the local file
          //input = getClass().getResourceAsStream("/res/raw/your.xml");
        input = url.openStream();
         
          //Creating object for SAXPARSER FACTORY
        SAXParserFactory spf = SAXParserFactory.newInstance();

        //CREATION OF OBJECT FOR SAXPARSER
        SAXParser sp = spf.newSAXParser();

        //READING AND PARSING
        XMLReader xr = sp.getXMLReader();
SaxHandler myXMLHandler = new SaxHandler();
xr.setContentHandler(myXMLHandler);
xr.parse(new InputSource(input));
}
catch(Exception e)
{
       e.printStackTrace();
 }





SaxHandler.java

public class SaxHandler extends DefaultHandler
{

Boolean currentElement = false;
String currentValue = null;
NewsItem objNewsItem;
boolean test_Des = false;

@Override
public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException
{
currentValue = "";
currentElement = true;

if (localName.equalsIgnoreCase("START TAB"))
{
//VECTOR CREATION
                        //OBJECT CREATION
}

}

@Override
public void endElement(String uri, String localName, String qName)throws SAXException
{
                //HERE YOU HAVE TO INSERT TAG NAMES THAT YOU HAVE TO READ
currentElement = false;
if (localName.equalsIgnoreCase("TAG NAME"))
{
VAR = currentValue;
}
else if(localName.equalsIgnoreCase("TAG NAME"))
{
VAR = currentValue;
}
else if(localName.equalsIgnoreCase("TAG NAME"))
{
        YOURVECTOR.add(objNewsItem);
}

}

@Override
public void characters(char[] ch, int start, int length)throws SAXException
{
if (currentElement)
{
currentValue += new String(ch, start, length);

}
}

}