JSON IN ANDROID.

Complex JSON String parsing in ANDROID

http://www.coderzheaven.com/2011/06/10/complex-json-string-parsing-in-android/

Parsing JSON Object in ANDROID.

http://www.coderzheaven.com/2011/06/09/parsing-json-objects-in-android/

How to remove duplicates from NSMutableArray ?

Hi,

For removing or deleting duplicates from a NSMutableArray , use the following lines of code. It will simply check for duplicates elements and get it deleted or removed.


NSArray *tempArr= [urArray copy];

NSInteger idx = [tempArr count] - 1;

for (id object in [tempArr reverseObjectEnumerator]) {

     if ([urArray indexOfObject:object inRange:NSMakeRange(0, index)] !=NSNotFound) {

           [urArray removeObjectAtIndex:idx];
     }

idx--;

}
[tempArr release];

:)

How to set iPhone/iPad table view transparent ?

Hi,

In order to simply set the iPhone/iPad table view transparent use the following line of code.

urTblView.backgroundColor = [UIColor clearColor];

Here ‘urTblView’ is the table view that you want to make transparent. Try it out. This single line will make your table view transparent and looks better.
:)

How to change z order value of a sprite in Cocos2D ?

Hi,

Sometimes during the execution of program you may need to change the z order value of sprite for better working. Use the following sample of code to change the z order value of a sprite in Cocos2D/Box2D iPhone programming.

CCSprite *urSprite = [CCSprite spriteWithFile:@"coderzheavenLogo.png"];
urSprite.position = ccp(240,160);
//First setting the z value to 1
[self addChild:urSprite z:1];
.
.
.
.
.
//After some other executions are over setting z value to 7
[self reorderChild:urSprite z:7];

:)

How to get a Sprite by tag in Cocos2D ?

Hi,

Sometimes you may need to get a sprite using tag while doing a project using Cocos2D. It’s easy to get a sprite by a tag in Cocos2D, use the following code to get it.

tempSprite = (CCSprite*)[self getChildByTag:7];

Here ‘tempSprite’ is your CCSprite and it will get the sprite with tag 7.
:)

How to make a sprite visible and invisible in Cocos2D ?

Hi,

In order to make a sprite completely invisible , use the following code while using Cocos2D, this code will set the visibility of your sprite to zero.

urSprite.visible = 0; 

In order to make a sprite completely visible, use the following code while using Cocos2D, this code will set the visibility of the sprite back to one.

urSprite.visible = 1; 

:)

Mute and Unmute Sound in Cocos2D

Hi,

In order to mute and unmute sound in Cocos2D iPhone, use the following code.

if ([[SimpleAudioEngine sharedEngine] mute]) {
            // This will unmute the sound
            [[SimpleAudioEngine sharedEngine] setMute:0];
}
else {
             //This will mute the sound
             [[SimpleAudioEngine sharedEngine] setMute:1];
}

:)

How to programmatically set UITextView font and size

Hi,

In order to set font and size of a UITextView programmatically use the following code.

self.urTxtView.font = [UIFont fontWithName:@"Zapfino" size:customSize];

:)

What is IL/MSIL/CIL and JIT in .NET ?

IL is Intermediate Language
IL is also known as MSIL (Microsoft Intermediate Language)
or
CIL (Common Intermediate Language)

JIL – IL is converted into machine code by Just-In-Time (JIT) compiler

Program for Palindrome checking in C Sharp/C#

Hi,

Sometimes you need to check for whether the given string is Palindrome or not. Given below is a sample code to check whether the given string is Palindrome or not using C Sharp/C#.

class Palindrome
{
  static void Main()
  {
      string checkStr, sampleStr;
      char[] temp;

      System.Console.Write("Enter your String: ");

      sampleStr = System.Console.ReadLine();

      checkStr = sampleStr.Replace(" ", "");

      checkStr = checkStr.ToLower();

      temp = checkStr.ToCharArray();

      System.Array.Reverse(temp);

      if(checkStr== new string(temp))
      {
          System.Console.WriteLine(""{0}" is a palindrome.",sampleStr);
      }

      else
      {
          System.Console.WriteLine(""{0}" is NOT a palindrome.",sampleStr);
      }

  }
}

:)

Global variables in ANDROID……

