<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>vimviv.com knowledge base</title>
	<atom:link href="http://blog.vimviv.com/index.php/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.vimviv.com</link>
	<description>blog.vimviv.com</description>
	<lastBuildDate>Sun, 20 Nov 2011 10:53:18 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>voice command example using proximity sensor, voice recognizer and TTS in android</title>
		<link>http://blog.vimviv.com/android/proximity-sensor-voice-recognizer-tts-android/</link>
		<comments>http://blog.vimviv.com/android/proximity-sensor-voice-recognizer-tts-android/#comments</comments>
		<pubDate>Sat, 19 Nov 2011 10:51:01 +0000</pubDate>
		<dc:creator>Administrator</dc:creator>
				<category><![CDATA[Android]]></category>

		<guid isPermaLink="false">http://blog.vimviv.com/?p=554</guid>
		<description><![CDATA[In this example we will combine some features of android like proximity sensor, voice recognizer and TTS. Proximity Sensor: is sensor that can recognize presence of nearby object. we will user this sensor to start voice recognition. So when you take you android device near to your face, proximity sensor will start the voice recognizer. [...]
Related posts:<ol>
<li><a href='http://blog.vimviv.com/android/intent-android/' rel='bookmark' title='What is Intent in android?'>What is Intent in android?</a></li>
<li><a href='http://blog.vimviv.com/android/prompt-user-enable-location-providers-android/' rel='bookmark' title='Prompt user to enable location providers in Android'>Prompt user to enable location providers in Android</a></li>
<li><a href='http://blog.vimviv.com/android/intent-filter-android/' rel='bookmark' title='What is intent filter in android?'>What is intent filter in android?</a></li>
</ol>]]></description>
			<content:encoded><![CDATA[<p>In this example we will combine some features of android like proximity sensor, voice recognizer and TTS.</p>
<p><strong>Proximity Sensor:</strong> is sensor that can recognize presence of nearby object. we will user this sensor to start voice recognition. So when you take you android device near to your face, proximity sensor will start the voice recognizer.</p>
<p><strong>Voice Recognizer: </strong>can convert speech to text. So when you give the voice command, its will return an array of matches and will start TTS.</p>
<p><strong>TTS(Text To Speech):</strong> can convert text speech. In this application we will use TTS to listen voice command response.<br />
<span id="more-554"></span><br />
<strong>1.</strong> First we will check to see if proximity sensor is available.</p>
<pre name="code" class="java">
SensorManager sensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
		Sensor proximitySensor = sensorManager
				.getDefaultSensor(Sensor.TYPE_PROXIMITY);

		if (proximitySensor == null) {
			Toast.makeText(this, "No proximity sensor", Toast.LENGTH_SHORT)
					.show();
		} else {
			sensorManager.registerListener(proximitySensorEventListener,
					proximitySensor, SensorManager.SENSOR_DELAY_NORMAL);
		}
</pre>
<p><strong>2.</strong> We will catch proximity event in onSensorChanged callback method in proximitySensorEventListener.</p>
<pre name="code" class="java">
SensorEventListener proximitySensorEventListener = new SensorEventListener() {

		@Override
		public void onAccuracyChanged(Sensor sensor, int accuracy) {
			// TODO Auto-generated method stub

		}

		@Override
		public void onSensorChanged(SensorEvent event) {
			if (event.sensor.getType() == Sensor.TYPE_PROXIMITY) {
				if (event.values[0] < proximitySensor.getMaximumRange()) {
					startSpeechRecognizer();
				}

			}
		}
	};
</pre>
<p><strong>3.</strong> Start speech recognizer, when any object comes near to mobile.</p>
<pre name="code" class="java">
private static final int VOICE_REQUEST_CODE = 101;
private void startSpeechRecognizer() {
		PackageManager pm = getPackageManager();
		List<ResolveInfo> activities = pm.queryIntentActivities(new Intent(
				RecognizerIntent.ACTION_RECOGNIZE_SPEECH), 0);
		if (activities.size() != 0) {
		Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
		intent.putExtra(RecognizerIntent.ACTION_RECOGNIZE_SPEECH,
				RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
		intent.putExtra(RecognizerIntent.EXTRA_PROMPT, "Voice Command");
		startActivityForResult(intent, VOICE_REQUEST_CODE);
		}else{
			Toast.makeText(this, "Speech Recognizer is not present", Toast.LENGTH_SHORT)
			.show();
		}

	}
</pre>
<p><strong>Explanation of above code:</strong><br />
<strong>queryIntentActivities:</strong> will return the list of activities that can handle action ACTION_RECOGNIZE_SPEECH.<br />
<strong>ACTION_RECOGNIZE_SPEECH:</strong> start implicit activity that can handle action ACTION_RECOGNIZE_SPEECH.<br />
<strong>EXTRA_PROMPT:</strong> It will show custom message "Voice Command" on voice recognizer activity.<br />
<a href="http://blog.vimviv.com/wp-content/uploads/2011/11/voice-command.png"><img src="http://blog.vimviv.com/wp-content/uploads/2011/11/voice-command.png" alt="Voice Command" title="voice-command" width="320" height="480" class="aligncenter size-full wp-image-559" /></a><br />
<strong>4.</strong> We will get result of speech recognizer in onActivityResult callback.</p>
<pre name="code" class="java">
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		if (requestCode == VOICE_REQUEST_CODE &#038;&#038; resultCode == RESULT_OK) {
			ArrayList<String> matches = data
					.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
			for (int i = 0; i < commands.length; i++) {
				if (matches.contains(commands[i])) {
					selectedCommand = i;
				}
			}
			if (isTtsInitialized) {
				if (selectedCommand >= 0) {
					tts.speak("you have selected" + commands[selectedCommand]
							+ " command.", TextToSpeech.QUEUE_FLUSH, null);
				} else {
					tts.speak("I am sorry. could you please repeat that.", TextToSpeech.QUEUE_FLUSH, null);
				}

			}

		}
}
</pre>
<p><strong>Explanation of above code:</strong></p>
<p><strong>requestCode and resultCode:</strong> first we will check requestCode and resultCode to insure that onActivityResult is called by speech recognizer with success response.<br />
<strong>getStringArrayListExtra:</strong> We will get all matches using getStringArrayListExtra. If matches contains our commands, TTS will say that otherwise it will say "I am sorry. could you please repeat that.".</p>
<p><strong>5.</strong> We will check to see if TTS is present. First we need to implement OnInitListener and override onInit method.</p>
<pre name="code" class="java">
@Override
	public void onInit(int status) {
		if (status == TextToSpeech.SUCCESS) {
			int result = tts.setLanguage(Locale.US);
			if (result == TextToSpeech.LANG_MISSING_DATA
					|| result == TextToSpeech.LANG_NOT_SUPPORTED) {
				Toast.makeText(this, "Language is not available",
						Toast.LENGTH_SHORT);
			} else {
				isTtsInitialized = true;
			}
		} else {
			Toast.makeText(this, "Initialization failed", Toast.LENGTH_SHORT);
		}

	}
</pre>
<p>After completion of TTS engine initialization start the TTs.</p>
<pre name="code" class="java">
Intent checkIntent = new Intent();
		checkIntent.setAction(TextToSpeech.Engine.ACTION_CHECK_TTS_DATA);
		startActivityForResult(checkIntent, TTS_REQUEST_CODE);
</pre>
<p><strong>6.</strong> We will get response in onActivityResult.</p>
<pre name="code" class="java">
@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {

		if (requestCode == TTS_REQUEST_CODE) {
			if (resultCode == TextToSpeech.Engine.CHECK_VOICE_DATA_PASS) {
				// success, create the TTS instance
				tts = new TextToSpeech(this, this);
			} else {
				// missing data, install it
				Intent installIntent = new Intent();
				installIntent
						.setAction(TextToSpeech.Engine.ACTION_INSTALL_TTS_DATA);
				startActivity(installIntent);
			}
		}
		super.onActivityResult(requestCode, resultCode, data);
	}
</pre>
<p><strong>7.</strong> Don't forget to unregister proximity sensor listener.</p>
<pre name="code" class="java">
@Override
	protected void onStop() {
		if (sensorManager != null) {
			sensorManager.unregisterListener(proximitySensorEventListener);
		}
		super.onStop();
	}
</pre>
<p>Instead of just listening the commands we can do lots of things like opening address book, messages, mails, etc. Here is the complete code.</p>
<pre name="code" class="java">
package com.vimviv;

import java.util.ArrayList;
import java.util.List;
import java.util.Locale;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.speech.RecognizerIntent;
import android.speech.tts.TextToSpeech;
import android.speech.tts.TextToSpeech.OnInitListener;
import android.widget.Toast;

public class VoiceCommandActivity extends Activity implements OnInitListener {

	private static final int VOICE_REQUEST_CODE = 101;
	private static final int TTS_REQUEST_CODE = 102;
	private static final String[] commands = { "open", "close", "find" };

	private int selectedCommand = -1;
	private String speachMessage = "I am sorry. could you please repeat that.";
	private boolean isTtsInitialized = false;
	TextToSpeech tts;
	SensorManager sensorManager;
	Sensor proximitySensor;

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

		// check proximity sensor
		sensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
		proximitySensor = sensorManager
				.getDefaultSensor(Sensor.TYPE_PROXIMITY);

		if (proximitySensor == null) {
			Toast.makeText(this, "No proximity sensor", Toast.LENGTH_SHORT)
					.show();
		} else {
			sensorManager.registerListener(proximitySensorEventListener,
					proximitySensor, SensorManager.SENSOR_DELAY_NORMAL);
		}

		//check for TTS data
		Intent checkIntent = new Intent();
		checkIntent.setAction(TextToSpeech.Engine.ACTION_CHECK_TTS_DATA);
		startActivityForResult(checkIntent, TTS_REQUEST_CODE);

	}

	SensorEventListener proximitySensorEventListener = new SensorEventListener() {

		@Override
		public void onAccuracyChanged(Sensor sensor, int accuracy) {
			// TODO Auto-generated method stub

		}

		@Override
		public void onSensorChanged(SensorEvent event) {
			// TODO Auto-generated method stub

			if (event.sensor.getType() == Sensor.TYPE_PROXIMITY) {
				if (event.values[0] < proximitySensor.getMaximumRange()) {
					startSpeachRecognition();
				}

			}
		}
	};

	private void startSpeachRecognition() {
		// Check to see if a recognition activity is present
				PackageManager pm = getPackageManager();
				List<ResolveInfo> activities = pm.queryIntentActivities(new Intent(
						RecognizerIntent.ACTION_RECOGNIZE_SPEECH), 0);
				if (activities.size() != 0) {
					Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
					intent.putExtra(RecognizerIntent.ACTION_RECOGNIZE_SPEECH,
							RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
					intent.putExtra(RecognizerIntent.EXTRA_PROMPT, "Voice Command");
					startActivityForResult(intent, VOICE_REQUEST_CODE);
				} else {
					Toast.makeText(this, "Recognizer not present", Toast.LENGTH_SHORT)
					.show();
				}

	}

	// Initialization response of TTS engine
	@Override
	public void onInit(int status) {
		if (status == TextToSpeech.SUCCESS) {
			int result = tts.setLanguage(Locale.US);
			if (result == TextToSpeech.LANG_MISSING_DATA
					|| result == TextToSpeech.LANG_NOT_SUPPORTED) {
				Toast.makeText(this, "Language is not available",
						Toast.LENGTH_SHORT);
			} else {
				isTtsInitialized = true;
			}
		} else {
			Toast.makeText(this, "Initialization failed", Toast.LENGTH_SHORT);
		}

	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		if (requestCode == VOICE_REQUEST_CODE &#038;&#038; resultCode == RESULT_OK) {
			ArrayList<String> matches = data
					.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
			for (int i = 0; i < commands.length; i++) {
				if (matches.contains(commands[i])) {
					selectedCommand = i;
				}
			}
			if (isTtsInitialized) {
				if (selectedCommand >= 0) {
					tts.speak("you have selected" + commands[selectedCommand]
							+ " command.", TextToSpeech.QUEUE_FLUSH, null);
					selectedCommand = -1;
				} else {
					tts.speak(speachMessage, TextToSpeech.QUEUE_FLUSH, null);
				}

			}

		}
		if (requestCode == TTS_REQUEST_CODE) {
			if (resultCode == TextToSpeech.Engine.CHECK_VOICE_DATA_PASS) {
				// success, create the TTS instance
				tts = new TextToSpeech(this, this);
			} else {
				// missing data, install it
				Intent installIntent = new Intent();
				installIntent
						.setAction(TextToSpeech.Engine.ACTION_INSTALL_TTS_DATA);
				startActivity(installIntent);
			}
		}
		super.onActivityResult(requestCode, resultCode, data);
	}

	@Override
	protected void onStop() {
		if (sensorManager != null) {
			sensorManager.unregisterListener(proximitySensorEventListener);
		}
		super.onStop();
	}

}
</pre>
<h4>Incoming search terms:</h4><ul><li><a href="http://blog.vimviv.com/android/proximity-sensor-voice-recognizer-tts-android/" title="android: example on proximity sensor">android: example on proximity sensor</a> (14)</li><li><a href="http://blog.vimviv.com/android/proximity-sensor-voice-recognizer-tts-android/" title="RecognizerIntent ACTION_RECOGNIZE_SPEECH">RecognizerIntent ACTION_RECOGNIZE_SPEECH</a> (14)</li><li><a href="http://blog.vimviv.com/android/proximity-sensor-voice-recognizer-tts-android/" title="voice command">voice command</a> (10)</li><li><a href="http://blog.vimviv.com/android/proximity-sensor-voice-recognizer-tts-android/" title="proximity sensor android example">proximity sensor android example</a> (8)</li><li><a href="http://blog.vimviv.com/android/proximity-sensor-voice-recognizer-tts-android/" title="voice recognition android example">voice recognition android example</a> (4)</li><li><a href="http://blog.vimviv.com/android/proximity-sensor-voice-recognizer-tts-android/" title="proximity sensor android">proximity sensor android</a> (3)</li><li><a href="http://blog.vimviv.com/android/proximity-sensor-voice-recognizer-tts-android/" title="android proximity sensor example">android proximity sensor example</a> (3)</li><li><a href="http://blog.vimviv.com/android/proximity-sensor-voice-recognizer-tts-android/" title="android voice command sample code">android voice command sample code</a> (3)</li><li><a href="http://blog.vimviv.com/android/proximity-sensor-voice-recognizer-tts-android/" title="android toast example">android toast example</a> (3)</li><li><a href="http://blog.vimviv.com/android/proximity-sensor-voice-recognizer-tts-android/" title="android proximity sensor java">android proximity sensor java</a> (2)</li></ul><div style='clear:both'></div><p>Related posts:</p><ol>
<li><a href='http://blog.vimviv.com/android/intent-android/' rel='bookmark' title='What is Intent in android?'>What is Intent in android?</a></li>
<li><a href='http://blog.vimviv.com/android/prompt-user-enable-location-providers-android/' rel='bookmark' title='Prompt user to enable location providers in Android'>Prompt user to enable location providers in Android</a></li>
<li><a href='http://blog.vimviv.com/android/intent-filter-android/' rel='bookmark' title='What is intent filter in android?'>What is intent filter in android?</a></li>
</ol>]]></content:encoded>
			<wfw:commentRss>http://blog.vimviv.com/android/proximity-sensor-voice-recognizer-tts-android/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>What is intent filter in android?</title>
		<link>http://blog.vimviv.com/android/intent-filter-android/</link>
		<comments>http://blog.vimviv.com/android/intent-filter-android/#comments</comments>
		<pubDate>Thu, 10 Nov 2011 15:35:49 +0000</pubDate>
		<dc:creator>Administrator</dc:creator>
				<category><![CDATA[Android]]></category>

		<guid isPermaLink="false">http://blog.vimviv.com/?p=534</guid>
		<description><![CDATA[In my previous post, What is Intent in android? I have disused about intents. In that post we have seen that there are two types of intents. 1. Explicit intents: We can call target component by its name. 2. implicit intents: We can call a target component on the basis of what we want to [...]
Related posts:<ol>
<li><a href='http://blog.vimviv.com/android/intent-android/' rel='bookmark' title='What is Intent in android?'>What is Intent in android?</a></li>
<li><a href='http://blog.vimviv.com/android/prompt-user-enable-location-providers-android/' rel='bookmark' title='Prompt user to enable location providers in Android'>Prompt user to enable location providers in Android</a></li>
<li><a href='http://blog.vimviv.com/android/started-android/' rel='bookmark' title='Getting Started With Android'>Getting Started With Android</a></li>
</ol>]]></description>
			<content:encoded><![CDATA[<p>In my previous post, <a href="http://blog.vimviv.com/android/intent-android/">What is Intent in android?</a> I have disused about intents. In that post we have seen that there are two types of intents.<br />
<strong>1. Explicit intents:</strong> We can call target component by its name.<br />
<strong>2. implicit intents:</strong> We can call a target component on the basis of what we want to perform.</p>
<p>In this post we will see, how does implicit intent work?<br />
According to google  intent filter is &#8220;To inform the system which implicit intents they can handle&#8221;</p>
<p>e.g. you have more that one activities in your application  and you know that there is no &#8220;main&#8221; method in android application. So how activity launcher will know that which activity to start first.<br />
<span id="more-534"></span><br />
For that we specify a intent filter.</p>
<pre name="code" class="xml">
<activity android:name=".HomeActivity"
                  android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
</pre>
<p>intent filter with &#8220;android.intent.action.MAIN&#8221; action and &#8220;android.intent.category.LAUNCHER&#8221; category tells to launcher to launch HomeActivity.</p>
<p>implicit Intent is luxury provided by android. You just need to know, what do you want to perform. you don&#8217;t bother who is going to perform and how that activity is going to perform.(there will be a problem, if no activity is there to perform your task)</p>
<p><strong>When will you specify intent filter?</strong><br />
if you think, any other application can use your activity to perform some task, specify a intent filter for that. You can be a performer using intent filter.</p>
<p>For example, you have created a activity that can display image and you think that other applications also use your activity to view images.</p>
<p><strong>1.</strong> specify intent filter like this.</p>
<pre name="code" class="xml">
<activity
            android:label="@string/app_name"
            android:name=".MyImageViewerActivity" >
            <intent-filter >
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
            <intent-filter >
                <action android:name="android.intent.action.VIEW" />

                <category android:name="android.intent.category.DEFAULT" />

                <data android:mimeType="image/*" />
            </intent-filter>
</pre>
<p>In the above code we can see that intent-filter has following properties.<br />
<strong>a. action: </strong>type for which this activity will respond for.Like this activity will respond for view action.<br />
<strong>b. category: </strong>implicit intents are divided in categories.Like this activity is in default category.<br />
<strong>c. data: </strong>Specify more information about intent-filter. Like this activity will handle file of mimeType &#8220;image/*&#8221;.</p>
<p><strong>2.</strong> Now you need to display image in your activity.</p>
<pre name="code" class="java">
public class MyImageViewerActivity extends Activity {

	TextView textView;
	ImageView imageView;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        Intent intent = getIntent();
        Uri uri = intent.getData();

        textView = (TextView) findViewById(R.id.textView);
        imageView = (ImageView) findViewById(R.id.imageView);

        if(uri != null){
        	imageView.setImageURI(uri);
        	textView.setText(uri.getPath());
        }

    }
}
</pre>
<p>using getData() we will get the path of the image and we will use that path to display image. </p>
<p><strong>3. </strong>When you install this application in your device and click on any image file, you will see a choice dialog that offer you to select between default application and your application. Just select your application to see the image.</p>
<p><a href="http://blog.vimviv.com/wp-content/uploads/2011/11/intent-filter.png"><img src="http://blog.vimviv.com/wp-content/uploads/2011/11/intent-filter.png" alt="intent filter choice dailog" title="intent-filter" width="320" height="480" class="aligncenter size-full wp-image-535" /></a></p>
<p><strong>4.</strong> Just select your app to view image:</p>
<p><a href="http://blog.vimviv.com/wp-content/uploads/2011/11/intent-filter-activity1.png"><img src="http://blog.vimviv.com/wp-content/uploads/2011/11/intent-filter-activity1.png" alt="intent filter activity" title="intent filter activity" width="320" height="480" class="aligncenter size-full wp-image-543" /></a><br />
<strong>When will you call implicit intent?</strong><br />
If you think that any other activity is available in device for handling your application&#8217;s specific task, just use the implicit intent to call that activity.</p>
<p>For example, if you want to open a browser to render URI. You can use implicit intent to call browser.</p>
<pre name="code" class="java">
Uri uri = Uri.parse( "http://blog.vimviv.com" );
startActivity(new Intent(Intent.ACTION_VIEW, uri));
</pre>
<p>In above example we can see that we have a uri and we want to view content of that uri. We don&#8217;t need to know that which activity is going to render this uri.</p>
<p><strong>Some more examples of intent filters.</strong><br />
a) intent filter for app launcher</p>
<pre name="code" class="xml">
			<intent-filter>
				<action android:name="android.intent.action.MAIN" />
				<category android:name="android.intent.category.LAUNCHER" />
			</intent-filter>
</pre>
<p>b)intent filter for image view.</p>
<pre name="code" class="xml">
			<intent-filter>
				<action android:name="android.intent.action.VIEW" />
				<category android:name="android.intent.category.DEFAULT" />
				<data android:mimeType="image/*" />
			</intent-filter>
</pre>
<p>c)intent filter for image send:</p>
<pre name="code" class="xml">
			<intent-filter>
				<action android:name="android.intent.action.SEND" />
				<category android:name="android.intent.category.DEFAULT" />
				<data android:mimeType="image/*" />
			</intent-filter>
</pre>
<p>d) intent filter for custom uri:</p>
<pre name="code" class="xml">
			<intent-filter>
				<action android:name="android.intent.action.VIEW" />
				<category android:name="android.intent.category.DEFAULT" />
				<category android:name="android.intent.category.BROWSABLE" />
				<data android:scheme="myapp" android:host="path" />
			</intent-filter>
</pre>
<p>e) intent filter for sms:</p>
<pre name="code" class="xml">
			<intent-filter>
				<action android:name="android.intent.action.VIEW" />
				<action android:name="android.intent.action.SENDTO" />
				<category android:name="android.intent.category.DEFAULT" />
				<category android:name="android.intent.category.BROWSABLE" />
				<data android:scheme="sms" />
				<data android:scheme="smsto" />
			</intent-filter>
</pre>
<p>f)intent filter phone dial:</p>
<pre name="code" class="xml">
            <intent-filter>
                <action android:name="android.intent.action.VIEW" />
                <action android:name="android.intent.action.DIAL" />
                <category android:name="android.intent.category.DEFAULT" />
                <category android:name="android.intent.category.BROWSABLE" />
                <data android:scheme="tel" />
            </intent-filter>
           </pre>
<p>Some examples for calling implicit intents:<br />
a) start contacts activity:</p>
<pre name="code" class="java">
Intent contacts = new Intent();
              contacts.setAction(android.content.Intent.ACTION_VIEW);
              contacts.setData(People.CONTENT_URI);
              startActivity(contacts);
