Showing posts with label toast. Show all posts
Showing posts with label toast. Show all posts

Saturday, 1 February 2014

Voice/web search and speech recognition

Speech Recognition:

 In this world of smartphones, you can write text message, can search content on internet without writing anything, Speech Recognition will do that for you, you just need to say it.
 Speech recognition is the translation of spoken words into text.So Speak instead of type, using your voice to control your phone or tablet. Search the web and your phone, call your contacts, send emails, get directions and listen to music, all with speech Recognition. And Here is the basic post on how to listen voice and search the spoken text on web.  

On Launch

voice search

OnSpeak

voice search

voice searchvoice search
When Spoke Search Hamad shaikh:

voice searchvoice search

Create new Android Project
Project Name: VoiceRecognition//tested from 2.3.3 to current android sdk 
Build Target: Android 2.3.3   //or greater than that
Application Name: Voice Recognition
Package Name: com.shaikhhamadali.blogspot.voicerecognition
Create layout file: layout_voice_recognition


 1. create layout:


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <EditText
        android:id="@+id/eTTextHint"
        android:gravity="top"
        android:inputType="textMultiLine"
        android:lines="1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="@string/etSearchHint"/>

    <Button
        android:id="@+id/btnSpeak"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:onClick="speak"
        android:padding="10dp"
        android:text="@string/btSpeak"
        tools:context=".VoiceRecognitionActivity" />

    <Spinner
        android:id="@+id/spTextMatches"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:entries="@array/saNoOfMatches"
        android:prompt="@string/sNoOfMatches"/>

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/tvTextMatches"
        android:textStyle="bold" />

    <ListView
        android:id="@+id/lVTextMatches"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

</LinearLayout>

-Manifest file:

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

    <uses-sdk
        android:minSdkVersion="9"
        android:targetSdkVersion="17" />
    
    <!-- Permissions -->
    <uses-permission android:name="android.permission.INTERNET" />
    
    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.shaikhhamadali.blogspot.voicerecognition.VoiceRecognitionActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

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

</manifest>


-Strings file:

<?xml version="1.0" encoding="utf-8"?>
<resources>

    <string name="app_name">VoiceRecognition</string>
    <string name="action_settings">Settings</string>
    <string name="btSpeak">Speak</string>
    <string name="tvTextMatches">Text Matches</string>
    <string name="sNoOfMatches">No of Matches</string>
    <string name="etSearchHint">Speech hint here</string>
    <string-array name="saNoOfMatches">
        <item>1</item>
        <item>2</item>
        <item>3</item>
        <item>4</item>
        <item>5</item>
        <item>6</item>
        <item>7</item>
        <item>8</item>
        <item>9</item>
        <item>10</item>
    </string-array>
</resources>


2.code of activity:


package com.shaikhhamadali.blogspot.voicerecognition;

import java.util.ArrayList;
import java.util.List;
import android.app.Activity;
import android.app.SearchManager;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.os.Bundle;
import android.speech.RecognizerIntent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Spinner;
import android.widget.Toast;

