Tell your application to change your activity background color through your voice command. And you can also move to the next activity by voice command. The voice recognition in android is a cool feature that user can interact with the application.
On "Next" starts next activity
Step 1: Create new project in your res ⇒ drawable put some image for your background and the mic image also.
Step2: In res ⇒ values rightclick on values folder ⇒ new ⇒ other and create a new xml and name it as color.xml
color.xml :
AndroidManifest.xml :
Step 5: Finally in your MainActivity copy the below code
MainActivity.java :
Background
Image Color
Image Color
On "Next" starts next activity
Step 1: Create new project in your res ⇒ drawable put some image for your background and the mic image also.
Step2: In res ⇒ values rightclick on values folder ⇒ new ⇒ other and create a new xml and name it as color.xml
color.xml :
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="green">#317533</color>
<color name="black">#000000</color>
</resources>
Step 3: And in your layout leave a text what to do and a mic image.AndroidManifest.xml :
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<Button
android:id="@+id/speakButton"
android:layout_width="wrap_content"
android:layout_alignParentBottom="true"
android:layout_height="wrap_content"
android:background="@drawable/mic" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:text="Tap mic and speak your color"
android:textSize="20sp" />
Step 4: Then right click on your project package create a new android activity.Step 5: Finally in your MainActivity copy the below code
MainActivity.java :
package com.examp.voice;
import java.util.ArrayList;
import java.util.List;
import android.app.Activity;
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.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
public class VoiceActivity extends Activity {
private static final int REQUEST_CODE = 1234;
ArrayList<String> colorlist;
Button speakButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_voice);
speakButton = (Button) findViewById(R.id.speakButton);
// Disable button if no recognition service is present
PackageManager pm = getPackageManager();
List<ResolveInfo> activities = pm.queryIntentActivities(new Intent(
RecognizerIntent.ACTION_RECOGNIZE_SPEECH), 0);
if (activities.size() == 0) {
speakButton.setEnabled(false);
Toast.makeText(getApplicationContext(), "No voice recognition service", Toast.LENGTH_LONG).show();
}
speakButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
startVoiceRecognitionActivity();
}
});
}
private void startVoiceRecognitionActivity() {
Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
intent.putExtra(RecognizerIntent.EXTRA_PROMPT, "Speak your colour");
startActivityForResult(intent, REQUEST_CODE);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
try{
if (requestCode == REQUEST_CODE && resultCode == RESULT_OK) {
colorlist = data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
}
super.onActivityResult(requestCode, resultCode, data);
for(int i=0;i<5;i++)
{
if(colorlist.get(i).equalsIgnoreCase("red"))
{
getWindow().setBackgroundDrawableResource(R.drawable.red);
}
else if(colorlist.get(i).startsWith("green"))
{
getWindow().setBackgroundDrawableResource(R.color.green);
}
else if(colorlist.get(i).contains("blue"))
{
getWindow().setBackgroundDrawableResource(R.drawable.blue);
}
else if(colorlist.get(i).equalsIgnoreCase("black"))
{
getWindow().setBackgroundDrawableResource(R.color.black);
}
else if(colorlist.get(i).equalsIgnoreCase("yellow"))
{
getWindow().setBackgroundDrawableResource(R.drawable.yellow);
}
else if(colorlist.get(i).equalsIgnoreCase("next")||colorlist.get(i).equalsIgnoreCase("start"))
{
Intent in=new Intent(VoiceActivity.this,SecActivity.class);
startActivity(in);
}
else if(colorlist.get(i).contains("stop")||colorlist.get(i).contains("finish"))
{
finish();
}
else
{
}}
}
catch(Exception e){
Toast.makeText(getApplicationContext(), "Color not recognized", Toast.LENGTH_SHORT).show();
e.printStackTrace();
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.voice, menu);
return true;
}
}
No comments:
Post a Comment