Search code examples
androidtextviewgenerated

How to Control Programmatically Generated TextViews


I appreciate your help if someone can provide me with a solution. I have two question:

1- I have a button that generates Params and TextView every time I click it. I set variables to textview "textnew" and param "lptxt". However, I only can control the last generated textview, typeface, font size, removing..etc. Is there a method to set automatic id and I can recall it in other methods? I tried isSelected, isTouched, hasFocus, and others but nothing worked.

2- How can I set my TextView back to the way it was after setting borders? Let's say I have set borders to the TextView of 10dp radius and a red color. but when touching the TextView, the borders change to 1dp radius as stated in drawable/corners.

Thank you

Here's my code

RelativeLayout rel0 = (RelativeLayout) findViewById(R.id.rel0);

OnClick:

textnew = new TextView(MainActivity.this);
    lptxt = new RelativeLayout.LayoutParams(
                                    RelativeLayout.LayoutParams.WRAP_CONTENT,
                                    RelativeLayout.LayoutParams.WRAP_CONTENT);
                            lptxt.addRule((RelativeLayout.CENTER_HORIZONTAL));
                            lptxt.addRule((RelativeLayout.CENTER_VERTICAL));
                            lptxt.addRule((RelativeLayout.CENTER_IN_PARENT));
                            textnew.setLayoutParams(lptxt);
                            rel0.addView(textnew);
                            rel0.bringChildToFront(textnew);
                            rel0.bringToFront();
                            textnew.setText(edittextdialog.getText().toString());

OnTouch:

case MotionEvent.ACTION_DOWN: {
                        textnew.setBackgroundResource(R.drawable.borders);
                        textnew.isSelected();

                        break;
                    }

BTW I'm working on API 11

Thank you in advance,


Solution

  • I found a workaround to this problem:

    I inflated a view on ACTION_UP, where I can change text size, color, font, and even delete it. The changes happen directly on the selected textview with problem.

    Thanx guys for contributing.

    Here's my code:

        case MotionEvent.ACTION_UP: {
    
        LayoutInflater lifirst = LayoutInflater.from(MainActivity.this);
        View lifirstpv = lifirst.inflate(R.layout.infirst, null);
        final TextView editfirst = TextView)lifirstpv.findViewById(R.id.editfirst);
        final TextView deletefirst = (TextView)lifirstpv.findViewById(R.id.deletefirst);
    
        pop = new PopupWindow(
    lifirstpv,RelativeLayout.LayoutParams.WRAP_CONTENT,
    RelativeLayout.LayoutParams.WRAP_CONTENT);
    
        pop.setOutsideTouchable(true); pop.setTouchable(true);
        deletefirst.setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
        rel0.removeView(teext);
        pop.dismiss();
        return true;
                       }
              });