In: Computer Science
Extend the Hello Goodbye application from the class to include the following using android studio:
1. Add a Text Color button underneath the existing Exclamation button, using the same text color and background image. When this button is clicked, toggle the display color for the Hello or Goodbye text between the original color and the color Red.
2. Add a Reset button underneath the new text color button, using the same text color and background image. When this button is clicked, return the text display to “Hello” in the original color.
activity main XML
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity" android:background="@drawable/background"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:textAppearance="?android:attr/textAppearanceLarge" android:text="@string/hello" android:id="@+id/textView" android:layout_alignParentTop="true" android:layout_centerHorizontal="true" android:textColor="@color/dusty_rose" android:textSize="60sp" /> <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/imageView" android:layout_centerVertical="true" android:layout_centerHorizontal="true" android:src="@drawable/greetimage" android:contentDescription="@string/exclaim_img" /> <Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/exclaim_btn" android:layout_below="@+id/imageView" android:layout_centerHorizontal="true" android:layout_marginTop="32dp" android:background="@drawable/exclamationbtn" /> <Button android:id="@+id/button2" android:layout_width="100dp" android:layout_height="wrap_content" android:layout_below="@+id/imageView" android:layout_centerHorizontal="true" android:layout_marginTop="102dp" android:background="@drawable/exclamationbtn" android:text="text color" /> </RelativeLayout>
Main activity.java
package com.cornez.hellogoodbye; import android.app.Activity; import android.os.Bundle; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.widget.Button; import android.widget.TextView; public class MainActivity extends Activity { //DECLARE OBJECTS TO INTERFACE WITH LAYOUT COMPONENTS Button exclaimBtn; Button textcolBtn; private TextView greetingTextView; //INDICATES HELLO IS CURRENTLY DISPLAYED private boolean isHello; @Override protected void onCreate(Bundle savedInstanceState) { //TASK 1: INFLATE THE MAIN SCREEN LAYOUT USED BY THE APP super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //TASK 2: ESTABLISH REFERENCES TO THE TEXTVIEW AND BUTTON Button exclaimBtn = (Button) findViewById(R.id.button1); Button textcolBtn = (Button) findViewById(R.id.button2); greetingTextView = (TextView) findViewById(R.id.textView); //TASK 3: INITIALIZE GREETINGS initializeGreeting(); //TASK 4: REGISTER THE LISTENER EVENT FOR THE BUTTON exclaimBtn.setOnClickListener(toggleGreeting); } private final View.OnClickListener toggleGreeting = new View.OnClickListener() { public void onClick(View btn) { //TASK: CONSTRUCT THE TOGGLE GREETING if (isHello) { isHello = false; greetingTextView.setText(R.string.goodbye); } else { isHello = true; greetingTextView.setText(R.string.hello); } } }; int conditionColor; private void initializeGreeting() { isHello = true; } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; // this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.menu_main, menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { // Handle action bar item clicks here. The action bar will // automatically handle clicks on the Home/Up button, so long // as you specify a parent activity in AndroidManifest.xml. int id = item.getItemId(); //noinspection SimplifiableIfStatement if (id == R.id.action_settings) { return true; } return super.onOptionsItemSelected(item); } }