Crenox

Counter Android Studio Program

May 1st, 2015
467
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.99 KB | None | 0 0
  1. OBJECTIVES/HOW IT WORKS:
  2. - Checks how many times you've clicked within a certain time allotment.
  3. -------------------------------------------------------------------------------------------------------------------------------
  4. package counter.samkough.com.counter;
  5.  
  6. import android.app.Activity;
  7. import android.content.Intent;
  8. import android.os.Bundle;
  9. import android.view.Menu;
  10. import android.view.MenuItem;
  11. import android.view.View;
  12. import android.widget.Button;
  13. import android.widget.ImageView;
  14. import android.widget.TextView;
  15.  
  16. import com.squareup.picasso.Picasso;
  17.  
  18.  
  19. public class MainActivity extends Activity
  20. {
  21. @Override
  22. protected void onCreate(Bundle savedInstanceState) {
  23. super.onCreate(savedInstanceState);
  24. setContentView(R.layout.activity_main);
  25.  
  26. Button button = (Button) findViewById(R.id.button);
  27. final TextView counter = (TextView) findViewById(R.id.count);
  28.  
  29. button.setOnClickListener(new View.OnClickListener()
  30. {
  31. @Override
  32. public void onClick(View v)
  33. {
  34. int i = Integer.parseInt(counter.getText().toString());
  35. i++;
  36. counter.setText(String.valueOf(i));
  37. }
  38. });
  39. Button switchButton = (Button) findViewById(R.id.switchButton);
  40.  
  41. switchButton.setOnClickListener(new View.OnClickListener()
  42. {
  43. @Override
  44. public void onClick(View v)
  45. {
  46. // creating the intent you need the context, then the activity you want to switch to
  47. Intent intent = new Intent(getApplicationContext(), SideActivity.class);
  48. // creates Bundle to send data to other class
  49. Bundle b = new Bundle();
  50. // gets the number of times you've clicked the button
  51. // first parameter is the name you assign it, and then the other is the actual number
  52. b.putInt("numberClicked", Integer.parseInt(counter.getText().toString()));
  53. // combining the intent and bundle together
  54. intent.putExtras(b);
  55. // starts the intent
  56. startActivity(intent);
  57. }
  58. });
  59.  
  60. Button buttonReset = (Button) findViewById(R.id.resetButton);
  61.  
  62. buttonReset.setOnClickListener(new View.OnClickListener() {
  63. @Override
  64. public void onClick(View v) {
  65. counter.setText(String.valueOf(0));
  66. }
  67. });
  68.  
  69. ImageView pic = (ImageView) findViewById(R.id.picture);
  70.  
  71. Picasso.with(getApplicationContext()).load("http://i.imgur.com/knTN4.jpg").into(pic);
  72.  
  73. }
  74.  
  75. @Override
  76. public boolean onCreateOptionsMenu(Menu menu)
  77. {
  78. // Inflate the menu; this adds items to the action bar if it is present.
  79. getMenuInflater().inflate(R.menu.menu_main, menu);
  80. return true;
  81. }
  82.  
  83. @Override
  84. public boolean onOptionsItemSelected(MenuItem item)
  85. {
  86. // Handle action bar item clicks here. The action bar will
  87. // automatically handle clicks on the Home/Up button, so long
  88. // as you specify a parent activity in AndroidManifest.xml.
  89. int id = item.getItemId();
  90.  
  91. //noinspection SimplifiableIfStatement
  92. if (id == R.id.action_settings)
  93. {
  94. return true;
  95. }
  96.  
  97. return super.onOptionsItemSelected(item);
  98. }
  99. }
  100. -------------------------------------------------------------------------------------------------------------------------------
  101. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  102. xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
  103. android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
  104. android:paddingRight="@dimen/activity_horizontal_margin"
  105. android:paddingTop="@dimen/activity_vertical_margin"
  106. android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">
  107.  
  108. <Button
  109. style="?android:attr/buttonStyleSmall"
  110. android:layout_width="wrap_content"
  111. android:layout_height="wrap_content"
  112. android:text="Click Me!"
  113. android:id="@+id/button"
  114. android:layout_below="@+id/count"
  115. android:layout_centerHorizontal="true" />
  116.  
  117. <TextView
  118. android:layout_width="wrap_content"
  119. android:layout_height="wrap_content"
  120. android:text="0"
  121. android:id="@+id/count"
  122. android:layout_alignParentTop="true"
  123. android:layout_centerHorizontal="true"
  124. android:layout_marginTop="48dp" />
  125.  
  126. <Button
  127. android:layout_width="wrap_content"
  128. android:layout_height="wrap_content"
  129. android:text="Switch"
  130. android:id="@+id/switchButton"
  131. android:layout_alignParentBottom="true"
  132. android:layout_alignParentEnd="true" />
  133.  
  134. <Button
  135. android:layout_width="wrap_content"
  136. android:layout_height="wrap_content"
  137. android:text="Reset"
  138. android:id="@+id/resetButton"
  139. android:layout_alignParentBottom="true"
  140. android:layout_alignParentStart="true" />
  141.  
  142. <ImageView
  143. android:layout_width="wrap_content"
  144. android:layout_height="wrap_content"
  145. android:id="@+id/picture"
  146. android:layout_centerVertical="true"
  147. android:layout_centerHorizontal="true" />
  148.  
  149. </RelativeLayout>
  150. -------------------------------------------------------------------------------------------------------------------------------
  151. package counter.samkough.com.counter;
  152.  
  153. import android.app.Activity;
  154. import android.content.Intent;
  155. import android.os.Bundle;
  156. import android.view.Menu;
  157. import android.view.MenuItem;
  158. import android.view.View;
  159. import android.widget.Button;
  160. import android.widget.ImageView;
  161. import android.widget.TextView;
  162.  
  163. import com.squareup.picasso.Picasso;
  164.  
  165.  
  166. public class SideActivity extends Activity {
  167.  
  168. @Override
  169. protected void onCreate(Bundle savedInstanceState)
  170. {
  171. super.onCreate(savedInstanceState);
  172. setContentView(R.layout.activity_side);
  173.  
  174. Bundle b = getIntent().getExtras();
  175. int timesClicked = b.getInt("numberClicked");
  176.  
  177. final TextView t = (TextView) findViewById(R.id.buttonClicked);
  178. t.setText(String.valueOf(timesClicked));
  179.  
  180. Button goBack = (Button) findViewById(R.id.goback);
  181.  
  182. goBack.setOnClickListener(new View.OnClickListener() {
  183. @Override
  184. public void onClick(View v)
  185. {
  186. Intent intent = new Intent(getApplicationContext(), MainActivity.class);
  187.  
  188. Bundle d = new Bundle();
  189.  
  190. d.putInt("numberClicked", Integer.parseInt(t.getText().toString()));
  191.  
  192. intent.putExtras(d);
  193.  
  194. startActivity(intent);
  195. }
  196. });
  197.  
  198. ImageView pic = (ImageView) findViewById(R.id.picture2);
  199.  
  200. 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);
  201. }
  202.  
  203. @Override
  204. public boolean onCreateOptionsMenu(Menu menu)
  205. {
  206. // Inflate the menu; this adds items to the action bar if it is present.
  207. getMenuInflater().inflate(R.menu.menu_side, menu);
  208. return true;
  209. }
  210.  
  211. @Override
  212. public boolean onOptionsItemSelected(MenuItem item)
  213. {
  214. // Handle action bar item clicks here. The action bar will
  215. // automatically handle clicks on the Home/Up button, so long
  216. // as you specify a parent activity in AndroidManifest.xml.
  217. int id = item.getItemId();
  218.  
  219. //noinspection SimplifiableIfStatement
  220. if (id == R.id.action_settings)
  221. {
  222. return true;
  223. }
  224.  
  225. return super.onOptionsItemSelected(item);
  226. }
  227. }
  228. -------------------------------------------------------------------------------------------------------------------------------
  229. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  230. xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
  231. android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
  232. android:paddingRight="@dimen/activity_horizontal_margin"
  233. android:paddingTop="@dimen/activity_vertical_margin"
  234. android:paddingBottom="@dimen/activity_vertical_margin"
  235. tools:context="counter.samkough.com.counter.SideActivity">
  236.  
  237. <TextView
  238. android:layout_width="wrap_content"
  239. android:layout_height="wrap_content"
  240. android:id="@+id/buttonClicked"
  241. android:layout_alignStart="@+id/picture2" />
  242.  
  243. <Button
  244. android:layout_width="wrap_content"
  245. android:layout_height="wrap_content"
  246. android:text="Go Back"
  247. android:id="@+id/goback"
  248. android:layout_alignParentBottom="true"
  249. android:layout_alignParentStart="true" />
  250.  
  251. <ImageView
  252. android:layout_width="wrap_content"
  253. android:layout_height="wrap_content"
  254. android:id="@+id/picture2"
  255. android:layout_above="@+id/goback"
  256. android:layout_centerHorizontal="true"
  257. android:layout_marginBottom="44dp" />
  258.  
  259. </RelativeLayout>
  260. -----------------------------------------------------------------------------------------------------------------------------
  261. <?xml version="1.0" encoding="utf-8"?>
  262. <manifest xmlns:android="http://schemas.android.com/apk/res/android"
  263. package="counter.samkough.com.counter" >
  264.  
  265. <application
  266. android:allowBackup="true"
  267. android:icon="@drawable/ic_launcher"
  268. android:label="@string/app_name"
  269. android:theme="@style/AppTheme" >
  270. <activity
  271. android:name=".MainActivity"
  272. android:label="@string/app_name" >
  273. <intent-filter>
  274. <action android:name="android.intent.action.MAIN" />
  275.  
  276. <category android:name="android.intent.category.LAUNCHER" />
  277. </intent-filter>
  278. </activity>
  279. <activity
  280. android:name=".SideActivity"
  281. android:label="@string/title_activity_side" >
  282. </activity>
  283. </application>
  284.  
  285. <uses-permission android:name="android.permission.INTERNET" />
  286.  
  287. </manifest>
  288. -------------------------------------------------------------------------------------------------------------------------------
  289. apply plugin: 'com.android.application'
  290.  
  291. android {
  292. compileSdkVersion 21
  293. buildToolsVersion "21.1.2"
  294.  
  295. defaultConfig {
  296. applicationId "counter.samkough.com.counter"
  297. minSdkVersion 21
  298. targetSdkVersion 21
  299. versionCode 1
  300. versionName "1.0"
  301. }
  302. buildTypes {
  303. release {
  304. minifyEnabled false
  305. proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
  306. }
  307. }
  308. }
  309.  
  310. dependencies {
  311. compile fileTree(dir: 'libs', include: ['*.jar'])
  312. compile 'com.squareup.picasso:picasso:2.5.2'
  313. }
Advertisement
Add Comment
Please, Sign In to add comment