I work on an application where a user can flip through pages (I use Fragment) and press buttons to start voice recognition, while voice recognition should recognize and compare words. The problem is that I can not figure out how to use Fragment to scroll through and recognize words together.
If I extend from FragmentActivity, it emphasizes onCreateView. If I extend from Fragment (as in the code below), it emphasizes onActivityResult.
Can anybody help me identify the method correctly? I would be extremely grateful :)
public class Fragment_1 extends Fragment{ @Nullable @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View v = inflater.inflate(R.layout.frag_1_fragment_1, container, false); ImageButton button1 = (ImageButton)v.findViewById(R.id.voiceRecognition1); button1.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH); intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM); intent.putExtra(RecognizerIntent.EXTRA_PROMPT, "You may speak!"); intent.putExtra(RecognizerIntent.EXTRA_MAX_RESULTS, 1); intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE, "en-US"); startActivityForResult(intent, 1); } }); final TextView frag1 = (TextView) v.findViewById(R.id.frag_1_frag_1); final TextView frag1ru = (TextView) v.findViewById(R.id.frag_1_frag_1_ru); frag1ru.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { if (frag1.getVisibility() == View.INVISIBLE) { frag1.setVisibility(View.VISIBLE); } else { frag1.setVisibility(View.INVISIBLE); } } }); return v; } @Override public void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); if (requestCode == 1 && resultCode == RESULT_OK) {//Выделяет красным RESULT_OK ArrayList<String> results; results = data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS); TextView speechText = (TextView)findViewById(R.id.textView1);//Выделяет красным findViewById String str=""; for(int i=0;i<results.size();i++){ str+= results.get(i); } if (str.equals("five")) { speechText.setText(str); }else{ speechText.setText("It's not: " + str); } } } }