Wednesday 27 February 2013

speech to text using android

main.xml:
<?xml version="1.0" encoding="utf-8"?>
 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/LinearLayout1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:text="Speach To Text Example"
        android:textAppearance="?android:attr/textAppearanceLarge" />

    <Button
        android:id="@+id/speakButton"
        android:layout_width="150dp"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_marginTop="30dp"
        android:text="Speak" />

    <TextView
        android:id="@+id/displayTextView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_marginTop="30dp"
        android:textAppearance="?android:attr/textAppearanceLarge" />

</LinearLayout>

activaty.java:

package com.test.speechtotext;


import android.os.Bundle;
import java.util.ArrayList;

import android.app.Activity;
import android.content.Intent;

import android.speech.RecognizerIntent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

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

        displayText = (TextView) findViewById(R.id.displayTextView);

        Button speakButton = (Button) findViewById(R.id.speakButton);

        speakButton.setOnClickListener(new OnClickListener() {

        public void onClick(View v) {
        // TODO Auto-generated method stub

        Intent in = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);

        in.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, "en-US");

        try {
        startActivityForResult(in, 1);
        } catch (Exception e) {
        // TODO: handle exception
        Toast.makeText(
        getApplicationContext(),
        "sorry, your device doesn't support speech to text feature",
        Toast.LENGTH_LONG).show();
        }

        }
        });
        }

        @Override
        protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        // TODO Auto-generated method stub
        if (requestCode == 1) {
        if (resultCode == RESULT_OK && data != null) {
        ArrayList<String> text = data
        .getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);

        displayText.setText(text.get(0));
        }
        }
        super.onActivityResult(requestCode, resultCode, data);
        }

    }

text to speech using android


The Android text to speech engine still seems to be a pretty underused resource in Android apps. However, implementing it in your own applications is straightforward. There are a few potential issues and choices you need to consider, but for most purposes, the process is not a complex one. In this tutorial we jump around a bit within one Android Activity, but don’t worry, the complete code is listed at the end. The aim is to give you a clear idea of the what’s going on at each processing stage so that you can successfully use the function in any app.
open it in your IDE. Otherwise, create a new Android project. You can use the code in this tutorial with any Activity class. For demonstration, we will first create some user interface elements. Again, if you already have your own UI, you can use it instead.

main.xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/LinearLayout1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Enter text to speak"
        android:textAppearance="?android:attr/textAppearanceLarge" />

    <EditText
        android:id="@+id/TexteditText"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:ems="10"
        android:inputType="textPersonName" >

        <requestFocus />
    </EditText>

    <Button
        android:id="@+id/speakButton"
        android:layout_width="130dp"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:text="Speak" />

</LinearLayout>

activity.java:

package com.test.testspeech;

import android.app.Activity;
import android.os.Bundle;
import android.speech.tts.TextToSpeech;
import android.speech.tts.TextToSpeech.OnInitListener;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class TexttospeechActivity extends Activity {
    /** Called when the activity is first created. */
            EditText edit;
            TextToSpeech textToSpeech;

             @Override
             protected void onCreate(Bundle savedInstanceState) {
              super.onCreate(savedInstanceState);
              setContentView(R.layout.main);
             
              textToSpeech = new TextToSpeech(getApplicationContext(), new OnInitListener() {
              
               public void onInit(int status) {
                // TODO Auto-generated method stub
                Toast.makeText(getApplicationContext(), "TextToSpeech engine intialiazed", Toast.LENGTH_LONG).show();
               }
              });
             
              edit = (EditText)findViewById(R.id.TexteditText);
             
              Button speakButton  = (Button)findViewById(R.id.speakButton);
             
              speakButton.setOnClickListener(new OnClickListener() {
              
               public void onClick(View arg0) {
                // TODO Auto-generated method stub
                String text = edit.getText().toString();
               
               
                textToSpeech.speak(text, RESULT_OK, null);
               
               }
              });
             
             }

}

