package
com.cs.voicetyper;
import
androidx.activity.result.ActivityResult;
import
androidx.activity.result.ActivityResultCallback;
import
androidx.activity.result.ActivityResultLauncher;
import
androidx.activity.result.contract.ActivityResultContracts;
import
androidx.appcompat.app.AppCompatActivity;
import
android.app.Activity;
import
android.content.ClipData;
import
android.content.ClipboardManager;
import
android.content.Intent;
import
android.os.Bundle;
import
android.speech.RecognizerIntent;
import
android.view.View;
import
android.widget.AdapterView;
import
android.widget.ArrayAdapter;
import
android.widget.EditText;
import
android.widget.ImageView;
import
android.widget.Spinner;
import
android.widget.Toast;
import
java.util.ArrayList;
public
class
MainActivity
extends
AppCompatActivity
implements
AdapterView.OnItemSelectedListener{
EditText etText;
ImageView ivMic,ivCopy;
Spinner spLangs;
String lcode =
"en-US"
;
String[] languages = {
"English"
,
"Tamil"
,
"Hindi"
,
"Spanish"
,
"French"
,
"Arabic"
,
"Chinese"
,
"Japanese"
,
"German"
};
String[] lCodes = {
"en-US"
,
"ta-IN"
,
"hi-IN"
,
"es-CL"
,
"fr-FR"
,
"ar-SA"
,
"zh-TW"
,
"jp-JP"
,
"de-DE"
};
@Override
protected
void
onCreate(Bundle savedInstanceState) {
super
.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
etText = findViewById(R.id.etSpeech);
ivMic = findViewById(R.id.ivSpeak);
ivCopy = findViewById(R.id.ivCopy);
spLangs = findViewById(R.id.spLang);
spLangs.setOnItemSelectedListener(
this
);
ArrayAdapter adapter =
new
ArrayAdapter(
this
, R.layout.support_simple_spinner_dropdown_item,languages);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spLangs.setAdapter(adapter);
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,lcode);
intent.putExtra(RecognizerIntent.EXTRA_PROMPT,
"Speak now!"
);
activityResultLauncher.launch(intent);
}
});
ivCopy.setOnClickListener(
new
View.OnClickListener() {
@Override
public
void
onClick(View view) {
ClipboardManager clipboardManager = (ClipboardManager) getSystemService(CLIPBOARD_SERVICE);
clipboardManager.setPrimaryClip(ClipData.newPlainText(
"label"
,etText.getText().toString().trim()));
Toast.makeText(MainActivity.
this
,
"Copied!"
, Toast.LENGTH_SHORT).show();
}
});
}
ActivityResultLauncher<Intent> activityResultLauncher = registerForActivityResult(
new
ActivityResultContracts.StartActivityForResult(),
new
ActivityResultCallback<ActivityResult>() {
@Override
public
void
onActivityResult(ActivityResult result) {
if
(result.getResultCode() == Activity.RESULT_OK && result.getData()!=
null
) {
ArrayList<String> d=result.getData().getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
etText.setText(etText.getText()+
" "
+d.get(
0
));
}
}
});
@Override
public
void
onItemSelected(AdapterView<?> adapterView, View view,
int
i,
long
l) {
lcode = lCodes[i];
}
@Override
public
void
onNothingSelected(AdapterView<?> adapterView) {
}
}