Friday, December 26, 2014

Speech to text in android

Hi friends This is speech to text in android as below:-


Layout:-

<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"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.speech.MainActivity" >

    <ListView
        android:id="@+id/lvOptions"
        android:layout_width="match_parent"
        android:layout_height="350dp"
        android:layout_alignParentLeft="true" >

    </ListView>

    <Button
        android:id="@+id/bSpeak"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="17dp"
        android:text="Speak" />

</RelativeLayout>







Java class:-


public class MainActivity extends ActionBarActivity {

Button speak;
    ListView options;
    ArrayList<String> results;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

 speak = (Button) findViewById(R.id.bSpeak);
       options = (ListView) findViewById(R.id.lvOptions);
 
       speak.setOnClickListener(new OnClickListener() {
 
           @Override
           public void onClick(View v) {
               // TODO Auto-generated method stub
               // This are the intents needed to start the Voice recognizer
               Intent i = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
               i.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
                       RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
               i.putExtra(RecognizerIntent.EXTRA_PROMPT, "Say something");
 
               startActivityForResult(i, 1010);
           }
       });
 
       // retrieves data from the previous state. This is incase the phones
       // orientation changes
       if (savedInstanceState != null) {
           results = savedInstanceState.getStringArrayList("results");
 
           if (results != null)
               options.setAdapter(new ArrayAdapter<String>(this,
                       android.R.layout.simple_list_item_1, results));
       }
   }
 
   @Override
   protected void onActivityResult(int requestCode, int resultCode, Intent data) {
       // TODO Auto-generated method stub
       // retrieves data from the VoiceRecognizer
       if (requestCode == 1010 && resultCode == RESULT_OK) {
           results = data
                   .getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
           options.setAdapter(new ArrayAdapter<String>(this,
                   android.R.layout.simple_list_item_1, results));
       }
 
       super.onActivityResult(requestCode, resultCode, data);
   }
 
   @Override
   protected void onSaveInstanceState(Bundle outState) {
       // This should save all the data so that when the phone changes
       // orientation the data is saved
       super.onSaveInstanceState(outState);
 
       outState.putStringArrayList("results", results);
   }
 
}


Thank you .....

Text to speech in android

Hi friends this is text to speech in android:-

Layout is :-

<?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"
    android:background="#ffffff">
   
<TextView android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="Text To Speech"
    android:padding="15dip"
    android:textColor="#0587d9"
    android:textSize="26dip"
    android:gravity="center"
    android:textStyle="bold"/>

    <EditText android:id="@+id/txtText"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:hint="Enter some text to speak"
        android:layout_marginTop="20dip"      
        android:layout_margin="10dip"/>
   
    <Button android:id="@+id/btnSpeak"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="Speak Out"
        android:layout_margin="10dip"/>

</LinearLayout>








Class:-


public class AndroidTextToSpeechActivity extends Activity implements
TextToSpeech.OnInitListener {
/** Called when the activity is first created. */

private TextToSpeech tts;
private Button btnSpeak;
private EditText txtText;

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

tts = new TextToSpeech(this, this);

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

txtText = (EditText) findViewById(R.id.txtText);



btnSpeak.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View arg0) {
speakOut();
}

});
}

@Override
public void onDestroy() {
// Don't forget to shutdown!
if (tts != null) {
tts.stop();
tts.shutdown();
}
super.onDestroy();
}

@Override
public void onInit(int status) {
// TODO Auto-generated method stub

if (status == TextToSpeech.SUCCESS) {

int result = tts.setLanguage(Locale.US);


if (result == TextToSpeech.LANG_MISSING_DATA
|| result == TextToSpeech.LANG_NOT_SUPPORTED) {
Log.e("TTS", "Language is not supported");
} else {
btnSpeak.setEnabled(true);
speakOut();
}

} else {
Log.e("TTS", "Initilization Failed");
}

}

private void speakOut() {

String text = txtText.getText().toString();

tts.speak(text, TextToSpeech.QUEUE_FLUSH, null);
}
}


Thank you in advance:-


Multiple Language App in android

Hello friends This is multiple language app in detail:-


