scroll view in relation to tts android

245 Views Asked by At

how can i scroll a view (recyclerview) in relation to my tts, ive looked at onUtterance but it seems to only have a start and stop listener, so i need to think outside the box, i give my tts a string from an Arraylist like this

 ArrayList<String> list = new ArrayList<>();

    for (int i = 0; i < SpeakRecyclerGrid.recyclerView.getChildCount(); i++) 
      {
       list.add(((EditText) SpeakRecyclerGrid.recyclerView.getChildAt(i)).getText().toString());
      }

      speakWords(words);

I was thinking about cutting the string up into sections and giving it to the TTS one string at a time and move the view as I go. I already give my gridlayout manager an int for the amount of columns (called columns). The array list adds a comma after every word, so I was thinking something like

  • find the nth/(column) comma
  • split the string
  • check if tts is speaking and listen for onUtterance onDone to pass new string
  • read the string
  • move the view

and keep doing this until theres no words left and coding for the remainder % im not sure how to do all of that so if anyone wants to help feel free, (I think Im looking at StringUtils and creating each smaller string with a for loop and passing them to the tts in onUtteranceListener onDone, but im still a little new to android), but mainly does anyone have a better way

1

There are 1 best solutions below

1
On

okay this is what i have so far but it could probably use some work im still an amateur, I've split the string into blocks based on the size of the screen, and the handler that scrolls the screen relies on the amount of letters in each broken up string, also the smoothscrolltoposition is scrolling a custom layout manager that scrolls pretty slowly im having an issue though where on some devices the array list counting the words will only reach 20 not sure why but ill ask and hopefully update this when ive fixed it so here is my speak and move method

public void speakAndMove(){

    final ArrayList<String> list = new ArrayList<>();

    SpeakGrid.recyclerView.getLayoutManager().scrollToPosition(0);

    for (int i = 0; i < SpeakRecyclerGrid.recyclerView.getChildCount(); i++) {
        list.add(((EditText) SpeakRecyclerGrid.recyclerView.getChildAt(i)).getText().toString());
    }

    Integer numOfWords = list.size();
    words = list.toString();
    Integer count = 0;
    Integer startPoint = 0;
    scrollPos = 0;
    final Integer speed = words.length() * 15;
    Integer leftOver = 0;
    final int columns = getResources().getInteger(R.integer.grid_columns);
    System.out.println(numOfWords);
    ArrayList<String> arrayList = new ArrayList<>();

    if (list.size() <= columns) {

        if (words.contains("[]")) {
            speakWords("");
        } else if (words.contains(", 's")) {
            formatString = words.replaceFirst(", 's", "'s");
            speakWords(formatString);
        } else if (words.contains(", ing")) {
            formatString = words.replaceFirst(", ing", "ing");
            speakWords(formatString);
        } else {
            speakWords(words);
        }
    }
    if (list.size()>=columns) {

        for (int i = 0; i < words.length(); i++) {

            if (words.charAt(i) == ',') {
                count++;
                if (count == columns) {

                    String ab = words.substring(startPoint, i + 1);
                    //speakWords(ab);
                    if (ab.contains(", 's")) {
                        formatString = ab.replaceFirst(", 's", "'s");
                        speakWords(formatString);
                    } else if (ab.contains(", ing")) {
                        formatString = ab.replaceFirst(", ing", "ing");
                        speakWords(formatString);
                    } else {
                        speakWords(ab);
                    }
                    startPoint = i + 1;
                    count = 0;
                    leftOver = words.length() - startPoint;
                }


                //SpeakGrid.recyclerView.getLayoutManager().scrollToPosition(scrollPos);
                System.out.println("main string"+" scroll " + scrollPos + " startpoint " + startPoint +" speed " + speed);
            }
        }
        if (leftOver > 0) {
            String ab2 = words.substring(startPoint, words.length());
            //speakWords(ab2);
            if (ab2.contains(", 's")) {
                formatString = ab2.replaceFirst(", 's", "'s");
                speakWords(formatString);
            } else if (ab2.contains(", ing")) {
                formatString = ab2.replaceFirst(", ing", "ing");
                speakWords(formatString);
            } else {
                speakWords(ab2);
            }
            //SpeakGrid.recyclerView.getLayoutManager().scrollToPosition(scrollPos);
            System.out.println("leftovers "+ leftOver + " scroll " + scrollPos + " startpoint " + startPoint +" count " + scrollPos);
            count = 0;
            //scrollPos = 0;
        }

    }

    final Handler h = new Handler();
    h.postDelayed(new Runnable() {
        public void run() {
            // This method will be executed once the timer is over
            // Start your app main activity

            scrollPos = scrollPos + columns;
            SpeakGrid.recyclerView.getLayoutManager().smoothScrollToPosition(SpeakGrid.recyclerView, null ,scrollPos);
            System.out.println("position "+ scrollPos + " speed " + speed + " list size " + list.size());
            if (scrollPos < list.size())
                h.postDelayed(this,speed);
            // close this activity
        }
    }, speed);


}