Monday, 8 February 2016

TextToSpeech Application


This is the android code for TextToSpeech Application.

MainActivity.java

package com.example.hanu.texttospeech;

import android.os.Bundle;
import android.speech.tts.TextToSpeech; //Class TextToSpeech
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Button;
import android.widget.EditText;

import java.util.Locale;

public class MainActivity extends AppCompatActivity implements TextToSpeech.OnInitListener {

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

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

        tts = new TextToSpeech(this,this);
        btnSpeak = (Button)findViewById(R.id.button);
        txtText = (EditText)findViewById(R.id.editText);
        btnSpeak.setOnClickListener(new View.OnClickListener() {

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

            }


        });

        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        FloatingActionButton fab = (FloatingActionButton)findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override            
              public void onClick(View view) {
                Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                        .setAction("Action", null).show();
            }
        });
    }

    @Override    
        public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.        
         getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override    
      public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will        
// automatically handle clicks on the Home/Up button, so long        
// as you specify a parent activity in AndroidManifest.xml.        
             int id = item.getItemId();

        //noinspection SimplifiableIfStatement        
            if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    @Override    
      public void onInit(int i) {
        if(i==TextToSpeech.SUCCESS){
            int result = tts.setLanguage(Locale.ENGLISH);
            if(result==TextToSpeech.LANG_NOT_SUPPORTED || result==TextToSpeech.LANG_MISSING_DATA){
                Log.e("TTS", "This language is not supported");
            }
            else{
                btnSpeak.setEnabled(true);
                speakout();
            }
        }
        else{
            Log.e("TTS" , "Initialization Failed");
        }

    }
    private void speakout() {
       String text=txtText.getText().toString();
        tts.speak(text,TextToSpeech.QUEUE_FLUSH,null);
    }
}

Content_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    
xmlns:app="http://schemas.android.com/apk/res-auto"    
xmlns:tools="http://schemas.android.com/tools"   
android:layout_width="match_parent"  
android:layout_height="match_parent"    
android:paddingBottom="@dimen/activity_verticaan"    
android:paddingLeft="@dimen/activity_horizontal_margin"    
android:paddingRight="@dimen/activity_horizontal_margin"    
android:paddingTop="@dimen/activity_vertical_margin"    
app:layout_behavior="@string/appbar_scrolling_view_behavior"    
tools:context="com.example.hanu.texttospeech.MainActivity"    
tools:showIn="@layout/activity_main">
<TextView android:layout_width="wrap_content"        
android:layout_height="wrap_content"        
android:text="Hello World!"        
android:id="@+id/textView" />
<Button android:layout_width="wrap_content"  
android:layout_height="wrap_content"        
android:text="New Button"        
android:id="@+id/button"        
android:layout_below="@+id/textView"        
android:layout_centerHorizontal="true"        
android:layout_marginTop="173dp" />
<EditText android:layout_width="wrap_content"        
android:layout_height="wrap_content"        
android:id="@+id/editText"        
android:layout_marginTop="53dp"      
android:layout_below="@+id/textView"     
android:layout_alignParentRight="true"        
android:layout_alignParentEnd="true"        
android:layout_toRightOf="@+id/textView"        
android:layout_toEndOf="@+id/textView" />
</RelativeLayout>


activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"    
xmlns:app="http://schemas.android.com/apk/res-auto"    
xmlns:tools="http://schemas.android.com/tools"    
android:layout_width="match_parent"    
android:layout_height="match_parent"    
android:fitsSystemWindows="true"    
tools:context="com.example.hanu.texttospeech.MainActivity">
<android.support.design.widget.AppBarLayout 
android:layout_width="match_parent"        
android:layout_height="wrap_content"        
android:theme="@style/AppTheme.AppBarOverlay">
<android.support.v7.widget.Toolbar            
android:id="@+id/toolbar"            
android:layout_width="match_parent"            
android:layout_height="?attr/actionBarSize"            
android:background="?attr/colorPrimary"            
app:popupTheme="@style/AppTheme.PopupOverlay" />
</android.support.design.widget.AppBarLayout>
<include layout="@layout/content_main" />
<android.support.design.widget.FloatingActionButton        
android:id="@+id/fab"        
android:layout_width="wrap_content"        
android:layout_height="wrap_content"        
android:layout_gravity="bottom|end"        
android:layout_margin="@dimen/fab_margin"        
android:src="@android:drawable/ic_dialog_email" />
</android.support.design.widget.CoordinatorLayout>

If uh have queries for this program . Comment the query in comment box below .

No comments:

Post a Comment