Question

In: Computer Science

Using Android Studio, create a one java android app that has 4 buttons:- Change Color Button...

Using Android Studio, create a one java android app that has 4 buttons:-

Change Color Button - When the Change Color button is clicked, it changes the current activity background to a randomly selected color

Speak Button - When the Speak button is clicked, it opens a new activity named SpeakActivity. On the SpeakActivity there are three controls: EditText, Button (Speak) and Button (Back). The Speak button uses the Text to Speech service to say the text entered in EditText. The Back button returns the user to the main/parent activity.

API Version Button - When the API Version button is clicked, display the Android API Version of the phone on a Toast in the current activity.

Serial Number Button - When the Serial Number button is clicked, send the serial number of your device to an email app as an implicit intent.

Layout Best practices & layout
  • all your screens look professional
  • all variables have meaningful names
  • all static strings are placed in the strings.xml file

Please show screenshots of your .xml and .java and the validated program.

Solutions

Expert Solution

/*this android code is developed in android API 26*/

//Menifest.xm

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.simpleactivity">

    <uses-permission android:name="android.permission.READ_PHONE_STATE" />
    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".SpeakActivity"></activity>
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

//Colors.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <color name="colorPrimary">#008577</color>
    <color name="colorPrimaryDark">#00574B</color>
    <color name="colorAccent">#D81B60</color>
    <item name="blue" type="color">#FF33B5E5</item>
    <item name="purple" type="color">#FFAA66CC</item>
    <item name="green" type="color">#FF99CC00</item>
    <item name="orange" type="color">#FFFFBB33</item>
    <item name="red" type="color">#FFFF4444</item>
    <item name="darkblue" type="color">#FF0099CC</item>
    <item name="darkpurple" type="color">#FF9933CC</item>
    <item name="darkgreen" type="color">#FF669900</item>
    <item name="darkorange" type="color">#FFFF8800</item>
    <item name="darkred" type="color">#FFCC0000</item>

    <integer-array name="androidcolors">
        <item>@color/blue</item>
        <item>@color/purple</item>
        <item>@color/green</item>
        <item>@color/orange</item>
        <item>@color/red</item>
        <item>@color/darkblue</item>
        <item>@color/darkpurple</item>
        <item>@color/darkgreen</item>
        <item>@color/darkorange</item>
        <item>@color/darkred</item>
    </integer-array>
</resources>

//============================= strings.xml

<resources>
    <string name="app_name">SimpleActivity</string>
    <string name="str_color">Change Color</string>
    <string name="str_speak">Speak</string>
    <string name="str_API">API Version</string>
    <string name="str_number">Serial Number</string>
    <string name="str_back">Back</string>
</resources>

//activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:id ="@+id/layout"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <Button
        android:id="@+id/button4"
        android:onClick="btnSerialNumber"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/str_number"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.832"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.437" />

    <Button
        android:id="@+id/button3"
        android:onClick="btnAPI_VERSION"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/str_API"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.266"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.437" />

    <Button
        android:id="@+id/button"

        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="btnSpeakClick"
        android:text="@string/str_speak"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.266"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.247" />

    <Button
        android:id="@+id/button2"
        android:onClick="btnChangeColor"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/str_color"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.832"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.247" />
</androidx.constraintlayout.widget.ConstraintLayout>

//========================= MainActivity.java

package com.example.simpleactivity;

import androidx.appcompat.app.AppCompatActivity;
import androidx.constraintlayout.widget.ConstraintLayout;
import androidx.core.app.ActivityCompat;

import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.graphics.Color;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;

import java.util.Random;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    public void btnChangeColor(View view) {
        int[] androidColors = getResources().getIntArray(R.array.androidcolors);
        int randomAndroidColor = androidColors[new Random().nextInt(androidColors.length)];
        ConstraintLayout constraintLayout = findViewById(R.id.layout);
        constraintLayout.setBackgroundColor(randomAndroidColor);
    }

    public void btnSpeakClick(View view) {
        Intent intent = new Intent(this, SpeakActivity.class);
        startActivity(intent);
    }

    public void btnAPI_VERSION(View view) {
        String api = Build.VERSION.RELEASE;
        Toast.makeText(this, api, Toast.LENGTH_LONG).show();
    }

    public void btnSerialNumber(View view) {
        //Require permissions for API >=23
        if (checkSelfPermission(Manifest.permission.READ_PHONE_STATE) != PackageManager.PERMISSION_GRANTED) {
            // TODO: Consider calling
            //    Activity#requestPermissions
            // here to request the missing permissions, and then overriding
            //   public void onRequestPermissionsResult(int requestCode, String[] permissions,
            //                                          int[] grantResults)
            // to handle the case where the user grants the permission. See the documentation
            // for Activity#requestPermissions for more details.
            requestPermissions(new String[]{Manifest.permission.READ_PHONE_STATE},PackageManager.PERMISSION_GRANTED);
            return;
        }

       String serial_number = Build.getSerial().toString();
        Toast.makeText(this,serial_number,Toast.LENGTH_LONG).show();
        Intent emailIntent = new Intent(Intent.ACTION_SEND);
        emailIntent.setData(Uri.parse("mailto:"));
        emailIntent.setType("text/plain");
        emailIntent.putExtra(emailIntent.EXTRA_TEXT, serial_number);
        try {
            startActivity(Intent.createChooser(emailIntent, "Email"));
            finish();

        } catch (android.content.ActivityNotFoundException ex) {
            Toast.makeText(MainActivity.this, "ERROR ....NO EMAIL CLIENT FOUND.", Toast.LENGTH_SHORT).show();
        }
    }
}

