In: Computer Science
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
|
Please show screenshots of your .xml and .java and the validated program.
/*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