0

Hello I am a beginner for android development , The requirement in my app is when user clicks on EditText the hint Text should also should be converted from Text to Speech using TTS . But i am not getting how to do it,I have tried in EditText's Onclicklistner its not working .

I have taken basic code from google.

XML CODE

 <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout 
        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:gravity="center"
        android:orientation="vertical"
        android:paddingLeft="20dp"
        android:paddingRight="20dp"
        tools:context=".MainActivity">
    <TextView
        android:id="@+id/textView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="50dp"
        android:gravity="center_horizontal"
        android:text="Android Text to Speech (TTS) Demo"
        android:textAppearance="@style/TextAppearance.AppCompat.Headline"/>
    <EditText
        android:id="@+id/et"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/edit_text_style"
        android:hint="Enter the text here"
        android:paddingBottom="10dp"
        android:paddingLeft="10dp"
        android:paddingRight="10dp"
        android:paddingTop="10dp" />
    <Button
        android:id="@+id/btn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginVertical="10dp"
        android:elevation="0dp"
        android:paddingHorizontal="30dp"
        android:text="Click to convert text to speech" />
</LinearLayout>

JAVA CODE

public class MainActivity extends AppCompatActivity
{
    private TextToSpeech textToSpeech;
    private Button btn;
    private EditText editText;
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        btn = (Button) findViewById(R.id.btn);
        editText = (EditText) findViewById(R.id.et);
        textToSpeech = new TextToSpeech(getApplicationContext(), new  
        TextToSpeech.OnInitListener()
        {
            @Override
            public void onInit(int status)
            {
                if (status == TextToSpeech.SUCCESS)
                {
                    int ttsLang = textToSpeech.setLanguage(Locale.UK);
                    if (ttsLang == TextToSpeech.LANG_MISSING_DATA || ttsLang == 
                    TextToSpeech.LANG_NOT_SUPPORTED)
                    {
                        Log.e("TTS", "The Language is not supported!");
                    }
                    else
                    {
                        Log.i("TTS", "Language Supported.");
                    }
                    Log.i("TTS", "Initialization success.");
                }
                else
                {
                    Toast.makeText(getApplicationContext(), "TTS Initialization 
                    failed!", Toast.LENGTH_SHORT).show();
                }
            }
        });
        editText.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View arg0)
            {
                String data = editText.getText().toString();
                int speechStatus = textToSpeech.speak(data, 
                TextToSpeech.QUEUE_FLUSH,null);
                if (speechStatus == TextToSpeech.ERROR)
                {
                    Log.e("TTS", "Error in converting Text to Speech!");
                }
            }
        });

//        btn.setOnClickListener(new View.OnClickListener()
//        {
//            @Override
//            public void onClick(View arg0)
//            {
//                String data = editText.getText().toString();
//               // Log.i("TTS", "button clicked: " + data);
//                int speechStatus = textToSpeech.speak(data, TextToSpeech.QUEUE_FLUSH, null);
//                if (speechStatus == TextToSpeech.ERROR)
//                {
//                    Log.e("TTS", "Error in converting Text to Speech!");
//                }
//            }
//        });
    }
    @Override
    public void onDestroy()
    {
        super.onDestroy();
        if (textToSpeech != null)
        {
            textToSpeech.stop();
            textToSpeech.shutdown();
        }
    }
}
Teja
  • 15
  • 2
  • 1
    have you tried `OnFocusChangeListener`? – ADM Sep 13 '20 at 11:49
  • No i didnt tried OnFocusChangeListener , i have written in editText.setOnClickListener(new View.OnClickListener(){ @Override public void onClick(View arg0) { String data = editText.getText().toString(); int speechStatus = textToSpeech.speak(data, TextToSpeech.QUEUE_FLUSH,null); if (speechStatus == TextToSpeech.ERROR) { Log.e("TTS", "Error in converting Text to Speech!"); } } }); – Teja Sep 13 '20 at 11:57
  • If this is what you tried, can you please add that code to your Question with an edit, please? – Scratte Sep 13 '20 at 20:58
  • I have edited my question with full code once plss check and tell me. – Teja Sep 14 '20 at 03:46
  • 1
    Did it work when you click the button (you have that part commented out)? Your real problem is simply how to detect when the user clicks the edittext, which you could test with a log statement. It's has nothing to do with tts. https://stackoverflow.com/questions/17341946/how-can-i-detect-focused-edittext-in-android/34430940 – Nerdy Bunz Sep 15 '20 at 02:00
  • Thank you Nerdy Bunz my question is solved and i have got additional information about on focus listner from your prefered link . Its actually simple in my code i have replaced getText() to getHint() then its working. – Teja Sep 16 '20 at 05:14

0 Answers0