</pre>
<p>b) install an app:</p>
<pre name="code" class="java">
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.fromFile(new File(Environment.getExternalStorageDirectory() + "app.apk")), "application/vnd.android.package-archive");
startActivity(intent);  
</pre>
<p>c) uninstall an app:</p>
<pre name="code" class="java">
Uri packageUri = Uri.parse("package:com.vimviv");
            Intent uninstallIntent =
              new Intent(Intent. ACTION_DELETE, packageUri);
            startActivity(uninstallIntent);
</pre>
<p>d) start sms application:</p>
<pre name="code" class="java">
Intent sendIntent = new Intent(Intent.ACTION_VIEW);        
sendIntent.setData(Uri.parse("sms:"));
</pre>
<p>We can also search for a matching intent:</p>
<pre name="code" class="java">
final PackageManager packageManager = context.getPackageManager();
    final Intent intent = new Intent(action);
    List<ResolveInfo> list =
            packageManager.queryIntentActivities(intent,
                    PackageManager.MATCH_DEFAULT_ONLY);
    return list.size() > 0;
</pre>
<p>So help others by creating re-usable activities.</p>
<h4>Incoming search terms:</h4><ul><li><a href="http://blog.vimviv.com/android/intent-filter-android/" title="what is intent filter in android">what is intent filter in android</a> (14)</li><li><a href="http://blog.vimviv.com/android/intent-filter-android/" title="intent filter image">intent filter image</a> (9)</li><li><a href="http://blog.vimviv.com/android/intent-filter-android/" title="intentfilter">intentfilter</a> (5)</li><li><a href="http://blog.vimviv.com/android/intent-filter-android/" title="what is intent filter">what is intent filter</a> (4)</li><li><a href="http://blog.vimviv.com/android/intent-filter-android/" title="android choice dialog">android choice dialog</a> (4)</li><li><a href="http://blog.vimviv.com/android/intent-filter-android/" title="what is the use of intent filter">what is the use of intent filter</a> (4)</li><li><a href="http://blog.vimviv.com/android/intent-filter-android/" title="example on implicit intents in android">example on implicit intents in android</a> (4)</li><li><a href="http://blog.vimviv.com/android/intent-filter-android/" title="intent-filter">intent-filter</a> (3)</li><li><a href="http://blog.vimviv.com/android/intent-filter-android/" title="intent filter android">intent filter android</a> (3)</li><li><a href="http://blog.vimviv.com/android/intent-filter-android/" title="usage of intent filter in android">usage of intent filter in android</a> (3)</li></ul><div style='clear:both'></div><p>Related posts:</p><ol>
<li><a href='http://blog.vimviv.com/android/intent-android/' rel='bookmark' title='What is Intent in android?'>What is Intent in android?</a></li>
<li><a href='http://blog.vimviv.com/android/prompt-user-enable-location-providers-android/' rel='bookmark' title='Prompt user to enable location providers in Android'>Prompt user to enable location providers in Android</a></li>
<li><a href='http://blog.vimviv.com/android/started-android/' rel='bookmark' title='Getting Started With Android'>Getting Started With Android</a></li>
</ol>]]></content:encoded>
			<wfw:commentRss>http://blog.vimviv.com/android/intent-filter-android/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Custom Annotation in Android</title>
		<link>http://blog.vimviv.com/android/custom-annotation-android/</link>
		<comments>http://blog.vimviv.com/android/custom-annotation-android/#comments</comments>
		<pubDate>Fri, 14 Oct 2011 15:33:02 +0000</pubDate>
		<dc:creator>Administrator</dc:creator>
				<category><![CDATA[Android]]></category>

		<guid isPermaLink="false">http://blog.vimviv.com/?p=522</guid>
		<description><![CDATA[For definition of java annotation i can&#8217;t think a better definition than wikipedia &#8220;An annotation, in the Java computer programming language, is a special form of syntactic metadata that can be added to Java source code. Classes, methods, variables, parameters and packages may be annotated. Unlike Javadoc tags, Java annotations can be reflective in that [...]
Related posts:<ol>
<li><a href='http://blog.vimviv.com/android/started-android/' rel='bookmark' title='Getting Started With Android'>Getting Started With Android</a></li>
</ol>]]></description>
			<content:encoded><![CDATA[<p>For definition of java annotation i can&#8217;t think a better definition than wikipedia &#8220;An annotation, in the Java computer programming language, is a special form of syntactic metadata that can be added to Java source code. Classes, methods, variables, parameters and packages may be annotated. Unlike Javadoc tags, Java annotations can be reflective in that they can be embedded in class files generated by the compiler and may be retained by the Java VM to be made retrievable at run-time.&#8221; <a href="http://en.wikipedia.org/wiki/Java_annotation">http://en.wikipedia.org/wiki/Java_annotation</a>  Some of  the java programmers use annotations without even know about annotations. When ever you override any method of super class in you class, eclipse automatically adds the @override annotation above method name.<br />
<span id="more-522"></span><br />
 e.g.</p>
<pre name="code" class="java">
@Override
    public String toString() {
    	return super.toString();
    }
</pre>
<p>In the above example code,<br />
<strong>@Override: </strong>gives the information that toString() method is overridden from Object class.<br />
In this post we will create custom annotations.   </p>
<p>1. First we will create a custom annotation for Database table name.</p>
<pre name="code" class="java">
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
public @interface DatabaseTable {

	String tableName();
}
</pre>
<p>Explanation of above code:<br />
<strong>@interface:</strong> We can see in the above code is similar to interface declaration. <strong>@Retention(RetentionPolicy.RUNTIME):</strong> We need this annotation type at runtime. We are using RetentionPolicy.RUNTIME.<br />
<strong>@Target(ElementType.TYPE): </strong> Indicates that this annotation type will be used to annotate class declaration  </p>
<p>2. Next we will create another annotation for column names.</p>
<pre name="code" class="java">
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.FIELD)
public @interface DatabaseField {

	String columnName() default "";
	String columnType();
	boolean canBeNull() default true;

}
</pre>
<p>Explanation of above code:<br />
<strong>@Target(ElementType.FIELD):</strong> Indicates that this annotation type will used to annotate fields declarations.<br />
<strong>default:</strong> is used to assign default value.  </p>
<p>3. We will create UserInfo class that we want to save in database.</p>
<pre name="code" class="java">
@DatabaseTable (tableName = "user")
public class UserData {

	@DatabaseField (columnName = "first_name", columnType ="TEXT")
	private String firstName;
	@DatabaseField (columnName = "last_name", columnType ="TEXT")
	private String lastName;
	@DatabaseField (columnName = "age", columnType ="INTEGER")
	private int age;

}
</pre>
<p>4. Create query for create table.</p>
<pre name="code" class="java">
public String createTableQuery(Class clazz){
		DatabaseTable annot = (DatabaseTable) clazz.getAnnotation(DatabaseTable.class);
		String table = annot.tableName();
		Field[] fields = clazz.getDeclaredFields();
		StringBuilder sb = new StringBuilder("CREATE TABLE ").append(table).append(
						" (_id integer primary key");
				for (Field field : fields) {
					DatabaseField fieldAnnot = (DatabaseField) field.getAnnotation(DatabaseField.class);
					String columnName = fieldAnnot.columnName();
					String columnType = fieldAnnot.columnType();
					sb.append(", ").append(columnName).append(" ").append(columnType);
				}
				sb.append(")");
				return sb.toString();
		}
</pre>
<p>5. We will call this method like this</p>
<pre name="code" class="java">
String sqlQuery = createTableQuery(UserInfo.Class);
</pre>
<p>Complete implementation of ORM(object relational mapping) is complex. You can check  <a href="http://ormlite.com/">OrmLite &#8211; Lightweight Object Relational Mapping (ORM) Java Package </a>for complete annotation based ORM implementation.   </p>
<p>We can also search at run time about annotated classes.</p>
<pre name="code" class="java">
public static List getModels(Context context, String packageName)
			throws IOException, URISyntaxException, ClassNotFoundException,
			NameNotFoundException {

		String apkName = context.getPackageManager().getApplicationInfo(packageName, 0).sourceDir;
		DexFile dexFile = new DexFile(apkName);
		PathClassLoader pathLoader = new PathClassLoader(apkName, Thread.currentThread().getContextClassLoader());
		DexClassLoader classLoader = new DexClassLoader(apkName,
				new ContextWrapper(context).getCacheDir().getAbsolutePath(),
				null, pathLoader);
		List classes = new ArrayList();
		Enumeration entries = dexFile.entries();

		while (entries.hasMoreElements()) {

			String entry = (String) entries.nextElement();

			if (entry.startsWith(packageName)) {
				Class entryClass = classLoader.loadClass(entry);
				if (entryClass != null) {
					Annotation[] annotations = entryClass.getAnnotations();
					for (Annotation annotation : annotations) {
						if (annotation instanceof DatabaseTable) {
							classes.add(entryClass);
						}
					}
				}
			}
		}

		return classes;
	}
</pre>
<p>This was a small example of custom annotation. We can use custom annotations in a lots of ways like configuration, unit testing, etc.</p>
<h4>Incoming search terms:</h4><ul><li><a href="http://blog.vimviv.com/android/custom-annotation-android/" title="android annotation example">android annotation example</a> (11)</li><li><a href="http://blog.vimviv.com/android/custom-annotation-android/" title="annotations in android">annotations in android</a> (6)</li><li><a href="http://blog.vimviv.com/android/custom-annotation-android/" title="android annotation">android annotation</a> (5)</li><li><a href="http://blog.vimviv.com/android/custom-annotation-android/" title="android annotations">android annotations</a> (4)</li><li><a href="http://blog.vimviv.com/android/custom-annotation-android/" title="how to create custom annotations in android">how to create custom annotations in android</a> (2)</li><li><a href="http://blog.vimviv.com/android/custom-annotation-android/" title="custom annotation android">custom annotation android</a> (2)</li><li><a href="http://blog.vimviv.com/android/custom-annotation-android/" title="android create annotation">android create annotation</a> (2)</li><li><a href="http://blog.vimviv.com/android/custom-annotation-android/" title="android create annotations">android create annotations</a> (2)</li><li><a href="http://blog.vimviv.com/android/custom-annotation-android/" title="how to create annotation in android">how to create annotation in android</a> (2)</li><li><a href="http://blog.vimviv.com/android/custom-annotation-android/" title="how to create annotation for coloum primary key in android">how to create annotation for coloum primary key in android</a> (2)</li></ul><div style='clear:both'></div><p>Related posts:</p><ol>
<li><a href='http://blog.vimviv.com/android/started-android/' rel='bookmark' title='Getting Started With Android'>Getting Started With Android</a></li>
</ol>]]></content:encoded>
			<wfw:commentRss>http://blog.vimviv.com/android/custom-annotation-android/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Prompt user to enable location providers in Android</title>
		<link>http://blog.vimviv.com/android/prompt-user-enable-location-providers-android/</link>
		<comments>http://blog.vimviv.com/android/prompt-user-enable-location-providers-android/#comments</comments>
		<pubDate>Wed, 13 Jul 2011 17:26:55 +0000</pubDate>
		<dc:creator>Administrator</dc:creator>
				<category><![CDATA[Android]]></category>

		<guid isPermaLink="false">http://blog.vimviv.com/?p=511</guid>
		<description><![CDATA[Now a days almost every smartphone applications use location based functionalities. But every articles say that disable wifi, 3g, gps etc for battery saving. So for every location based applications lots of problems are there like. What are location providers available in device? You can enable GPS using some method but best methods is ask [...]
Related posts:<ol>
<li><a href='http://blog.vimviv.com/blackberry/location-gps-blackberry/' rel='bookmark' title='How to get location without GPS in Blackberry'>How to get location without GPS in Blackberry</a></li>
<li><a href='http://blog.vimviv.com/android/android-some-basic-keywords/' rel='bookmark' title='Android &#8211; some basic keywords'>Android &#8211; some basic keywords</a></li>
<li><a href='http://blog.vimviv.com/android/intent-android/' rel='bookmark' title='What is Intent in android?'>What is Intent in android?</a></li>
</ol>]]></description>
			<content:encoded><![CDATA[<p>Now a days almost every smartphone applications use location based functionalities. But every articles say that disable wifi, 3g, gps etc for battery saving. So for every location based applications lots of problems are there like. What are location providers available in device? You can enable GPS using some method but best methods is ask user to enable location providers.<br />
In this post we will see that how can we prompt user to enable location providers.<br />
<span id="more-511"></span></p>
<pre name="code" class="java">
 private void checkLocationProviders(){
	    //String provider = Settings.Secure.getString(getContentResolver(),Settings.Secure.LOCATION_PROVIDERS_ALLOWED);
	       if(lm.isProviderEnabled(LocationManager.GPS_PROVIDER)){

	    	   Toast.makeText(EnableLocationActivity.this, "GPS provider Enabled: ",Toast.LENGTH_LONG).show();

	       }else if(lm.isProviderEnabled(LocationManager.NETWORK_PROVIDER)){

	    	   Toast.makeText(EnableLocationActivity.this, "Network provider Enabled: ",Toast.LENGTH_LONG).show();

	       }else{
	    	   AlertDialog.Builder builder = new AlertDialog.Builder(this);
	    	   builder.setMessage("Location providers are not available. Enable GPS or network providers.")
	    	          .setCancelable(false)
	    	          .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
	    	              public void onClick(DialogInterface dialog, int id) {
	    	            	  Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
	    	   	          startActivityForResult(intent, 1);
	    	              }
	    	          })
	    	          .setNegativeButton("No", new DialogInterface.OnClickListener() {
	    	              public void onClick(DialogInterface dialog, int id) {
	    	            	  EnableLocationActivity.this.finish();
	    	              }
	    	          }).show();

	       }

	   }

	   @Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		   checkLocationProviders();
	super.onActivityResult(requestCode, resultCode, data);
	}
