Skip to content

Instantly share code, notes, and snippets.

@leesc22
Created June 8, 2018 09:17
Show Gist options
  • Save leesc22/db3b1a09ab1ae243c03704f3fff48f08 to your computer and use it in GitHub Desktop.
Save leesc22/db3b1a09ab1ae243c03704f3fff48f08 to your computer and use it in GitHub Desktop.
Android for Beginners : Cookies Kotlin Code
<?xml version="1.0" encoding="utf-8"?>
<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>
package com.example.android.cookies
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.view.View
import android.widget.ImageView
import android.widget.TextView
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
}
/**
* Called when the cookie should be eaten.
*/
fun eatCookie(view: View) {
// TODO: Find a reference to the ImageView in the layout. Change the image.
val androidCookieImageView = findViewById<ImageView>(R.id.android_cookie_image_view) as ImageView
androidCookieImageView.setImageResource(R.drawable.after_cookie)
// TODO: Find a reference to the TextView in the layout. Change the text.
val statusTextView = findViewById<TextView>(R.id.status_text_view) as TextView
statusTextView.text = "I'm so full"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment