Top Banner
Activities
17

Activities - gorskicompsci.cagorskicompsci.ca/ICS4U/1_Android/ppt_Buttons_Activities.pdf · Gmail has 2 main activities. (1) View email list (2) Compose new emails. They each have

May 29, 2020

Download

Documents

dariahiddleston
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Activities - gorskicompsci.cagorskicompsci.ca/ICS4U/1_Android/ppt_Buttons_Activities.pdf · Gmail has 2 main activities. (1) View email list (2) Compose new emails. They each have

Activities

Page 2: Activities - gorskicompsci.cagorskicompsci.ca/ICS4U/1_Android/ppt_Buttons_Activities.pdf · Gmail has 2 main activities. (1) View email list (2) Compose new emails. They each have

Activity – a single focused thing that your user can do.

Informally: A type of screen.

Page 3: Activities - gorskicompsci.cagorskicompsci.ca/ICS4U/1_Android/ppt_Buttons_Activities.pdf · Gmail has 2 main activities. (1) View email list (2) Compose new emails. They each have

Gmail has 2 main activities.(1) View email list (2) Compose new emails.They each have an activity which focuses on something the user can do.

Page 4: Activities - gorskicompsci.cagorskicompsci.ca/ICS4U/1_Android/ppt_Buttons_Activities.pdf · Gmail has 2 main activities. (1) View email list (2) Compose new emails. They each have
Page 5: Activities - gorskicompsci.cagorskicompsci.ca/ICS4U/1_Android/ppt_Buttons_Activities.pdf · Gmail has 2 main activities. (1) View email list (2) Compose new emails. They each have

How many activities?

Page 6: Activities - gorskicompsci.cagorskicompsci.ca/ICS4U/1_Android/ppt_Buttons_Activities.pdf · Gmail has 2 main activities. (1) View email list (2) Compose new emails. They each have

How many activities?

1. There is one

screen.

Page 7: Activities - gorskicompsci.cagorskicompsci.ca/ICS4U/1_Android/ppt_Buttons_Activities.pdf · Gmail has 2 main activities. (1) View email list (2) Compose new emails. They each have
Page 8: Activities - gorskicompsci.cagorskicompsci.ca/ICS4U/1_Android/ppt_Buttons_Activities.pdf · Gmail has 2 main activities. (1) View email list (2) Compose new emails. They each have
Page 9: Activities - gorskicompsci.cagorskicompsci.ca/ICS4U/1_Android/ppt_Buttons_Activities.pdf · Gmail has 2 main activities. (1) View email list (2) Compose new emails. They each have

package ca.gorskicompsci.www.countmein;

import android.support.v7.app.AppCompatActivity;

import android.os.Bundle;

import android.view.View;

import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

int count =0;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

}

public void subtract(View view){

TextView txt = (TextView) findViewById(R.id.numberView);

count--;

txt.setText(""+count);

}

public void addtoit(View view){

TextView txt = (TextView) findViewById(R.id.numberView);

count++;

txt.setText(""+count);

}

}

Still Libraries

Essentially: make my XML file into the init method.

The code for my subtract button.

The code for my add button.

Page 10: Activities - gorskicompsci.cagorskicompsci.ca/ICS4U/1_Android/ppt_Buttons_Activities.pdf · Gmail has 2 main activities. (1) View email list (2) Compose new emails. They each have

<TextView

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="0"

android:textSize="200sp"

android:padding="20dp"

android:id="@+id/numberView"

android:layout_gravity="center"/>

<ImageButton

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:src="@drawable/buttonadd"

android:scaleType="center"

android:onClick="addtoit"

android:id="@+id/addbutton" />

<ImageButton

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:src="@drawable/buttondelete"

android:scaleType="center"

android:onClick="subtract"

android:id="@+id/subtractbutton" />

<TextView

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="Press the buttons to count."

android:textSize="20sp"

android:padding="20sp"

android:layout_gravity="center" />

Connect to

method

Connect to

method

View name

Page 11: Activities - gorskicompsci.cagorskicompsci.ca/ICS4U/1_Android/ppt_Buttons_Activities.pdf · Gmail has 2 main activities. (1) View email list (2) Compose new emails. They each have

package ca.gorskicompsci.www.countmein;

import android.support.v7.app.AppCompatActivity;

import android.os.Bundle;

import android.view.View;

import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

int count =0;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

}

public void subtract(View view){

TextView txt = (TextView) findViewById(R.id.numberView);

count--;

txt.setText(""+count);

}

public void addtoit(View view){

TextView txt = (TextView) findViewById(R.id.numberView);

count++;

txt.setText(""+count);

}

}

Global Variable

Method name matches onclick

Needed to find the textview in

the XMLChange variable Set text

on screen

Page 12: Activities - gorskicompsci.cagorskicompsci.ca/ICS4U/1_Android/ppt_Buttons_Activities.pdf · Gmail has 2 main activities. (1) View email list (2) Compose new emails. They each have

Pretty much, You always

want to add whatever they

suggest.

Page 13: Activities - gorskicompsci.cagorskicompsci.ca/ICS4U/1_Android/ppt_Buttons_Activities.pdf · Gmail has 2 main activities. (1) View email list (2) Compose new emails. They each have

public void clicked(View view){

TextView t = (TextView) findViewById(R.id.txt);

ImageView i = (ImageView) findViewById(R.id.pic);

EditText e = (EditText) findViewById(R.id.write);

String word = e.getText().toString();

t.setText(word);

i.setImageResource(R.drawable.hp2);

e.setEnabled(false);

int num = Integer.parseInt(word);

int rNum = (int) (Math.random()*3+1);

t.setText(num+" "+rNum);

}

Some code snippets:

Page 14: Activities - gorskicompsci.cagorskicompsci.ca/ICS4U/1_Android/ppt_Buttons_Activities.pdf · Gmail has 2 main activities. (1) View email list (2) Compose new emails. They each have

onCreate = init.onCreate goes through the XML file and writes the java code for it. Called “inflation” process.

Page 15: Activities - gorskicompsci.cagorskicompsci.ca/ICS4U/1_Android/ppt_Buttons_Activities.pdf · Gmail has 2 main activities. (1) View email list (2) Compose new emails. They each have
Page 16: Activities - gorskicompsci.cagorskicompsci.ca/ICS4U/1_Android/ppt_Buttons_Activities.pdf · Gmail has 2 main activities. (1) View email list (2) Compose new emails. They each have

Once android has created the Views for us,We have variables in the code.We need to connect viewID to code that we need to use later.

That is findViewById.findViewByIDreturns an object. We need to cast it to it’s type.

Page 17: Activities - gorskicompsci.cagorskicompsci.ca/ICS4U/1_Android/ppt_Buttons_Activities.pdf · Gmail has 2 main activities. (1) View email list (2) Compose new emails. They each have