public class VoiceRecognitionActivity extends Activity {
 //variables
 //code to get result
 private static final int VOICE_RECOGNITION_REQUEST_CODE = 1001;
 //declare controls
 private EditText eTTextHint;
 private ListView lVTextMatches;
 private Spinner spTextMatches;
 private Button btnSpeak;

 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.layout_voice_recognition);
  //Initialize controls
  initializeControls();
  // Check if voice recognition is present 
  isVoiceRecognitionAvaialble();
 }
 private void initializeControls() {
  eTTextHint = (EditText) findViewById(R.id.eTTextHint);
  lVTextMatches = (ListView) findViewById(R.id.lVTextMatches);
  spTextMatches = (Spinner) findViewById(R.id.spTextMatches);
  btnSpeak = (Button) findViewById(R.id.btnSpeak);
 }
 public void isVoiceRecognitionAvaialble() {
  PackageManager pm = getPackageManager();
  //start an activity that will prompt the user for speech and send it through a speech recognizer
  List<ResolveInfo> activities = pm.queryIntentActivities(new Intent(
    RecognizerIntent.ACTION_RECOGNIZE_SPEECH), 0);
  //check's that is activity available
  if (activities.size() == 0) {
   btnSpeak.setEnabled(false);
   btnSpeak.setText("Voice recognizer not present");
   Toast.makeText(this, "Voice recognizer not present",
     Toast.LENGTH_SHORT).show();
  }
 }

 public void speak(View view) {
  //create instance of Intent and pass RecognizerIntent.ACTION_RECOGNIZE_SPEECH
  Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);

  // Specify the calling package to identify your application
  intent.putExtra(RecognizerIntent.EXTRA_CALLING_PACKAGE, getClass()
    .getPackage().getName());

  // Display a hint to the user about what he should say.
  intent.putExtra(RecognizerIntent.EXTRA_PROMPT, eTTextHint.getText()
    .toString());

  // Given an hint to the recognizer about what the user is going to say
  //There are two form of language model available
  //1.LANGUAGE_MODEL_WEB_SEARCH : For short phrases
  //2.LANGUAGE_MODEL_FREE_FORM  : If not sure about the words or phrases and its domain.
  intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
    RecognizerIntent.LANGUAGE_MODEL_WEB_SEARCH);

  // If number of Matches is not selected then return show toast message
  if (spTextMatches.getSelectedItemPosition() == AdapterView.INVALID_POSITION) {
   Toast.makeText(this, "Please select No. of Matches from spinner",
     Toast.LENGTH_SHORT).show();
   return;
  }

  int noOfMatches = Integer.parseInt(spTextMatches.getSelectedItem()
    .toString());
  // Specify how many results you want to receive. The results will be
  // sorted where the first result is the one with higher confidence.
  intent.putExtra(RecognizerIntent.EXTRA_MAX_RESULTS, noOfMatches);
  //Start the Voice recognizer activity for the result.
  startActivityForResult(intent, VOICE_RECOGNITION_REQUEST_CODE);
 }

 @Override
 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
  if (requestCode == VOICE_RECOGNITION_REQUEST_CODE)

   //If Voice recognition is successful then it returns RESULT_OK
   if(resultCode == RESULT_OK) {
    // creates list of all the text matches
    ArrayList<String> textMatchList = data
      .getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
    //proceed further if is list is not empty
    if (!textMatchList.isEmpty()) {
     // If first Match contains the 'search' word
     // Then start web search.
     if (textMatchList.get(0).contains("search")) {

      String searchQuery = textMatchList.get(0);
      searchQuery = searchQuery.replace("search","");
      //create instance of Intent and pass Intent.ACTION_WEB_SEARCH
      Intent search = new Intent(Intent.ACTION_WEB_SEARCH);
      //put text you want to search
      search.putExtra(SearchManager.QUERY, searchQuery);
      //start activity and pass search intent
      startActivity(search);
     } else {
      // populate the Matches
      lVTextMatches.setAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,textMatchList));
     }

    }
    //Result code for various error.
   }else if(resultCode == RecognizerIntent.RESULT_AUDIO_ERROR){
    showToastMessage("Audio Error");
   }else if(resultCode == RecognizerIntent.RESULT_CLIENT_ERROR){
    showToastMessage("Client Error");
   }else if(resultCode == RecognizerIntent.RESULT_NETWORK_ERROR){
    showToastMessage("Network Error");
   }else if(resultCode == RecognizerIntent.RESULT_NO_MATCH){
    showToastMessage("No Match");
   }else if(resultCode == RecognizerIntent.RESULT_SERVER_ERROR){
    showToastMessage("Server Error");
   }
  super.onActivityResult(requestCode, resultCode, data);
 }
 /**
  * Helper method to show the toast message
  **/
 void showToastMessage(String message){
  Toast.makeText(this, message, Toast.LENGTH_SHORT).show();
 }
}

