This shows you the differences between two versions of the page.
dapm:laboratoare:07 [2018/04/13 13:16] ioana_maria.culic |
dapm:laboratoare:07 [2020/03/30 00:38] (current) sabina.horincar [Who does not respect the instructions won't receive a grade for the lab.] |
||
---|---|---|---|
Line 1: | Line 1: | ||
===== Laboratorul 07. ===== | ===== Laboratorul 07. ===== | ||
- | **Voice Capabilities (How to make speech to text converter)**\\ | + | ===== 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.\\ | ||
+ | |||
+ | {{:dapm:laboratoare:lab7_fragmente.png?800|}} \\ | ||
+ | |||
+ | If an activity stops, its fragments are also stopped. If an activity is destroyed, its fragments are also destroyed.\\ | ||
+ | __Method + Description__ : | ||
+ | - onAttach() : Called when the fragment has been associated with the activity (the Activity is passed in here) | ||
+ | - onCreate() : Fragment is created. The onCreate() method is called after the onCreate() method of the activity but before the onCreateView() method of the fragment. | ||
+ | - onCreateView() : Called to create the view hierarchy associated with the fragment. | ||
+ | - onActivityCreated() : Called when the activity's onCreate() method has returned. | ||
+ | - onStart() : The onStart() method is called once the fragment gets visible. | ||
+ | - onResume() : Fragment becomes active. | ||
+ | - 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. | ||
+ | - onStop() : Fragment becomes not visible. | ||
+ | - 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. | ||
+ | - onDestroy() : Not guaranteed to be called by the Android platform. | ||
+ | - 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.\\ | 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.\\ | ||
Line 15: | Line 87: | ||
startActivityForResult(intent, REQ_CODE_SPEECH_INPUT); | startActivityForResult(intent, REQ_CODE_SPEECH_INPUT); | ||
+ | private final int REQ_CODE_SPEECH_INPUT = 100; | ||
// This callback is invoked when the Speech Recognizer returns. | // This callback is invoked when the Speech Recognizer returns. | ||
// This is where you process the intent and extract the speech text from the intent. | // This is where you process the intent and extract the speech text from the intent. | ||
Line 33: | Line 106: | ||
\\ | \\ | ||
- | **Text-To-Speech in Android**\\ | + | =====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.\\ | Text-To-Speech (TTS)- Also known as "speech synthesis", TTS enables your Android device to "speak" text of different languages.\\ | ||
Line 58: | Line 131: | ||
====== Exercises : ====== | ====== 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:\\ | **Ex 1 ** Create an app that has 2 buttons:\\ | ||
- | -"Tap to Open Mic" button, to make speech to text converter\\ | + | -"Tap to Open Mic" button, to make a speech to text converter\\ |
- | -"Listen it" button, to convert the Text-To-Speech\\ | + | -"Listen" button, to convert the Text-To-Speech\\ |
\\ | \\ | ||
{{:dapm:laboratoare:lab7_ex1.jpg?200|}} | {{:dapm:laboratoare:lab7_ex1.jpg?200|}} | ||
{{:dapm:laboratoare:lab7_ex11.jpg?200|}} | {{:dapm:laboratoare:lab7_ex11.jpg?200|}} | ||
+ | \\ | ||
+ | **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** |