Monday, September 12, 2011

Blackberry Color Label Field

Following code example use for create custom color label filed.


import net.rim.device.api.i18n.ResourceBundleFamily;
import net.rim.device.api.ui.Graphics;
import net.rim.device.api.ui.component.LabelField;

public class ColorLableField extends LabelField{
private int color = 0x000000;
public ColorLableField(String text){
super(text);
}
public ColorLableField(Object text, int offset, int length, long style){
super(text, offset, length, style);
}
public ColorLableField(Object text, long style){
super(text, style);
}
public ColorLableField(ResourceBundleFamily rb, int key){
super(rb, key);
}

//Set the color of the text in this field
public void setColor(int newColor){
color = newColor;
}

//Set the foreground color and then call paint method of the parent class
public void paint(Graphics graphics){
graphics.setColor(color);
super.paint(graphics);
}
}

public final class MyScreen extends MainScreen{

public MyScreen()
{
setTitle("MyTitle");
ColorLableField lbl = new ColorLableField("this is text");
lbl.setColor(Color.RED);
this.add(lbl);
}
}

Monday, August 1, 2011

Call RSS Feed In android

Here Example of Call RSS Feed in android application or Java.
public void writeNews() {
try {
DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
URL u = new URL("http://www.itpro.co.uk/blogs/feed/"); // your feed url
Document doc = builder.parse(u.openStream());
NodeList nodes = doc.getElementsByTagName("item");
for (int i = 0; i < nodes.getLength(); i++) {
Element element = (Element) nodes.item(i);
System.out.println("Title: " + getElementValue(element, "title"));
System.out.println("Link: " + getElementValue(element, "link"));
System.out.println("Publish Date: " + getElementValue(element, "pubDate"));
System.out.println("Author: " + getElementValue(element, "dc:creator"));
System.out.println("Description: " + getElementValue(element, "description"));
System.out.println();
}//for
}//try
catch (Exception ex) {
ex.printStackTrace();
}
}

private String getCharacterDataFromElement(Element e) {
try {
Node child = e.getFirstChild();
if (child instanceof CharacterData) {
CharacterData cd = (CharacterData) child;
return cd.getData();
}
} catch (Exception ex) {
}
return "";
} //private String getCharacterDataFromElement

protected float getFloat(String value) {
if (value != null && !value.equals("")) {
return Float.parseFloat(value);
} else {
return 0;
}
}

protected String getElementValue(Element parent, String label) {
return getCharacterDataFromElement((Element) parent.getElementsByTagName(label).item(0));
}

Friday, July 1, 2011

One difference between implementing Runnable and extending Thread is that by extending Thread, each of your threads has a unique object associated with it, whereas implementing Runnable, many threads can share the same Object instance. for example.


public class Main {

   public static void main(String[] args) {
     RunnableThread r = new RunnableThread();
     Thread t1 = new Thread(r, "Thread A");
     Thread t2 = new Thread(r, "Thread B");
     ExtendsThread t3 = new ExtendsThread("Thread C");
     ExtendsThread t4 = new ExtendsThread("Thread D");
     t1.start();
     t2.start();
     t3.start();
     t4.start();

   }
 }



class RunnableThread implements Runnable {

private int counter;

  public void run() {
   try {
     for (int i = 0; i != 2; i++) {
   System.out.println(Thread.currentThread().getName()+ ":"counter++);
      Thread.sleep(1000);
      }
    } catch (InterruptedException ine) {
    System.err.println(ine);
   }
 }
}

class ExtendsThread extends Thread {
  private int counter;
  ExtendsThread(String name) {
  super(name);
 }

 @Override
 public void run() {
  try {
   for (int i = 0; i != 2; i++) {
   System.out.println(Thread.currentThread().getName() + ": "+ counter++);
   Thread.sleep(1000);
   }
  } catch (InterruptedException ine) {
   System.err.println(ine);
 }
 }
}

Thursday, June 30, 2011

9 ways to improve your programming skills.

1. Learn a new programming language.
2. Read a good, challenging programming book.
Like: The Art of Computer Programming
Structure and interpretation of computer Programs.
A Discipline of Programming
3. Join an Open Source Project.
You Can join: GitHub, SourceForge,gitorius, bitBucket, Ohloh
4. Solve Programming puzzles.
5. Program Start writing a program. scratch. design all of the architechture and implement.
6. Read And Study Code.
7. Hang Out as programming sites and read blogs.
8. Write about coding.
9. learn low level programming like C, Assembly language.

JUnit Test Case with Example.

Here is Sample example on Junit Text Case.

Let's Test Some Code

public class Main {

public static void main(String[] args) {
}
static public int add(int a, int b) {
return a + b;
}

}


now i want to test about class using Junit Test case. fot test we require to import "junit.framework.*" and extend TestCase

import junit.framework.*;
public class TestMaths extends TestCase{

public void testMaths() {
int num1 = 2;
int num2 = 3;
int total = 5;
int sum = 0;
sum = Main.add(num1, num2);
assertEquals(sum, total);
}
}


Here i am using Netbeans. now run TestMaths file you can see out put on console.

Summary:
That's it! This example may be help full to getting start JUnit.

enjoy!

Tuesday, June 21, 2011

Android Flip View Example

Here i try to explain Android Flip View. Flip View is use to flip one view to another with use android set of animation.

Create Following Activity FlipeActivity.java file
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.ViewFlipper;

public class FlipeActivity extends Activity {
ViewFlipper flipper;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
flipper=(ViewFlipper)findViewById(R.id.details);
flipper.setInAnimation(AnimationUtils.loadAnimation(this, R.anim.push_left_in));
flipper.setOutAnimation(AnimationUtils.loadAnimation(this, R.anim.push_left_out));
Button btn=(Button)findViewById(R.id.flip_me);
btn.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
flipper.showNext();
}
});

}


}

we load here Animation xml file so we create first "anim" folder and create following file with set.
push_left_in.xml
xmlns:android="http://schemas.android.com/apk/res/android">
android:fromXDelta="100%p" android:toXDelta="15" android:duration="500"/>


push_left_out.xml
xmlns:android="http://schemas.android.com/apk/res/android">
android:fromXDelta="0" android:toXDelta="-100%p" android:duration="500"/>

main.xml
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>

you can find more animation set here with example..
http://code.google.com/p/android-helloworld-samples/source/browse/trunk/ApiDemos/res/anim/

Monday, June 20, 2011

Android Services Example

Create Following Class for Services.

import android.app.Service;
import android.content.Intent;

import android.os.IBinder;
import android.util.Log;


public class MyServices extends Service{
private static final String TAG = MyServices.class.getSimpleName();
@Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return null;
}
@Override
public void onCreate() {
super.onCreate();
Log.d(TAG,"MyServices Created");
}
@Override
public void onStart(Intent intent, int startId) {
super.onStart(intent, startId);
Log.d(TAG,"MyServices Started");
}
@Override
public void onDestroy() {
super.onDestroy();
Log.d(TAG,"MyServices Destroyd");
}

}

Register Services in AndroidManifest.xml file.

service android:name=".MyServices"


Call(Start) Services in Activity.

startService(new Intent(this,MyServices.class));