Hi all…….
In today’s exampl e I will show you how to deal with global variables in ANDROID.
You know global variables are those variables that you can access across any variables.
It’s value becomes changed when you change it in any of the classes in your project. These types of variables act as like cookies in a browser to store your login information across different pages. Now Let’s see how to implement this………
For this demo I am using three classes, one is the global class which has the global variable and other two normal classes.
In the global class I have a variable named “global_var” which I am accessing across the other two classes and I am changing the value across the classes. Please check the Logcat for seeing the output.

First class “Global_var_test.java

package pack.coderzheaven.test;

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

public class Global_var_test extends Activity {

	Button b;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main1);

        b = (Button)findViewById(R.id.Button01);
        GlobalClass global = new GlobalClass();
        System.out.println("var1 value in first class before changing = " + global.global_var1);
        global.global_var1 = "value2";
        System.out.println("var1 value in first class after changing = " + global.global_var1);

        b.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				startActivity(new Intent(Global_var_test.this, SecondClass.class));
			}
		});
    }
}

Next SecondClass.java

package pack.coderzheaven.test;

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

public class SecondClass extends Activity{

		Button b;
		@Override
	    public void onCreate(Bundle savedInstanceState) {
	        super.onCreate(savedInstanceState);
	        setContentView(R.layout.main2);

	        b = (Button)findViewById(R.id.Button01);

	        GlobalClass global = new GlobalClass();
	        System.out.println("var1 value in second class before changing = " + global.global_var1);
	        global.global_var1 = "value3";
	        System.out.println("var1 value in second class after changing = " + global.global_var1);

	        b.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View v) {
					startActivity(new Intent(SecondClass.this, SecondClass.class));
				}
			});
	    }

}

Now the GlobalClass.java which holds the global variable.
You can have any datatype as global variable….

package pack.coderzheaven.test;

import android.app.Application;

class GlobalClass extends Application{

	String global_var1 = "Value1";

}

Please check your logcat to see the global value changes…..
Get more ANDROID posts from here
Happy coding………
Please leave your valuable comments………

How to use Global variables in ANDROID?

Hi all…….
In today’s exampl e I will show you how to deal with global variables in ANDROID.
You know global variables are those variables that you can access across any variables.
It’s value becomes changed when you change it in any of the classes in your project. These types of variables act as like cookies in a browser to store your login information across different pages. Now Let’s see how to implement this………
For this demo I am using three classes, one is the global class which has the global variable and other two normal classes.
In the global class I have a variable named “global_var” which I am accessing across the other two classes and I am changing the value across the classes. Please check the Logcat for seeing the output.

First class “Global_var_test.java

package pack.coderzheaven.test;

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

public class Global_var_test extends Activity {

	Button b;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main1);

        b = (Button)findViewById(R.id.Button01);
        GlobalClass global = new GlobalClass();
        System.out.println("var1 value in first class before changing = " + global.global_var1);
        global.global_var1 = "value2";
        System.out.println("var1 value in first class after changing = "; + global.global_var1);

        b.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				startActivity(new Intent(Global_var_test.this, SecondClass.class));
			}
		});
    }
}

Next SecondClass.java

package pack.coderzheaven.test;

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

public class SecondClass extends Activity{

		Button b;
		@Override
	    public void onCreate(Bundle savedInstanceState) {
	        super.onCreate(savedInstanceState);
	        setContentView(R.layout.main2);

	        b = (Button)findViewById(R.id.Button01);

	        GlobalClass global = new GlobalClass();
	        System.out.println("var1 value in second class before changing = " + global.global_var1);
	        global.global_var1 = "value3";
	        System.out.println("var1 value in second class after changing = " + global.global_var1);

	        b.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View v) {
					startActivity(new Intent(SecondClass.this, SecondClass.class));
				}
			});
	    }

}

Now the GlobalClass.java which holds the global variable.
You can have any datatype as global variable….

package pack.coderzheaven.test;

import android.app.Application;

class GlobalClass extends Application{

	String global_var1 = "Value1"

}

Please leave your valuable comments………

Copy a file in C Sharp/C# without using FileInfo

Hi,

We have discussed here copying a file using FileInfo. Now we are going to see how we can do the same without using FileInfo in C Sharp/C#.

using System;
using System.IO;

