0

I am trying to learn android programming. I have been successful in creating screens and navigating through them. Now I am trying something adventurous.

I am creating a text editor like MS Word where you can type text and insert images from the gallery using a context menu.

enter image description here

I went through the link Get/pick an image from Android's built-in Gallery app programmatically. However I have couple of questions.

  1. Is EditText the right control for this?
  2. How do I invoke the context menu from where the cursor is?
  3. How do I paste the image where the cursor is?

Any insights to this will be of great help.

Siddharth Rout
  • 147,039
  • 17
  • 206
  • 250
  • i think you should use [ImageSpan](https://developer.android.com/reference/android/text/style/ImageSpan) to add image with text – Amrdroid Jan 28 '19 at 12:30

1 Answers1

1

Alrighty, lets tackle them one by one:

Is EditText the right control for this?

Sure, unless you want to create an application just like MS word, this will work perfectly.

something like multiple Edit Text can be used as different pages on word and you can specify a length an width with the usual XML attributes for each "page", separate them by a view to get the ending of page and start of a new page mechanism.

now to achieve an adding of a picture with the cursor, you can just create a context menu and add that image to your layout programmatically, ending your edit text, creating an image view and continuing with another edit text, lets see how this can be done:

first your context menu:

@Override
public void onCreateContextMenu(ContextMenu menu, View v,
                            ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.context_menu, menu);
if(item.getTitle().equals("Insert Image") ){

InsertImage(Uri imageUri);
      }
}

How do I invoke the context menu from where the cursor is?

How do I paste the image where the cursor is?

you need to implement something called View.OnCreateContextMenuListener which is done with a long click at wherever you press, so as you will see you will either have to implement another method to get where the cursor is and then split your edit text into two different edit text and insert your image or you will have to always have the image under the text view as your picture suggests

Secondly, when the context menu is created we can call a method call it "InsertImage" with a argument of Uri passed from the menuInflater or defined as a variable in your class to create an ImageView to be after your EditText assuming your parent layout is a linear layout :

LinearLayout LLayout = findViewById(R.id.linearLayout);

ImageView mImageView = new ImageView(this);
mImageView.setLayoutParams(new LinearLayout.LayoutParams(
                                     LinearLayout.LayoutParams.WRAP_CONTENT,
                                     LinearLayout.LayoutParams.WRAP_CONTENT));

LLayout.addView(mImageView);

And after the method "InsertImage" is done you can return a boolean to give you true if image is uploaded or if it is not, if it is, then you will create another method to start a new edit Text exactly like the imageView but if it is not uploaded then you can just call removeView same way as addView

0xA
  • 1,519
  • 1
  • 9
  • 23
  • I am slightly confused. Where does the code for ContextMenu goes? MainActivity.Java? And where and how do I call the context menu code from? – Siddharth Rout Feb 03 '19 at 15:26