Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Android for Beginners : Cookies Starting 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" />
<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" />
</LinearLayout>
package com.example.android.cookies;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
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.
}
}
@whooof

This comment has been minimized.

Copy link

@whooof whooof commented Oct 2, 2015

There is no android:text parameter set in Button

@udacityandroid

This comment has been minimized.

Copy link
Owner Author

@udacityandroid udacityandroid commented Nov 5, 2015

@whooof whoops, good catch! Fixed.

@Sarcares

This comment has been minimized.

Copy link

@Sarcares Sarcares commented Mar 27, 2016

In the Button of XML files you always suggest to write the text in lowercase letters and set the textAllCaps property to true if we want the uppercase letters. It could be a good idea to follow this standard even here...

@quantbo

This comment has been minimized.

Copy link

@quantbo quantbo commented Apr 19, 2016

I agree with Sarcares. Set android:textAllCaps="true"

@Prathamking8

This comment has been minimized.

Copy link

@Prathamking8 Prathamking8 commented Apr 25, 2016

I am not able to change the image neither text after clicking on eat cookie button could you please help me with entire code. I am beginner and I am trying to do but not getting any clue.

Thanks

@ghost

This comment has been minimized.

Copy link

@ghost ghost commented Apr 26, 2016

Pratham, hey bhai...

The image and text will not change unless you write the logic(code to actually change them) in java by following the code. This is just a template for us to write onwards.

@zesiddiq

This comment has been minimized.

Copy link

@zesiddiq zesiddiq commented Jun 11, 2016

Can anybody provide the code for eatCookie method ?

@NitinKushwah-xx

This comment has been minimized.

Copy link

@NitinKushwah-xx NitinKushwah-xx commented Jun 26, 2016

Hey everyone, this was actually very simple..I initially sat for 3 hours to do the require changes but then went again back to Documentation in Androird and found really simple. Here is the full code

activity_main.xml

<!--
this should be inital image when app loaded
-->

<ImageView
    android:id="@+id/before_cookie_image_view"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="1"
    android:scaleType="centerCrop"
    android:src="@drawable/beforecookie" />

<TextView
    android:id="@+id/status_initial_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:onClick="eatCookie"
    android:text="EAT COOKIE" />

For MainActivity.java

package com.example.android.cookies2;

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) {
    // TODO: Find a reference to the ImageView in the layout. Change the image.
    ImageView beforeCookieImageView = (ImageView) findViewById(R.id.before_cookie_image_view);
    beforeCookieImageView.setImageResource(R.drawable.aftercookie);

    // TODO: Find a reference to the TextView in the layout. Change the text.
    TextView statusInitialTextView = (TextView) findViewById(R.id.status_initial_text_view);
    statusInitialTextView.setText("I am Totally full");
}

}

Do not forget to put images into the drawable folder.....

@ghost

This comment has been minimized.

Copy link

@ghost ghost commented Jul 2, 2016

Hi,
I don't seem to get this working. I've tried both designing the app and writing the java code from schratch myself, and copy-paste the complete code.

In both cases the kinda works, but it is insanely slow, and I get messages telling me that it skipped xx-frames (200+), and that it may be doing too much in main activity.

Any way to fix this? I've tried running other apps, and they run fine, so it's not caused by a slow emulator.

Thanks!

Edit:
It seems to run fine when i load it onto my phone (actual phone), but the logcat goes amok when I do, and writes what appears to be tens of lines each second ://

@Momen-a

This comment has been minimized.

Copy link

@Momen-a Momen-a commented Jul 5, 2016

@Prathamking8

1st put the 2 android ( after_cookie.jpg , before_cookie.jpg ) in ( Cookies\app\src\main\res\drawable )
here is my edit for codes :--

In main_activity java file