</pre>
<p>Explanation of above code.<br />
1. First we need to create LocationManager.<br />
LocationManager lm = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);</p>
<p>2. using isProviderEnabled Check location providers are already enabled or not.<br />
<a href="http://blog.vimviv.com/wp-content/uploads/2011/07/device2.png"><img src="http://blog.vimviv.com/wp-content/uploads/2011/07/device2.png" alt="location providers" title="location providers" width="320" height="480" class="aligncenter size-full wp-image-514" /></a></p>
<p>3. If location providers are not enabled, ask user to enable location providers and launch location settings activity.<br />
<a href="http://blog.vimviv.com/wp-content/uploads/2011/07/device4.png"><img src="http://blog.vimviv.com/wp-content/uploads/2011/07/device4.png" alt="Prompt user" title="Prompt user" width="320" height="480" class="aligncenter size-full wp-image-515" /></a><br />
4. Using onActivityResult check user has changed location settings or not.<br />
<a href="http://blog.vimviv.com/wp-content/uploads/2011/07/device5.png"><img src="http://blog.vimviv.com/wp-content/uploads/2011/07/device5.png" alt="Activity result" title="Activity result" width="320" height="480" class="aligncenter size-full wp-image-516" /></a><br />
5. Dont forget to add user permission to AndroidMenifest.xml file.</p>
<pre name="code" class="java">
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"></uses-permission>
</pre>
<h4>Incoming search terms:</h4><ul><li><a href="http://blog.vimviv.com/android/prompt-user-enable-location-providers-android/" title="enable location provider">enable location provider</a> (10)</li><li><a href="http://blog.vimviv.com/android/prompt-user-enable-location-providers-android/" title="how to enable location provider in android">how to enable location provider in android</a> (8)</li><li><a href="http://blog.vimviv.com/android/prompt-user-enable-location-providers-android/" title="android enable location provider">android enable location provider</a> (5)</li><li><a href="http://blog.vimviv.com/android/prompt-user-enable-location-providers-android/" title="android alertdialog gsp not available">android alertdialog gsp not available</a> (5)</li><li><a href="http://blog.vimviv.com/android/prompt-user-enable-location-providers-android/" title="enable network provider android">enable network provider android</a> (4)</li><li><a href="http://blog.vimviv.com/android/prompt-user-enable-location-providers-android/" title="android check network provider enabled">android check network provider enabled</a> (3)</li><li><a href="http://blog.vimviv.com/android/prompt-user-enable-location-providers-android/" title="set location network provider enabled android">set location network provider enabled android</a> (3)</li><li><a href="http://blog.vimviv.com/android/prompt-user-enable-location-providers-android/" title="check location providers android">check location providers android</a> (3)</li><li><a href="http://blog.vimviv.com/android/prompt-user-enable-location-providers-android/" title="no location providers enabled and available">no location providers enabled and available</a> (3)</li><li><a href="http://blog.vimviv.com/android/prompt-user-enable-location-providers-android/" title="android location provider">android location provider</a> (3)</li></ul><div style='clear:both'></div><p>Related posts:</p><ol>
<li><a href='http://blog.vimviv.com/blackberry/location-gps-blackberry/' rel='bookmark' title='How to get location without GPS in Blackberry'>How to get location without GPS in Blackberry</a></li>
<li><a href='http://blog.vimviv.com/android/android-some-basic-keywords/' rel='bookmark' title='Android &#8211; some basic keywords'>Android &#8211; some basic keywords</a></li>
<li><a href='http://blog.vimviv.com/android/intent-android/' rel='bookmark' title='What is Intent in android?'>What is Intent in android?</a></li>
</ol>]]></content:encoded>
			<wfw:commentRss>http://blog.vimviv.com/android/prompt-user-enable-location-providers-android/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>EditText in Android</title>
		<link>http://blog.vimviv.com/android/edittext-android/</link>
		<comments>http://blog.vimviv.com/android/edittext-android/#comments</comments>
		<pubDate>Tue, 24 May 2011 15:45:12 +0000</pubDate>
		<dc:creator>Administrator</dc:creator>
				<category><![CDATA[Android]]></category>

		<guid isPermaLink="false">http://blog.vimviv.com/?p=483</guid>
		<description><![CDATA[You are developing game apps, business apps or any app you need EditText widget for taking input from users. Android EditText widget is functionally rich and customizable.I have worked on many platforms and i have noticed that on most of the platforms you have do lots of coding for UI customization. But in android you [...]
Related posts:<ol>
<li><a href='http://blog.vimviv.com/blackberry/blackberry-application-code-signing/' rel='bookmark' title='Blackberry application signing'>Blackberry application signing</a></li>
<li><a href='http://blog.vimviv.com/android/started-android/' rel='bookmark' title='Getting Started With Android'>Getting Started With Android</a></li>
</ol>]]></description>
			<content:encoded><![CDATA[<p>You are developing game apps, business apps or any app you need EditText widget for taking input from users. Android EditText widget is functionally rich and customizable.I have worked on many platforms and i have noticed that on most of the platforms you have do lots of coding for UI customization. But in android you can easily customize UI elements as per your requirements. In this post i am trying to collect most of the information about android EditText. EditText is the subclass of TextView class.<br />
Like other widgets you can add a simple EditText </p>
<p><span id="more-483"></span><br />
using java code</p>
<pre name="code" class="java">
EditText editText = new EditText(context);
</pre>
<p>or using XML</p>
<pre name="code" class="xml">
<EditText
    android:layout_height="wrap_content"
    android:layout_width="match_parent"
    android:id="@+id/editText1"></EditText>
</pre>
<p><strong>Single-line EditText:</strong></p>
<p>Single-line EditText means if you press &#8220;enter key&#8221; on EditText,focus will go to the next widget. Default behavior of EditText is multi-line.It means if you press &#8220;enter key&#8221;, a new line will add to the EditText.<br />
using android:singleLine=&#8221;true&#8221; property you can make single-line EditText. But this property is now deprecated.<br />
So if you add any inputtype property, EditText will become single-line EditText.<br />
e.g. android:inputType=&#8221;text&#8221;</p>
<p><strong>Multi-line EditText:</strong><br />
There are two properties are available for multi-line Edittext.<br />
<strong>a. android:lines: </strong><br />
Using &#8220;lines&#8221; property you can specify height of edittext in terms of lines.<br />
e.g. android:lines = &#8220;3&#8243; means only 3 lines will be visible in EditText and for 4rth line, you have to scroll vertically.<br />
<div id="attachment_485" class="wp-caption aligncenter" style="width: 421px"><a href="http://blog.vimviv.com/wp-content/uploads/2011/05/lines1.jpg"><img src="http://blog.vimviv.com/wp-content/uploads/2011/05/lines1.jpg" alt="android:lines" title="lines" width="411" height="130" class="size-full wp-image-485" /></a><p class="wp-caption-text">android:lines</p></div></p>
<p><strong>android:inputType</strong><br />
Using the inputType property we can restrict use to enter the correct data.<br />
e.g.</p>
<pre name="code" class="xml">
<EditText
    android:layout_height="wrap_content"
    android:layout_width="fill_parent"
    android:id="@+id/editText1"
    android:inputType="number"></EditText>
</pre>
<p><strong>b.android:maxLines</strong><br />
In the above example user can enter only numbers in the EditText widget. If you specify a inputType, EditText will become single line.<br />
using maxLines property you can specify the maximum height of the EditText.<br />
e.g.android:maxLines=&#8221;3&#8243; means first only single line will be visible and when you press &#8220;enter key&#8221;, EditText&#8217;s height will expend till the maxLines.</p>
<div id="attachment_486" class="wp-caption aligncenter" style="width: 417px"><a href="http://blog.vimviv.com/wp-content/uploads/2011/05/maxline.jpg"><img src="http://blog.vimviv.com/wp-content/uploads/2011/05/maxline.jpg" alt="android:maxline" title="maxline" width="407" height="78" class="size-full wp-image-486" /></a><p class="wp-caption-text">android:maxLines</p></div>
<div id="attachment_487" class="wp-caption aligncenter" style="width: 424px"><a href="http://blog.vimviv.com/wp-content/uploads/2011/05/maxline-1.jpg"><img src="http://blog.vimviv.com/wp-content/uploads/2011/05/maxline-1.jpg" alt="android:maxLine-1" title="maxline-1" width="414" height="97" class="size-full wp-image-487" /></a><p class="wp-caption-text">android:maxLine-1</p></div>
<p><strong>Gravity:</strong><br />
<div id="attachment_488" class="wp-caption aligncenter" style="width: 414px"><a href="http://blog.vimviv.com/wp-content/uploads/2011/05/gravity.jpg"><img src="http://blog.vimviv.com/wp-content/uploads/2011/05/gravity.jpg" alt="gravity" title="gravity" width="404" height="125" class="size-full wp-image-488" /></a><p class="wp-caption-text">gravity</p></div><br />
In the above image we can see that default alignment of cursor in EditText is vertical center. We can control alignment of cursor using &#8220;gravity&#8221; proprty.<br />
e.g. android:gravity=&#8221;top&#8221; will align cursor to the top left of the EditText.</p>
<p><strong>Read-only EditText:</strong><br />
Sometimes we need to disable EditText. For that we can use &#8220;editable&#8221; and &#8220;enabled&#8221; properties of EditText.<br />
<div id="attachment_489" class="wp-caption aligncenter" style="width: 417px"><a href="http://blog.vimviv.com/wp-content/uploads/2011/05/disable.jpg"><img src="http://blog.vimviv.com/wp-content/uploads/2011/05/disable.jpg" alt="disable" title="disable" width="407" height="72" class="size-full wp-image-489" /></a><p class="wp-caption-text">disable</p></div></p>
<pre name="code" class="xml">
<EditText
    android:layout_height="wrap_content"
    android:layout_width="match_parent"
    android:id="@+id/editText1"
    android:editable="false"
    android:enabled="false"></EditText>
</pre>
<p><strong>Hint(Watermark) in EditText:</strong><br />
If you are using &#8220;hint&#8221; property of EditText, a small description of field will be visible inside field and if user clicks on EditText, it disappears and user is free to write something in it. So there is no need of label for EditText, if you are using &#8220;hint&#8221; property.<br />
<div id="attachment_490" class="wp-caption aligncenter" style="width: 420px"><a href="http://blog.vimviv.com/wp-content/uploads/2011/05/hint.jpg"><img src="http://blog.vimviv.com/wp-content/uploads/2011/05/hint.jpg" alt="android:hint" title="hint" width="410" height="72" class="size-full wp-image-490" /></a><p class="wp-caption-text">android:hint</p></div><br />
e.g.</p>
<pre name="code" class="xml">
 <EditText
                android:hint="Enter you name"
                android:id="@+id/editText"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"></EditText>
</pre>
<p>or from java code</p>
<pre name="code" class="java">
editText.setHint("Enter you name");
</pre>
<p><strong>Error popup in edittext:</strong><br />
Sometimes user entered invalid data in the EditText and you want to notify user that you have entered an invalid data. In this type of case you can use setError() method of EditText. It will show small popup of the error message.<br />
<div id="attachment_491" class="wp-caption aligncenter" style="width: 424px"><a href="http://blog.vimviv.com/wp-content/uploads/2011/05/error.jpg"><img src="http://blog.vimviv.com/wp-content/uploads/2011/05/error.jpg" alt="setError" title="error" width="414" height="146" class="size-full wp-image-491" /></a><p class="wp-caption-text">setError</p></div></p>
<pre name="code" class="java">
editText.setError("Invalid Input");
</pre>
<p><strong>Hide soft keyboard:</strong><br />
Sometimes you want to hide soft keyboard on some action. You can do this using this code.</p>
<pre name="code" class="java">
InputMethodManager imm = (InputMethodManager)getSystemService(INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(getWindowToken(), 0);
</pre>
<p>If in an activity first focus is on EditText, soft keyboard will appear on activity launch. If you don&#8217;t want this, you can use this code.<br />
<activity android:windowSoftInputMode="stateHidden"></p>
<p><strong>Listen IME key events and enter key:</strong></p>
<pre name="code" class="java">
editText.setOnEditorActionListener(new OnEditorActionListener() {

			@Override
			public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
				if(event!=null &#038;&#038; event.getAction()==KeyEvent.ACTION_DOWN){
					//Handle enter key
					return true;
				}
				if(actionId == EditorInfo.IME_ACTION_NEXT){
					//Handle IME NEXT key
					return true;
				}
				if(actionId == EditorInfo.IME_ACTION_DONE){
					//Handle IME DONE key
					return true;
				}
				return false;
			}
		});
</pre>
<p><strong>android:layout_weight</strong><br />
using layout_weight we can specify the ratio of the size of the child items in the layout.<br />
e.g.</p>
<pre name="code" class="xml">
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
    <EditText
    android:text="EditText"
    android:layout_height="wrap_content"
    android:layout_width="0dip"
    android:id="@+id/editText1"
    android:layout_weight="2"></EditText>
    <EditText
    android:text="EditText"
    android:layout_height="wrap_content"
    android:layout_width="0dip"
    android:id="@+id/editText2"
    android:layout_weight="1"></EditText>

</LinearLayout>
</pre>
<p><div id="attachment_503" class="wp-caption aligncenter" style="width: 423px"><a href="http://blog.vimviv.com/wp-content/uploads/2011/05/weight.jpg"><img src="http://blog.vimviv.com/wp-content/uploads/2011/05/weight.jpg" alt="layout_weight" title="weight" width="413" height="66" class="size-full wp-image-503" /></a><p class="wp-caption-text">layout_weight</p></div><br />
Explanation of the above XML<br />
<strong>android:layout_weight:</strong> Using android:layout_weight we are specifying that first EditText will take 2/3 of the layout width and second EditText will take 1/3 of the layout_width;<br />
<strong>android:layout_width=&#8221;0dip&#8221;:</strong> If you give the layout_width = &#8220;wrap_content&#8221;,EditText will expend its width while you are typing in it.If you give the layout_width=&#8221;fill_parent&#8221;, first EditText will take 1/3 of the layout width and second EditText will take 2/3 of the layout width. Because of that i am giving layout_weight=&#8221;0dip&#8221;.</p>
<p><strong>Drawable:</strong><br />
Best thing about android widgets are, you can easily customize them. And for customization you no need to rush codes in the layout xml. So create a drawable xml in the drawable folder<br />
e.g.</p>
<pre name="code" class="xml">
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <gradient
        android:endColor="#8dc73f"
        android:centerColor="#d4d4d4"
        android:startColor="#d4d4d4"/>
    <stroke
        android:width="1dp"
        color="#8dc73f" />
    <corners
        android:radius="5dp" />
<padding
        android:left="7dp"
        android:top="7dp"
        android:right="7dp"
        android:bottom="7dp" />
</shape>
</pre>
<p><div id="attachment_505" class="wp-caption aligncenter" style="width: 425px"><a href="http://blog.vimviv.com/wp-content/uploads/2011/05/drawable.jpg"><img src="http://blog.vimviv.com/wp-content/uploads/2011/05/drawable.jpg" alt="drawable" title="drawable" width="415" height="59" class="size-full wp-image-505" /></a><p class="wp-caption-text">drawable</p></div><br />
and apply that drawable to the widgets.</p>
<pre name="code" class="xml">
<EditText
    android:text="EditText"
    android:layout_height="wrap_content"
    android:layout_width="fill_parent"
    android:id="@+id/editText1"
    android:background="@drawable/edittext"></EditText>
</pre>
<p><strong>Selector:</strong><br />
EditText can be in different states like focused, disabled, pressed, etc. You can see in the previous example EditText will look same in all states.So if you want to give different looks to EditText for different states. you need to create selector xml.</p>
<pre name="code" class="xml">
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item
     android:state_pressed="true"
     android:state_enabled="true"
        android:drawable="@drawable/edittext_focused" />
    <item
     android:state_focused="false"
     android:state_enabled="true"
        android:drawable="@drawable/edittext" />
    <item
     android:state_enabled="false"
        android:drawable="@drawable/edittext_disabled" />
</selector>
</pre>
<p>I will keep update this article because there are lots of things that we can do with EditText widget. If you want to suggest something to improve this post, your comments are always welcome here.</p>
<h4>Incoming search terms:</h4><ul><li><a href="http://blog.vimviv.com/android/edittext-android/" title="edittext no new line android">edittext no new line android</a> (62)</li><li><a href="http://blog.vimviv.com/android/edittext-android/" title="edittext android">edittext android</a> (52)</li><li><a href="http://blog.vimviv.com/android/edittext-android/" title="android edittext">android edittext</a> (38)</li><li><a href="http://blog.vimviv.com/android/edittext-android/" title="android custom edittext">android custom edittext</a> (36)</li><li><a href="http://blog.vimviv.com/android/edittext-android/" title="android edit default return key">android edit default return key</a> (33)</li><li><a href="http://blog.vimviv.com/android/edittext-android/" title="edit text in android">edit text in android</a> (30)</li><li><a href="http://blog.vimviv.com/android/edittext-android/" title="edittext in android">edittext in android</a> (29)</li><li><a href="http://blog.vimviv.com/android/edittext-android/" title="android edittext on top of image">android edittext on top of image</a> (21)</li><li><a href="http://blog.vimviv.com/android/edittext-android/" title="custom edittext android">custom edittext android</a> (21)</li><li><a href="http://blog.vimviv.com/android/edittext-android/" title="android edittext disable enter key">android edittext disable enter key</a> (20)</li></ul><div style='clear:both'></div><p>Related posts:</p><ol>
<li><a href='http://blog.vimviv.com/blackberry/blackberry-application-code-signing/' rel='bookmark' title='Blackberry application signing'>Blackberry application signing</a></li>
<li><a href='http://blog.vimviv.com/android/started-android/' rel='bookmark' title='Getting Started With Android'>Getting Started With Android</a></li>
</ol>]]></content:encoded>
			<wfw:commentRss>http://blog.vimviv.com/android/edittext-android/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>What is Intent in android?</title>
		<link>http://blog.vimviv.com/android/intent-android/</link>
		<comments>http://blog.vimviv.com/android/intent-android/#comments</comments>
		<pubDate>Sun, 15 May 2011 13:04:06 +0000</pubDate>
		<dc:creator>Administrator</dc:creator>
				<category><![CDATA[Android]]></category>

		<guid isPermaLink="false">http://blog.vimviv.com/?p=476</guid>
		<description><![CDATA[According to google “an Intent object, is a passive data structure holding an abstract description of an operation to be performed”. intent is a very powerful concept. I have seen following advantages of intents. 1. It loosely couples your application. 2. It can activate three core components of the android applications activities, services, and broadcast [...]
Related posts:<ol>
<li><a href='http://blog.vimviv.com/android/started-android/' rel='bookmark' title='Getting Started With Android'>Getting Started With Android</a></li>
<li><a href='http://blog.vimviv.com/android/android-some-basic-keywords/' rel='bookmark' title='Android &#8211; some basic keywords'>Android &#8211; some basic keywords</a></li>
</ol>]]></description>
			<content:encoded><![CDATA[<p>According to google “an Intent object, is a passive data structure holding an abstract description of an operation to be performed”.<br />
intent is a very powerful concept. I have seen following advantages of intents.<br />
1. It loosely couples your application.<br />
2. It can activate three core components of the android applications activities, services, and broadcast receivers.<br />
3. Using intents you can start any other activity that is installed in the system.<br />
e.g. you can start address book application and you can get contacts from address.<br />
4. Using intents you can expose your applications activity for other applications.<br />
e.g. You can register your own “Send SMS by myApp” application for sending SMS.<br />
<span id="more-476"></span><br />
<strong>Intents are of two types.</strong><br />
<strong>Explicit Intents: </strong>explicit intents used when you know the name of activity that you want to launch.<br />
e.g. </p>
<pre name="code" class="java">
Intent intent = new Intent(FirstActivity.this,SecondActivity.class);
startActivity(intent);
</pre>
<p><strong>Implicit Intents:</strong> Instead of giving name of the intent you tell the system what you want to perform and system will find suitable activity for your task.<br />
e.g.</p>
<pre name="code" class="java">
intent = new Intent(Intent.ACTION_VIEW,
Uri.parse("http://www.vimviv.com"));
startActivity(intent);
</pre>
<p><strong><br />
Some example of explicit intents:</strong><br />
<strong>1. Start Activity:</strong></p>
<pre name="code" class="java">
Intent intent = new Intent(FirstActivity.this, SecondActivity.class);
startActivity(intent);
</pre>
<p><strong><br />
 2. Get result from activity:</strong><br />
<strong><br />
a. FirstActivity.java</strong></p>
<pre name="code" class="java">
public class FirstActivity extends Activity {

	Button button;
	private static final int REQUEST_CODE = 10;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        LinearLayout ll = new LinearLayout(this);
        button = new Button(this);
        ll.addView(button);
        setContentView(ll);
        button.setText("Call Second Activity");
        button.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				Intent i = new Intent(FirstActivity.this, SecondActivity.class);
				i.putExtra("data", "Hi from First Activity");
				// Set the request code to any code you like, you can identify the
				// callback via this code
				startActivityForResult(i, REQUEST_CODE);

			}
		});
    }
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    	if (resultCode == RESULT_OK &#038;&#038; requestCode == REQUEST_CODE) {
			if (data.hasExtra("result")) {
				String message = data.getExtras().getString("result");
				Toast.makeText(FirstActivity.this, message, Toast.LENGTH_SHORT).show();
			}
		}
    }
}
</pre>
<p>Explanation of above code is as below:<br />
<strong>setContentView:</strong> we have added button to linear layout and after that linear layout to activity.<br />
<strong>putExtra:</strong> using startActivity we are launching SecondActivity and with putExtra method we are sending data also.<br />
<strong>onActivityResult:</strong> When Second Activity will finish it will call the onActivityResult method of FirstActivity.</p>
<p><strong>b. SecondActivity.java</strong></p>
<pre name="code" class="java">
public class SecondActivity extends Activity{

