· 9 years ago · Nov 18, 2016, 09:42 AM
1Android domashna:
2
3// Manifest:
4<?xml version="1.0" encoding="utf-8"?>
5<manifest xmlns:android="http://schemas.android.com/apk/res/android"
6 package="com.example.user.rockpaperlizard">
7 <uses-feature android:name="android.hardware.camera" android:required="false"></uses-feature>
8 <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
9 <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
10
11 <application
12
13 android:allowBackup="true"
14 android:icon="@mipmap/ic_launcher"
15 android:label="@string/app_name"
16 android:supportsRtl="true"
17 android:theme="@style/AppTheme">
18 <activity android:name=".MenuActivity">
19 <intent-filter>
20 <action android:name="android.intent.action.MAIN" />
21
22 <category android:name="android.intent.category.LAUNCHER" />
23 </intent-filter>
24 </activity>
25
26 <activity android:name=".AddEditPlayerActivity">
27 </activity>
28 <activity android:name=".PlayerListActivity"></activity>
29 <provider
30 android:name="android.support.v4.content.FileProvider"
31 android:authorities="com.example.android.fileprovider"
32 android:exported="false"
33 android:grantUriPermissions="true">
34 <meta-data
35 android:name="android.support.FILE_PROVIDER_PATHS"
36 android:resource="@xml/file_paths"></meta-data>
37 </provider>
38 </application>
39
40</manifest>
41
42
43
44
45
46//xml/file_paths.xml
47<?xml version="1.0" encoding="utf-8"?>
48<paths xmlns:android="http://schemas.android.com/apk/res/android">
49 <external-path name="my_images" path="Android/data/com.example.user.rockpaperlizard/files/Pictures" />
50</paths>
51bachelors.fmi.uni.rockpaperlizard
52
53
54
55//AddEditPlayerActivity.java
56package com.example.user.rockpaperlizard;
57
58import android.app.Activity;
59import android.content.Intent;
60import android.graphics.Bitmap;
61import android.graphics.BitmapFactory;
62import android.graphics.drawable.Drawable;
63import android.net.Uri;
64import android.nfc.Tag;
65import android.os.Environment;
66import android.provider.MediaStore;
67import android.support.v4.content.FileProvider;
68import android.support.v7.app.AppCompatActivity;
69import android.os.Bundle;
70import android.util.Log;
71import android.view.View;
72import android.widget.Button;
73import android.widget.EditText;
74import android.widget.ImageView;
75import android.widget.RadioButton;
76import android.widget.RadioGroup;
77import android.widget.Toast;
78
79import java.io.File;
80import java.io.IOException;
81import java.text.SimpleDateFormat;
82import java.util.Date;
83
84public class AddEditPlayerActivity extends AppCompatActivity {
85
86
87 private static final String TAG = "";
88 Button cancelButton;
89 ImageView picture;
90 RadioGroup genderRadioGroup;
91 EditText firstNameEditText;
92 EditText lastNameEditText;
93 EditText emailEditText;
94 String mCurrentPhotoPath;
95 Button okButton;
96 DatabaseHelper dbHelper = new DatabaseHelper(this);
97 static final int REQUEST_IMAGE_CAPTURE = 1;
98 private Bitmap mImageBitmap;
99
100
101
102
103 @Override
104 protected void onCreate(Bundle savedInstanceState) {
105 super.onCreate(savedInstanceState);
106 setContentView(R.layout.activity_add_edit_player);
107
108 genderRadioGroup = (RadioGroup) findViewById(R.id.genderRadioGroup);
109 firstNameEditText = (EditText) findViewById(R.id.firstNameEditText);
110 lastNameEditText = (EditText) findViewById(R.id.lastNameEditText);
111 emailEditText = (EditText) findViewById(R.id.emailEditText);
112 picture = (ImageView) findViewById(R.id.avatarImageView);
113 cancelButton = (Button) findViewById(R.id.cancelButton);
114
115 okButton = (Button) findViewById(R.id.okButton);
116 okButton.setOnClickListener(onClick);
117 picture.setOnClickListener(onImageClick);
118 }
119
120 View.OnClickListener onClick = new View.OnClickListener() {
121 @Override
122 public void onClick(View view) {
123
124 Toast.makeText(AddEditPlayerActivity.this,"Player Created Successfully",Toast.LENGTH_LONG).show();
125 int radioButtonID = genderRadioGroup.getCheckedRadioButtonId();
126 RadioButton rb = (RadioButton) findViewById(radioButtonID);
127
128
129 String firstName = firstNameEditText.getText().toString();
130 String lastName = lastNameEditText.getText().toString();
131 String email = emailEditText.getText().toString();
132 String gender = rb.getText().toString();
133
134 Player player = new Player(firstName, lastName,
135 email, mCurrentPhotoPath, gender);
136 dbHelper.insertNewPlayer(player);
137
138 }
139 };
140
141 View.OnClickListener onImageClick = new View.OnClickListener() {
142 @Override
143 public void onClick(View view) {
144 Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
145 if (cameraIntent.resolveActivity(getPackageManager()) != null) {
146 // Create the File where the photo should go
147 File photoFile = null;
148 try {
149 photoFile = createImageFile();
150 } catch (IOException ex) {
151 // Error occurred while creating the File
152 Log.i(TAG, "IOException");
153 }
154 // Continue only if the File was successfully created
155 if (photoFile != null) {
156 cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(photoFile));
157 startActivityForResult(cameraIntent, REQUEST_IMAGE_CAPTURE);
158 }
159 }
160 }
161 };
162 private File createImageFile() throws IOException {
163 // Create an image file name
164 String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
165 String imageFileName = "JPEG_" + timeStamp + "_";
166 File storageDir = Environment.getExternalStoragePublicDirectory(
167 Environment.DIRECTORY_PICTURES);
168 File image = File.createTempFile(
169 imageFileName, // prefix
170 ".jpg", // suffix
171 storageDir // directory
172 );
173
174 // Save a file: path for use with ACTION_VIEW intents
175 mCurrentPhotoPath = "file:" + image.getAbsolutePath();
176 return image;
177 }
178 @Override
179 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
180 if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == RESULT_OK) {
181 try {
182 mImageBitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(), Uri.parse(mCurrentPhotoPath));
183 picture.setImageBitmap(mImageBitmap);
184 } catch (IOException e) {
185 e.printStackTrace();
186 }
187 }
188 }
189
190
191}
192
193
194
195
196
197
198
199//activity_add_edit_player.xml
200<?xml version="1.0" encoding="utf-8"?>
201
202<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
203 xmlns:tools="http://schemas.android.com/tools"
204 android:layout_width="match_parent"
205 android:layout_height="match_parent"
206 android:paddingBottom="@dimen/activity_vertical_margin"
207 android:paddingLeft="@dimen/activity_horizontal_margin"
208 android:paddingRight="@dimen/activity_horizontal_margin"
209 android:paddingTop="@dimen/activity_vertical_margin"
210 tools:context="com.example.user.rockpaperlizard.AddEditPlayerActivity"
211 android:orientation="vertical"
212 android:weightSum="10">
213
214 <ImageView
215 android:layout_width="wrap_content"
216 android:layout_height="0dp"
217 android:id="@+id/avatarImageView"
218 android:layout_gravity="center_horizontal"
219 android:src="@drawable/avatar"
220 android:layout_weight="5"
221 android:clickable="true"/>
222
223 <RadioGroup
224 android:layout_width="wrap_content"
225 android:layout_height="0dp"
226 android:layout_gravity="center_horizontal"
227 android:orientation="horizontal"
228 android:layout_weight="1"
229 android:id="@+id/genderRadioGroup">
230
231 <RadioButton
232 android:layout_width="wrap_content"
233 android:layout_height="wrap_content"
234 android:text="male"
235 android:id="@+id/maleRadioButton"
236 android:layout_gravity="center_horizontal"
237 android:checked="true" />
238
239 <RadioButton
240 android:layout_width="wrap_content"
241 android:layout_height="wrap_content"
242 android:text="female"
243 android:id="@+id/femaleRadioButton"
244 android:layout_gravity="center_horizontal"
245 android:checked="false" />
246 </RadioGroup>
247
248 <EditText
249 android:layout_width="match_parent"
250 android:layout_height="0dp"
251 android:id="@+id/firstNameEditText"
252 android:layout_weight="1"
253 android:hint="First Name" />
254
255 <EditText
256 android:layout_width="match_parent"
257 android:layout_height="0dp"
258 android:id="@+id/lastNameEditText"
259 android:layout_weight="1"
260 android:hint="LastName" />
261
262 <EditText
263 android:layout_width="match_parent"
264 android:layout_height="0dp"
265 android:inputType="textEmailAddress"
266 android:ems="10"
267 android:id="@+id/emailEditText"
268 android:layout_weight="1"
269 android:hint="Email" />
270
271 <LinearLayout
272 android:orientation="horizontal"
273 android:layout_width="match_parent"
274 android:layout_height="0dp"
275 android:layout_gravity="center_horizontal"
276 android:weightSum="2"
277 android:layout_weight="1">
278
279 <Button
280 android:layout_width="0dp"
281 android:layout_height="wrap_content"
282 android:text="OK"
283 android:id="@+id/okButton"
284 android:layout_weight="1" />
285
286 <Button
287 android:layout_width="0dp"
288 android:layout_height="wrap_content"
289 android:text="Cancel"
290 android:id="@+id/cancelButton"
291 android:layout_weight="1" />
292 </LinearLayout>
293
294</LinearLayout>
295
296
297
298
299
300
301
302
303
304//DatabaseHelper.java
305
306package com.example.user.rockpaperlizard;
307
308/**
309 * Created by User on 9.11.2016 г..
310 */
311
312import android.content.ContentValues;
313import android.content.Context;
314import android.database.Cursor;
315import android.database.SQLException;
316import android.database.sqlite.SQLiteDatabase;
317import android.database.sqlite.SQLiteOpenHelper;
318import android.util.Log;
319
320public class DatabaseHelper extends SQLiteOpenHelper{
321 private static final String LOG_TAG = "DBException";
322 SQLiteDatabase db;
323
324 public static final String DB_NAME = "rpsls.db";
325 public static final int DB_VERSION = 1;
326
327 public static final String TABLE_PLAYERS = "players";
328 public static final String PLAYERS_ID = "_id";
329 public static final String PLAYERS_FNAME = "fname";
330 public static final String PLAYERS_LNAME = "lname";
331 public static final String PLAYERS_EMAIL = "email";
332 public static final String PLAYERS_SCORE = "score";
333 public static final String PLAYERS_GENDER = "gender";
334 public static final String PLAYERS_PICTURE = "picture";
335
336 public static final String CREATE_TABLE_PLAYERS =
337 "CREATE TABLE '" + TABLE_PLAYERS + "'(" +
338 "'" + PLAYERS_ID + "' INTEGER PRIMARY KEY AUTOINCREMENT, " +
339 "'" + PLAYERS_FNAME + "' TEXT NOT NULL, " +
340 "'" + PLAYERS_LNAME + "' TEXT, " +
341 "'" + PLAYERS_EMAIL + "' TEXT NOT NULL, " +
342 "'" + PLAYERS_GENDER + "' TEXT NOT NULL, " +
343 "'" + PLAYERS_PICTURE + "' TEXT, " +
344 "'" + PLAYERS_SCORE + "' INTEGER DEFAULT 0)";
345
346
347 public DatabaseHelper(Context context) {
348 super(context, DB_NAME, null, DB_VERSION);
349 }
350
351 @Override
352 public void onCreate(SQLiteDatabase sqLiteDatabase) {
353 sqLiteDatabase.execSQL(CREATE_TABLE_PLAYERS);
354 }
355 @Override
356 public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {
357 //TODO: ЗÐПÐЗИ ДÐÐÐИТЕ!!!!!!
358 sqLiteDatabase.execSQL("DROP TABLE IF EXISTS " + TABLE_PLAYERS);
359 onCreate(sqLiteDatabase);
360 }
361
362 public void insertNewPlayer(Player player){
363 try {
364 db = this.getWritableDatabase();
365
366 ContentValues cv = new ContentValues();
367
368 cv.put(PLAYERS_FNAME, player.getFname());
369 cv.put(PLAYERS_LNAME, player.getLname());
370 cv.put(PLAYERS_GENDER, player.getGender());
371 cv.put(PLAYERS_EMAIL, player.getEmail());
372 cv.put(PLAYERS_PICTURE, player.getPicture());
373
374 db.insertOrThrow(TABLE_PLAYERS, null, cv);
375 }catch (SQLException e){
376 Log.e(LOG_TAG, e.getMessage());
377 }finally {
378 if(db != null)
379 db.close();
380 }
381 }
382
383 public Cursor getAllPlayers() {
384
385 try {
386 db = this.getReadableDatabase();
387
388 String query = "SELECT " + PLAYERS_FNAME + ","
389 + PLAYERS_LNAME + ","
390 + PLAYERS_SCORE + ","
391 + PLAYERS_ID + ","
392 + PLAYERS_PICTURE + " FROM " + TABLE_PLAYERS;
393
394 return db.rawQuery(query, null);
395
396 }catch (SQLException e){
397 Log.e(LOG_TAG, e.getMessage());
398 }
399
400 return null;
401 }
402}
403
404
405
406
407
408
409
410//PlayerListActivity.java
411
412package com.example.user.rockpaperlizard;
413
414import android.database.Cursor;
415import android.support.v7.app.AppCompatActivity;
416import android.os.Bundle;
417import android.widget.Button;
418import android.widget.CursorAdapter;
419import android.widget.ListView;
420import android.widget.SimpleCursorAdapter;
421
422
423public class PlayerListActivity extends AppCompatActivity {
424
425 ListView playersListView;
426 Button addNewPlayerButton;
427 DatabaseHelper dbHelper = new DatabaseHelper(this);
428 SimpleCursorAdapter adapter;
429 Cursor cursor=null;
430
431 String[] from = new String[]{
432 DatabaseHelper.PLAYERS_FNAME,
433 DatabaseHelper.PLAYERS_LNAME,
434 DatabaseHelper.PLAYERS_SCORE,
435 DatabaseHelper.PLAYERS_PICTURE
436 };
437
438 int[] to = new int[]{
439 R.id.firstTextView,
440 R.id.lastTextView,
441 R.id.scoreTextView,
442 R.id.avatarImageView
443 };
444
445 @Override
446 protected void onCreate(Bundle savedInstanceState) {
447 super.onCreate(savedInstanceState);
448 setContentView(R.layout.activity_player_list);
449
450 playersListView = (ListView) findViewById(R.id.playerListView);
451 addNewPlayerButton = (Button) findViewById(R.id.addButton);
452
453 cursor = dbHelper.getAllPlayers();
454
455 adapter = new SimpleCursorAdapter(this,
456 R.layout.custom_list_row,
457 cursor,
458 from,
459 to,
460 CursorAdapter.FLAG_REGISTER_CONTENT_OBSERVER);
461 playersListView.setAdapter(adapter);
462
463
464 }
465
466 @Override
467 protected void onStop() {
468 if (cursor != null) {
469 cursor.close();
470 }
471 super.onStop();
472 }
473}
474
475
476
477
478
479//activity_player_list.xml
480<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
481xmlns:tools="http://schemas.android.com/tools"
482android:layout_width="match_parent"
483android:layout_height="match_parent"
484android:paddingBottom="@dimen/activity_vertical_margin"
485android:paddingLeft="@dimen/activity_horizontal_margin"
486android:paddingRight="@dimen/activity_horizontal_margin"
487android:paddingTop="@dimen/activity_vertical_margin"
488tools:context="com.example.user.rockpaperlizard.PlayerListActivity"
489android:orientation="vertical"
490android:weightSum="10">
491
492<ListView
493 android:layout_width="match_parent"
494 android:layout_height="0dp"
495 android:id="@+id/playerListView"
496 android:layout_gravity="center_horizontal"
497 android:layout_weight="9" />
498
499<Button
500 android:layout_width="wrap_content"
501 android:layout_height="0dp"
502 android:text="Add"
503 android:id="@+id/addButton"
504 android:layout_gravity="center_horizontal"
505 android:layout_weight="1" />
506</LinearLayout>
507
508
509
510
511
512
513
514//custom_list_row.xml
515<?xml version="1.0" encoding="utf-8"?>
516<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
517 android:orientation="horizontal" android:layout_width="match_parent"
518 android:layout_height="match_parent">
519
520 <ImageView
521 android:layout_width="50dp"
522 android:layout_height="50dp"
523 android:id="@+id/avatarImageView"
524 android:src="@drawable/avatar"
525 android:layout_marginLeft="5dp"
526 android:layout_marginRight="10dp" />
527
528 <LinearLayout
529 android:orientation="vertical"
530 android:layout_width="wrap_content"
531 android:layout_height="match_parent"
532 android:layout_marginRight="10dp">
533
534 <TextView
535 android:layout_width="wrap_content"
536 android:layout_height="wrap_content"
537 android:textAppearance="?android:attr/textAppearanceLarge"
538 android:text="Large Text"
539 android:id="@+id/firstTextView" />
540
541 <TextView
542 android:layout_width="wrap_content"
543 android:layout_height="wrap_content"
544 android:textAppearance="?android:attr/textAppearanceLarge"
545 android:text="Large Text"
546 android:id="@+id/lastTextView" />
547
548 </LinearLayout>
549
550 <TextView
551 android:layout_width="wrap_content"
552 android:layout_height="wrap_content"
553 android:textAppearance="?android:attr/textAppearanceLarge"
554 android:text="Large Text"
555 android:id="@+id/scoreTextView"
556 android:layout_marginRight="10dp" />
557
558 <LinearLayout
559 android:orientation="vertical"
560 android:layout_width="match_parent"
561 android:layout_height="match_parent">
562
563 <ImageView
564 android:layout_width="25dp"
565 android:layout_height="25dp"
566 android:id="@+id/editImageView"
567 android:src="@drawable/edit" />
568
569 <ImageView
570 android:layout_width="25dp"
571 android:layout_height="25dp"
572 android:id="@+id/deleteImageView"
573 android:src="@drawable/delete" />
574 </LinearLayout>
575</LinearLayout>
576
577
578
579
580
581
582//Player.java
583package com.example.user.rockpaperlizard;
584
585/**
586 * Created by User on 9.11.2016 г..
587 */
588
589public class Player {
590 private int id;
591 private String fname;
592 private String lname;
593 private String email;
594 private String picture;
595 private String gender;
596 private int score;
597
598 public Player(){
599
600 }
601
602 public Player(String fname, String lname, String email, String picture, String gender) {
603 this.fname = fname;
604 this.lname = lname;
605 this.email = email;
606 this.picture = picture;
607 this.gender = gender;
608 }
609
610 public int getId() {
611 return id;
612 }
613
614 public void setId(int id) {
615 this.id = id;
616 }
617
618 public String getFname() {
619 return fname;
620 }
621
622 public void setFname(String fname) {
623 this.fname = fname;
624 }
625
626 public String getLname() {
627 return lname;
628 }
629
630 public void setLname(String lname) {
631 this.lname = lname;
632 }
633
634 public String getEmail() {
635 return email;
636 }
637
638 public void setEmail(String email) {
639 this.email = email;
640 }
641
642 public String getPicture() {
643 return picture;
644 }
645
646 public void setPicture(String picture) {
647 this.picture = picture;
648 }
649
650 public String getGender() {
651 return gender;
652 }
653
654 public void setGender(String gender) {
655 this.gender = gender;
656 }
657
658 public int getScore() {
659 return score;
660 }
661
662 public void setScore(int score) {
663 this.score = score;
664 }
665}
666
667
668
669
670
671
672//MenuActivity.java
673
674package com.example.user.rockpaperlizard;
675
676import android.content.Intent;
677import android.support.v7.app.AppCompatActivity;
678import android.os.Bundle;
679import android.view.View;
680import android.widget.Button;
681
682public class MenuActivity extends AppCompatActivity {
683
684 Button addEditButton;
685 Button showPlayersButton;
686
687 @Override
688 protected void onCreate(Bundle savedInstanceState) {
689 super.onCreate(savedInstanceState);
690 setContentView(R.layout.activity_menu);
691 addEditButton = (Button) findViewById(R.id.addEditButton);
692 showPlayersButton = (Button) findViewById(R.id.showPlayersButton);
693
694 addEditButton.setOnClickListener(onClick);
695 showPlayersButton.setOnClickListener(onClick);
696 }
697
698 View.OnClickListener onClick = new View.OnClickListener() {
699 @Override
700 public void onClick(View view) {
701 Intent intent = new Intent();
702 switch (view.getId()){
703 case R.id.addEditButton:
704 intent = new Intent(MenuActivity.this,
705 AddEditPlayerActivity.class);
706 break;
707 case R.id.showPlayersButton:
708 intent = new Intent(MenuActivity.this,
709 PlayerListActivity.class);
710 break;
711 }
712 startActivity(intent);
713 }
714 };
715}
716
717
718
719
720
721
722//activity_menu.xml
723
724
725<?xml version="1.0" encoding="utf-8"?>
726<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
727xmlns:tools="http://schemas.android.com/tools"
728android:layout_width="match_parent"
729android:layout_height="match_parent"
730android:paddingBottom="@dimen/activity_vertical_margin"
731android:paddingLeft="@dimen/activity_horizontal_margin"
732android:paddingRight="@dimen/activity_horizontal_margin"
733android:paddingTop="@dimen/activity_vertical_margin"
734tools:context="com.example.user.rockpaperlizard.MenuActivity">
735
736<Button
737 android:layout_width="wrap_content"
738 android:layout_height="wrap_content"
739 android:text="Edit User"
740 android:id="@+id/addEditButton"
741 android:layout_alignParentTop="true"
742 android:layout_centerHorizontal="true" />
743
744<Button
745 android:layout_width="wrap_content"
746 android:layout_height="wrap_content"
747 android:text="Players"
748 android:id="@+id/showPlayersButton"
749 android:layout_below="@+id/addEditButton"
750 android:layout_centerHorizontal="true"
751 android:layout_marginTop="61dp" />
752</RelativeLayout>