Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android - create TextView (or EditText) programmatically and set it in a specific place on the screen giving coordinates

I am building an android app where the user paints some objects in the screen. One type of object is a Text object. The user creates the object by dragging his finger and the object is shown as a rectangle that can be moved/reshaped. When user taps the text object, I launch a new activity where user enters text which I return on the onActivityResult method.

Now I want to show the text in the object. I can have access to stuff as the coordinates of the Rectangle etc from my text class. What I want to do in essence, is to create a TextView (or EditText) programmatically and set its bounds as the bounds of my rectangle that my object is painted in. Is there a method that can help me do it?

(another approach would be to use the canvas.drawTextOnPath method in my text object. But this seems more complicated since my text might get out of the object, and I would also have to handle the multilines)

Thank you very much in advance!

EDIT: trying GAMA's approach

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 show the text as expected but I do not see anything in the screen (tried to set it on the bottom left to begin with)

like image 735
george Avatar asked Dec 09 '22 23:12

george


1 Answers

                    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 );
like image 87
Sachin Gurnani Avatar answered May 14 '23 11:05

Sachin Gurnani