Android - create a TextView (or EditText) programmatically and set it to a specific location on the screen, giving the coordinates

I am creating an Android application where the user draws some objects on the screen. One type of object is a Text object. The user creates an object by dragging his finger, and the object is displayed as a rectangle that can be moved / changed. When the user deletes the text object, I start a new action when the user enters the text, which I return using the onActivityResult method.

Now I want to show the text in the object. I can access materials like Rectangle coordinates, etc. From my text class. What I want to do essentially is to create a TextView (or EditText) programmatically and set its borders as the borders of my rectangle into which my object was drawn. Is there any way that can help me?

(another approach is to use the canvas.drawTextOnPath method in my text object, but this seems more complicated as my text can exit the object and I will also have to process multi-line lines)

Thank you in advance!

EDIT: attempt to use GAMA

protected void onActivityResult(int requestCode, int resultCode, Intent data) { switch(requestCode) { case 1: if (resultCode == Activity.RESULT_OK) { String text=data.getStringExtra("text"); System.out.println(text); TextView tv=new TextView(this); //LayoutParams lp = new LayoutParams(new ViewGroup.MarginLayoutParams((int)texts.get(index).width,(int)texts.get(index).height)); LayoutParams lp = new LayoutParams(new ViewGroup.MarginLayoutParams(100,100)); //tv.setLayoutParams(lp); //lp.setMargins((int)texts.get(index).Sx, (int)texts.get(index).Sy, (int)texts.get(index).Lx, (int)texts.get(index).Ly); tv.setLayoutParams(lp); tv.setTextSize(10); tv.setTextColor(Color.RED); tv.setText(text); lp.setMargins(0,0,0,0); //tv.setVisibility(View.VISIBLE); System.out.println("got "+tv.getText()); } break; } } 

both prints display the text as expected, but I can’t see anything on the screen (first I tried to set it in the lower left corner)

+4
source share
3 answers

Actually, drawTextOnPath is your best bet. He will not bleed. All you have to do is create a path going from the left, central vertical of your rectangle to the right central vertical. The method will take care of resizing the text so that it remains within the path.

You can customize the path using Paint.getTextWidth() . If the width is larger than the field, expand your path with the line Paint.getTextHeight() below the first line.

+3
source
  EditText edText = new EditText(this); edText .setId(i); edText .setLayoutParams(new LinearLayout.LayoutParams( LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, 1f)); edText .setWidth(100); edText .setImeOptions(EditorInfo.IME_ACTION_NEXT); edText .setInputType(InputType.TYPE_CLASS_NUMBER); edText .setKeyListener(DigitsKeyListener.getInstance()); edText .setMaxLines(1); edText .setOnFocusChangeListener(this); edText .setOnEditorActionListener(this); edText .addTextChangedListener(this); //this linearlayout id is declared inside your xml file LinearLayout linear=(LinearLayout)findViewById(R.id.linearLayout1); linear.addView(edText ); 
+10
source

Try the following:

 TextView tv=new TextView(this); LayoutParams lp = new LayoutParams(new ViewGroup.MarginLayoutParams(width,height)); tv.setLayoutParams(lp); lp.setMargins(0, 0, 0, 0); 
+1
source

Source: https://habr.com/ru/post/1416111/


All Articles