class MainClass {
  public static void Main(string[] args) {

    int i;
    FileStream fileIn;
    FileStream fileOut;

    try {
      fileIn = new FileStream("opFile.txt", FileMode.Open);
    }
    catch(FileNotFoundException exc) {
      Console.WriteLine(exc.Message + "nFile Not Found");
      return;
    }

    try {
      fileOut = new FileStream("clFile.txt", FileMode.Create);
    }
    catch(IOException exc) {
      Console.WriteLine(exc.Message + "nError Opening File");
      return;
    }

    try {
      do {
        i = fileIn.ReadByte();
        if(i != -1)
           fileOut.WriteByte((byte)i);
      } while(i != -1);
    }
    catch(IOException exc) {
      Console.WriteLine(exc.Message + "Error");
    }

    fileIn.Close();
    fileOut.Close();
  }
}

Copying a file using FileInfo – C Sharp/C#

Hi,

For copying a file using FileInfo in C Sharp/C#, use the following code. For copying a file without using file info check the example given here.

using System;
using System.IO;

public class MainClass
{
  static void Main(string[] args)
  {

    FileInfo testFile = new FileInfo(@"c:NewWorkstestOld.txt");

    testFile.Create();

    testFile.CopyTo(@"c:NewProjtestNew.txt");

  }
}

:)

Applet FlowLayout Example

The FlowLayout class puts components in a row, sized at their preferred size.

Creates a new flow layout manager with the indicated alignment and the indicated horizontal and vertical gaps. The hgap and vgap arguments specify the number of pixels to put between components.

import java.applet.*;
import java.awt.*;
/*
  <applet code="FlowLayoutApplet" width=300 height=200>
  </applet>
*/

public class test extends Applet
{

  public void init()
  {
    setLayout(new FlowLayout(FlowLayout.RIGHT, 5, 5));
    for (int i = 0; i < 20; i++)
    {
    	add(new Button("Button" + i));
    }
  }
}

The output window look like this

How to send email from and ANDROID Application programatically?

Hello all……..
In today’s post I will show you send mail from an android application progrmatically..
Let’s go to the code fast……
This is the code in the mail java file….

package com.coderzheaven;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class sendMailDemo extends Activity {
    Button send;
    EditText address, subject, emailbody;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        address = (EditText) findViewById(R.id.address);
        subject = (EditText) findViewById(R.id.subject);
        emailbody = (EditText) findViewById(R.id.body);
        send = (Button) findViewById(R.id.send);

        send.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
            	sendEmail();
            }
        });
    }

    public void sendEmail(){

    	if(!address.getText().toString().trim().equalsIgnoreCase("")){
    	  final Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND);
	      emailIntent.setType("plain/text");
	      emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL, new String[]{ address.getText().toString()});
	      emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, subject.getText());
	      emailIntent.putExtra(android.content.Intent.EXTRA_TEXT, emailbody.getText());
	      sendMailDemo.this.startActivity(Intent.createChooser(emailIntent, "Send mail..."));
	    }
    	else{
    		Toast.makeText(getApplicationContext(), "Please enter an email address..", Toast.LENGTH_LONG).show();
    	}
      }
	}

Now the layout file (main.xml)

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout android:id="@+id/LinearLayout01" android:layout_width="fill_parent"
	android:layout_height="fill_parent"
	xmlns:android="http://schemas.android.com/apk/res/android"
	android:orientation="vertical"
	android:background="@drawable/android"
	>
	<TextView
		android:layout_width="wrap_content"
		android:layout_height="wrap_content"
		android:id="@+id/emailaddress"
		android:text="Email Address"
		android:textStyle="bold">
	</TextView>

	<EditText
		android:layout_width="wrap_content"
		android:layout_height="wrap_content"
		android:width="250dip"
		android:hint="email address"
		android:id="@+id/address">
	</EditText>

	<TextView
		android:layout_width="wrap_content"
		android:layout_height="wrap_content"
		android:text="Subject"
		android:textStyle="bold">
	</TextView>

	<EditText
		android:layout_width="wrap_content"
		android:layout_height="wrap_content"
		android:width="250dip"
		android:hint="Subject"
		android:id="@+id/subject">
	</EditText>

	<TextView
		android:layout_width="wrap_content"
		android:layout_height="wrap_content"
		android:text="Your Message"
		android:textStyle="bold">
	</TextView>
	<EditText
		android:layout_width="fill_parent"
		android:layout_height="wrap_content"
		android:lines="5"
		android:hint="Your message here!!"
		android:id="@+id/body">
	</EditText>
	<Button
		android:layout_width="wrap_content"
		android:layout_height="wrap_content"
		android:id="@+id/send"
		android:text="Send Email"
		android:width="150dip">
	</Button>