/**
 * 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.
    ImageView aftercookieView = (ImageView) findViewById(R.id.before_cookie);
    aftercookieView.setImageResource(R.drawable.after_cookie);


    // TODO: Find a reference to the TextView in the layout. Change the text.
    TextView doneTextView = (TextView)findViewById(R.id.status_text_view);
    doneTextView.setText("Thanks Momen!");
}

And in the .XML file

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

@maruf00014

This comment has been minimized.

Copy link

@maruf00014 maruf00014 commented Jul 17, 2016

Yes...I made it....!!!

@menacius

This comment has been minimized.

Copy link

@menacius menacius commented Jul 27, 2016

I'm getting W/OpenGLRenderer: Bitmap too large to be uploaded into a texture (3604x4488, max=4096x4096)

:(

@buxx67

This comment has been minimized.

Copy link

@buxx67 buxx67 commented Aug 12, 2016

Here is the full code I used, I tried a couple of the versions previously listed but they were a little off on my system so here is the code I used that worked for me:

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 afterCookieView = (ImageView) findViewById(R.id.android_cookie_image_view);
    afterCookieView.setImageResource(R.drawable.after_cookie);

    TextView afterCookieTextView = (TextView) findViewById(R.id.status_text_view);
    afterCookieTextView.setText("I'm So Full");
}

}

@arashna47

This comment has been minimized.

Copy link

@arashna47 arashna47 commented Sep 25, 2016

Hello I get R error in MainActivity.java
what should I do?

@divyanshujhawar

This comment has been minimized.

Copy link

@divyanshujhawar divyanshujhawar commented Sep 28, 2016

The line to be copied in build.gradle file is-
compile "com.android.support:appcompat-v7:22.1.0" or "com.android.support:appcompat-v7:22.1.0"
and where it need to be copied

@ZedFable

This comment has been minimized.

Copy link

@ZedFable ZedFable commented Sep 30, 2016

@arashna47: Did you copy the images into your resource folder?

@crank95: It took me forever to track down the file and when I did find it it already had a newer version of the line. Considering you posted this 2 days ago, I reckon your already all set like I was- no action required.

@ZedFable

This comment has been minimized.

Copy link

@ZedFable ZedFable commented Sep 30, 2016

So I feel like after a lot of research I "get" the concept of casting, and I understand why we are doing it in this case and how to- I can repeat the process. However, there is something that still confuses me about this:

As I understand, when Android starts my program it inflates all the XML into proper objects, so the TextView's become TextView objects, the ImageView become ImageView objects, etc. They are all set up and exist before we run any code under the button. So, what seems weird to me is that by running this line:

ImageView afterCookieView = (ImageView) findViewById(R.id.android_cookie_image_view);

It looks like we are creating a NEW ImageView object and just putting the viewID of the old one in it. Then when we run this line-

afterCookieView.setImageResource(R.drawable.after_cookie);

We are the .setImageReource function to change the properties of this NEW afterCookieView object, not the ORIGINAL one that already exists from the XML layout. I know this can't be right because the code works, but I'm confused as to why it wouldn't be something more like this:

android_cookie_image_view.setImageResource(R.drawable.after_cookie);

Does that make sense? I know for convention reasons, the above is not valid but its confusing to me that if we know the android_cookie_image id, why can't we just run the method on that object directly?

@Vitnere

This comment has been minimized.

Copy link

@Vitnere Vitnere commented Nov 7, 2016

For you who have problems, here is my code. Tested and working.

`

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

`

`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;

import static com.example.android.cookies.R.id.android_cookie_image_view;
import static com.example.android.cookies.R.id.status_text_view;

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.
    ImageView image = (ImageView) findViewById(android_cookie_image_view);
    image.setImageResource(R.drawable.after_cookie);

    // TODO: Find a reference to the TextView in the layout. Change the text.
    TextView text = (TextView) findViewById(status_text_view);
    text.setText("I'm so full");

}

}`

@Randyranga

This comment has been minimized.

Copy link

@Randyranga Randyranga commented Jan 17, 2017

100% WORKING

XML layout:-

Note - Add onClick for Eat Cookie text

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

MainActivity.java:

Note - Instead of "ranga" , type "android" or your name , wherever necessary

package com.example.ranga.cookies;

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

import static com.example.ranga.cookies.R.id.android_cookie_image_view;
import static com.example.ranga.cookies.R.id.status_text_view;

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.
    ImageView image = (ImageView) findViewById(android_cookie_image_view);
    image.setImageResource(R.drawable.after_cookie);

    // TODO: Find a reference to the TextView in the layout. Change the text.
    TextView text = (TextView) findViewById(status_text_view);
    text.setText("I'm so full");
}

}

@Duaahelwa

This comment has been minimized.

Copy link

@Duaahelwa Duaahelwa commented Jan 22, 2017

@divyanshujhawar how did you solve your problem with copying the line "com.android.support:appcompat-v7:22.1.0" into the build.gradle file and which library have you updated

@Shrijana2016

This comment has been minimized.

Copy link

@Shrijana2016 Shrijana2016 commented Jan 24, 2017

@Duaahelwa You probably figured it out already, but you can skip that step as android studio suggests to use the latest version, which is the default version.

@Shrijana2016

This comment has been minimized.

Copy link

@Shrijana2016 Shrijana2016 commented Jan 24, 2017

This is what worked for me. In MainActivity.java use the following code. Remember to update the package name to whatever name you had chosen for the application while building the new project.

package com.example.android.myapplication;

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

import com.example.android.myapplication.R;

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.
    ImageView beforeCookieImageView = (ImageView) findViewById(R.id.android_cookie_image_view);
    beforeCookieImageView.setImageResource(R.drawable.after_cookie);
    // TODO: Find a reference to the TextView in the layout. Change the text.
    TextView statusInitialTextView = (TextView) findViewById(R.id.status_text_view);
    statusInitialTextView.setText("I am Totally full");

}

}

In activity_main.xml use the following lines of code:

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

This comment has been minimized.

Copy link

@zutamacka zutamacka commented Jan 30, 2017

Was easy. Just this:

/**
 * 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.
    ImageView cookieImageView = (ImageView) findViewById(R.id.android_cookie_image_view);
    cookieImageView.setImageResource(R.drawable.after_cookie);

    // TODO: Find a reference to the TextView in the layout. Change the text.
    TextView statusTextView = (TextView) findViewById(R.id.status_text_view);
    statusTextView.setText("I've eated it!!!");
}

...sorry. lolcat fan...

Now I'm trying to figure out how to make the button reversable....

@AleksanderJaz

This comment has been minimized.

Copy link

@AleksanderJaz AleksanderJaz commented Feb 6, 2017

Initially, when trying to run on my device, I got compile errors which I dod not quite understand:

Error:Could not find method compile() for arguments [com.android.support:appcompat-v7:22.1.0] on root project 'Cookies' of type org.gradle.api.Project.

Please install the Android Support Repository from the Android SDK Manager.
Open Android SDK Manager

Then I removed the line compile "com.android.support:appcompat-v7:22.1.0" and everything worked fine... Why?

I have a Moto G3, Android 6.0.1, API 23

@KaranKishanKumar

This comment has been minimized.

Copy link

@KaranKishanKumar KaranKishanKumar commented Feb 28, 2017

android:layout_marginRight="16dp" this line in TextView is redundant and it doesn't makes any changes in the appearance of the app.

@Theoharoula

This comment has been minimized.

Copy link

@Theoharoula Theoharoula commented Mar 14, 2017

cannot see the image in the preview when i copy and paste the xml code. :( help?

@InderSethi

This comment has been minimized.

Copy link

@InderSethi InderSethi commented Mar 15, 2017

I did It it was not too Hard like I was thinking.

@sabinaioana25

This comment has been minimized.

Copy link

@sabinaioana25 sabinaioana25 commented Mar 21, 2017

Hi! Here is the code I used. Worked perfectly for me! 😃
This exercise was really really fun to dp and I'm happy that I grasped the concept of casting; now I am thinking of maybe using this newly acquired knowledge in my ScoreCounter app. and update some features. ✌️

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

    <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:onClick="eatCookie"
        android:text="EAT COOKIE" />
</LinearLayout>

Main Java:

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) {
        // TODO: Find a reference to the ImageView in the layout. Change the image.
        ImageView beforeCookieImageView = (ImageView) findViewById(R.id.android_cookie_image_view);
        beforeCookieImageView.setImageResource(R.drawable.after);

        // TODO: Find a reference to the TextView in the layout. Change the text.
        TextView statusFirstTextView = (TextView) findViewById(R.id.status_text_view);
        statusFirstTextView.setText("I'm so full!");
    }
}
@Irissm

This comment has been minimized.

Copy link

@Irissm Irissm commented Mar 22, 2017

Great exercise to get the concept of casting :)
This is the code I used and it works:
JAVA:
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) {
    // TODO: Find a reference to the ImageView in the layout. Change the image.
    ImageView cookieImage = (ImageView) findViewById(R.id.android_cookie_image_view);
    cookieImage.setImageResource(R.drawable.after_cookie);
    // TODO: Find a reference to the TextView in the layout. Change the text.
    TextView statusText = (TextView) findViewById(R.id.status_text_view);
    statusText.setText("I'm so full");
}

}
XML:

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

This comment has been minimized.

Copy link

@kostromina kostromina commented Mar 29, 2017

It was not easy...

@davidpinto1979

This comment has been minimized.

Copy link

@davidpinto1979 davidpinto1979 commented Mar 29, 2017

it was a bit difficult really. a real challange 👍

@Kalinakov

This comment has been minimized.

Copy link

@Kalinakov Kalinakov commented Apr 5, 2017

great exercise!

@Vercsi

This comment has been minimized.

Copy link

@Vercsi Vercsi commented Apr 11, 2017

Thanks! :) 👍

@Vercsi

This comment has been minimized.

Copy link

@Vercsi Vercsi commented Apr 11, 2017

Wow! :O

@razajaveid

This comment has been minimized.

Copy link

@razajaveid razajaveid commented Apr 28, 2017

wow, i have done with this.. :)

@kamran7502

This comment has been minimized.

Copy link

@kamran7502 kamran7502 commented May 8, 2017

/**
* 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.
ImageView imageAfterEat = (ImageView) findViewById(R.id.android_cookie_image_view);
imageAfterEat.setImageResource(after_cookie);

    // TODO: Find a reference to the TextView in the layout. Change the text.
    TextView textAfterEating = (TextView) findViewById(R.id.status_text_view);
    textAfterEating.setText("I am Full");

}
@kamran7502

This comment has been minimized.

Copy link

@kamran7502 kamran7502 commented May 8, 2017

package com.example.android.cookies; is wrong

package com.examples.android.cookies;

@Marwa-Eltayeb

This comment has been minimized.

Copy link

@Marwa-Eltayeb Marwa-Eltayeb commented May 13, 2017

I've made it without needing to use this code compile 'com.android.support:appcompat-v7:22.1.0'

public void eatCookie(View view) {
// TODO: 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);

    // TODO: 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");
}
@omokehinde

This comment has been minimized.

Copy link

@omokehinde omokehinde commented May 16, 2017

This also works fine
`package com.example.android.cookies;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

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) {
    // TODO: Find a reference to the ImageView in the layout. Change the image.
    ImageView cookieState = (ImageView) findViewById(R.id.android_cookie_image_view);
    cookieState.setImageDrawable(getResources().getDrawable(R.drawable.after_cookie));

    // TODO: Find a reference to the TextView in the layout. Change the text.
    TextView textViewStatus = (TextView) findViewById(R.id.status_text_view);
    textViewStatus.setText("I'm so full");

}

}`
the method
setImageDrawable(getResources().getDrawable(R.drawable.after_cookie)); could have been added so it can accept the answer for those who will type in setImageDrawable

@AngaKoko

This comment has been minimized.

Copy link

@AngaKoko AngaKoko commented May 21, 2017

do anyone know why we had to edit the gradle file?

@ezz91

This comment has been minimized.

Copy link

@ezz91 ezz91 commented Jun 1, 2017

I wanted to make the app more interactive by placing another button to refill cookies, which i did and it works and all, but i ran into a problem where the Image takes all the space with either match_parent or wrap-content, so i had to choose a specific dp. Won't this ruin the layout if it ran into a tablet or so?

any ideas how would this be fix-able?

UPDATE: I Set it both linear layouts to match parent but added weight of weight="0.2" to the first layout vertical image one and "1" to the button 2nd horizontal layout, sorta fixed it. But is there a better solution?

XML
`

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="445dp"
    android:orientation="vertical">

    <ImageView
        android:id="@+id/android_cookie_image_view"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="0.50"
        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" />
</LinearLayout>

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal">

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="16dp"
        android:onClick="eatCookie"
        android:text="EAT COOKIE" />

    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="16dp"
        android:onClick="moreCookiesPlease"
        android:text="Refill please" />
</LinearLayout>

`

JAVA
`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) {
    // TODO: Find a reference to the ImageView in the layout. Change the image.
    ImageView beforeEatCookieImageView = (ImageView) findViewById(R.id.android_cookie_image_view);
    beforeEatCookieImageView.setImageResource(R.drawable.after_cookie);

    // TODO: Find a reference to the TextView in the layout. Change the text.
    TextView beforeStatusTextView = (TextView) findViewById(R.id.status_text_view);
    beforeStatusTextView.setText("I'm so Full");

}

public void moreCookiesPlease (View view) {
    //Place more cookie
        ImageView moreCookiesImageView = (ImageView) findViewById(R.id.android_cookie_image_view);
        moreCookiesImageView.setImageResource(R.drawable.before_cookie);

        //change text again to im hungry
        TextView afterStatusTextView = (TextView) findViewById(R.id.status_text_view);
    afterStatusTextView.setText("I'm so Hungry");

    }

}`

@dillman-jamie

This comment has been minimized.

Copy link

@dillman-jamie dillman-jamie commented Jun 5, 2017

Hello Everyone,
I had a little fun with this exercise by adding a ToggleButton to keep the hungry Cookie Droid full :)

activity_main:
`

<ImageView
    android:id="@+id/android_cookie"
    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/cookie_message"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginLeft="16dp"
    android:layout_marginRight="16dp"
    android:layout_marginTop="16dp"
    android:text="@string/cookie_before"
    android:textColor="@android:color/white"
    android:textSize="34sp" />

<ToggleButton
    android:id="@+id/cookie_toggle"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_margin="16dp"
    android:textSize="18sp"
    android:textOn="@string/toggle_after"
    android:textOff="@string/toggle_before" />

`

strings.xml:
`

<string name="app_name">Cookies2</string>
<string name="cookie_before">I\'m So Hungry!</string>
<string name="cookie_after">One is Not Enough!</string>
<string name="toggle_before">Give Me Cookie!</string>
<string name="toggle_after">More Cookie Please!</string>

`

MainAcitivity.java:
`package com.example.android.cookies2;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.ToggleButton;

public class MainActivity extends AppCompatActivity {

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


    ToggleButton cookieToggle = (ToggleButton) findViewById(R.id.cookie_toggle);
    final TextView cookieMessage = (TextView) findViewById(R.id.cookie_message);

    cookieToggle.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {

            //Changes ImageView and Textview message when ToggleButton is clicked on and off
            if (isChecked) {
                ImageView eatCookie = (ImageView) findViewById(R.id.android_cookie);
                eatCookie.setImageResource(R.drawable.after_cookie);

                cookieMessage.setText(R.string.cookie_after);
            } else {
                ImageView eatCookie = (ImageView) findViewById(R.id.android_cookie);
                eatCookie.setImageResource(R.drawable.before_cookie);

                cookieMessage.setText(R.string.cookie_before);
            }

        }
    });
}

}
`

@ridbay

This comment has been minimized.

Copy link

@ridbay ridbay commented Jun 21, 2017

This works for me (Thanks to Ranga)
XML
`

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

`

MainActivity.java
`package com.example.android.myapplicationnew;

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

import static com.example.android.myapplicationnew.R.id.android_cookie_image_view;
import static com.example.android.myapplicationnew.R.id.status_text_view;
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.
    ImageView image = (ImageView) findViewById(android_cookie_image_view);
    image.setImageResource(R.drawable.after_cookie);

    // TODO: Find a reference to the TextView in the layout. Change the text.
    TextView text = (TextView) findViewById(status_text_view);
    text.setText("I'm so full");
}

}`

@rene1177

This comment has been minimized.

Copy link

@rene1177 rene1177 commented Jul 3, 2017

Worked just fine. Of course, we only have to write next code with our path.
activity_main.xml
....
tools:context="(my path).MainActivity">
.....

MainActivity.java

public void eatCookie(View view)
{

    // TODO: Find a reference to the ImageView in the layout. Change the image.
    ImageView androidCookieImageView = (ImageView) findViewById(R.id.android_cookie_image_view);
    androidCookieImageView.setImageResource(R.drawable.after_cookie);

    // TODO: Find a reference to the TextView in the layout. Change the text.
   TextView statusTextView = (TextView) findViewById(R.id.status_text_view);
    statusTextView.setText("Ooo.I need one more cookie....");

}

}

@nocturnoAkaMylo

This comment has been minimized.

Copy link

@nocturnoAkaMylo nocturnoAkaMylo commented Jul 4, 2017

i got it done the apps worked just fine. I answered the second and the third one correctly which is setImageResource() and setText(), but box 1. I just don't get it what she wants. Can any one help, please?

@ag597482

This comment has been minimized.

Copy link

@ag597482 ag597482 commented Jul 7, 2017

try this new one...

package com.example.amangupta.app1;

import android.graphics.drawable.Drawable;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;

public class MainActivity extends AppCompatActivity {

int f=0;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.n);
}
public void eatCookie(View view) {
    // TODO: Find a reference to the ImageView in the layout. Change the image.
       ImageView i=(ImageView)findViewById(R.id.android_cookie_image_view);
    Button b=(Button)findViewById(R.id.b);

    if( f%2==0) {
        i.setImageResource(R.drawable.b2);

        b.setText("haha mai kha gya");
        f++;

    }
    else
    {
        i.setImageResource(R.drawable.b1);

        b.setText("fir khao");
        f++;
    }        // TODO: Find a reference to the TextView in the layout. Change the text.

}

}

@vikas-appdev

This comment has been minimized.

Copy link

@vikas-appdev vikas-appdev commented Jul 23, 2017

@ag597482 very funny bhai ,

@sheepa23

This comment has been minimized.

Copy link

@sheepa23 sheepa23 commented Jul 26, 2017

I did not need to use compile 'com.android.support:appcompat-v7:22.1.0'
Also, had to update the path tools:context="com.example.andriod.cookies.MainActivity"

@Sophie713

This comment has been minimized.

Copy link

@Sophie713 Sophie713 commented Oct 10, 2017

I also got the button to disappear. =)
Button btnEatCookie = (Button) findViewById(R.id.button_eat);
btnEatCookie.setVisibility(View.GONE);

@sergey-shchemenok

This comment has been minimized.

Copy link

@sergey-shchemenok sergey-shchemenok commented Nov 16, 2017

Using global boolean variable "full" we can change states from hungry to full and vice versa
if (!full) {
iv.setImageResource(R.drawable.after_cookie);
tv.setText("I'm so full");
full = true;
}else {
iv.setImageResource(R.drawable.before_cookie);
tv.setText("I'm so hungry");
full = false;
}

@lamineprfs

This comment has been minimized.

Copy link

@lamineprfs lamineprfs commented Nov 23, 2017

We don't need to cast the view because the data types already returns the same methode

@ShashankAC

This comment has been minimized.

Copy link

@ShashankAC ShashankAC commented Dec 16, 2017

In the Java file i am getting a red R in the "setContentView(R.layout.activity_main);" can anyone help me here ?

@MarinaDMM

This comment has been minimized.

Copy link

@MarinaDMM MarinaDMM commented Dec 19, 2017

It work correctly!
`

<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:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_margin="16dp"
    android:onClick="eatCookie"
    android:text="EAT COOKIE" />
` `package com.example.android.cookies;

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

    ImageView beforeCookieImageView = (ImageView) findViewById(R.id.android_cookie_image_view);
    beforeCookieImageView.setImageResource(R.drawable.after_cookie);

    // TODO: Find a reference to the TextView in the layout. Change the text.

    TextView statusTextView = (TextView) findViewById(R.id.status_text_view);
    statusTextView.setText("I am So full!");
}

}`
Just don't forget to upload images!

@RobertMcDonald33

This comment has been minimized.

Copy link

@RobertMcDonald33 RobertMcDonald33 commented Jan 3, 2018

the image crashes my program. If I delete the image the program will at least run without pressing the button.

@algarvi0

This comment has been minimized.

Copy link

@algarvi0 algarvi0 commented Jan 4, 2018

ALl works good .. some changes:

Layout...
`

<ImageView
    android:id="@+id/android_cookie_image_view"
    android:layout_width="wrap_content"
    android:layout_height="0dp"
    android:layout_weight="1"
    android:scaleType="centerCrop"
    android:src="@drawable/before_cookie"
    android:contentDescription="@string/image_description" />

<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="@string/i_m_so_hungry"
    android:textColor="@android:color/white"
    android:textSize="34sp" />

<LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="horizontal">

    <Button
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_margin="16dp"
        android:layout_weight="1"
        android:onClick="eatCookie"
        android:text="@string/eat_cookie" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="16dp"
        android:onClick="dontEatCookie"
        android:text="@string/dont_eat" />


</LinearLayout>

`
Java

`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 beforeCookieImageView = (ImageView) findViewById(R.id.android_cookie_image_view);
    beforeCookieImageView.setImageResource(R.drawable.after_cookie);


    TextView statusInitialTextView = (TextView) findViewById(R.id.status_text_view);
    statusInitialTextView.setText("I am Totally full");

}

public void dontEatCookie(View view) {

    ImageView beforeCookieImageView = (ImageView) findViewById(R.id.android_cookie_image_view);
    beforeCookieImageView.setImageResource(R.drawable.before_cookie);


    TextView statusInitialTextView = (TextView) findViewById(R.id.status_text_view);
    statusInitialTextView.setText("I'm so hungry");

}

}`

@waseemmaya

This comment has been minimized.

Copy link

@waseemmaya waseemmaya commented Jan 7, 2018

Why didn't we insert "new" keyword in

TextView textView = (TextView) findViewById(R.id.status_text_view);

ImageView imageView = (ImageView) findViewById(R.id.android_cookie_image_view);

@aluszg

This comment has been minimized.

Copy link

@aluszg aluszg commented Jan 7, 2018

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:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_margin="16dp"
    android:onClick="eatCookie"
    android:text="EAT COOKIE" />
  • images upload to AndroidStudioProjects\Cookies\app\src\main\res\drawable
@aluszg

This comment has been minimized.

Copy link

@aluszg aluszg commented Jan 7, 2018

MainActivity.java

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) {

    // TODO: Find a reference to the ImageView in the layout. Change the image.
    ImageView androidCookieImageView = (ImageView) findViewById(R.id.android_cookie_image_view);
    androidCookieImageView.setImageResource(R.drawable.after_cookie);

    // TODO: Find a reference to the TextView in the layout. Change the text.
    TextView statusTextView = (TextView) findViewById(R.id.status_text_view);
    statusTextView.setText("I'm so full");
}

}

@OldWolf55

This comment has been minimized.

Copy link

@OldWolf55 OldWolf55 commented Jan 10, 2018

Hi . It worked with very few lines of code. I have created the global variable "message" to be passed to setText, but it worked even with a string just after the method setText.
Here the XML and java code:

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

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);
}

String message = "I'm so full" ;

/**
 * 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.
    ImageView  img = (ImageView) findViewById(R.id.android_cookie_image_view) ;
    img.setImageResource(R.drawable.after_cookie);

    // TODO: Find a reference to the TextView in the layout. Change the text.

    TextView eating = (TextView) findViewById(R.id.status_text_view) ;
    eating.setText(message) ;

}

}

@GeorgeShao

This comment has been minimized.

Copy link

@GeorgeShao GeorgeShao commented Jan 12, 2018

Yes I got it to work!

@Saphiriela

This comment has been minimized.

Copy link

@Saphiriela Saphiriela commented Jan 29, 2018

Hi,

here is my code which works correctly :) So happy that my Android eats the cookie!

XML code:

<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:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_margin="16dp"
    android:onClick="eatCookie"
    android:text="Eat Cookie"
    android:textAllCaps="true"/>

and Java code:
package com.example.android.cookie;

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) {
    // TODO: Find a reference to the ImageView in the layout. Change the image.
    ImageView beforeCookieImageView = (ImageView) findViewById(R.id.android_cookie_image_view);
    beforeCookieImageView.setImageResource(R.drawable.after_cookie);

    // TODO: Find a reference to the TextView in the layout. Change the text.
    TextView statusInitialTextView = (TextView) findViewById(R.id.status_text_view);
    statusInitialTextView.setText("I am so full");
}

}

@mantisbead

This comment has been minimized.

Copy link

@mantisbead mantisbead commented Jan 29, 2018

@Shrijana2016 I don't know if your advice about skipping the gradle dependency helped @Duaahelwa but it definitely helped me. So thanks a lot!

@paulokoduwa

This comment has been minimized.

Copy link

@paulokoduwa paulokoduwa commented Jan 30, 2018

here's my xml file

here's my java code
package com.example.android.cookie;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
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 changeImage(View view) {
    // TODO: Find a reference to the ImageView in the layout. Change the image.
    ImageView afterCookie = findViewById(R.id.android_cookie_image_view);
    afterCookie.setImageResource(R.drawable.after_cookie);

    // TODO: Find a reference to the TextView in the layout. Change the text.
    TextView afterText = (TextView) findViewById(R.id.status_text_view);
    afterText.setText("I m so full");

}

}

hope it works for you

@Mohamadmahgoub

This comment has been minimized.

Copy link

@Mohamadmahgoub Mohamadmahgoub commented Jan 31, 2018

just copy the code above
add for it in the xml file in the button
add this : android:onclick="eatCookie"
and in java file
write this
/**

  • 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.
    ImageView beforeCookieImageView = (ImageView) findViewById(R.id.before_cookie_image_view);
    beforeCookieImageView.setImageResource(R.drawable.aftercookie);

    // TODO: Find a reference to the TextView in the layout. Change the text.
    TextView statusInitialTextView = (TextView) findViewById(R.id.status_initial_text_view);
    statusInitialTextView.setText("I am Totally full");
    }
    you are welcome :)

@OskarHofmann

This comment has been minimized.

Copy link

@OskarHofmann OskarHofmann commented Feb 1, 2018

I solved this problem with the method "setImageDrawable" instead of setImageResource. Works fine but sadly the quiz marks this solution as wrong...

@leonidaa

This comment has been minimized.

Copy link

@leonidaa leonidaa commented Feb 4, 2018

@nitinkushwah thanks

@Maxwe1

This comment has been minimized.

Copy link

@Maxwe1 Maxwe1 commented Feb 6, 2018

The Solution is so easy

** Nowadays you don't need to cast your return View object to any other view like TextView or ImageView anymore.**

I have made some changes to the code a little bit for reversible action so as the app doesn't stop at one end after pressing the button:

The Java Code:

`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 {

boolean full = false;

@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.
    ImageView cookie = (ImageView) findViewById(R.id.android_cookie_image_view);


    // TODO: Find a reference to the TextView in the layout.
    TextView statusText = (TextView) findViewById(R.id.status_text_view);


    //Find a reference to the ButtonView in the layout.
    Button btn = findViewById(R.id.btn_view);

    if (!full) {
        cookie.setImageResource(R.drawable.after_cookie);
        statusText.setText("I'm so full");
        full = true;
        btn.setText("Pass Time");

    }else {
        cookie.setImageResource(R.drawable.before_cookie);
       statusText.setText("I'm so hungry");
        full = false;
        btn.setText("EAT COOKIE");
    }



}

}`

The 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="com.example.android.cookies.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/btn_view"
    android:layout_width="wrap_content"

    android:layout_height="wrap_content"

    android:layout_margin="16dp"

    android:text="EAT COOKIE"
    android:onClick="eatCookie"/>

`

I could have removed the (TextView), (ImageView) and (ButtonView) for casting and the code will still work.

@LailaHussain

This comment has been minimized.

Copy link

@LailaHussain LailaHussain commented Feb 10, 2018

I did it .... Wow!


This is my code in Java


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) {
    // TODO: Find a reference to the ImageView in the layout. Change the image.
    TextView textView=(TextView)findViewById(R.id.status_text_view);
    textView.setText("I'm so full");
    // TODO: Find a reference to the TextView in the layout. Change the text.
    ImageView imageView=(ImageView)findViewById(R.id.android_cookie_image_view);
    imageView.setImageResource(R.drawable.after_cookie);

}

}


This is which I add in Button in XML


android:onClick="eatCookie"


Good Lock For You

@abdullahelsheikh

This comment has been minimized.

Copy link

@abdullahelsheikh abdullahelsheikh commented Feb 18, 2018

Thank You

@abdahma01

This comment has been minimized.

Copy link

@abdahma01 abdahma01 commented Feb 20, 2018

xxx

@aminabenabdallah

This comment has been minimized.

Copy link

@aminabenabdallah aminabenabdallah commented Feb 23, 2018

It works! I like the idea of the exercise !

@N-M-Joli

This comment has been minimized.

Copy link

@N-M-Joli N-M-Joli commented Feb 23, 2018

XML

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

This comment has been minimized.

Copy link

@N-M-Joli N-M-Joli commented Feb 23, 2018

Java

package j.com.example.android.myapplication;

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

import j.com.example.android.myapplication.R;

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.
    ImageView BeforeatCookie=(ImageView) findViewById(R.id.android_cookie_image_view);
    BeforeatCookie.setImageResource(R.drawable.after_cookie);
    // TODO: Find a reference to the TextView in the layout. Change the text.
    TextView aftereat=(TextView) findViewById(R.id.status_text_view);
    aftereat.setText("I am Full ");
}

}

@badrddinb

This comment has been minimized.

Copy link

@badrddinb badrddinb commented Mar 1, 2018

DONE (Missing before_cookie)

@Hend2233

This comment has been minimized.

Copy link

@Hend2233 Hend2233 commented Mar 11, 2018

@nitinkushwah Thank you very much 🥇

@darcdev

This comment has been minimized.

Copy link

@darcdev darcdev commented Apr 13, 2018

This is my code Cool Lesson!

`

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

`

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 {

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

/**
 * Called when the cookie should be eaten.
 */
