Skip to content Skip to sidebar Skip to footer

How To Get The Id Of A Dynamically Created Textview?

I'm trying to dynamically add and remove TextView for an android app I'm making but I'm running into difficulty setting and getting the TextView's id. I seem to be getting null poi

Solution 1:

The TextView doesn't contain all the children in the layout, the LinearLayout does. Make linlfinal, then you can use it in nTV's OnClickListener (As long as the view contains the layout. If not, you're going to need to make some decisions about what exactly you want to achieve - making a reference to the layout earlier on might work).

finalLinearLayoutlinl= (LinearLayout) view.findViewById(R.id.linear_layout_tags);
linl.addView(nTv);

nTv.setOnClickListener(newView.OnClickListener() {         
    publicvoidonClick(View v) {
        EditTextet= (EditText) linl.findViewById(R.id.edittext_tags);
        TextViewt= ((TextView)v);
        et.setText(t.getText().toString());
        linl.removeView(v);
    }

With your approach the id is not really needed, since you always have a reference to the View.

However, if you wanted to work with views (very redundant example, but it's for the sake of explanation):

nTv.setId(tag_id);
linl.addView(nTv);

TextView duplicateTextViewReference (TextView) linl.findViewById (tag_id);

Post a Comment for "How To Get The Id Of A Dynamically Created Textview?"