Skip to content

Instantly share code, notes, and snippets.

@udacityandroid
Created June 27, 2015 21:54
Show Gist options
  • Star 30 You must be signed in to star a gist
  • Fork 21 You must be signed in to fork a gist
  • Save udacityandroid/dbc83e6d49dd1164d2b5 to your computer and use it in GitHub Desktop.
Save udacityandroid/dbc83e6d49dd1164d2b5 to your computer and use it in GitHub Desktop.
Android for Beginners : Cookies Solution Code
<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");
}
}
@CodeToLead
Copy link

<android.support.constraint.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=".MainActivity">

<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" />

    <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:id="@+id/status_button_view"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="16dp"
        android:text="EAT COOKIE"
        android:onClick="eatCookie"
        />

    <Button
        android:id="@+id/exit_button_view"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="16dp"
        android:text="Exit App"
        android:onClick="exitApp"
        />


</LinearLayout>

</android.support.constraint.ConstraintLayout>

@CodeToLead
Copy link

MainActivity.java code as follows

package com.example.android.cookies;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;
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) {
    // TODO: Find a reference to the ImageView in the layout. Change the image.

    // TODO: Find a reference to the TextView in the layout. Change the text.
    // Find a reference to the ImageView in the layout. Change the image.
    ImageView imageView = (ImageView) findViewById(R.id.android_cookie_image_view);
    TextView textView = (TextView) findViewById(R.id.status_text_view);
    Button btnView = (Button) findViewById(R.id.status_button_view);
    String buttonText = btnView.getText().toString();
    Log.i(buttonText, buttonText);

    switch (buttonText) {
        case "EAT COOKIE":
            imageView.setImageResource(R.drawable.after_cookie);
            // Find a reference to the TextView in the layout. Change the text.
            textView.setText("I'm so full");
            btnView.setText("Fill It");
            break;
        case "Fill It":
            imageView.setImageResource(R.drawable.before_cookie);
            // Find a reference to the TextView in the layout. Change the text.
            textView.setText("I'm so hungry");
            btnView.setText("EAT COOKIE");
            break;

    }
}
    public void exitApp(View view) {
        Intent homeIntent = new Intent(Intent.ACTION_MAIN);
        homeIntent.addCategory( Intent.CATEGORY_HOME );
        homeIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        startActivity(homeIntent);
    }

}

@CodeToLead
Copy link

image

@meanday
Copy link

meanday commented May 8, 2018

This would not work for me when I edited the gradle script as outlined in the Udacity instruction. Once I left the gradle script alone, I had zero problems.
This is using Android Studio 3.1

@Mohhmd
Copy link

Mohhmd commented May 31, 2018

its working

@muskan2408
Copy link

 setContentView(R.layout.activity_main); app crash on this line pls help me out

@ahmedashouryounes
Copy link

screenshot_2018-06-02_001902 1

<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" />

<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="8dp"
    android:text="I'm so hungry"
    android:textColor="@android:color/white"
    android:textSize="28sp" />

<Button
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_margin="8dp"
    android:background="#FFA726"
    android:onClick="eatCookie"
    android:text="EAT COOKIE"
    android:textSize="18sp" />

<Button
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_margin="8dp"
    android:background="#FFA726"
    android:onClick="agian"
    android:text="Again"
    android:textSize="18sp" />

@ahmedashouryounes
Copy link

screenshot_2018-06-02_001908 1

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) {
    ImageView image = (ImageView) findViewById(R.id.android_cookie_image_view);
    image.setImageResource(R.drawable.after_cookie);
    TextView text = (TextView) findViewById(R.id.status_text_view);
    text.setText("I'm so full");
}

public void agian(View view) {
    ImageView image = (ImageView) findViewById(R.id.android_cookie_image_view);
    image.setImageResource(R.drawable.before_cookie);
    TextView text = (TextView) findViewById(R.id.status_text_view);
    text.setText("I'm so hungry");
}

}

@tooptooptoop
Copy link

tooptooptoop commented Jun 14, 2018

screenshot_2018-06-14-00-47-12 1
screenshot_2018-06-14-00-47-02 1

@asmaadahab
Copy link

untitled
احل الخطا دا ازاى

@ahmedmtaher
Copy link