boolean buttons = false;

public void eatCookie(View view) {


    if (buttons == false) {


        // TODO: 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);


        // TODO: 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");
        // TODO: change button content
        Button button = (Button) findViewById(R.id.text_button);
        button.setText("Hours Later...");

        buttons = true;

    } else {
        
        // TODO: 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.before_cookie);


        // TODO: 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 Hungry");

        //TODO: change button content
        Button button = (Button) findViewById(R.id.text_button);
        button.setText("Hours Later...");

        buttons = false;

    }

}

}`

@Imsurajkr

This comment has been minimized.

Copy link

@Imsurajkr Imsurajkr commented Apr 22, 2018

`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) {
    // TODO: Find a reference to the ImageView in the layout. Change the image.
    ImageView beforeCookieImageView = (ImageView) findViewById(R.id.android_cookie_image_view);
    beforeCookieImageView.setImageResource(R.drawable.after_cookie);

    // TODO: Find a reference to the TextView in the layout. Change the text.
    TextView statusTextView = (TextView) findViewById(R.id.status_text_view);
    statusTextView.setText("I am Totally Full");
}

}`

@alialzarei

This comment has been minimized.

Copy link

@alialzarei alialzarei commented Apr 30, 2018

Done :)

@Utkarshsteve

This comment has been minimized.

Copy link

@Utkarshsteve Utkarshsteve commented May 1, 2018

THE XML FILE IS:

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

THE JAVA CODE IS:

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) {
    // TODO: Find a reference to the ImageView in the layout. Change the image.
    ImageView bciImageView = (ImageView) findViewById(R.id.android_cookie_image_view);
    bciImageView.setImageResource(R.drawable.after_cookie);

    // TODO: Find a reference to the TextView in the layout. Change the text.
    TextView tiTextView = (TextView) findViewById(R.id.status_text_view);
    tiTextView.setText("I'm so full");

}

}

@Eduese

This comment has been minimized.

Copy link

@Eduese Eduese commented May 25, 2018

just felt like to re-strategize by adding a RESET button to return to previous xml "I'm so Hungry" from "I'm so full
screenshot_2018-05-25-13-25-05
"
screenshot_2018-05-25-13-43-20

@amine2ng

This comment has been minimized.

Copy link

@amine2ng amine2ng commented May 31, 2018

thanks

@H1D1

This comment has been minimized.

Copy link

@H1D1 H1D1 commented Jun 2, 2018

hello when i add the : compile 'com.android.support:appcompat-v7:22.1.0'

in

dependencies {
compile 'com.android.support:appcompat-v7:22.1.0'
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'com.android.support:appcompat-v7:27.1.1'
implementation 'com.android.support.constraint:constraint-layout:1.1.0'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test🏃1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
}
it shows me this error :

//AAPT2 error: check logs for details

im blocked any solution plz, thanks

@waffyved

This comment has been minimized.

Copy link

@waffyved waffyved commented Jun 22, 2018

This is the code I edited , while I was at it I had my fun , I added a state so whenever the button is pressed it traverse between those two state
state 1- "I m hungry"
state 2- "I m so full"

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

</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.ImageView;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

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

boolean state=true;

/**
 * Called when the cookie should be eaten.
 */
public void eatCookie(View view) {

    if(state==true) {
        // TODO: Find a reference to the ImageView in the layout. Change the image.
        ImageView androidCookieImageView = (ImageView) findViewById(R.id.android_cookie_image_view);
        androidCookieImageView.setImageResource(R.drawable.after_cookie);

        // TODO: Find a reference to the TextView in the layout. Change the text.
        TextView statusTextView = (TextView) findViewById(R.id.status_text_view);
        statusTextView.setText("I'm so full");
    }
    else
        setContentView(R.layout.activity_main);
    state = state ^ true;
}

}

@LubnaElfarra

This comment has been minimized.

Copy link

@LubnaElfarra LubnaElfarra commented Jul 16, 2018

what the wrong here
cannot resolve symble R in main activity

@LubnaElfarra

This comment has been minimized.

Copy link

@LubnaElfarra LubnaElfarra commented Jul 16, 2018

image

@DynamOo

This comment has been minimized.

Copy link

@DynamOo DynamOo commented Jul 23, 2018

@LubnaElfarra, Try any of these solutions:
1- Build -> Clean Project
2- Tools -> Android -> Sync Project with Gradle Files
3- Restart Android Studio

@harunaadoga

This comment has been minimized.

Copy link

@harunaadoga harunaadoga commented Jul 27, 2018

I had same issue and a simple project rebuild worked for me.
So go to Build > Rebuild Project and see if it works.

@Wiserohin

This comment has been minimized.

Copy link

@Wiserohin Wiserohin commented Aug 12, 2018

@Sarcares, a button always has only capitals. Therefore, you could even write (eAtcOOkiE), and it would automatically set to all capitals to (EATCOOKIE).

@Bavalpreet

This comment has been minimized.

Copy link

@Bavalpreet Bavalpreet commented Aug 27, 2018

my code is running but after clicking on eat cookie picture does't change

@KarlDavidson

This comment has been minimized.

Copy link

@KarlDavidson KarlDavidson commented Sep 5, 2018

FYI for those using a newer version of Android Studio, you may not even need to include the compile command in later in the project. There should already be a few in the gradle build.

@Kinhagen

This comment has been minimized.

Copy link

@Kinhagen Kinhagen commented Oct 1, 2018

Where can I download the images?

@driss65

This comment has been minimized.

Copy link

@driss65 driss65 commented Dec 31, 2018

If you have android studio 3.2.1 , you dont need to add Compile code.
you will see that the code is already placed there :

implementation 'com.android.support:appcompat-v7:28.0.0'
@rohanjai777

This comment has been minimized.

Copy link

@rohanjai777 rohanjai777 commented Jan 15, 2019

This Code is Correct-
No need to Paste the Code in Gradle because we are using newer version of Android.
Name of Project is "cookie".
XML -

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

JAVA-
package com.example.android.cookie;

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

import com.example.android.cookie.R;

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.
    ImageView beforeCookieImageView = (ImageView) findViewById(R.id.android_cookie_image_view);
    beforeCookieImageView.setImageResource(R.drawable.after_cookie);
    // TODO: Find a reference to the TextView in the layout. Change the text.
    TextView statusInitialTextView = (TextView) findViewById(R.id.status_text_view);
    statusInitialTextView.setText("I am Totally full");

}

}

@chydee

This comment has been minimized.

Copy link

@chydee chydee commented Apr 17, 2019

Wow, I think I'm getting a hold on Logging. so sweet

@DariusThurman

This comment has been minimized.

Copy link

@DariusThurman DariusThurman commented Aug 28, 2019

There is no android:text parameter set in Button

implementation 'com.android.support:appcompat-v7:28.0.0'

Implementation has replaced the compile in the dependencies for gradle modular

@xMagicXs

This comment has been minimized.

Copy link

@xMagicXs xMagicXs commented Sep 2, 2019

this works for me ^_^ ( i didnt need to replace the v7 thing )
xml code here..

<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="@string/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="@string/eat_cookie"
    android:onClick="eatCookie"/>

and main activity..

package com.example.cookies;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
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) {
    // TODO: Find a reference to the ImageView in the layout. Change the image.
    ImageView beforeCookieImageVIew = (ImageView) findViewById ( R.id.android_cookie_image_view );
    beforeCookieImageVIew.setImageResource ( R.drawable.after_cookie );

    // TODO: Find a reference to the TextView in the layout. Change the text.
    TextView statusTextView = (TextView) findViewById ( R.id.status_text_view );
    statusTextView.setText ( "I am so full!" );

}

}

Important ( Do not forget to move the images into the drawable folder first or you will have nothing to show)

@Palamakumbura

This comment has been minimized.

Copy link

@Palamakumbura Palamakumbura commented Sep 4, 2019

Can anyone here tell what is this error?

09-04 09:58:06.402 4775-4775/com.example.android.cookies W/ResourceType: Failure getting entry for 0x7f060056 (t=5 e=86) (error -75)
09-04 09:58:06.404 4775-4775/com.example.android.cookies D/AndroidRuntime: Shutting down VM

09-04 09:58:06.430 4775-4775/com.example.android.cookies E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.android.cookies, PID: 4775
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.android.cookies/com.example.android.cookies.MainActivity}: android.view.InflateException: Binary XML file line #11: Binary XML file line #11: Error inflating class ImageView
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
Caused by: android.view.InflateException: Binary XML file line #11: Binary XML file line #11: Error inflating class ImageView
at android.view.LayoutInflater.inflate(LayoutInflater.java:539)
at android.view.LayoutInflater.inflate(LayoutInflater.java:423)
at android.view.LayoutInflater.inflate(LayoutInflater.java:374)
at androidx.appcompat.app.AppCompatDelegateImpl.setContentView(AppCompatDelegateImpl.java:469)
at androidx.appcompat.app.AppCompatActivity.setContentView(AppCompatActivity.java:140)
at com.example.android.cookies.MainActivity.onCreate(MainActivity.java:16)
at android.app.Activity.performCreate(Activity.java:6237)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476) 
at android.app.ActivityThread.-wrap11(ActivityThread.java) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344) 
at android.os.Handler.dispatchMessage(Handler.java:102) 
at android.os.Looper.loop(Looper.java:148) 
at android.app.ActivityThread.main(ActivityThread.java:5417) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 
Caused by: android.view.InflateException: Binary XML file line #11: Error inflating class ImageView
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:782)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:704)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:835)
at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:798)
at android.view.LayoutInflater.inflate(LayoutInflater.java:515)
at android.view.LayoutInflater.inflate(LayoutInflater.java:423) 
at android.view.LayoutInflater.inflate(LayoutInflater.java:374) 
at androidx.appcompat.app.AppCompatDelegateImpl.setContentView(AppCompatDelegateImpl.java:469) 
at androidx.appcompat.app.AppCompatActivity.setContentView(AppCompatActivity.java:140) 
at com.example.android.cookies.MainActivity.onCreate(MainActivity.java:16) 
at android.app.Activity.performCreate(Activity.java:6237) 
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107) 
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369) 
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476) 
at android.app.ActivityThread.-wrap11(ActivityThread.java) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344) 
at android.os.Handler.dispatchMessage(Handler.java:102) 
at android.os.Looper.loop(Looper.java:148) 
at android.app.ActivityThread.main(ActivityThread.java:5417) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 
Caused by: android.content.res.Resources$NotFoundException: Resource "com.example.android.cookies:drawable/before_cookie" (7f060056) is not a Drawable (color or path): TypedValue{t=0x1/d=0x7f060056 a=-1 r=0x7f060056}
at android.content.res.Resources.loadDrawableForCookie(Resources.java:2602)
at android.content.res.Resources.loadDrawable(Resources.java:2540)
at android.content.res.TypedArray.getDrawable(TypedArray.java:870)
at android.widget.ImageView.(ImageView.java:152)
at android.widget.ImageView.(ImageView.java:140)
at androidx.appcompat.widget.AppCompatImageView.(AppCompatImageView.java:72)
at androidx.appcompat.widget.AppCompatImageView.(AppCompatImageView.java:68)
at androidx.appcompat.app.AppCompatViewInflater.createImageView(AppCompatViewInflater.java:182)
at androidx.appcompat.app.AppCompatViewInflater.createView(AppCompatViewInflater.java:106)
at androidx.appcompat.app.AppCompatDelegateImpl.createView(AppCompatDelegateImpl.java:1266)
at androidx.appcompat.app.AppCompatDelegateImpl.onCreateView(AppCompatDelegateImpl.java:1316)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:746)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:704) 
at android.view.LayoutInflater.rInflate(LayoutInflater.java:835) 
at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:798) 
at android.view.LayoutInflater.inflate(LayoutInflater.java:515) 
at android.view.LayoutInflater.inflate(LayoutInflater.java:423) 
at android.view.LayoutInflater.inflate(LayoutInflater.java:374) 
at androidx.appcompat.app.AppCompatDelegateImpl.setContentView(AppCompatDelegateImpl.java:469) 
at androidx.appcompat.app.AppCompatActivity.setContentView(AppCompatActivity.java:140) 
at com.example.android.cookies.MainActivity.onCreate(MainActivity.java:16) 
at android.app.Activity.performCreate(Activity.java:6237) 
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107) 
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369) 
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476) 
at android.app.ActivityThread.-wrap11(ActivityThread.java) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344) 
at android.os.Handler.dispatchMessage(Handler.java:102) 
at android.os.Looper.loop(Looper.java:148) 
at android.app.ActivityThread.main(ActivityThread.java:5417) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 
09-04 09:58:17.545 4775-4775/com.example.android.cookies I/Process: Sending signal. PID: 4775 SIG: 9

@alicechen15

This comment has been minimized.

Copy link

@alicechen15 alicechen15 commented Sep 17, 2019

compile 'com.android.support:appcompat-v7:22.1.0'
should be
implementation 'androidx.appcompat:appcompat:1.1.0-rc01'

@sanketgitmod

This comment has been minimized.

Copy link

@sanketgitmod sanketgitmod commented May 3, 2020

image

@chetan081

This comment has been minimized.

Copy link

@chetan081 chetan081 commented Oct 15, 2020

thanks @alicechen15

@SonuRay0779

This comment has been minimized.

Copy link

@SonuRay0779 SonuRay0779 commented Nov 23, 2020

please anyone explain step 2
Add Gradle Dependency
Why we are adding compile 'com.android.support:appcompat-v7:22.1.0' this code to Gradle
and what is it's effect on app.

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