Multiple Language Selection Application:-
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">

    
   
    <TextView
        android:id="@+id/tvinamea"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/greet"
        />
   
   
       <TextView
        android:id="@+id/textView1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/langselection"
        android:textAppearance="?android:attr/textAppearanceMedium"
         android:gravity="center" android:paddingTop="25sp"/>
      
     <Spinner
        android:id="@+id/spndata"
        android:layout_width="fill_parent"
        android:layout_marginLeft="20dp"
        android:layout_marginRight="20dp"
        android:layout_height="wrap_content"
        android:entries="@array/languages" 
        />
   
    <TextView
        android:id="@+id/descreption"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/descreption"
        android:textSize="25sp"
        android:gravity="center"
         android:paddingTop="25sp" />
    
    
</LinearLayout>

.class:-

package com.example.multiplecountry;

import java.util.Locale;

import android.app.Activity;
import android.content.Intent;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.Spinner;
import android.widget.TextView;

public class MainActivity extends Activity {
                Locale myLocale;
                Spinner spndata;
                TextView tviname,tviDescript;

                @Override
                protected void onCreate(Bundle savedInstanceState) {
                                super.onCreate(savedInstanceState);
                                setContentView(R.layout.activity_main);

                                spndata = (Spinner) findViewById(R.id.spndata);
                                 
                               

                                spndata.setOnItemSelectedListener(new OnItemSelectedListener() {

                                                @Override
                                                public void onItemSelected(AdapterView<?> parent, View v,
                                                                                int pos, long id) {
                                                                // TODO Auto-generated method stub
                                                                String country = parent.getItemAtPosition(pos).toString();
                                                                /* System.out.println("Country=="+country); */
                                                               
                                                                   if (pos == 1) {
                                                                                  
                                   
                                    setLocale("ta");
                                } else if (pos == 2) {
                 
                                   
                                    setLocale("hi");
                                } else if (pos == 3) {
                 
                                    
                                    setLocale("en");
                                }
                                else if (pos == 4) {
                 
                                   
                                    setLocale("fr");
                                }
                                                                 
                                else if (pos == 5) {
                 
   
                                                setLocale("ru");
                                }
                                else if (pos == 6) {
                 
   
                                                setLocale("it");
                                }
                                else if (pos == 7) {
                 
                                   
                                    setLocale("ja");
                                }
                                else if (pos == 8) {
                 
                                   
                                    setLocale("ta");
                                }
                                                                 

                                                }

                                                @Override
                                                public void onNothingSelected(AdapterView<?> arg0) {
                                                                // TODO Auto-generated method stub

                                                }
                                });

                }
                 
                private void setLocale(String lang) {
                                // TODO Auto-generated method stub
                                myLocale = new Locale(lang);
                                Resources res = getResources();
                                DisplayMetrics dm = res.getDisplayMetrics();
                                Configuration conf = res.getConfiguration();
                                conf.locale = myLocale;
                                res.updateConfiguration(conf, dm);
                                Intent refresh = new Intent(this, MainActivity.class);
                                startActivity(refresh);
                }

}








Values-en:-

<resources>

    <string name="app_name">Androidlocalization</string>
    <string name="hello_world">Hello world!</string>
    <string name="title_activity_android_localize">AndroidLocalize</string>
    <string name="greet">Welldone Amit sharma!!</string>
    <string name="descreption">Welldone Amit sharma!!</string>
    <string name="langselection">Select the language in which you want to greet Amit sharma!!!!</string>
    <string name="chooselang">Choose the language</string>
      <string-array name="languages">
        <item>Select language</item>
        <item>தமிழ்</item>
        <item>हिंदी</item>
        <item>English</item>
        <item>Franch</item>
         <item>ぐるたび</item>
         <item>italiano</item>
         <item>Россия</item>
    </string-array>

</resources>


Values-fr:-

<resources>

     <string name="app_name">Androidlocalization</string>
    <string name="hello_world">Hello world!</string>
    <string name="title_activity_android_localize">AndroidLocalize</string>
    <string name="greet">Welldone Amit sharma!!</string>
    <string name="descreption">Welldone Amit sharma!!</string>
    <string name="langselection">Select the language in which you want to greet Amit sharma!!!!</string>
    <string name="chooselang">Choose the language</string>
      <string-array name="languages">
        <item>Select language</item>
        <item>தமிழ்</item>
        <item>हिंदी</item>
        <item>English</item>
        <item>Franch</item>
         <item>ぐるたび</item>
         <item>italiano</item>
         <item>Россия</item>
    </string-array>

