Laboratorul 07.

Fragments


A fragment is an independent Android component which can be used by an activity. A fragment encapsulates functionality so that it is easier to reuse within activities and layouts.
A fragment runs in the context of an activity, but has its own life cycle and typically its own user interface. It is also possible to define fragments without an user interface, i.e., headless fragments.
One difference when creating a Fragment is that you must use the onCreateView() callback to define the layout. In fact, this is the only callback you need in order to get a fragment running. For example, here's a simple fragment that specifies its own layout:

public class ArticleFragment extends Fragment {
  @Override
  public View onCreateView(LayoutInflater inflater, ViewGroup container,
      Bundle savedInstanceState) {
      // Inflate the layout for this fragment
      return inflater.inflate(R.layout.article_view, container, false);
  }
}


Defining fragments
To define a new fragment you either extend the android.app.Fragment class or one of its subclasses, similar to the way you would with an Activity class. Subclasses are for example, ListFragment, DialogFragment, PreferenceFragment or WebViewFragment.


Fragment life-cycle
A fragment has its own life cycle. But it is always connected to the life cycle of the activity which uses the fragment.


If an activity stops, its fragments are also stopped. If an activity is destroyed, its fragments are also destroyed.
Method + Description :

  1. onAttach() : Called when the fragment has been associated with the activity (the Activity is passed in here)
  2. onCreate() : Fragment is created. The onCreate() method is called after the onCreate() method of the activity but before the onCreateView() method of the fragment.
  3. onCreateView() : Called to create the view hierarchy associated with the fragment.
  4. onActivityCreated() : Called when the activity's onCreate() method has returned.
  5. onStart() : The onStart() method is called once the fragment gets visible.
  6. onResume() : Fragment becomes active.
  7. onPause() : Fragment is visible but becomes not active anymore, e.g., if another activity is animating on top of the activity which contains the fragment.
  8. onStop() : Fragment becomes not visible.
  9. onDestroyView() : Destroys the view of the fragment. If the fragment is recreated from the backstack this method is called and afterwards the onCreateView method.Called when the view hierarchy associated with the fragment is being removed.
  10. onDestroy() : Not guaranteed to be called by the Android platform.
  11. onDetach() : Called when the fragment is being disassociated from the activity.


Add a Fragment to an Activity using XML
Here is an example layout file that adds two fragments to an activity when the device screen is considered “large” (specified by the large qualifier in the directory name).
res/layout-large/news_articles.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:orientation="horizontal"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent">

  <fragment android:name="com.example.android.fragments.HeadlinesFragment"
            android:id="@+id/headlines_fragment"
            android:layout_weight="1"
            android:layout_width="0dp"
            android:layout_height="match_parent" />

  <fragment android:name="com.example.android.fragments.ArticleFragment"
            android:id="@+id/article_fragment"
            android:layout_weight="2"
            android:layout_width="0dp"
            android:layout_height="match_parent" />

</LinearLayout>

Then apply the layout to your activity:

public class MainActivity extends FragmentActivity {
  @Override
  public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.news_articles);
  }
}

Voice Capabilities

(How to make speech to text converter)

User voice will be converted into text and shown on Android Screen. As soon as a user say something, Android will recognize his/her voice and convert it into text. It will do it through RecognizerIntent. Here you don’t need any internet connection. It will work in Offline mode.

In the next java code, we are triggering an Intent named RecognizerIntent which asks for speech input and then sends it through speech recognizer. It does it through ACTION_RECOGNIZE_SPEECH. The results will be returned via activity results in onActivityResult(int, int, Intent). If request code is REQ_CODE_SPEECH_INPUT then corresponding text is written in output screen.

 Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
 intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
          RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
 intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE, "en-En");
 intent.putExtra(RecognizerIntent.EXTRA_PROMPT, "Hi speak something");
 // Start the activity, the intent will be populated with the speech text
  startActivityForResult(intent, REQ_CODE_SPEECH_INPUT);
 private final int REQ_CODE_SPEECH_INPUT = 100;
 // This callback is invoked when the Speech Recognizer returns.
 // This is where you process the intent and extract the speech text from the intent.
 @Override
 protected void onActivityResult(int requestCode, int resultCode,
      Intent data) {
     super.onActivityResult(requestCode, resultCode, data);
     if (requestCode == REQ_CODE_SPEECH_INPUT && resultCode == RESULT_OK) {
        List<String> results = data.getStringArrayListExtra(
              RecognizerIntent.EXTRA_RESULTS);
         String spokenText = results.get(0);
         // Do something with spokenText
  }
 }


https://developer.android.com/training/wearables/apps/voice.html

Text-To-Speech in Android


Text-To-Speech (TTS)- Also known as “speech synthesis”, TTS enables your Android device to “speak” text of different languages.
The TTS engine that ships with the Android platform supports a number of languages: English, French, German, Italian and Spanish. Also, depending on which side of the Atlantic you are on, American and British accents for English are both supported.
The TTS engine needs to know which language to speak, as a word like “Paris”, for example, is pronounced differently in French and English.

In order to use this class, you need to instantiate an object of this class and also specify the initListener. Its syntax is given below −

   TextToSpeech ttobj=new TextToSpeech(getApplicationContext(), new TextToSpeech.OnInitListener() {
      @Override
      public void onInit(int status) {
      }
   });

In this listener, you have to specify the properties for TextToSpeech object , such as its language ,pitch e.t.c. Language can be set by calling setLanguage() method. Its syntax is given below −

ttobj.setLanguage(Locale.UK);


Once you have set the language, you can call speak method of the class to speak the text. Its syntax is given below −

ttobj.speak(toSpeak, TextToSpeech.QUEUE_FLUSH, null);


https://android-developers.googleblog.com/2009/09/introduction-to-text-to-speech-in.html
https://www.tutorialspoint.com/android/android_text_to_speech.htm

Exercises :

Attention, for this lab you will NOT work in TEAMS!!

Who does not respect the instructions won't receive a grade for the lab.

Follow the following instructions:
- For the students who do NOT have a laptop with Android Studio:

Create a document describing in your OWN words the following information:
Ex 1 What are fragments?
Ex 2 Advantages and disadvantages of using fragments.
Ex 3 Explain in which conditions would you use fragments in an mobile application.
Ex 4 Find 2 tutorials that use fragments, explain them in a few words and attach the links in the document.

Attention!! The document has to contain 1-page minimum (minimum 300 words, single-spaced, font 11pt).
Convert it to PDF and send it via email to sabina.horincar@gmail.com specifying the name and the group.

- For the ones who have a laptop with Android Studio:

Ex 1 Create an app that has 2 buttons:
-“Tap to Open Mic” button, to make a speech to text converter
-“Listen” button, to convert the Text-To-Speech


Ex 2 https://examples.javacodegeeks.com/android/core/app/fragment/android-fragments-example/
Ex 3 Upload the project on Github and write a README file explaining in minimum 100 words the exercises, what you've learned from them, the problems that you had, and don't forget to include a VIDEO of the applications.

Attention!! Only the person who owns the Github page will receive the grade of the lab

dapm/laboratoare/07.txt · Last modified: 2020/03/30 00:38 by sabina.horincar
CC Attribution-Share Alike 3.0 Unported
www.chimeric.de Valid CSS Driven by DokuWiki do yourself a favour and use a real browser - get firefox!! Recent changes RSS feed Valid XHTML 1.0