	Button button;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		LinearLayout ll = new LinearLayout(this);
		button = new Button(this);
		ll.addView(button);
		setContentView(ll);
		button.setText("back");
		String message = getIntent().getExtras().getString("data");
		Toast.makeText(this, message, Toast.LENGTH_SHORT).show();
		button.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				finish();

			}
		});
	}

	@Override
	public void finish() {
		Intent data = new Intent();
		data.putExtra("result", "Hi from second activity");
		setResult(RESULT_OK, data);
		super.finish();
	}
}
</pre>
<p><strong>Explanation of above code is as below:</strong><br />
<strong>getExtras:</strong> Using the getExtras method we are fetching of data sent by FirstActivity.<br />
<strong>finish() :</strong>finish method will remove SecondActivity from the display stack and send data to the FirstActivity.</p>
<p><strong>3. Start Service:</strong></p>
<pre name="code" class="java">
Intent intent=new Intent("com.vimviv.service.serviceClass");
startService(intent);
</pre>
<p><strong>implicit intents examples:</strong></p>
<p><strong>1. make a call:</strong></p>
<pre name="code" class="java">
Intent callIntent = new Intent(Intent.ACTION_CALL, Uri.parse(“1234567890”));
startActivity(callIntent);
</pre>
<p><strong>2. Launch google map</strong></p>
<pre name="code" class="java">
intent = new Intent(Intent.ACTION_VIEW,
Uri.parse("geo:37.423156,-122.084917?z=19"));
startActivity(intent);
</pre>
<p><strong>3. email client:</strong></p>
<pre name="code" class="java">
final Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND);