</LinearLayout>

The AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.coderzheaven"
      android:versionCode="1"
      android:versionName="1.0">
    <application android:icon="@drawable/icon" android:label="Send Mail Demo">
        <activity android:name=".sendMailDemo"
                  android:label="Send Mail Demo">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
</manifest>

Note: However if you test this in your emulator, it will not work. Install it in your device to test it.

TextView with link in ANDROID…….

Hi all…….

All of you may be familiar with TextViews in ANDROID.
But how many of you know that we have have html as text in a textView. This post is a simple example to show this.

Create a fresh project and copy this code to the main java file.

package com.coderzheaven;

import android.app.Activity;
import android.os.Bundle;
import android.text.Html;
import android.text.method.LinkMovementMethod;
import android.widget.TextView;

public class TextViewLinkDemo extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        TextView tv = (TextView) findViewById(R.id.tv);
        tv.setText( Html.fromHtml("<b>This is a textView with a link </b>  " +
                    " <br /> <a href="http://www.coderzheaven.com">Coderzheaven</a> " +
                    "created in the Java source code using HTML."));
         tv.setMovementMethod(LinkMovementMethod.getInstance());
    }
}

The 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:id="@+id/tv"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="@string/hello"
    />
</LinearLayout>

The AndroidManifest.xml file

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.coderzheaven"
      android:versionCode="1"
      android:versionName="1.0">
    <application android:icon="@drawable/icon" android:label="@string/app_name">
        <activity android:name=".TextViewLinkDemo"
                  android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
</manifest>
Link in TextView Demo

Link in TextView Demo

PHP – echo alternative/shorthand

Hi,

For printing statements in PHP we make use of echo like this.

&lt;?php echo &quot;Coderz Heaven&quot;; ?&gt; 

An alternative way of echo or a shorthand of echo will look like this.

&lt;?=&quot;Coderz Heaven&quot;?&gt;

:)

ANDROID Tabbars Example……..

This is a simple example showing how to use tabbars in ANDROID.
First create a new project and copy this code to it.

package com.coderzheaven;

import android.app.TabActivity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TabHost;
import android.widget.TabHost.TabSpec;

public class tabbar extends TabActivity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        /** TabHost will have Tabs */
        TabHost tabHost = (TabHost)findViewById(android.R.id.tabhost);

        /** TabSpec used to create a new tab.
         * By using TabSpec only we can able to setContent to the tab.
         * By using TabSpec setIndicator() we can set name to tab. */

        /** tid1 is firstTabSpec Id. Its used to access outside. */
        TabSpec firstTabSpec = tabHost.newTabSpec("tab_id1");
        TabSpec secondTabSpec = tabHost.newTabSpec("tab_id2");
        TabSpec thirdTabSpec = tabHost.newTabSpec("tab_id3");

        /** TabSpec setIndicator() is used to set name for the tab. */
        /** TabSpec setContent() is used to set content for a particular tab. */
        firstTabSpec.setIndicator("First").setContent(new Intent(this,FirstTab.class));
        secondTabSpec.setIndicator("Second ").setContent(new Intent(this,SecondTab.class));
        thirdTabSpec.setIndicator("Third").setContent(new Intent(this,ThirdTab.class));

        /** Add tabSpec to the TabHost to display. */
        tabHost.addTab(firstTabSpec);
        tabHost.addTab(secondTabSpec);
        tabHost.addTab(thirdTabSpec);

    }
}

Now create three another java files by right clicking the src folder and name it FirstTab.java, SecondTab.java, ThirdTab.java.

Now copy the following code to FirstTab.java.

package com.coderzheaven;

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

public class FirstTab extends Activity {
	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		/* First Tab Content */
		TextView textView = new TextView(this);
		textView.setText("First Tab");
		setContentView(textView);
	}
}

SecondTab.java

package com.coderzheaven;

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

public class SecondTab extends Activity {
	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		/* Second Tab Content */
		TextView textView = new TextView(this);
		textView.setText("Second Tab");
		setContentView(textView);

	}
}

Now ThirdTab.java.

package com.coderzheaven;

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