C:\Users\Ahmed Eltaher\AndroidStudioProjects\cookies\app\src\main\res\values\strings.xml: Error: null is not a valid resource name (reserved Java keyword)

@DaNa167
Copy link

DaNa167 commented Jun 20, 2018

[
untitled

](url)

my application is crashing what is the wrong here??

@DaNa167
Copy link

DaNa167 commented Jun 20, 2018

untitled

@DaNa167
Copy link

DaNa167 commented Jun 20, 2018

untitled

@ahossam88
Copy link

What is the Activity method you call to get the TextView and ImageView objects?

@ahmedwagih1964
Copy link

Done! had to change the code provided for the Gradle - modify the version to the current version, also "compile" is now deprecated.

@varanshu-engineer
Copy link

There is a warning in the code

WARNING: Configuration 'compile' is obsolete and has been replaced with 'implementation' and 'api'.

@rajatnaik12
Copy link

@Varanshu in new format they use implementation rather than compile, which was used before. You can change it to implementation and it should be. in project find the gradle.app and at the end in that file you will find the compile statements and change them to implementation. Do update your Android studio and supporting libraries like gradle and etc.

@rajatnaik12
Copy link

@meanday you are using new tools and was not supposed to not change anything in gardle files. only code in activity_main. xml and . java

@rajatnaik12
Copy link

TextView textView=new TextView(this) ;
i suggest you go through documentation when you find struck somewhere. If documentation didn't helped serch on Google or ask in forums like stackoverflow.com

@ilart1991
Copy link

ilart1991 commented Aug 7, 2018

My code for it:

    public void eatCookie(View view) {
        changePicText();
    }

    private void changePicText() {
        ImageView endOfFood = findViewById(R.id.android_cookie_image_view);
        endOfFood.setImageResource(R.drawable.after_cookie);
        TextView newText = findViewById(R.id.status_text_view);
        newText.setText("I'm so full");
    }

@aseilhamid
Copy link

It crashes with me also ... but I could fix this by copying the images to drawable folder in project files as I found them in folder called
drawable-v24 only

@treedbox
Copy link

my code:
activity_main.xml _________________

<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" />

    <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:id="@+id/eat_button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="16dp"
        android:onClick="eatCookie"
        android:text="EAT COOKIE" />

</LinearLayout>

MainActivity.java _________________

package com.example.android.cookies;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;


public class MainActivity extends AppCompatActivity {

    protected boolean toEat = true;

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

    /**
     * Changes message on textView
     */
    private void displayMessage(String message) {
        TextView statusTextView = findViewById(R.id.status_text_view);
        statusTextView.setText(message);
    }

    /**
     * Changes button text
     */
    private void displayButtonText(String text) {
        Button eatButton = findViewById(R.id.eat_button);
        eatButton.setText(text);
    }

    /**
     * Changes image
     */
    private void displayImage(int img) {
        ImageView androidCookieImageView = findViewById(R.id.android_cookie_image_view);
        androidCookieImageView.setImageResource(img);
        androidCookieImageView.setImageResource(img);
    }

    /**
     * Called when button eat_button is pressed
     */
    public void eatCookie(View view) {
        //if toEat is true, eat like it was the last meal
        if (toEat) {
            displayImage(R.drawable.after_cookie);
            displayMessage("I'm so full"); //show some regret
            displayButtonText("Eat another Cookie!"); //suppress the regret

            toEat = false; //not to eat anymore
        } else {
            //if toEat is false, allow eat another cookie
            displayImage(R.drawable.before_cookie);
            displayMessage("Well, aways has space for some more :P"); //make a banal excuse
            displayButtonText("Eat the Cookie"); //Test your limits

            toEat = true; //to eat
        }
    }
}

@Rudien
Copy link

Rudien commented Oct 27, 2018

I succeeded !

@TejoNagesh
Copy link

ok done

@TejoNagesh
Copy link

hungry

@HeshamNatora
Copy link

image
you will notice that i didn't cast my objects view and it works.....but i don't know why it doesn't matter?!!!!

@OneGrumpyBunny
Copy link

OneGrumpyBunny commented Jul 9, 2019

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.

@thisisruslan
Copy link

well Done

@NChandra-Dev
Copy link

cookies error 1
cookies error 2

Can anybody help me? How to resolve these errors?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment