Monday, 24 October 2011

Talking Clock And Text To Seach Converter

Designing Part:



Layout file: Main.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" 
    android:background="@drawable/bg23">
<TextView 
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="@string/hello"/>
<AnalogClock
    android:id="@+id/myAnalogClock"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"/>
<TextView
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="I can speech"/>
<TextView
    android:id="@+id/myText"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" />
   
<TextView android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceMedium"
    android:id="@+id/textView1"
    android:text="Enter The Text For Speech:"/>
   
<EditText android:layout_width="match_parent"
    android:id="@+id/txtspeek"
    android:hint="Enterr Text Here"
    android:layout_height="109dp"/>
    
<Button android:layout_height="wrap_content"
    android:layout_width="match_parent"
    android:text="Speek"
    android:textSize="20dip"
    android:textStyle="bold"
    android:id="@+id/speek"/>
   
</LinearLayout>

Coding Part:

TalkingClockActivity.java.java as shown in below:


package com.murali.speechtime;
import java.util.Calendar;
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.AnalogClock;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class TalkingClockActivity extends Activity implements OnInitListener {
        TextToSpeech   myTTS;
        AnalogClock   
MyAnalogClock;
        TextView
MyText;
        Button
Speek;
        EditText
TextSpeech;
       
private int mHour, mMinute;   
        /** Called when the activity is first created. */
        @Override
        public void onCreate(Bundle savedInstanceState) {
       
super.onCreate(savedInstanceState);
          setContentView(R.layout.
main);   
      
          myTTS = new TextToSpeech(this, this);
         
MyAnalogClock = (AnalogClock)findViewById(R.id.myAnalogClock);
         
MyAnalogClock.setOnClickListener(MyAnalogClockOnClickListener);
         
MyText = (TextView)findViewById(R.id.myText);
        
TextSpeech=(EditText) findViewById(R.id.txtspeek);
        
Speek=(Button) findViewById(R.id.speek);

        
Speek.setOnClickListener(new OnClickListener() {
         @Override
   
     public void onClick(View v) {
                       
// TODO Auto-generated method stub
                         String speech=TextSpeech.getText().toString();
                         
myTTS.speak(speech, TextToSpeech.QUEUE_FLUSH, null);
                  }
            });
        }

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

       }
      
@Override
       protected void onDestroy() {
  
     super.onDestroy();
      
myTTS.shutdown();
       }
    private AnalogClock.OnClickListener MyAnalogClockOnClickListener = new     AnalogClock.OnClickListener(){
         @Override
          public void onClick(View v) {           
          final Calendar c = Calendar.getInstance();

               
mHour = c.get(Calendar.HOUR_OF_DAY);
               
mMinute = c.get(Calendar.MINUTE);       
                String myTime= "Now is "
                                                 
 + String.valueOf(mHour)
                                                   +
" Hour "
                               
                   +String.valueOf(mMinute)
                                                   +
" Minute";
             MyText.setText(myTime);
            
myTTS.speak(myTime, TextToSpeech.QUEUE_FLUSH, null);
              }
           };
      }

We click on Analog Clock It tell Current Time.

1 comment:

  1. I have read your blog its very attractive and impressive. I like it your blog.

    Java Training in Chennai Java Training in Chennai | Core Java Training in Chennai

    Online Java Training Online Java Training | Java J2EE Online Training | JavaEE Training Institute in Chennai m

    ReplyDelete