-4

I am developing meme create application and generating text view Programmatically. If i generate 2 text views programmatically always i can get text of last generated text view. I can't get text of previous generated text view. How can I get text of which I on click text view .

// Create TextView programmatically.

textView = new StickerTextView(MainActivity.this);
textView.setText(edt.getText().toString());
textView.setTextSize(1000);

textView.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {

        if (popupWindow !=null)
        {
            popupclose();
        }

        text = "";

        text = textView.getText().toString();
            popupwindow(v);
    }
});

public void popupwindow(View v) {

    FrameLayout fl = (FrameLayout) findViewById(R.id.canvasView);

    LayoutInflater layoutInflater = (LayoutInflater)getBaseContext().getSystemService(LAYOUT_INFLATER_SERVICE);
    popupView = layoutInflater.inflate(R.layout.my_action2,null);

    popupWindow = new PopupWindow(popupView, RelativeLayout.LayoutParams.WRAP_CONTENT,RelativeLayout.LayoutParams.WRAP_CONTENT);
    ImageButton btnClose = (ImageButton)popupView.findViewById(R.id.imageView_close);

    btnClose.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            //popupWindow.dismiss();
            popupclose();
        }
    });

    Button btntext = (Button)popupView.findViewById(R.id.btnone);

    btntext.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

            showEditDialog1();


        }
    });

 public void showEditDialog1() {

        AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(this, R.style.MyDialogTheme);
        LayoutInflater inflater = this.getLayoutInflater();
        final View dialogView = inflater.inflate(R.layout.custom_dialog, null);
        dialogBuilder.setView(dialogView);

        windowManager = (WindowManager) getSystemService(WINDOW_SERVICE);
        edt1 = (EditText) dialogView.findViewById(R.id.edit1);
        edt1.setText(text);

        dialogBuilder.setTitle(Html.fromHtml("<font color='#FFFFFF'>EDIT TEXT </font>"));
        //dialogBuilder.setMessage("Enter text below");
        dialogBuilder.setPositiveButton(Html.fromHtml("<font color='#FFFFFF'>Done</font>"), new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int whichButton) {


        // Create TextView programmatically.
        textView.setText("");
        textView.setText(edt1.getText().toString());

        textView.setTextSize(1000);


        textView.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View view) {

                return false;
            }
        });
        textView.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent me) {

                if (me.getAction() == MotionEvent.ACTION_DOWN) {
                    status = START_DRAGGING;
                    final float x = me.getX();
                    final float y = me.getY();
                    lastXAxis = x;
                    lastYAxis = y;
                    v.setVisibility(View.INVISIBLE);
                } else if (me.getAction() == MotionEvent.ACTION_UP) {
                    status = STOP_DRAGGING;
                    flag = 0;
                    v.setVisibility(View.VISIBLE);
                } else if (me.getAction() == MotionEvent.ACTION_MOVE) {
                    if (status == START_DRAGGING) {
                        flag = 1;
                        v.setVisibility(View.VISIBLE);
                        final float x = me.getX();
                        final float y = me.getY();
                        final float dx = x - lastXAxis;
                        final float dy = y - lastYAxis;
                        xAxis += dx;
                        yAxis += dy;
                        v.setX((int) xAxis);
                        v.setY((int) yAxis);
                        v.invalidate();
                    }
                }
                return false;
            }
        });

        // Add TextView to LinearLayout
        if (canvas != null && textView == null) {
            canvas.addView(textView);
        }

        hidekeypad(MainActivity.this);
    }
});
4
  • You are using same instance variable for TextView each time this will override the last one . Commented Feb 17, 2018 at 6:16
  • can u give me sample code Commented Feb 17, 2018 at 6:18
  • stackoverflow.com/questions/27989079/… Commented Feb 17, 2018 at 6:18
  • Khein Htoo if i update text of previus text view is updating last generated textb view please help me Commented Feb 17, 2018 at 6:42

1 Answer 1

1

You should get the value from argument(view v) which is passed via onClick override function of onClickLisenter interface. And because you are using StickerTextView then:

@Override
                public void onClick(View v) {

                    if (popupWindow !=null)
                    {
                        popupclose();
                    }

                    text = "";

                    text = ((StickerTextView)v).getText().toString();


                        popupwindow(v);



                }
Sign up to request clarification or add additional context in comments.

3 Comments

if i update text of previews text view is updating last generated text view please help me –
Dear Sir How can Update Text of which Textview I get text
argument(v) is the text view that you clicked, which means that you can cast it to text view and update it using setText().

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.