emailIntent .setType("plain/text");
emailIntent .putExtra(android.content.Intent.EXTRA_EMAIL, newString[]{"yourmail@website.com"});
emailIntent .putExtra(android.content.Intent.EXTRA_SUBJECT, mySubject);

emailIntent .putExtra(android.content.Intent.EXTRA_TEXT, myBodyText);

startActivity(Intent.createChooser(intent, "Send mail));
</pre>
<p><strong>4. start SMS app:</strong></p>
<pre name="code" class="java">
Intent sendIntent = new Intent(Intent.ACTION_VIEW);
sendIntent.setData(Uri.parse("sms:"));
sendIntent.putExtra("sms_body", x);
</pre>
<p><strong><br />
5. To start a search in the Internet:</strong></p>
<pre name="code" class="java">
Intent intent = new Intent(Intent.ACTION_WEB_SEARCH );
intent.putExtra(SearchManager.QUERY, "Android");
startActivity(intent);
</pre>
<p>For implicit intents read my next post <a href="http://blog.vimviv.com/android/intent-filter-android/">What is intent filter in android?</a></p>
<h4>Incoming search terms:</h4><ul><li><a href="http://blog.vimviv.com/android/intent-android/" title="intent in android">intent in android</a> (198)</li><li><a href="http://blog.vimviv.com/android/intent-android/" title="what is intent in android">what is intent in android</a> (133)</li><li><a href="http://blog.vimviv.com/android/intent-android/" title="intent android">intent android</a> (101)</li><li><a href="http://blog.vimviv.com/android/intent-android/" title="Android intent">Android intent</a> (55)</li><li><a href="http://blog.vimviv.com/android/intent-android/" title="intents in android">intents in android</a> (29)</li><li><a href="http://blog.vimviv.com/android/intent-android/" title="intent java">intent java</a> (19)</li><li><a href="http://blog.vimviv.com/android/intent-android/" title="intent in java">intent in java</a> (11)</li><li><a href="http://blog.vimviv.com/android/intent-android/" title="android intents">android intents</a> (10)</li><li><a href="http://blog.vimviv.com/android/intent-android/" title="java intent">java intent</a> (8)</li><li><a href="http://blog.vimviv.com/android/intent-android/" title="intents android">intents android</a> (7)</li></ul><div style='clear:both'></div><p>Related posts:</p><ol>
<li><a href='http://blog.vimviv.com/android/started-android/' rel='bookmark' title='Getting Started With Android'>Getting Started With Android</a></li>
<li><a href='http://blog.vimviv.com/android/android-some-basic-keywords/' rel='bookmark' title='Android &#8211; some basic keywords'>Android &#8211; some basic keywords</a></li>
</ol>]]></content:encoded>
			<wfw:commentRss>http://blog.vimviv.com/android/intent-android/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Getting Started With Android</title>
		<link>http://blog.vimviv.com/android/started-android/</link>
		<comments>http://blog.vimviv.com/android/started-android/#comments</comments>
		<pubDate>Sat, 16 Apr 2011 17:54:21 +0000</pubDate>
		<dc:creator>Administrator</dc:creator>
				<category><![CDATA[Android]]></category>

		<guid isPermaLink="false">http://blog.vimviv.com/?p=473</guid>
		<description><![CDATA[Google Android is an open source platform that includes Linux Kernal based operating system and SDK for mobile or tablet PC application development. When &#8220;Android SDK release 1&#8243; came in 21 October 2008, lots of mobile platforms were already available for mobile application developers. But because of its familiar java programming support and open source [...]
Related posts:<ol>
<li><a href='http://blog.vimviv.com/android/android-some-basic-keywords/' rel='bookmark' title='Android &#8211; some basic keywords'>Android &#8211; some basic keywords</a></li>
</ol>]]></description>
			<content:encoded><![CDATA[<p>Google Android is an open source platform that includes Linux Kernal based operating system and SDK for mobile or tablet PC application development. When &#8220;Android SDK release 1&#8243; came in 21 October 2008, lots of mobile platforms were already available for mobile application developers. But because of its familiar java programming support and open source platform it became popular very soon in smartphone application development.<br />
At the time of  writing this post, current version of android is 2.3.3 (Gingerbread) for smartphones and Android 3.0 (Honeycomb) for tablet PCs.<br />
<span id="more-473"></span><br />
I have developed mobile applications in different mobile platforms like windows phone 7, Blackberry RIM etc., but whenever I develop any application in android I really enjoy the coding. So if you are interested in mobile application development I’ll suggest to try android platform atleast once.</p>
<p><strong><br />
Environment setup:</strong> &#8220;http://developer.android.com/sdk/installing.html&#8221; provides updated and complete step by step process to setup android Environment setup with troubleshooting.</p>
<p>In this post we will create “AddTwoNumbers” application In which we will learn.<br />
1. How to create simple screens in android?<br />
2. How to add simple UI components on the screens in android?<br />
3. How to listen button click event?<br />
4. How to switch between screens?<br />
5. How to send data from one screen to another?</p>
<p><strong>Step 1:</strong> Create new android project in eclipse. In &#8220;New Android Project&#8221; wizard you have to fill some information about your project.</p>
<p>Project name: AddTwoNumbers<br />
Build Target: Android 2.2<br />
Application name: AddTwoNumbers<br />
Package name: com.vimviv<br />
Create Activity: HomeActivity<br />
Min SDK Version: 8</p>
<p>When you click on &#8220;finish&#8221; button it will automatically generate different folder structures, HomeActivity.java file and other files that are useful for your project.</p>
<p><strong>Step 2:</strong> The thing which I like most in android is that UI development part is completely separate from your logical java code. In Android you can do UI designing in 3 different ways.<br />
1. using drag and drop interface.<br />
2. using XML UI file structure.<br />
3. In you java code also you can do UI designing.</p>
<p>I usually prefer XML interface for UI designing. So go to res -> layout -> main.xml<br />
For this particular application I am using two TextViews as a label, two EditTexts for numbers input and one &#8220;Add&#8221; button.</p>
<pre name="code" class="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/number1_view"
    	android:text="Number 1"
    	android:layout_width="fill_parent"
    	android:layout_height="wrap_content" ></TextView>
    <EditText
    	android:id="@+id/number1_text"
    	android:inputType="number"
    	android:layout_width="fill_parent"
    	android:layout_height="wrap_content"></EditText>
    <TextView
	   android:id="@+id/number2_view"
	   android:text="Number 2"
	   android:layout_width="fill_parent"
	   android:layout_height="wrap_content" ></TextView>
    <EditText
     	android:id="@+id/number2_text"
	    android:layout_height="wrap_content"
	    android:layout_width="match_parent"
	    android:inputType="number"></EditText>
    <Button
    	android:id="@+id/add_button"
	    android:text="Add"
	    android:layout_width="fill_parent"
	    android:layout_height="wrap_content" ></Button>
</LinearLayout>
</pre>
<p>Explanation of above XML is as below:</p>
<p><strong>LinearLayout:</strong> Androids provides different layouts for arranging UI components on the screen. In this example I have used LinearLayout. As the name suggest using LinearLayout you can arrange UI components in horizontally or vertically. Default orientation of LinearLayout is horizontal. But in this example I want to arrange UI components vertically so I have given orientation &#8220;vertical&#8221;.</p>
<p><strong>android:layout_width &#038; android:layout_height:</strong> We can specify width and height of the layout but for good portability on different screen sizes, we should give general values like &#8220;fill_parent&#8221;(take all available space) and &#8220;wrap_content&#8221; (takes the space that will be enaugh to show the content).</p>
<p><strong>android:id :</strong> We have to provide some unique id to the UI component that will be helpful to UI component from java code.</p>
<p>Now in the layout folder create a new layout &#8220;result.xml&#8221; for result screen and add this code.</p>
<pre name="code" class="xml">
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="match_parent"
  android:orientation="vertical" android:layout_height="match_parent">
    <TextView
	    android:id="@+id/result_view"
	    android:text=""
	    android:layout_width="fill_parent"
	    android:layout_height="fill_parent"
	    android:gravity="center_vertical|center_horizontal"/>
</LinearLayout>
</pre>
<p>Explanation of above XML is as below:</p>
<p><strong>android:gravity :</strong> Using gravity property we give alignment to the TextView&#8217;s text. In this example I have given &#8220;center_vertical|center_horizontal&#8221; alignment.</p>
<p><strong>Step 3:</strong> Open the HomeActivity.java and add this code.</p>
<pre name="code" class="java">

package com.vimviv;

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;

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

        final EditText numberText1 = (EditText) findViewById(R.id.number1_text);
        final EditText numberText2 = (EditText) findViewById(R.id.number2_text);
        Button addButton = (Button) findViewById(R.id.add_button);

        addButton.setOnClickListener(new OnClickListener() {

			public void onClick(View arg0) {
				int num1 = Integer.parseInt(numberText1.getText().toString());
				int num2 = Integer.parseInt(numberText2.getText().toString());
				int result = num1+num2;
				Intent intent = new Intent(HomeActivity.this,ResultActivity.class);
				intent.putExtra("result","Result: "+result);
				startActivity(intent);
			}
		});

    }
}
</pre>
<p>Explanation of the above code is as below:</p>
<p><strong>Activity:</strong> For creating an activity you have extend the Activity class.</p>
<p><strong>onCreate:</strong> When activity is first created onCreate method is called. In this method first line should be super.onCreate(savedInstanceState) . setContentView method will set the content view to main.xml that we have created as our first screen.<br />
<strong><br />
findViewById:</strong> Using findViewById method we can access xml defined UI components in java code.</p>
<p><strong>Listener:</strong> In android listener pattern is simple, if you want to listen for any event call the method setOn<Event>Listener, pass the On<Event>Listener object and you will catch the event in on<Event> method.</p>
<p>In this example we are adding the click event listener so just replace <Event> with click.</p>
<p><strong>Intent:</strong> Using intents we can launch an activity and using putExtra method of intent we can send data to the other activity.</p>
<p>Now create new Activity ResultActivity.java and add this code.</p>
<pre name="code" class="java">
package com.vimviv;

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