</resources>


Values-hi:-

<resources>

    <string name="app_name">Androidlocalization</string>
    <string name="hello_world">Hello world!</string>
    <string name="title_activity_android_localize">AndroidLocalize</string>
    <string name="greet">बधाई अमित शर्मा!!</string>
    <string name="descreption">बधाई अमित शर्मा !!</string>
    <string name="langselection">जिस भाषा में आप अमित शर्मा  को नमस्कार करना चाहते हैं का चयन करें!!!!</string>
   
   
    <string name="chooselang">Choose the language</string>
      <string-array name="languages">
        <item>Select language</item>
        <item>தமிழ்</item>
        <item>हिंदी</item>
        <item>English</item>
              <item>Franch</item>
               <item>ぐるたび</item>
               <item>italiano</item>
<item>Россия</item>


</string-array>

</resources>
Values-it

<resources>

    <string name="app_name">Androidlocalization</string>
  
    <string name="hello_world">Ciao mondo!</string>
    <string name="title_activity_android_localize">AndroidLocalize</string>
    <string name="greet">Welldone Amit Sharma !!</string>
    <string name="descreption">Welldone Amit Sharma !!</string>
    <string name="langselection">Selezionare la lingua in cui si desidera salutare Amit Sharma !!!!</string>
     <string name="chooselang">Choose the language</string>
    
   
    <string-array name="languages">
        <item>Select language</item>
        <item>தமிழ்</item>
        <item>हिंदी</item>
        <item>English</item>
        <item>Franch</item>
         <item>ぐるたび</item>
         <item>italiano</item>
         <item>Россия</item>
    </string-array>

</resources>

Values-ja:-

<resources>

    <string name="app_name">Androidlocalization</string>
    <string name="hello_world">Hello world!</string>
    <string name="title_activity_android_localize">AndroidLocalize</string>
    <string name="greet">Welldoneアミットのシャルマ!!</string>
    <string name="descreption">Welldoneアミットのシャルマ!!</string>
    <string name="langselection">あなたはアミットのシャルマに挨拶したい言語を選択します!!!!</string>
    <string name="chooselang">言語を選択</string>
      <string-array name="languages">
        <item>Select language</item>
        <item>தமிழ்</item>
        <item>हिंदी</item>
        <item>English</item>
        <item>Franch</item>
        <item>ぐるたび</item>
        <item>italiano</item>
        <item>Россия</item>
    </string-array>

</resources>

Values-ru:-

<resources>

    <string name="app_name">Androidlocalization</string>
    <string name="hello_world">Привет, мир!</string>
    <string name="title_activity_android_localize">AndroidLocalize</string>
    <string name="greet">Welldone Амит Шарма !!</string>
    <string name="descreption">Welldone Амит Шарма !!</string>
    <string name="langselection">Выберите язык, на котором вы хотите, чтобы приветствовать Амит Шарма !!!!</string>
    <string name="chooselang">Choose the language</string>
      <string-array name="languages">
        <item>Select language</item>
        <item>தமிழ்</item>
        <item>हिंदी</item>
        <item>English</item>
        <item>Franch</item>
         <item>ぐるたび</item>
       <item>Россия</item>
    </string-array>

</resources>

Values-ta:-


<resources>

    <string name="app_name">Androidlocalization</string>
    <string name="title_activity_android_localize">AndroidLocalize</string>
    <string name="greet">பாராட்டுக்கள் அமித் சர்மா!!</string>
    <string name="descreption">பாராட்டுக்கள் அமித் சர்மா!!</string>
    <string name="langselection">நீங்கள் சச்சினை வாழ்த்த விரும்பும் மொழியினை தேர்ந்தெடுக்கவும்!!!!</string>
    <string name="chooselang">மொழியை தேர்ந்தெடு</string>

    <string-array name="languages">
        <item>Select language</item>
        <item>தமிழ்</item>
        <item>हिंदी</item>
        <item>English</item>
              <item>Franch</item>
               <item>ぐるたび</item>
               <item>italiano</item>
               <item>Россия</item>
    </string-array>

</resources>