1. Are you ready for the Galaxy S20? Here is everything we know so far!

RecyclerView or ScrollView

Discussion in 'Android Development' started by Artem Polovin, Mar 28, 2020.

  1. Artem Polovin

    Artem Polovin Lurker
    Thread Starter

    Code (Text):
    1. public class NoteMaker extends AppCompatActivity {
    2.     private EditText editTextField;
    3.     boolean isEmpty;
    4.     private String textForUpdating;
    5.     private char imageRecognitionChar = 145;
    6.     private SpannableStringBuilder spannableStringBuilder;
    7.  
    8.     @Override
    9.     protected void onCreate(Bundle savedInstanceState) {
    10.         super.onCreate(savedInstanceState);
    11.         setContentView(R.layout.note_maker);
    12.         getSupportActionBar().setDefaultDisplayHomeAsUpEnabled(true);
    13.         spannableStringBuilder = new SpannableStringBuilder();
    14.         editTextField = findViewById(R.id.textID);
    15.         textForUpdating = MainActivity.text;
    16.         if (textForUpdating.equals("")) {
    17.             isEmpty = true;
    18.         } else isEmpty = false;
    19.         findFileNameInText();
    20.     }
    21.  
    22.     public void onClick(View view) {
    23.         editTextField.setText("");
    24.     }
    25.  
    26.  
    27.     @Override
    28.     public boolean onCreateOptionsMenu(Menu menu) {
    29.         getMenuInflater().inflate(R.menu.text, menu);
    30.         return true;
    31.     }
    32.  
    33.     @Override
    34.     public boolean onOptionsItemSelected(@NonNull MenuItem item) {
    35.         Intent intent;
    36.         switch (item.getItemId()) {
    37.             case R.id.saveBtn:
    38.                 intent = new Intent();
    39.                 intent.putExtra("editTextField", editTextField.getText().toString());
    40.                 if (isEmpty) {
    41.                     setResult(1, intent);
    42.                 } else {
    43.                     setResult(2, intent);
    44.                 }
    45.                 finish();
    46.                 break;
    47.             case R.id.addImage:
    48.                 intent = new Intent(getApplicationContext(), Gallery.class);
    49.                 startActivityForResult(intent, 1);
    50.                 break;
    51.         }
    52.         return super.onOptionsItemSelected(item);
    53.     }
    54.  
    55.     @Override
    56.     protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
    57.         super.onActivityResult(requestCode, resultCode, data);
    58.         if (data != null) {
    59.             String imageName = data.getStringExtra("image");
    60.             addImageInEditText(getDrawable(imageName), imageName);
    61.         }
    62.     }
    63.  
    64.     private void addImageInEditText(Drawable drawable, String imageName) {
    65.         imageName = imageRecognitionChar + imageName;
    66.         drawable.setBounds(0, 0, 1000, 500);
    67.         int selectionCursorPos = editTextField.getSelectionStart();
    68.         editTextField.getText().insert(selectionCursorPos, imageName);
    69.         selectionCursorPos = editTextField.getSelectionStart();
    70.         SpannableStringBuilder builder = new SpannableStringBuilder(editTextField.getText());
    71.         int startPos = selectionCursorPos - imageName.length();
    72.         builder.setSpan(new ImageSpan(drawable), startPos, selectionCursorPos, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
    73.         editTextField.setText(builder);
    74.         editTextField.setSelection(selectionCursorPos);
    75.  
    76.     }
    77.  
    78.     private Drawable getDrawable(String imageFile) {
    79.         Drawable d = null;
    80.         try (InputStream in = getResources().getAssets().open(imageFile)) {
    81.             d = Drawable.createFromStream(in, null);
    82.         } catch (IOException e) {
    83.             e.printStackTrace();
    84.         }
    85.         return d;
    86.     }
    87.  
    88.     private void findFileNameInText() {
    89.         if (!textForUpdating.contains("" + imageRecognitionChar)) {
    90.             editTextField.setText(textForUpdating);
    91.             return;
    92.         }
    93.         String[] array = textForUpdating.split("" + imageRecognitionChar);
    94.         for (int i = 0; i < array.length; i++) {
    95.             String text = array[i];
    96.             if (!text.contains(".jpg") && !text.contains(".png")) {
    97.                 spannableStringBuilder.append(text);
    98.                 continue;
    99.             }
    100.             String imageFile = text.substring(0, text.indexOf(".") + 4);
    101.             editTextField.setText(text);
    102.             loadImageToEditTExt(getDrawable(imageFile), imageFile);
    103.         }
    104.         editTextField.setText(spannableStringBuilder);
    105.     }
    106.  
    107.     private void loadImageToEditTExt(Drawable drawable, String imageFile) {
    108.         drawable.setBounds(0, 0, 1000, 500);
    109.         int startPos = 0;
    110.         editTextField.getText().insert(startPos, "" + imageRecognitionChar);
    111.         int selectionCursorPos = imageFile.length() + 1;
    112.         SpannableStringBuilder builder = new SpannableStringBuilder(editTextField.getText());
    113.         builder.setSpan(new ImageSpan(drawable), startPos, selectionCursorPos, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
    114.         spannableStringBuilder.append(builder);
    115.     }
    116.  
    117. }
    118.  
    Hello everyone. I am going to make a notes app and I want to add to note images ,videos. But I do not know what better to choose for that, RecyclerView or just create one editText field, add Scrollview and use SpannableStringBuilder. I tried two ways and in both ways I had problems. Now I made notes app by SpannableStringBuilder and in my note I can add only text and image. I keep notes in SQLite. Every thing works good but I have a one problem. I do not know how to add one more element to my note , for exemple video. If somebody has done it before please help me. I need to know with what better way to do it.
    Thank you
     


    #1 Artem Polovin, Mar 28, 2020
    Last edited: Mar 28, 2020

    1. Download the Forums for Android™ app!


      Download

       
Loading...
Similar Threads - RecyclerView ScrollView
  1. rowlanda38
    Replies:
    0
    Views:
    180
  2. shubham1835
    Replies:
    1
    Views:
    785
  3. Dnlaua
    Replies:
    0
    Views:
    774
  4. Nitrogen247
    Replies:
    0
    Views:
    858
  5. GotParty
    Replies:
    4
    Views:
    1,590
  6. Artem Polovin
    Replies:
    0
    Views:
    1,437
  7. Hardik Thummar
    Replies:
    0
    Views:
    959
  8. umtblbl
    Replies:
    3
    Views:
    513
  9. umtblbl
    Replies:
    9
    Views:
    2,926
  10. umtblbl
    Replies:
    0
    Views:
    835

Share This Page

Loading...