Skip to content

Instantly share code, notes, and snippets.

@udacityandroid
Last active November 22, 2022 21:34
Show Gist options
  • Save udacityandroid/8c4604af1d6b6afe12d6 to your computer and use it in GitHub Desktop.
Save udacityandroid/8c4604af1d6b6afe12d6 to your computer and use it in GitHub Desktop.
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.
}
}
@shaalle
Copy link

shaalle commented Jun 11, 2022

Here is My 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" />




=====================================================
Here is My Java Code

package com.shaalle.cookies;

import android.os.Bundle;

import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {
ImageView cookieImageView;
TextView statusTextView;

@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.
    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.
    statusTextView = (TextView) findViewById(R.id.status_text_view);
    statusTextView.setText("I'm so full");
}
/**
 * Resets the cookie to it's default state.
 */
public void reset(View view) {
    // TODO: Find a reference to the ImageView in the layout. Change the image.
    cookieImageView = (ImageView) findViewById(R.id.android_cookie_image_view);
    cookieImageView.setImageResource(R.drawable.before_cookie);
    // TODO: Find a reference to the TextView in the layout. Change the text.
    statusTextView = (TextView) findViewById(R.id.status_text_view);
    statusTextView.setText("I'm so hungry");
}

}

Here is the app screenshots

33ff61f3-a2ab-44fa-959b-6311816f81d2
7bf50f7d-2c5a-4785-8b24-6872a2253fb8

@calilos
Copy link

calilos commented Nov 22, 2022

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

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

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

    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="16dp"
        android:layout_weight="1"
        android:onClick="goWc"
        android:text="GO WC" />

</LinearLayout>

MainActivity.java

package com.example.cookies;

import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;

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");
}
public void goWc (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.before_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("Ohhhh, amazing");
}

}

Screenshots:

image
Click on eat cookie:
image
Click on go WC:
image

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