Advertisement
Guest User

Untitled

a guest
Mar 23rd, 2019
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.47 KB | None | 0 0
  1. import android.content.Intent;
  2.  
  3. import android.net.Uri;
  4.  
  5. import android.support.annotation.Nullable;
  6.  
  7. import android.support.v7.app.AppCompatActivity;
  8.  
  9. import android.os.Bundle;
  10.  
  11. import android.text.Editable;
  12.  
  13. import android.text.TextWatcher;
  14.  
  15. import android.view.View;
  16.  
  17. import android.widget.EditText;
  18.  
  19. import android.widget.ImageView;
  20.  
  21. import android.widget.SeekBar;
  22.  
  23. import android.widget.TextView;
  24.  
  25.  
  26.  
  27. import java.net.URI;
  28.  
  29. import java.text.DateFormat;
  30.  
  31. import java.text.SimpleDateFormat;
  32.  
  33. import java.util.Date;
  34.  
  35.  
  36.  
  37. public class MainActivity extends AppCompatActivity {
  38.  
  39.  
  40.  
  41. private static final int IMAGE_REQUEST_CODE = 50;
  42.  
  43.  
  44.  
  45. static int nextId = 0;
  46.  
  47.  
  48.  
  49. private JournalEntry entry;
  50.  
  51.  
  52.  
  53. private TextView dateView;
  54.  
  55. private EditText entryTextView;
  56.  
  57. private SeekBar dayRatingView;
  58.  
  59. private ImageView dayImageView;
  60.  
  61.  
  62.  
  63. @Override
  64.  
  65. protected void onCreate(Bundle savedInstanceState) {
  66.  
  67. super.onCreate(savedInstanceState);
  68.  
  69. setContentView(R.layout.activity_main);
  70.  
  71.  
  72.  
  73. createJournalEntry();
  74.  
  75.  
  76.  
  77. dateView = findViewById(R.id.journal_entry_date);
  78.  
  79. dateView.setText(entry.getDate());
  80.  
  81.  
  82.  
  83. entryTextView = findViewById(R.id.journal_entry_text);
  84.  
  85. entryTextView.addTextChangedListener(new TextWatcher() {
  86.  
  87. @Override
  88.  
  89. public void beforeTextChanged(CharSequence s, int start, int count, int after) {
  90.  
  91.  
  92.  
  93. }
  94.  
  95.  
  96.  
  97. @Override
  98.  
  99. public void onTextChanged(CharSequence s, int start, int before, int count) {
  100.  
  101.  
  102.  
  103. }
  104.  
  105.  
  106.  
  107. @Override
  108.  
  109. public void afterTextChanged(Editable s) {
  110.  
  111. String entryString = s.toString();
  112.  
  113. entry.setEntryText(entryString);
  114.  
  115. }
  116.  
  117. });
  118.  
  119.  
  120.  
  121. dayRatingView = findViewById(R.id.journal_entry_seekbar);
  122.  
  123. dayRatingView.setMax(5);
  124.  
  125. dayRatingView.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
  126.  
  127. @Override
  128.  
  129. public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
  130.  
  131. if(fromUser) {
  132.  
  133. entry.setDayRating(progress);
  134.  
  135. }
  136.  
  137. }
  138.  
  139.  
  140.  
  141. @Override
  142.  
  143. public void onStartTrackingTouch(SeekBar seekBar) {
  144.  
  145.  
  146.  
  147. }
  148.  
  149.  
  150.  
  151. @Override
  152.  
  153. public void onStopTrackingTouch(SeekBar seekBar) {
  154.  
  155.  
  156.  
  157. }
  158.  
  159. });
  160.  
  161.  
  162.  
  163. dayImageView = findViewById(R.id.journal_entry_image);
  164.  
  165. findViewById(R.id.add_image_button).setOnClickListener(new View.OnClickListener() {
  166.  
  167. @Override
  168.  
  169. public void onClick(View v) {
  170.  
  171. Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
  172.  
  173. intent.setType("image/*");
  174.  
  175. startActivityForResult(intent, IMAGE_REQUEST_CODE);
  176.  
  177. }
  178.  
  179. });
  180.  
  181. }
  182.  
  183.  
  184.  
  185. private void createJournalEntry() {
  186.  
  187. entry = new JournalEntry(nextId++);
  188.  
  189.  
  190.  
  191. DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
  192.  
  193. Date date = new Date();
  194.  
  195.  
  196.  
  197. entry.setDate(dateFormat.format(date));
  198.  
  199. }
  200.  
  201.  
  202.  
  203.  
  204.  
  205. @Override
  206.  
  207. protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
  208.  
  209. if(resultCode == RESULT_OK && requestCode == IMAGE_REQUEST_CODE) {
  210.  
  211. if (data != null) {
  212.  
  213. Uri dataUri = data.getData();
  214.  
  215. entry.setImage(dataUri.toString());
  216.  
  217.  
  218.  
  219. dayImageView.setImageURI(dataUri);
  220.  
  221. }
  222.  
  223. }
  224.  
  225. }
  226.  
  227. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement