Search This Blog

Android

One & Only best book/source to start working on Android:
SDK: http://developer.android.com/sdk/index.html
Dev Guide: http://developer.android.com/guide/basics/what-is-android.html
Videos: http://developer.android.com/videos/index.html#v=opZ69P-0Jbc

My Learning (Errors that I faced):
INSTALL_PARSE_FAILED_MANIFEST_MALFORMED:
During creating an android project, I faced an issue after adding a Data-Base i.e.
"INSTALL_PARSE_FAILED_MANIFEST_MALFORMED".
After analysis, I came to know that permission was not provided in Manifest file.
Wrong way: android:name = ". DataBase"
Correct way: android:name = ". DataBase" android:authorities="com.android. Entries"
Please take care of this for your project.

Conversion to Dalvik format failed with error 1:
I faced this error, then I removed all unused Libraries from "Java Build Path" and kept only required library. Finally it start working. :)
******************************************************************************
Tab Widgets:

TabHost contains two items:
  1. TabWidget has the row of tab buttons
  2. FrameLayout contains tab contents
There are two ways are there to create Tab Widgets:
1. By extending  “TabActivity”:
Acquire.java:

package com.android.wg;
import android.app.TabActivity;
import android.content.res.Resources;
import android.os.Bundle;
import android.widget.TabHost;

public class Acquire extends TabActivity {
      /** Called when the activity is first created. */
      @Override
      public void onCreate(Bundle savedInstanceState) {   
            super.onCreate(savedInstanceState);   
          setContentView(R.layout.tabwidget);
          TabHost MyTabs = getTabHost();
          Resources res = getResources(); // Resource object to get Drawables
          MyTabs.addTab(MyTabs.newTabSpec("Tab1")
                                    .setIndicator("TAB 1",res.getDrawable(android.R.drawable.btn_star))
                                    .setContent(R.id.textviewTab1));
          MyTabs.addTab(MyTabs.newTabSpec("Tab2")
                        .setIndicator("TAB 2",res.getDrawable(android.R.drawable.btn_star))
                        .setContent(R.id.textviewTab2));
          MyTabs.setCurrentTab(0);
      }
}


Tabwidget.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">
      <TabWidget
            android:id="@android:id/tabs"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
      />
      <FrameLayout
            android:id="@android:id/tabcontent"
            android:layout_width= "fill_parent"
            android:layout_height="fill_parent">
            <TextView                
                  android:id="@+id/textviewTab1"               
                  android:layout_width="fill_parent"               
                  android:layout_height="fill_parent"                
                  android:text="You are in Tab 1" />
            <TextView                
                  android:id="@+id/textviewTab2"               
                  android:layout_width="fill_parent"               
                  android:layout_height="fill_parent"                
                  android:text="You are in Tab 2" />
      </FrameLayout>
   </LinearLayout>
</TabHost>

2. By extending  “Activity”:
Acquire.java:
package com.android.wg;

import android.app.Activity;
import android.content.res.Resources;
import android.os.Bundle;
import android.widget.TabHost;

public class Acquire extends Activity {
      /** Called when the activity is first created. */
      @Override
      public void onCreate(Bundle savedInstanceState) {   
            super.onCreate(savedInstanceState);   
          setContentView(R.layout.tabwidget);
          Resources res = getResources(); // Resource object to get Drawables
          TabHost MyTabs = (TabHost)findViewById(R.id.tabhost);
          MyTabs.setup();
         
          MyTabs.addTab(MyTabs.newTabSpec("Tab1")
                                    .setIndicator("TAB 1",res.getDrawable(android.R.drawable.btn_star))
                                    .setContent(R.id.textviewTab1));
          MyTabs.addTab(MyTabs.newTabSpec("Tab2")
                        .setIndicator("TAB 2",res.getDrawable(android.R.drawable.btn_star))
                        .setContent(R.id.textviewTab2));
          MyTabs.setCurrentTab(0);
      }
}

/*
Below Code:
MyTabs.addTab(MyTabs.newTabSpec("Tab1")
                                    .setIndicator("TAB 1",res.getDrawable(android.R.drawable.btn_star))
                                    .setContent(R.id.textviewTab1));
can be used as:
TabHost.TabSpec TSpec;
TSpec =tabs.newTabSpec("Tab1");
TSpec.setIndicator("TAB 1",res.getDrawable(android.R.drawable.btn_star));
TSpec.setContent(R.id.textviewTab1);
MyTabs.addTab(TSpec);
*/

Tabwidget.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">
   <TabHost
        android:id="@+id/tabhost"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent">
      <TabWidget
            android:id="@+id/tabs"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
      />
      <FrameLayout
            android:id="@+id/tabcontent"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:paddingTop="62px">
            <TextView                
                  android:id="@+id/textviewTab1"               
                  android:layout_width="fill_parent"               
                  android:layout_height="fill_parent"                
                  android:text="You are in Tab 1" />
            <TextView                
                  android:id="@+id/textviewTab2"               
                  android:layout_width="fill_parent"               
                  android:layout_height="fill_parent"                
                  android:text="You are in Tab 2" />
      </FrameLayout>
   </TabHost>
</LinearLayout>


Android Link: http://developer.android.com/resources/tutorials/views/hello-tabwidget.html
****************************************************************************** 

XmlPullParser:
XML Pull Parser is an interface that defines parsing functionality.
Basically used methods & Events in parsing:
next:                            provides access to high level parsing events.
nextToken():               provides access to lower level tokens.
getEventType():          determine current event state of the parser.
getName():                  provide the tag name of current event.
Various Event types [that comes a output of getEventType() or next()]
START_DOCUMENT:          Starting of Document.
END_DOCUMENT:              End of Document.
START_TAG:                        Starting of Tag (e.g. <foo>)
END_TAG:                             End of Tag (e.g. </foo>)
TEXT:                                     State in between of Start Tag & End Tag.
Example:
import java.io.IOException;
 import java.io.StringReader;

 import org.xmlpull.v1.XmlPullParser;
 import org.xmlpull.v1.XmlPullParserException.html;
 import org.xmlpull.v1.XmlPullParserFactory;

 public class SimpleXmlPullApp
 {

     public static void main (String args[])
         throws XmlPullParserException, IOException
     {
         XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
         factory.setNamespaceAware(true);
         XmlPullParser xpp = factory.newPullParser();

         xpp.setInput( new StringReader ( "<foo>Hello World!</foo>" ) );
         int eventType = xpp.getEventType();
         while (eventType != XmlPullParser.END_DOCUMENT) {
          if(eventType == XmlPullParser.START_DOCUMENT) {
              System.out.println("Start document");
          } else if(eventType == XmlPullParser.END_DOCUMENT) {
              System.out.println("End document");
          } else if(eventType == XmlPullParser.START_TAG) {
              System.out.println("Start tag "+xpp.getName());
          } else if(eventType == XmlPullParser.END_TAG) {
              System.out.println("End tag "+xpp.getName());
          } else if(eventType == XmlPullParser.TEXT) {
              System.out.println("Text "+xpp.getText());
          }
          eventType = xpp.next();
         }
     }
 }
Output:
Start document
 Start tag foo
 Text Hello World!
 End tag foo
Android Link: http://developer.android.com/reference/org/xmlpull/v1/XmlPullParser.html
 ******************************************************************************