//=========================activity_speak.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".SpeakActivity">

    <EditText
        android:id="@+id/editText"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:ems="10"
        android:inputType="text"
        android:text=""
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.434"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.173" />

    <Button
        android:id="@+id/button5"
        android:onClick="btnNowSpeak"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/str_speak"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.198"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.499" />

    <Button
        android:id="@+id/button6"
        android:onClick="btnBack"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/str_back"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.808"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.499" />
</androidx.constraintlayout.widget.ConstraintLayout>

//==================================SpeakActivity.java

package com.example.simpleactivity;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.speech.tts.TextToSpeech;
import android.speech.tts.TextToSpeechService;
import android.view.View;
import android.widget.EditText;

import java.util.Locale;

public class SpeakActivity extends AppCompatActivity {
    TextToSpeech textToSpeech;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_speak);
         textToSpeech=new TextToSpeech(getApplicationContext(), new TextToSpeech.OnInitListener() {
            @Override
            public void onInit(int status) {
                if(status != TextToSpeech.ERROR) {
                    textToSpeech.setLanguage(Locale.US);
                }
            }
        });
    }

    public void btnNowSpeak(View view)
    {
        EditText ed = findViewById(R.id.editText);
        String sentence = ed.getText().toString();
        textToSpeech.speak(sentence,TextToSpeech.QUEUE_FLUSH,null,UI_MODE_SERVICE);
    }
    public void btnBack(View view)
    {
        Intent intent = new Intent(this,MainActivity.class);
        startActivity(intent);
    }
    public void onPause() {
        if (textToSpeech != null) {
            textToSpeech.stop();
            textToSpeech.shutdown();
        }
        super.onPause();
    }
}

//Outputs

//If you need any help regarding this solution ........... please leave a comment .......... thanks


Related Solutions

Create a Scorekeeper app in android studio by using java language - Layouts | Widgets Create...
Create a Scorekeeper app in android studio by using java language - Layouts | Widgets Create the layout for your score keeping app. The app should have: Two team names (TextViews) Two scores (TextViews) Buttons to increase/ decrease the scores An amount to change the score by (RadioButtons) You must have at least two score options The scores can be changed by anything you want American football: 1, 2, 3, 6 Basketball: 1, 2, 3 Freestyle wrestling: 1, 2, 3,...
android studio Begin a new app and create a Java class for products with data: productName,...
android studio Begin a new app and create a Java class for products with data: productName, productCode and price and use a file of objects to store product data and read back and display on screen. Do this by creating a simple form using EditTexts, buttons or ActionBar items and display output using an Alert.
On Android Studio, create a 4 function calculator. The only buttons you need are 0-9, *,...
On Android Studio, create a 4 function calculator. The only buttons you need are 0-9, *, /, +, -, a clear, and enter button. Use StringTokenizer or String.split for the calculator code.
On Android Studio, create a 4 function calculator. The only buttons you need are 0-9, *,...
On Android Studio, create a 4 function calculator. The only buttons you need are 0-9, *, /, +, -, a clear, and enter button. Having this java code below, I need the XML code that organizes the layout of the calculator (using either linearLayout or relativeLayout). As shown in the JAVA Code - Button button0, button1, button2, button3, button4, button5, button6, button7, button8, button9, buttonadd, buttonsubtract, buttonmultiply, buttondivide, buttonequals, buttonclear and TextView display - is what is needed in the...
For this IP, you will create a very simple drawing app using Android Studio. The purpose...
For this IP, you will create a very simple drawing app using Android Studio. The purpose of this assignment is to give you more building blocks to use when programming apps. For full credit for this assignment, you should complete the following: Create a menu and display menu items on the app bar Detect when the user touches the screen and moves a finger Be able to change the color and width of a line Be able to save an...
I am doing a 4 function calculator app in java on android studio which calculates math...
I am doing a 4 function calculator app in java on android studio which calculates math expressions (add, subtract, multiply, and divide). I ONLY NEED THE CODE THE EQUAL BUTTON. When the user clicks the equal button, it should take the entire sequence of numbers and operators on the screen, such as 1+2*5 , and save them into a String [I am pretty sure this can be done using the toString() call]. Then, the String should be split using StringTokenizer,...
Hi! I need it in android studio and in java Design a game app “BouncingBall ”...
Hi! I need it in android studio and in java Design a game app “BouncingBall ” in which the user’s goal is to prevent a bouncing ball from falling off the bottom of the screen. When the user presses the start button, a ball bounces off the top, left and right sides (the “walls”) of the screen. A horizontal bar on the bottom of the screen serves as a paddle to prevent the ball from hitting the bottom of the...
Hi! I need it in android studio and in java Design a game app “BouncingBall ”...
Hi! I need it in android studio and in java Design a game app “BouncingBall ” in which the user’s goal is to prevent a bouncing ball from falling off the bottom of the screen. When the user presses the start button, a ball bounces off the top, left and right sides (the “walls”) of the screen. A horizontal bar on the bottom of the screen serves as a paddle to prevent the ball from hitting the bottom of the...
use android studio or MIT inventor Create a Presidents Quiz App for your project with the...
use android studio or MIT inventor Create a Presidents Quiz App for your project with the following requirements: You must have images Buttons Label Conditional Statement Homepage with instructions Sound (music, buzzer, etc.) - Ensure you are using the correct layout: Gravity(Android Studio), Horizontal or Vertical Arrangement (MIT). - 40POINTS Points System (indicating how many answers they got wrong/correct). 20POINT 1-2 questions per President (45+ questions)
Android Studio. Java. Please create an application that -> An activity that allows user to enter...
Android Studio. Java. Please create an application that -> An activity that allows user to enter name, gender, date of birth, state of residence (selected from a pre-defined list: CA, AZ, NV, OR), email address and favorite website. This activity has a button "Show Data" that displays detail entered
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT