• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

RecyclerView or ScrollView

Code:
public class NoteMaker extends AppCompatActivity {
    private EditText editTextField;
    boolean isEmpty;
    private String textForUpdating;
    private char imageRecognitionChar = 145;
    private SpannableStringBuilder spannableStringBuilder;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.note_maker);
        getSupportActionBar().setDefaultDisplayHomeAsUpEnabled(true);
        spannableStringBuilder = new SpannableStringBuilder();
        editTextField = findViewById(R.id.textID);
        textForUpdating = MainActivity.text;
        if (textForUpdating.equals("")) {
            isEmpty = true;
        } else isEmpty = false;
        findFileNameInText();
    }

    public void onClick(View view) {
        editTextField.setText("");
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.text, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(@NonNull MenuItem item) {
        Intent intent;
        switch (item.getItemId()) {
            case R.id.saveBtn:
                intent = new Intent();
                intent.putExtra("editTextField", editTextField.getText().toString());
                if (isEmpty) {
                    setResult(1, intent);
                } else {
                    setResult(2, intent);
                }
                finish();
                break;
            case R.id.addImage:
                intent = new Intent(getApplicationContext(), Gallery.class);
                startActivityForResult(intent, 1);
                break;
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (data != null) {
            String imageName = data.getStringExtra("image");
            addImageInEditText(getDrawable(imageName), imageName);
        }
    }

    private void addImageInEditText(Drawable drawable, String imageName) {
        imageName = imageRecognitionChar + imageName;
        drawable.setBounds(0, 0, 1000, 500);
        int selectionCursorPos = editTextField.getSelectionStart();
        editTextField.getText().insert(selectionCursorPos, imageName);
        selectionCursorPos = editTextField.getSelectionStart();
        SpannableStringBuilder builder = new SpannableStringBuilder(editTextField.getText());
        int startPos = selectionCursorPos - imageName.length();
        builder.setSpan(new ImageSpan(drawable), startPos, selectionCursorPos, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        editTextField.setText(builder);
        editTextField.setSelection(selectionCursorPos);

    }

    private Drawable getDrawable(String imageFile) {
        Drawable d = null;
        try (InputStream in = getResources().getAssets().open(imageFile)) {
            d = Drawable.createFromStream(in, null);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return d;
    }

    private void findFileNameInText() {
        if (!textForUpdating.contains("" + imageRecognitionChar)) {
            editTextField.setText(textForUpdating);
            return;
        }
        String[] array = textForUpdating.split("" + imageRecognitionChar);
        for (int i = 0; i < array.length; i++) {
            String text = array[i];
            if (!text.contains(".jpg") && !text.contains(".png")) {
                spannableStringBuilder.append(text);
                continue;
            }
            String imageFile = text.substring(0, text.indexOf(".") + 4);
            editTextField.setText(text);
            loadImageToEditTExt(getDrawable(imageFile), imageFile);
        }
        editTextField.setText(spannableStringBuilder);
    }

    private void loadImageToEditTExt(Drawable drawable, String imageFile) {
        drawable.setBounds(0, 0, 1000, 500);
        int startPos = 0;
        editTextField.getText().insert(startPos, "" + imageRecognitionChar);
        int selectionCursorPos = imageFile.length() + 1;
        SpannableStringBuilder builder = new SpannableStringBuilder(editTextField.getText());
        builder.setSpan(new ImageSpan(drawable), startPos, selectionCursorPos, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        spannableStringBuilder.append(builder);
    }

}
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
 
Last edited:
Back
Top Bottom