-
-
Save udacityandroid/dbc83e6d49dd1164d2b5 to your computer and use it in GitHub Desktop.
<LinearLayout 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:background="#B388FF" | |
android:orientation="vertical" | |
tools:context=".MainActivity"> | |
<ImageView | |
android:id="@+id/android_cookie_image_view" | |
android:layout_width="match_parent" | |
android:layout_height="0dp" | |
android:layout_weight="1" | |
android:scaleType="centerCrop" | |
android:src="@drawable/before_cookie" | |
android:text="@string/hello_world" /> | |
<TextView | |
android:id="@+id/status_text_view" | |
android:layout_width="match_parent" | |
android:layout_height="wrap_content" | |
android:layout_marginLeft="16dp" | |
android:layout_marginRight="16dp" | |
android:layout_marginTop="16dp" | |
android:text="I'm so hungry" | |
android:textColor="@android:color/white" | |
android:textSize="34sp" /> | |
<Button | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:layout_margin="16dp" | |
android:text="Eat cookie" | |
android:onClick="eatCookie" /> | |
</LinearLayout> |
package com.example.android.cookies; | |
import android.os.Bundle; | |
import android.support.v7.app.AppCompatActivity; | |
import android.view.View; | |
import android.widget.ImageView; | |
import android.widget.TextView; | |
public class MainActivity extends AppCompatActivity { | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_main); | |
} | |
/** | |
* Called when the cookie should be eaten. | |
*/ | |
public void eatCookie(View view) { | |
// Find a reference to the ImageView in the layout. Change the image. | |
ImageView imageView = (ImageView) findViewById(R.id.android_cookie_image_view); | |
imageView.setImageResource(R.drawable.after_cookie); | |
// Find a reference to the TextView in the layout. Change the text. | |
TextView textView = (TextView) findViewById(R.id.status_text_view); | |
textView.setText("I'm so full"); | |
} | |
} |
I succeeded !
ok done
I don't know about anyone else, but I had to escape the single quote in "I'm so full". Note, I used the strings.xml resource file to hold the string value. Here's my eatCookie() method
public void eatCookie(View view) {
ImageView changeImage = (ImageView) findViewById(R.id.android_cookie_image_view);
changeImage.setImageResource(R.drawable.after_cookie);
TextView changeText = (TextView) findViewById(R.id.status_text_view);
changeText.setText(R.string.sofull);
}
and the strings.xml file:
< resources >
< string name="app_name" >Cookies< /string >
< string name="sofull" >I \ 'm so full!< /string >
< /resources >
Also, I did not need to add anything to my gradle.build app file. There were errors when I added it, and with many things in this course, that code was obsolete for my version. (Android Studio 3.4.1). So, I skipped that step.
well Done
my code:
activity_main.xml _________________
MainActivity.java _________________