Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- OBJECTIVES/HOW IT WORKS:
- - Checks how many times you've clicked within a certain time allotment.
- -------------------------------------------------------------------------------------------------------------------------------
- package counter.samkough.com.counter;
- import android.app.Activity;
- import android.content.Intent;
- import android.os.Bundle;
- import android.view.Menu;
- import android.view.MenuItem;
- import android.view.View;
- import android.widget.Button;
- import android.widget.ImageView;
- import android.widget.TextView;
- import com.squareup.picasso.Picasso;
- public class MainActivity extends Activity
- {
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- Button button = (Button) findViewById(R.id.button);
- final TextView counter = (TextView) findViewById(R.id.count);
- button.setOnClickListener(new View.OnClickListener()
- {
- @Override
- public void onClick(View v)
- {
- int i = Integer.parseInt(counter.getText().toString());
- i++;
- counter.setText(String.valueOf(i));
- }
- });
- Button switchButton = (Button) findViewById(R.id.switchButton);
- switchButton.setOnClickListener(new View.OnClickListener()
- {
- @Override
- public void onClick(View v)
- {
- // creating the intent you need the context, then the activity you want to switch to
- Intent intent = new Intent(getApplicationContext(), SideActivity.class);
- // creates Bundle to send data to other class
- Bundle b = new Bundle();
- // gets the number of times you've clicked the button
- // first parameter is the name you assign it, and then the other is the actual number
- b.putInt("numberClicked", Integer.parseInt(counter.getText().toString()));
- // combining the intent and bundle together
- intent.putExtras(b);
- // starts the intent
- startActivity(intent);
- }
- });
- Button buttonReset = (Button) findViewById(R.id.resetButton);
- buttonReset.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- counter.setText(String.valueOf(0));
- }
- });
- ImageView pic = (ImageView) findViewById(R.id.picture);
- Picasso.with(getApplicationContext()).load("http://i.imgur.com/knTN4.jpg").into(pic);
- }
- @Override
- public boolean onCreateOptionsMenu(Menu menu)
- {
- // Inflate the menu; this adds items to the action bar if it is present.
- getMenuInflater().inflate(R.menu.menu_main, menu);
- return true;
- }
- @Override
- public boolean onOptionsItemSelected(MenuItem item)
- {
- // Handle action bar item clicks here. The action bar will
- // automatically handle clicks on the Home/Up button, so long
- // as you specify a parent activity in AndroidManifest.xml.
- int id = item.getItemId();
- //noinspection SimplifiableIfStatement
- if (id == R.id.action_settings)
- {
- return true;
- }
- return super.onOptionsItemSelected(item);
- }
- }
- -------------------------------------------------------------------------------------------------------------------------------
- <RelativeLayout 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:paddingLeft="@dimen/activity_horizontal_margin"
- android:paddingRight="@dimen/activity_horizontal_margin"
- android:paddingTop="@dimen/activity_vertical_margin"
- android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">
- <Button
- style="?android:attr/buttonStyleSmall"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="Click Me!"
- android:id="@+id/button"
- android:layout_below="@+id/count"
- android:layout_centerHorizontal="true" />
- <TextView
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="0"
- android:id="@+id/count"
- android:layout_alignParentTop="true"
- android:layout_centerHorizontal="true"
- android:layout_marginTop="48dp" />
- <Button
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="Switch"
- android:id="@+id/switchButton"
- android:layout_alignParentBottom="true"
- android:layout_alignParentEnd="true" />
- <Button
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="Reset"
- android:id="@+id/resetButton"
- android:layout_alignParentBottom="true"
- android:layout_alignParentStart="true" />
- <ImageView
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:id="@+id/picture"
- android:layout_centerVertical="true"
- android:layout_centerHorizontal="true" />
- </RelativeLayout>
- -------------------------------------------------------------------------------------------------------------------------------
- package counter.samkough.com.counter;
- import android.app.Activity;
- import android.content.Intent;
- import android.os.Bundle;
- import android.view.Menu;
- import android.view.MenuItem;
- import android.view.View;
- import android.widget.Button;
- import android.widget.ImageView;
- import android.widget.TextView;
- import com.squareup.picasso.Picasso;
- public class SideActivity extends Activity {
- @Override
- protected void onCreate(Bundle savedInstanceState)
- {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_side);
- Bundle b = getIntent().getExtras();
- int timesClicked = b.getInt("numberClicked");
- final TextView t = (TextView) findViewById(R.id.buttonClicked);
- t.setText(String.valueOf(timesClicked));
- Button goBack = (Button) findViewById(R.id.goback);
- goBack.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v)
- {
- Intent intent = new Intent(getApplicationContext(), MainActivity.class);
- Bundle d = new Bundle();
- d.putInt("numberClicked", Integer.parseInt(t.getText().toString()));
- intent.putExtras(d);
- startActivity(intent);
- }
- });
- ImageView pic = (ImageView) findViewById(R.id.picture2);
- Picasso.with(getApplicationContext()).load("http://upload.wikimedia.org/wikipedia/en/thumb/3/34/Android_Studio_icon.svg/768px-Android_Studio_icon.svg.png").into(pic);
- }
- @Override
- public boolean onCreateOptionsMenu(Menu menu)
- {
- // Inflate the menu; this adds items to the action bar if it is present.
- getMenuInflater().inflate(R.menu.menu_side, menu);
- return true;
- }
- @Override
- public boolean onOptionsItemSelected(MenuItem item)
- {
- // Handle action bar item clicks here. The action bar will
- // automatically handle clicks on the Home/Up button, so long
- // as you specify a parent activity in AndroidManifest.xml.
- int id = item.getItemId();
- //noinspection SimplifiableIfStatement
- if (id == R.id.action_settings)
- {
- return true;
- }
- return super.onOptionsItemSelected(item);
- }
- }
- -------------------------------------------------------------------------------------------------------------------------------
- <RelativeLayout 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:paddingLeft="@dimen/activity_horizontal_margin"
- android:paddingRight="@dimen/activity_horizontal_margin"
- android:paddingTop="@dimen/activity_vertical_margin"
- android:paddingBottom="@dimen/activity_vertical_margin"
- tools:context="counter.samkough.com.counter.SideActivity">
- <TextView
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:id="@+id/buttonClicked"
- android:layout_alignStart="@+id/picture2" />
- <Button
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="Go Back"
- android:id="@+id/goback"
- android:layout_alignParentBottom="true"
- android:layout_alignParentStart="true" />
- <ImageView
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:id="@+id/picture2"
- android:layout_above="@+id/goback"
- android:layout_centerHorizontal="true"
- android:layout_marginBottom="44dp" />
- </RelativeLayout>
- -----------------------------------------------------------------------------------------------------------------------------
- <?xml version="1.0" encoding="utf-8"?>
- <manifest xmlns:android="http://schemas.android.com/apk/res/android"
- package="counter.samkough.com.counter" >
- <application
- android:allowBackup="true"
- android:icon="@drawable/ic_launcher"
- android:label="@string/app_name"
- android:theme="@style/AppTheme" >
- <activity
- android:name=".MainActivity"
- android:label="@string/app_name" >
- <intent-filter>
- <action android:name="android.intent.action.MAIN" />
- <category android:name="android.intent.category.LAUNCHER" />
- </intent-filter>
- </activity>
- <activity
- android:name=".SideActivity"
- android:label="@string/title_activity_side" >
- </activity>
- </application>
- <uses-permission android:name="android.permission.INTERNET" />
- </manifest>
- -------------------------------------------------------------------------------------------------------------------------------
- apply plugin: 'com.android.application'
- android {
- compileSdkVersion 21
- buildToolsVersion "21.1.2"
- defaultConfig {
- applicationId "counter.samkough.com.counter"
- minSdkVersion 21
- targetSdkVersion 21
- versionCode 1
- versionName "1.0"
- }
- buildTypes {
- release {
- minifyEnabled false
- proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
- }
- }
- }
- dependencies {
- compile fileTree(dir: 'libs', include: ['*.jar'])
- compile 'com.squareup.picasso:picasso:2.5.2'
- }
Advertisement
Add Comment
Please, Sign In to add comment