public class ThirdTab extends Activity {
	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		/* Second Tab Content */
		TextView textView = new TextView(this);
		textView.setText("Third Tab");
		setContentView(textView);

	}
}

Main.xml file

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

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.coderzheaven"
      android:versionCode="1"
      android:versionName="1.0">
    <application android:icon="@drawable/icon" android:label="@string/app_name">
    <activity android:name=".FirstTab" />
	<activity android:name=".SecondTab" />
    <activity android:name=".ThirdTab" />
        <activity android:name=".tabbar"
                  android:label="TabBar Demo">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
</manifest>

You can place anything in each tab by setting the contentView or dynamically adding controls.
You can do anything with this tabbar by editing the xml, like placing it below or giving images for each tab etc etc….

Please leave your valuable comments…

Using Tabbars in ANDROID, A Simple illustration……….

This is a simple example showing how to use tabbars in ANDROID.
First create a new project and copy this code to it.

package com.coderzheaven;

import android.app.TabActivity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TabHost;
import android.widget.TabHost.TabSpec;

public class tabbar extends TabActivity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        /** TabHost will have Tabs */
        TabHost tabHost = (TabHost)findViewById(android.R.id.tabhost);

        /** TabSpec used to create a new tab.
         * By using TabSpec only we can able to setContent to the tab.
         * By using TabSpec setIndicator() we can set name to tab. */

        /** tid1 is firstTabSpec Id. Its used to access outside. */
        TabSpec firstTabSpec = tabHost.newTabSpec("tab_id1");
        TabSpec secondTabSpec = tabHost.newTabSpec("tab_id2");
        TabSpec thirdTabSpec = tabHost.newTabSpec("tab_id3");

        /** TabSpec setIndicator() is used to set name for the tab. */
        /** TabSpec setContent() is used to set content for a particular tab. */
        firstTabSpec.setIndicator("First").setContent(new Intent(this,FirstTab.class));
        secondTabSpec.setIndicator("Second ").setContent(new Intent(this,SecondTab.class));
        thirdTabSpec.setIndicator("Third").setContent(new Intent(this,ThirdTab.class));

        /** Add tabSpec to the TabHost to display. */
        tabHost.addTab(firstTabSpec);
        tabHost.addTab(secondTabSpec);
        tabHost.addTab(thirdTabSpec);

    }
}

Now create three another java files by right clicking the src folder and name it FirstTab.java, SecondTab.java, ThirdTab.java.

Now copy the following code to FirstTab.java.

package com.coderzheaven;

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

public class FirstTab extends Activity {
	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		/* First Tab Content */
		TextView textView = new TextView(this);
		textView.setText("First Tab");
		setContentView(textView);
	}
}

SecondTab.java

package com.coderzheaven;

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

public class SecondTab extends Activity {
	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		/* Second Tab Content */
		TextView textView = new TextView(this);
		textView.setText("Second Tab");
		setContentView(textView);

	}
}
 

Now ThirdTab.java.

package com.coderzheaven;

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

public class ThirdTab extends Activity {
	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		/* Second Tab Content */
		TextView textView = new TextView(this);
		textView.setText("Third Tab");
		setContentView(textView);

	}
}
 

Main.xml file

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

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="tabbar.pack"
      android:versionCode="1"
      android:versionName="1.0">
    <application android:icon="@drawable/icon" android:label="@string/app_name">
    <activity android:name=".FirstTab" />
	<activity android:name=".SecondTab" />
    <activity android:name=".ThirdTab" />
        <activity android:name=".tabbar"
                  android:label="TabBar Demo">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
</manifest>

You can place anything in each tab by setting the contentView or dynamically adding controls.
You can do anything with this tabbar by editing the xml, like placing it below or giving images for each tab etc etc….

Please leave your valuable comments…

Using Tabbars in ANDROID, A Simple Example……….

This is a simple example showing how to use tabbars in ANDROID.
First create a new project and copy this code to it.

package com.coderzheaven;

import android.app.TabActivity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TabHost;
import android.widget.TabHost.TabSpec;

public class tabbar extends TabActivity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        /** TabHost will have Tabs */
        TabHost tabHost = (TabHost)findViewById(android.R.id.tabhost);

        /** TabSpec used to create a new tab.
         * By using TabSpec only we can able to setContent to the tab.
         * By using TabSpec setIndicator() we can set name to tab. */

        /** tid1 is firstTabSpec Id. Its used to access outside. */
        TabSpec firstTabSpec = tabHost.newTabSpec("tab_id1");
        TabSpec secondTabSpec = tabHost.newTabSpec("tab_id2");
        TabSpec thirdTabSpec = tabHost.newTabSpec("tab_id3");

        /** TabSpec setIndicator() is used to set name for the tab. */
        /** TabSpec setContent() is used to set content for a particular tab. */
        firstTabSpec.setIndicator("First").setContent(new Intent(this,FirstTab.class));
        secondTabSpec.setIndicator("Second ").setContent(new Intent(this,SecondTab.class));
        thirdTabSpec.setIndicator("Third").setContent(new Intent(this,ThirdTab.class));

        /** Add tabSpec to the TabHost to display. */
        tabHost.addTab(firstTabSpec);
        tabHost.addTab(secondTabSpec);
        tabHost.addTab(thirdTabSpec);

    }
}


Now create three another java files by right clicking the src folder and name it FirstTab.java, SecondTab.java, ThirdTab.java.

Now copy the following code to FirstTab.java.

package com.coderzheaven;

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

public class FirstTab extends Activity {
	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		/* First Tab Content */
		TextView textView = new TextView(this);
		textView.setText("First Tab");
		setContentView(textView);
	}
}

SecondTab.java

package com.coderzheaven;

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

public class SecondTab extends Activity {
	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		/* Second Tab Content */
		TextView textView = new TextView(this);
		textView.setText("Second Tab");
		setContentView(textView);

	}
}

Now ThirdTab.java.

package com.coderzheaven;

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

public class ThirdTab extends Activity {
	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		/* Second Tab Content */
		TextView textView = new TextView(this);
		textView.setText("Third Tab");
		setContentView(textView);

	}
}

Main.xml file

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

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="tabbar.pack"
      android:versionCode="1"
      android:versionName="1.0">
    <application android:icon="@drawable/icon" android:label="@string/app_name">
    <activity android:name=".FirstTab" />
	<activity android:name=".SecondTab" />
    <activity android:name=".ThirdTab" />
        <activity android:name=".tabbar"
                  android:label="TabBar Demo">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
</manifest>
TabBar in ANDROID

TabBar in ANDROID Demo

You can place anything in each tab by setting the contentView or dynamically adding controls.
You can do anything with this tabbar by editing the xml, like placing it below or giving images for each tab etc etc….

Please leave your valuable comments…

C Sharp/C# – Read a complete text file

Hi,

Use the following code example to read a complete text file using C Sharp/C#.

using System;
using System.Data;
using System.IO;



class Class1{

  static void Main(string[] args){

      StreamReader sampleStreaming= new StreamReader("trialText.txt");

      Console.WriteLine(sampleStreaming.ReadToEnd());

      sampleStreaming.Close();
    }
}
 :) 


C Sharp/C# – Writing to a text file

Hi,

Use the following example code, if you want to write to a text file using C Sharp/C#.

using System;
using System.IO;

class MainClass
{
    public static void Main()
    {
        FileStream trialStream = new FileStream("sampleText.txt", FileMode.Create);

        StreamWriter trialWrite = new StreamWriter(trialStream );

        trialWrite.WriteLine("{0} {1}", "CoderzHeaven", 100);

        trialWrite.Close();

        trialStream .Close();
    }
}

:)

How to split a String in ANDROID?


Hi all…..
Splitting a string in android is really easy.checkout the following snippet.

package com.coderzheaven;

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

public class SplitStringDemo extends Activity {
    String str = "India, USA, Japan, Russia, France";
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        String arr[] = str.split(",");
        for(int i = 0; i < arr.length; i++){
        	System.out.println("arr["+i+"] = " + arr[i].trim());
        }
    }
}

Check the Logcat for output.
Note: Make sure to put the escape character before the delimiter for splitting,otherwise the output may be each character.

Open a webpage in Titanium (iPhone or ANDROID).

Opening a webpage in Titanium is really simple. Look at the following sample.

function openWebpage(){
	var URL = "http://www.google.com"	;
	Titanium.Platform.openURL(URL);
};

If you are running in iPhone then it will open safari and if it is ANDROID, it will open the default browser with the supplied URL String.
Happy coding……..

Please leave your valuable comments.