out put:



Tuesday 26 February 2013

Building Web Apps in WebView


If you want to deliver a web application (or just a web page) as a part of a client application, you can do it using WebView. The WebView class is an extension of Android's View class that allows you to display web pages as a part of your activity layout. It does not include any features of a fully developed web browser, such as navigation controls or an address bar. All that WebView does, by default, is show a web page.

A common scenario in which using WebView is helpful is when you want to provide information in your application that you might need to update, such as an end-user agreement or a user guide. Within your Android application, you can create an Activity that contains a WebView, then use that to display your document that's hosted online.

This document shows you how to get started with WebView and how to do some additional things, such as handle page navigation and bind JavaScript from your web page to client-side code in your Android application.

To add a WebView to your Application, simply include the <WebView> element in your activity layout. For example, here's a layout file in which the WebView fills the screen:

<?xml version="1.0" encoding="utf-8"?>
<WebView  xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/webview"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
/>

To load a web page in the WebView, use loadUrl(). For example:

WebView myWebView = (WebView) findViewById(R.id.webview);
myWebView.loadUrl("http://www.example.com");

Before this will work, however, your application must have access to the Internet. To get Internet access, request the INTERNET permission in your manifest file. For example:

<manifest ... >
    <uses-permission android:name="android.permission.INTERNET" />
    ...
</manifest>

That's all you need for a basic WebView that displays a web page.
JavaScript is disabled in a WebView by default. You can enable it through the WebSettings attached to your WebView. You can retrieve WebSettings with getSettings(), then enable JavaScript with setJavaScriptEnabled().
For example:

WebView myWebView = (WebView) findViewById(R.id.webview);
WebSettings webSettings = myWebView.getSettings();
webSettings.setJavaScriptEnabled(true);

WebSettings provides access to a variety of other settings that you might find useful. For example, if you're developing a web application that's designed specifically for the WebView in your Android application, then you can define a custom user agent string with setUserAgentString(), then query the custom user agent in your web page to verify that the client requesting your web page is actually your Android application.
from your Android SDK tools/ directory

When developing a web application that's designed specifically for the WebView in your Android application, you can create interfaces between your JavaScript code and client-side Android code. For example, your JavaScript code can call a method in your Android code to display a Dialog, instead of using JavaScript's alert() function.

To bind a new interface between your JavaScript and Android code, call addJavascriptInterface(), passing it a class instance to bind to your JavaScript and an interface name that your JavaScript can call to access the class.

For example, you can include the following class in your Android application:
public class WebAppInterface {

    Context mContext;

    /** Instantiate the interface and set the context */

    WebAppInterface(Context c) {

        mContext = c;

    }


    /** Show a toast from the web page */

    @JavascriptInterface

    public void showToast(String toast) {

        Toast.makeText(mContext, toast, Toast.LENGTH_SHORT).show();

    }

}
 
Example webview:  
 


 main.xml:

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

    <WebView
        android:id="@+id/webview"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" />

</LinearLayout>


Activity:

package com.inventiveera.webview;

import android.app.Activity;
import android.os.Bundle;
import android.webkit.WebSettings;
import android.webkit.WebView;

public class WebViewActivity extends Activity {
            /** Called when the activity is first created. */

            WebView webview;

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

                        webview = (WebView) findViewById(R.id.webview);

                        webview.loadUrl("http://aspdotnetsiva.blogspot.in/");
                        WebSettings ws = webview.getSettings();
                        ws.setJavaScriptEnabled(true);
                        ws.setBuiltInZoomControls(true);
                        ws.setMinimumFontSize(20);
            }
}


mainifest.xml:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.inventiveera.webview"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk android:minSdkVersion="10" />
            <uses-permission android:name="android.permission.INTERNET"/>

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
        <activity
            android:label="@string/app_name"
            android:name=".WebViewActivity" >
            <intent-filter >
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>