public class ResultActivity extends Activity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.result);
		String result = getIntent().getStringExtra("result");

		TextView resultView = (TextView) findViewById(R.id.result_view);
		resultView.setText(result);
	}
}
</pre>
<p>Step 4: Whenever you create a new activity don&#8217;t forget to add its information in AndroidManifest.xml</p>
<pre name="code" class="xml">
<activity android:name=".ResultActivity"></activity>
</pre>
<p>The application is ready now. Go to windows->Android SDK and AVD manager and create new virtual device and run this application in that device.</p>
<h4>Incoming search terms:</h4><ul><li><a href="http://blog.vimviv.com/android/started-android/" title="android post data between screens">android post data between screens</a> (2)</li><li><a href="http://blog.vimviv.com/android/started-android/" title="android listener pattern">android listener pattern</a> (2)</li><li><a href="http://blog.vimviv.com/android/started-android/" title="dragdrop using listeners android tutorial">dragdrop using listeners android tutorial</a> (2)</li><li><a href="http://blog.vimviv.com/android/started-android/" title="java android listener pattern">java android listener pattern</a> (2)</li><li><a href="http://blog.vimviv.com/android/started-android/" title="how send data from one screen to another next screen in android">how send data from one screen to another next screen in android</a> (2)</li><li><a href="http://blog.vimviv.com/android/started-android/" title="how to navigate one screen to another screen using onclicklistener in android">how to navigate one screen to another screen using onclicklistener in android</a> (1)</li><li><a href="http://blog.vimviv.com/android/started-android/" title="listener and components in android">listener and components in android</a> (1)</li><li><a href="http://blog.vimviv.com/android/started-android/" title="how to give space between the ui componets in blackberry java application">how to give space between the ui componets in blackberry java application</a> (1)</li><li><a href="http://blog.vimviv.com/android/started-android/" title="knowledge base open source windows android">knowledge base open source windows android</a> (1)</li><li><a href="http://blog.vimviv.com/android/started-android/" title="java findviewbyid method">java findviewbyid method</a> (1)</li></ul><div style='clear:both'></div><p>Related posts:</p><ol>
<li><a href='http://blog.vimviv.com/android/android-some-basic-keywords/' rel='bookmark' title='Android &#8211; some basic keywords'>Android &#8211; some basic keywords</a></li>
</ol>]]></content:encoded>
			<wfw:commentRss>http://blog.vimviv.com/android/started-android/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Switch from one screen to another in BlackBerry</title>
		<link>http://blog.vimviv.com/blackberry/switch-screen-blackberry/</link>
		<comments>http://blog.vimviv.com/blackberry/switch-screen-blackberry/#comments</comments>
		<pubDate>Sat, 02 Apr 2011 10:25:13 +0000</pubDate>
		<dc:creator>Administrator</dc:creator>
				<category><![CDATA[Blackberry RIM]]></category>

		<guid isPermaLink="false">http://blog.vimviv.com/?p=467</guid>
		<description><![CDATA[whenever you start developing UI application on any platform, after learning some basic controls you have some basic questions like &#8220;How to switch from one screen to another?&#8221;, &#8220;How to send data from one screen to another?&#8221;. In this post i am going to make a simple blackberry application in which i will add two [...]
Related posts:<ol>
<li><a href='http://blog.vimviv.com/blackberry/screen-layout-blackberry/' rel='bookmark' title='Screen layout in blackberry'>Screen layout in blackberry</a></li>
</ol>]]></description>
			<content:encoded><![CDATA[<p>whenever you start developing UI application on any platform, after learning some basic controls you have some basic questions like &#8220;How to switch from one screen to another?&#8221;, &#8220;How to send data from one screen to another?&#8221;.<br />
In this post i am going to make a simple blackberry application in which i will add two numbers on one screen and i will display result on second screen. For UI applications i think &#8220;AddTwoNumbers&#8221; is also a &#8220;hello world&#8221; like program.<br />
<span id="more-467"></span><br />
In blackberry ui application maintains a stack of screens.Top most screen in stack will be visible to user.<br />
So if you want to add a screen to display stack, you have push the screen and if you want to remove screen from display stack, you have to pop screen.<br />
UiApplication class has the methods for adding screen to the stack and for removing that screen from the stack.<br />
pushScreen(Screen screen)<br />
popScreen(Screen screen)</p>
<p>I will use these methods in my simple application.</p>
<p>1. We are creating a Ui application. So first we will create a class that will extend UiApplication class.</p>
<pre name="code" class="java">

public class App extends UiApplication{

	public static void main(String[] args) {

		new App().enterEventDispatcher();
	}

	public App() {
		pushScreen(new FirstScreen());
	}

}
</pre>
<p>In the above code.<br />
&#8220;main&#8221; method is the entry point for the application. enterEventDispatcher() method will make this thread to event dispatcher thread. pushScreen(new FirstScreen()) method will push &#8220;FirstScreen&#8221; to the display stack.</p>
<p>2. We need to create our FirstScreen. For that create a class with name FirstScreen and extend MainScreen.</p>
<pre name="code" class="java">
class FirstScreen extends MainScreen{

	public FirstScreen() {
		final BasicEditField number1 = new BasicEditField("Number 1:","",10,EditField.NO_NEWLINE|EditField.FILTER_NUMERIC);
		final BasicEditField number2 = new BasicEditField("Number 2:","",10,EditField.NO_NEWLINE|EditField.FILTER_NUMERIC);
		ButtonField addButton = new ButtonField("Add",ButtonField.CONSUME_CLICK);
		addButton.setChangeListener(new FieldChangeListener() {

			public void fieldChanged(Field field, int context) {
				int num1 = Integer.parseInt(number1.getText());
				int num2 = Integer.parseInt(number2.getText());
				int result = num1 + num2;
				UiApplication ui = UiApplication.getUiApplication();
				//ui.popScreen(FirstScreen.this);
				ui.pushScreen(new ResultScreen(result));

			}
		});
		add(number1);
		add(new SeparatorField());
		add(number2);
		add(new SeparatorField());
		add(addButton);
	}

}
</pre>
<p>In above code:<br />
I have created 2 fields for taking number inputs from user and 1 &#8220;ADD&#8221; button for calculating addition of two numbers and switch to the result screen.<br />
UiApplication class has the pushScreen and popScreen methods but we are in the MainScreen class. So we need the UiApplication object for calling these two methods.</p>
<p>In this example i have commented out popScreen method because if you don&#8217;t pop current screen, user can use blackberry device&#8217;s &#8220;ESC&#8221; button as a back button. So on the ResultScreen if user presses &#8220;ESC&#8221; button, blackberry application manager will automatically pop the current screen and send user to the<br />
FirstScreen.But if you pop the current screen, only one screen is there in the display stack so &#8220;ESC&#8221; button will exit the application.</p>
<p>2. similar to FirstScreen i will create ResultScreen by extending MainScreen. In the ResultScreen instead of default constructor i will use parametrized constructor </p>
<p>that will take the result from the previous screen.</p>
<pre name="code" class="java">

class ResultScreen extends MainScreen{

	public ResultScreen(int result) {
		LabelField resultLabel = new LabelField("Result: "+result);
		add(resultLabel);
	}
}
</pre>
<p>So in this example we have seen that how we can switch between screens and how we send data to the another screen.</p>
<p>Some more scenarios you can face while switching screens.</p>
<p>You can push or pop screens only if you are in event thread. So suppose you are not in event thread or you don&#8217;t know that your are in event thread or not or you don&#8217;t have the object of current screen. how you can switch screen.<br />
I have created a simple method for that.</p>
<pre name="code" class="java">
public void switchScreen(final Screen nextScreen) {
		final UiApplication ui = UiApplication.getUiApplication();
		final Screen currentScreen = ui.getActiveScreen();

		if (UiApplication.isEventDispatchThread()) {
			ui.popScreen(currentScreen);
			ui.pushScreen(nextScreen);
		} else {
			ui.invokeLater(new Runnable() {
				public void run() {
					ui.popScreen(currentScreen);
					ui.pushScreen(nextScreen);
				}
			});
		}
	}
</pre>
<p>In the above code.<br />
UiApplication.isEventDispatchThread() method will check that you are in event thread or not.<br />
ui.getActiveScreen() method will get the object of current active screen.<br />
one more method getScreenCount() is also useful for checking that how many screens are present in the display stack.</p>
<h4>Incoming search terms:</h4><ul><li><a href="http://blog.vimviv.com/blackberry/switch-screen-blackberry/" title="pop screen blackberry">pop screen blackberry</a> (14)</li><li><a href="http://blog.vimviv.com/blackberry/switch-screen-blackberry/" title="screen count stack blackberry">screen count stack blackberry</a> (10)</li><li><a href="http://blog.vimviv.com/blackberry/switch-screen-blackberry/" title="blackberry java how to get the current ui app">blackberry java how to get the current ui app</a> (8)</li><li><a href="http://blog.vimviv.com/blackberry/switch-screen-blackberry/" title="blackberry switching screens">blackberry switching screens</a> (5)</li><li><a href="http://blog.vimviv.com/blackberry/switch-screen-blackberry/" title="move screens blackberry java">move screens blackberry java</a> (5)</li><li><a href="http://blog.vimviv.com/blackberry/switch-screen-blackberry/" title="blackberry how to create a screen class">blackberry how to create a screen class</a> (4)</li><li><a href="http://blog.vimviv.com/blackberry/switch-screen-blackberry/" title="blackberry jde getscreencount">blackberry jde getscreencount</a> (4)</li><li><a href="http://blog.vimviv.com/blackberry/switch-screen-blackberry/" title="Blackberry switch screen">Blackberry switch screen</a> (3)</li><li><a href="http://blog.vimviv.com/blackberry/switch-screen-blackberry/" title="blackberry switch screens">blackberry switch screens</a> (3)</li><li><a href="http://blog.vimviv.com/blackberry/switch-screen-blackberry/" title="bb java one ui to other">bb java one ui to other</a> (3)</li></ul><div style='clear:both'></div><p>Related posts:</p><ol>
<li><a href='http://blog.vimviv.com/blackberry/screen-layout-blackberry/' rel='bookmark' title='Screen layout in blackberry'>Screen layout in blackberry</a></li>
</ol>]]></content:encoded>
			<wfw:commentRss>http://blog.vimviv.com/blackberry/switch-screen-blackberry/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>Implement HotKey in BlackBerry</title>
		<link>http://blog.vimviv.com/blackberry/implement-hotkey-blackberry/</link>
		<comments>http://blog.vimviv.com/blackberry/implement-hotkey-blackberry/#comments</comments>
		<pubDate>Wed, 16 Mar 2011 18:34:08 +0000</pubDate>
		<dc:creator>Administrator</dc:creator>
				<category><![CDATA[Blackberry RIM]]></category>

		<guid isPermaLink="false">http://blog.vimviv.com/?p=456</guid>
		<description><![CDATA[hotkeys are keyboard shortcuts for menu options e.g. using alt+f you can open file menu option. Although blackberry trackballs are very smooth for navigation, we can implement hotkeys in blackberry application also. Because in mobile applications we should minimize the number of key presses. In this screen shot you can see a list with some [...]
No related posts.]]></description>
			<content:encoded><![CDATA[<p>hotkeys are keyboard shortcuts for menu options e.g. using alt+f you can open file menu option. Although blackberry trackballs are very smooth for navigation, we can implement hotkeys in blackberry application also. Because in mobile applications we should minimize the number of key presses.</p>
<p>In this screen shot you can see a list with some options. So if you want to select &#8220;About&#8221; option without hotkeys, you have to scroll down to the &#8220;About&#8221; and press.<br />
<span id="more-456"></span><br />
<a href="http://blog.vimviv.com/wp-content/uploads/2011/03/list.png"><img src="http://blog.vimviv.com/wp-content/uploads/2011/03/list.png" alt="" title="list" width="320" height="240" class="aligncenter size-full wp-image-457" /></a></p>
<p>But using hotkey only you have to press single key &#8216;A&#8217; for &#8220;About&#8221; option.</p>
<p><a href="http://blog.vimviv.com/wp-content/uploads/2011/03/list-01.png"><img src="http://blog.vimviv.com/wp-content/uploads/2011/03/list-01.png" alt="hotkey" title="list-01" width="320" height="240" class="aligncenter size-full wp-image-458" /></a></p>
<p>There are two steps to create hotkeys in blackberry application.</p>
<p>1. <strong>Underline hotkey character:</strong> If you want the &#8216;N&#8217; as hotkey in &#8220;New Game&#8221;, insert the Unicode of underscore(u0332) after &#8216;N&#8217;.</p>
<pre name="code" class="java">
String[] listItems = { "Nu0332ew Game", "Lu0332evel", "Hu0332elp",
				"Au0332bout" };
		ObjectListField listField = new ObjectListField();
		listField.set(listItems);
		add(listField);
</pre>
<p><strong>2. Key press event:</strong> Now we have handle key press event of hotkey.</p>
<pre name="code" class="java">
protected boolean keyChar(char c, int status, int time) {
		if(c == 'N' || c == 'n'){
			Dialog.inform("New Game");
		}
		if(c == 'L' || c == 'l'){
			Dialog.inform("Level");
		}
		if(c == 'H' || c == 'h'){
			Dialog.inform("Help");
		}
		if(c == 'A' || c == 'a'){
			Dialog.inform("About");
		}

		return super.keyChar(c, status, time);
	}
</pre>
<p>We can also create HotKey for application launching.</p>
<p>1. create resource file in your project.<br />
2. in resource file add key as &#8220;App&#8221; and values as &#8220;HotKu0332ey&#8221;(Your application name).<br />
3. in BlackBerry_App_Descriptor.xml file check the option &#8220;Internationalized resource bundle available&#8221;.<br />
4. select the Resource bundle and select the TitleID as App.</p>
<p>Note:<br />
a. For sample application HotKey i have used the shortcut key as &#8216;k&#8217;. because &#8216;h&#8217;, &#8216;o&#8217; and &#8216;t&#8217; are already register for  &#8220;Help&#8221;, &#8220;options&#8221; and &#8220;task&#8221; applications respectively. So you have to avoid collisions with other applications.</p>
<p>b. make sure to disable &#8220;Dial from home screen&#8221; option (Options &#8211;>Phone Options &#8211;> General options &#8211;> Dial from home screen)in phone setting.</p>
<p>Now if you press &#8216;k&#8217; key in home screen, blackberry will launch HotKey application.</p>
<h4>Incoming search terms:</h4><ul><li><a href="http://blog.vimviv.com/blackberry/implement-hotkey-blackberry/" title="blackberry hotkey">blackberry hotkey</a> (6)</li><li><a href="http://blog.vimviv.com/blackberry/implement-hotkey-blackberry/" title="hotkey blackberry">hotkey blackberry</a> (5)</li><li><a href="http://blog.vimviv.com/blackberry/implement-hotkey-blackberry/" title="blackberry hotkeys">blackberry hotkeys</a> (2)</li><li><a href="http://blog.vimviv.com/blackberry/implement-hotkey-blackberry/" title="additional hotkey for blackberry">additional hotkey for blackberry</a> (1)</li><li><a href="http://blog.vimviv.com/blackberry/implement-hotkey-blackberry/" title="hotkey on blackberry">hotkey on blackberry</a> (1)</li><li><a href="http://blog.vimviv.com/blackberry/implement-hotkey-blackberry/" title="hotkey to blackberry options">hotkey to blackberry options</a> (1)</li><li><a href="http://blog.vimviv.com/blackberry/implement-hotkey-blackberry/" title="how to create hot key blackberry">how to create hot key blackberry</a> (1)</li><li><a href="http://blog.vimviv.com/blackberry/implement-hotkey-blackberry/" title="implement hotkey on blackberry app">implement hotkey on blackberry app</a> (1)</li><li><a href="http://blog.vimviv.com/blackberry/implement-hotkey-blackberry/" title="keyboard shortcut blackberry java">keyboard shortcut blackberry java</a> (1)</li><li><a href="http://blog.vimviv.com/blackberry/implement-hotkey-blackberry/" title="keychar and trackball blackberry java">keychar and trackball blackberry java</a> (1)</li></ul><div style='clear:both'></div><p>No related posts.</p>]]></content:encoded>
			<wfw:commentRss>http://blog.vimviv.com/blackberry/implement-hotkey-blackberry/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Native apps vs Web Apps vs Hybrid Apps</title>
		<link>http://blog.vimviv.com/web-apps/native-apps-web-apps-hybrid-apps/</link>
		<comments>http://blog.vimviv.com/web-apps/native-apps-web-apps-hybrid-apps/#comments</comments>
		<pubDate>Sun, 06 Mar 2011 12:15:04 +0000</pubDate>
		<dc:creator>Administrator</dc:creator>
				<category><![CDATA[Web Apps]]></category>
		<category><![CDATA[CSS3]]></category>
		<category><![CDATA[HTML5]]></category>
		<category><![CDATA[phonegap]]></category>
		<category><![CDATA[web-apps]]></category>

		<guid isPermaLink="false">http://blog.vimviv.com/?p=450</guid>
		<description><![CDATA[Native Apps: In mobile native apps are very common. Native apps mean applications that are written specifically for type of mobile devices. Native apps can take advantage of mobile phone&#8217;s functionalities. Native apps can be downloaded and installed. Main disadvantage of native app is if you want to develop same application for different smartphones like [...]
No related posts.]]></description>
			<content:encoded><![CDATA[<p><strong>Native Apps:</strong> In mobile native apps are very common. Native apps mean applications that are written specifically for type of mobile devices. Native apps can take advantage of mobile phone&#8217;s functionalities. Native apps can be downloaded and installed. Main disadvantage of native app is if you want to develop same application for different smartphones like iphone, android, blackberry, etc., you have to write apps in different languages in different platforms. For blackberry and android you can take little benefit of java but iphone is completely different it uses objective c.</p>
<p>&nbsp;</p>
<p><strong>Pros of native apps:</strong></p>
<p>1. You can take advantage of hardware features of devices.</p>
<p>2. You can sell your application on app stores.</p>
<p>3. User can download and install your app.</p>
<p><strong>Cons of native apps:</strong></p>
<p>1. You have to use different platforms, SDK’s, languages for different mobile platforms.</p>
<p>2. You have to pay to become iphone or android or blackberry developer.</p>
<p>3. You have to maintan different builds for different mobile platforms so maintenance, debugging will be slow.</p>
<p>&nbsp;</p>
<p><strong> </strong><br />
<span id="more-450"></span><br />
<strong>Web Apps:</strong> web apps reside on sever and there is no need to install or download the apps. It just a web page optimized for mobile devices. Web Apps can be written in HTML, CSS and javascript. You can simply bookmark the apps for future use.</p>
<p><strong>Pros of web apps:</strong></p>
<p>1. You can use simple HTML, CSS and JS for most of the mobile platforms.</p>
<p>2. You can maintain and fix bugs in real time.</p>
<p><strong>Cons of web apps:</strong></p>
<p>1. You cannot access all hardware functions of mobile devices.</p>
<p>2. You cannot sell your app.</p>
<p>3. For selling from your app you have implement your own checkout system.</p>
<p>&nbsp;</p>
<p><strong>Hybrid apps:</strong> Hybrid apps are just web apps with native wrapper. Hybrid apps run in web view not in native browser. So you can download and install hybrid apps.</p>
<p>There are many mobile development frameworks are available using them you can create hybrid apps.</p>
<p>1. <strong>PhoneGap:</strong> is an open-source mobile development framework. Using <strong>PhoneGap</strong> you can create apps in HTML, CSS and javascript.  PhoneGap also gives access to the native features from javascript.</p>
<p><strong>Supporting platforms:</strong></p>
<p>iOS, android, blackberry, palm webos, symbian, windows mobile</p>
<p><strong>2. Appcelerator Titanium:</strong> Another powerfull mobile development framework is Titanium. Its similar to phonegap with greater native support.</p>
<p><strong>Supporting platforms:</strong></p>
<p>iOS, android</p>
<p>You can see other mobile development frameworks on wikipedia.</p>
<p><a href="http://en.wikipedia.org/wiki/Multiple_phone_web_based_application_framework" target="_blank">Multiple phone web-based application framework</a></p>
<p>I can’t say that web apps will end the native apps but I can say that web apps are great development platform and every mobile developer should consider.<!--more--></p>
<h4>Incoming search terms:</h4><ul><li><a href="http://blog.vimviv.com/web-apps/native-apps-web-apps-hybrid-apps/" title="native application wiki">native application wiki</a> (23)</li><li><a href="http://blog.vimviv.com/web-apps/native-apps-web-apps-hybrid-apps/" title="phonegap hybrid app">phonegap hybrid app</a> (15)</li><li><a href="http://blog.vimviv.com/web-apps/native-apps-web-apps-hybrid-apps/" title="native vs hybrid">native vs hybrid</a> (15)</li><li><a href="http://blog.vimviv.com/web-apps/native-apps-web-apps-hybrid-apps/" title="hybrid web app">hybrid web app</a> (14)</li><li><a href="http://blog.vimviv.com/web-apps/native-apps-web-apps-hybrid-apps/" title="native vs web vs hybrid">native vs web vs hybrid</a> (14)</li><li><a href="http://blog.vimviv.com/web-apps/native-apps-web-apps-hybrid-apps/" title="hybrid vs native">hybrid vs native</a> (14)</li><li><a href="http://blog.vimviv.com/web-apps/native-apps-web-apps-hybrid-apps/" title="hybrid app framework">hybrid app framework</a> (13)</li><li><a href="http://blog.vimviv.com/web-apps/native-apps-web-apps-hybrid-apps/" title="native web hybrid">native web hybrid</a> (12)</li><li><a href="http://blog.vimviv.com/web-apps/native-apps-web-apps-hybrid-apps/" title="hybrid app vs native app">hybrid app vs native app</a> (9)</li><li><a href="http://blog.vimviv.com/web-apps/native-apps-web-apps-hybrid-apps/" title="hybrid web apps">hybrid web apps</a> (9)</li></ul><div style='clear:both'></div><p>No related posts.</p>]]></content:encoded>
			<wfw:commentRss>http://blog.vimviv.com/web-apps/native-apps-web-apps-hybrid-apps/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

<!-- Performance optimized by W3 Total Cache. Learn more: http://www.w3-edge.com/wordpress-plugins/

Page Caching using disk: enhanced (Requested URI is rejected)

Served from: blog.vimviv.com @ 2012-02-23 15:23:42 -->
