Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

1. You will develop a flash card app for math. The main screen looks as follows

ID: 3824821 • Letter: 1

Question

1. You will develop a flash card app for math. The main screen looks as follows with an ImageButton and an EditText field

The user will type in the answer and click the image button. If the answer is incorrect a toast is displayed as follows:

If the answer is correct, an alert dialog is displayed as follows.

If YES is selected a new flash card math problem is displayed and the process repeats.

If NO is selected, the app ends.

There are only four flash cards, so when the fourth card has been displayed, the app starts at the beginning card again. This continues until the user ends the app by selecting NO in the alert dialog box.

Android Emulator Nexus 5 API 23:5554 Test 1 3 nter answer a 9:35

Explanation / Answer

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

    android:layout_width="fill_parent"

    android:layout_height="fill_parent"

    android:orientation="vertical"

    android:background="#ffffff">

    

<TextView android:layout_width="fill_parent"

     android:layout_height="wrap_content"

     android:text="Text To Speech"

     android:padding="15dip"

     android:textColor="#800000"

     android:textSize="26dip"

     android:gravity="center"

     android:textStyle="bold|italic"/>

    <EditText android:id="@+id/txtText"

        android:layout_width="fill_parent"

        android:layout_height="wrap_content"

        android:hint="Enter some text to speak"

        android:layout_marginTop="20dip"        

        android:layout_margin="10dip"/>

    

    <ImageButton

        android:id="@+id/btnSpeak"

        android:layout_width="fill_parent"

        android:layout_height="wrap_content"

        android:layout_marginTop="50dip"

        android:layout_marginBottom="10dip"

        android:src="@drawable/bol"

        android:background="#FFFFFF"

        android:text="Speak Out"

         />

    <TextView

        android:id="@+id/textView1"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:text="CLICK ME"

        android:gravity="center"

        android:layout_marginLeft="148dip"

        android:textStyle="bold|italic"/>

</LinearLayout>

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

package com.example.textspeech;

import java.util.Locale;

import android.os.Bundle;

import android.app.Activity;

import android.speech.tts.TextToSpeech;

import android.util.Log;

import android.view.Menu;

import android.view.View;

import android.widget.EditText;

import android.widget.ImageButton;

public class MainActivity extends Activity implements TextToSpeech.OnInitListener {

/** Called when the activity is first created. */

// Creation of Objects :

private TextToSpeech tts;

private ImageButton btnSpeak;

private EditText txtText;

@Override

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

tts = new TextToSpeech(this, this);

// Finding the Id of the Image Button i.e btnSpeak that is provided in the layout

btnSpeak = (ImageButton) findViewById(R.id.btnSpeak);

// Finding the Id of the EditText that is dragged in the layout

txtText = (EditText) findViewById(R.id.txtText);

// button on click event

btnSpeak.setOnClickListener(new View.OnClickListener() {

@Override

public void onClick(View arg0) {

speakOut();

}

});

}

@Override

public void onDestroy() {

// Don't forget to shutdown!

if (tts != null)

{

tts.stop();

tts.shutdown();

}

super.onDestroy();

}

@Override

public void onInit(int status) {

// TODO Auto-generated method stub

if (status == TextToSpeech.SUCCESS) {

//Setting the language to US...you can change it to different language if you want

int result = tts.setLanguage(Locale.US);

// tts.setPitch(5); // set pitch level you can modify it if required

// tts.setSpeechRate(2); // set speech speed rate if required

if (result == TextToSpeech.LANG_MISSING_DATA

|| result == TextToSpeech.LANG_NOT_SUPPORTED) {

// If the language is not supported display it in the logcat

Log.e("TTS", "Language is not supported");

}

else

{

btnSpeak.setEnabled(true);

speakOut();

}

}

else

{

Log.e("TTS", "Initilization Failed");

}

}

private void speakOut() {

String text = txtText.getText().toString();

tts.speak(text, TextToSpeech.QUEUE_FLUSH, null);

}

}

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

package com.example.textspeech;

import java.util.Locale;

import android.os.Bundle;

import android.app.Activity;

import android.speech.tts.TextToSpeech;

import android.util.Log;

import android.view.Menu;

import android.view.View;

import android.widget.EditText;

import android.widget.ImageButton;

public class MainActivity extends Activity implements TextToSpeech.OnInitListener {

/** Called when the activity is first created. */

// Creation of Objects :

private TextToSpeech tts;

private ImageButton btnSpeak;

private EditText txtText;

@Override

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

tts = new TextToSpeech(this, this);

// Finding the Id of the Image Button i.e btnSpeak that is provided in the layout

btnSpeak = (ImageButton) findViewById(R.id.btnSpeak);

// Finding the Id of the EditText that is dragged in the layout

txtText = (EditText) findViewById(R.id.txtText);

// button on click event

btnSpeak.setOnClickListener(new View.OnClickListener() {

@Override

public void onClick(View arg0) {

speakOut();

}

});

}

@Override

public void onDestroy() {

// Don't forget to shutdown!

if (tts != null)

{

tts.stop();

tts.shutdown();

}

super.onDestroy();

}

@Override

public void onInit(int status) {

// TODO Auto-generated method stub

if (status == TextToSpeech.SUCCESS) {

//Setting the language to US...you can change it to different language if you want

int result = tts.setLanguage(Locale.US);

// tts.setPitch(5); // set pitch level you can modify it if required

// tts.setSpeechRate(2); // set speech speed rate if required

if (result == TextToSpeech.LANG_MISSING_DATA

|| result == TextToSpeech.LANG_NOT_SUPPORTED) {

// If the language is not supported display it in the logcat

Log.e("TTS", "Language is not supported");

}

else

{

btnSpeak.setEnabled(true);

speakOut();

}

}

else

{

Log.e("TTS", "Initilization Failed");

}

}

private void speakOut() {

String text = txtText.getText().toString();

tts.speak(text, TextToSpeech.QUEUE_FLUSH, null);

}

}