3. note that:


      4. conclusion:

      • Some information about how to use Voice Recognition Service in Android using RecognizerIntent.
      • know how to use Intents to Recognize voice, WebSearch and what search keyword do.

      5. About the post:

      • The code seems to explain itself due to comments, but if you have any questions you can freely ask too!
      • Don’t mind to write a comment whatever you like to ask, to know,to suggest or recommend.
      • Hope you enjoy it!

      6. Source Code:

              you can download the source code GoogleDriveGithub

      Tuesday, 10 September 2013

      Plain Toast

      Custom Toast

      Custom Toast

      Custom Toast

      Create new Android Project
      Project Name: typesoftoasts
      //tested from 2.3.3 to current android sdk 
      Build Target: Android 2.3.3   //or greater than that
      Application Name: Types Of Toasts
      Package Name: com.shaikhhamadali.blogspot.typesoftoast
      Create layout file: activity_toast_types

      1.create layout:


      • activity_toast_types:

      <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
          xmlns:tools="http://schemas.android.com/tools"
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          tools:context=".ToastTypes" >
      
          <TextView
              android:id="@+id/tVHeading"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:layout_centerHorizontal="true"
              android:text="Plain and Custom Toast demo"
              android:textStyle="bold|italic"
              android:textColor="#6A96B0"
              android:textSize="50dp" />
      
          <Button
              android:id="@+id/btnPlain"
              android:layout_width="350dp"
              android:layout_height="100dp"
              android:layout_below="@+id/textView1"
              android:layout_centerHorizontal="true"
              android:layout_marginLeft="48dp"
              android:layout_marginTop="74dp"
              android:text="Plain Toast"
              android:textSize="40dp" />
      
          <Button
              android:id="@+id/btnCustom"
              android:layout_width="350dp"
              android:layout_height="100dp"
              android:layout_alignRight="@+id/btnPlain"
              android:layout_below="@+id/button1"
              android:layout_marginTop="200dp"
              android:text="Custom Toast"
              android:textSize="40dp" />
      
      </RelativeLayout>

      • customized_toast:


      <?xml version="1.0" encoding="utf-8"?>
      <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
             android:id="@+id/customizedToast"
             android:orientation="vertical"     
             android:layout_width="wrap_content"
             android:layout_height="wrap_content"
             android:background="@drawable/gradiant"
             android:layout_margin="25dip">
             <LinearLayout 
                    android:layout_margin="5dip"
                    android:orientation="horizontal"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content">
                    <ImageView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:src="@drawable/celebration"/>
                   <LinearLayout
                          android:orientation="vertical"
                          android:layout_width="fill_parent"
                          android:layout_height="fill_parent"
                          android:gravity="center">
                          <TextView
                              android:paddingLeft="20dip"
                              android:layout_width="fill_parent"
                              android:layout_height="fill_parent"
                              android:id="@+id/tVToast"
                              android:textColor="#ffffff"
                              android:textSize="25sp"
                              android:textStyle="bold"
                              android:gravity="center"/>
                  </LinearLayout>
            </LinearLayout>
      </LinearLayout>


      • gradiant:

      <?xml version="1.0" encoding="utf-8"?>
      
      <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle" >
      <gradient 
      android:type="linear"
      android:centerX="50%" 
      android:startColor="#FF003333" 
      android:centerColor="#FF05C1FF" 
      android:endColor="#FF003333" 
      android:angle="270"/>
      <!-- <gradient
          android:centerColor="#FF05C1FF"
          android:centerX="50%"
          android:centerY="50%"
          android:endColor="#FF003333"
          android:gradientRadius="50"
          android:startColor="#FF003333"
          android:type="radial" /> -->
      
      <corners
          android:bottomLeftRadius="15dp"
          android:bottomRightRadius="15dp"
          android:topLeftRadius="15dp"
          android:topRightRadius="15dp" />
      
      </shape>

      2. code of activity:


      package com.shaikhhamadali.blogspot.typesoftoast;
      
      import android.os.Bundle;
      import android.app.Activity;
      import android.view.LayoutInflater;
      import android.view.View;
      import android.view.View.OnClickListener;
      import android.view.ViewGroup;
      import android.widget.Button;
      import android.widget.TextView;
      import android.widget.Toast;
      
      public class ToastTypes extends Activity {
      
       Button btnPlain,btnCustom;
       
       @Override
       protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_toast_types);
        
        
        btnPlain=(Button)findViewById(R.id.btnPlain);
        btnPlain.setOnClickListener(new OnClickListener() {
         
         @Override
         public void onClick(View v) {
          // TODO Auto-generated method stub
          //create instance of toast make text (context,text,time);
          Toast t=Toast.makeText(getApplicationContext(), "welcome to hamad's blog", Toast.LENGTH_SHORT);
          //show toast
          t.show();
          
          
         }
        });
        btnCustom=(Button)findViewById(R.id.btnCustom);
        btnCustom.setOnClickListener(new OnClickListener() {
         
         @Override
         public void onClick(View v) {
          // TODO Auto-generated method stub
          //create instance of inflater
          LayoutInflater inflater = getLayoutInflater();
          //create instance of View and inflate out toast layout in view
          View layout = inflater.inflate(R.layout.customized_toast, (ViewGroup) findViewById(R.id.customizedToast));
          //set your desired text to out toast text view
          ((TextView) layout.findViewById(R.id.tVToast)).setText("welcome to hamad's blog.");
          //create instance of Toast
          Toast toast = new Toast(getApplicationContext());
          //set time duration;
          toast.setDuration(Toast.LENGTH_SHORT);
          //set view of toast that is custom toast layout
          toast.setView(layout);
          //show toast
          toast.show();
         }
        });
        
       }
      }

      also can use toast like this (for plain Toast):

      Toast.makeText(getApplicationContext(), "welcome to hamad's blog", Toast.LENGTH_SHORT).show();

      3. note that:

      • you can use these on button on click,on action_down,on the fly etc.
      • Toast.LENGTH_SHORT duration is about 2 seconds or 2000 mili seconds.
      • Toast.LENGTH_LONG duration is about 3.5 seconds and 3500 mili seconds.
      • you can set the typeface of your toast text view.
      • you can use image as background.
      • you can apply custom shape to your toast.
      • to generate custom shape online i have added new link in useful links named as "Gradient Shape Generator Online" (android patterns).

        4. conclusion:


      • Some information about basic/simple Toast.
      • Some information about Custom Toast.
      • Know how to create your custom generated toast.
      • know how to show simple toast in different ways.

        5. About the post:


      • The code seems to explain itself due to comments, if you have any question you can ask too!
      •  Don’t mind to write a comment whatever you like to ask, to know,to suggest or recommend.
      •  Hope you enjoy it!

        6. Source Code:
              you can download the source code here

      Cheers,

      Hamad Ali Shaikh