Task 13
Task 13
• CartFragment.java
• package com.example.myapplication;
import android.app.DownloadManager;
import android.content.Intent;
import android.os.Bundle;
import androidx.activity.result.contract.ActivityResultContracts;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import android.speech.RecognitionService;
import android.speech.RecognizerIntent;
import android.speech.tts.TextToSpeech;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.Locale;
TextToSpeech textToSpeech;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup
container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view= inflater.inflate(R.layout.fragment_cart,
container, false);
ivMic = view.findViewById(R.id.IVMic);
tvText = view.findViewById(R.id.TVmictext);
ivMic.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new
Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE,
Locale.getDefault());
intent.putExtra(RecognizerIntent.EXTRA_PROMPT,"Speak Now");
try {
startActivityForResult(intent,REQUEST_CODE_SPEECH_INPUT);
}catch (Exception e)
{
Toast.makeText(getActivity(),"
"+e.getMessage(),Toast.LENGTH_SHORT).show();
}
}
});
return view;
}
@Override
public void onActivityResult(int requestCode, int resultCode,
@Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(requestCode == REQUEST_CODE_SPEECH_INPUT)
{
if(resultCode == -1 && data!=null)
{
ArrayList<String> result =
data.getStringArrayListExtra(
RecognizerIntent.EXTRA_RESULTS);
tvText.setText(result.get(0));
textToSpeech = new TextToSpeech(getActivity(),
new TextToSpeech.OnInitListener() {
@Override
public void onInit(int i) {
textToSpeech.setLanguage(Locale.KOREAN);
textToSpeech.speak(result.get(0),
textToSpeech.QUEUE_FLUSH,
null,
null);
}
});
}
}
}
}
• Fragment_cart.xml
• <?xml version="1.0" encoding="utf-8"?>
<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"
tools:context=".CartFragment"
>
<EditText
android:layout_width="300dp"
android:layout_height="40dp"
android:id="@+id/TVmictext"
android:text="Tab Mic to speak"
android:textColor="@color/black"
android:textStyle="bold"
android:textSize="18dp"
android:layout_marginTop="20dp"
android:background="@drawable/etshape"
android:inputType="text"
android:paddingLeft="16dp"
/>
<ImageView
android:id="@+id/IVMic"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_marginTop="20dp"
android:src="@drawable/mic_24"
android:tint="@color/LightPink"
tools:ignore="UseAppTint" />
/>
</LinearLayout>
</LinearLayout>
• Output