Search code examples
androidandroid-layoutonactivityresult

Identify a specific dynamically generated TextView in order to setText


I have a function that generates a specific number of TextViews dynamically.

    TextView tv;
    EditText et;

    public TextView textViewGenerate(final Activity activity, String tag, Integer id) {

        tv = new TextView(activity);

        GradientDrawable gd = new GradientDrawable();
        gd.setColor(0xFFFFFF);
        gd.setCornerRadius(4);
        gd.setStroke(1, 0xFF757575);

        tv.setBackground(gd);

        LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
                LinearLayout.LayoutParams.MATCH_PARENT,
                LinearLayout.LayoutParams.WRAP_CONTENT,
                1.0f
        );

        tv.setPadding(7, 9, 0, 0);
        tv.setGravity(Gravity.START);
        tv.setTextSize(22);

        tv.setTag(tag);
        tv.setId(id);
        tv.setHint("Enter Module Serial Number");

        //Click to launch camera
        tv.setClickable(true);
        tv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //Intent intent = new Intent(ct, MctCameraActivity.class);
                Intent intent = new Intent(activity, MctCameraActivity.class);
                activity.startActivityForResult(intent, 1);
                //ct.startActivity(intent);
                //ToDo: finish receiving the data from the activityForResult
            }
        });

        lp.setMargins(10, 0, 10, 0);
        lp.setMarginStart(10);
        lp.setMarginEnd(10);

        tv.setLayoutParams(lp);

        return tv;
    }

}

The onClick launches another activity that loads the camera in order to capture barcode data. In the main activity I create the TextViews with

        final LayoutElements le = new LayoutElements();
        mainLayout = (LinearLayout)findViewById(R.id.mctScanPageMain);

        for(int i = 0; i < 3; i++) {
            mainLayout.addView(le.textViewGenerate(this, "NewID" + i, i));
        }

The onActivityResult returns the value from the camera activity but the issue is that it sets every generated TextView to have the same barcode number (the barcode number is returned as a string). How can I modify this so that only the text of the selected TextView gets changed in the onActivityResult?

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if(requestCode == 1 && resultCode == RESULT_OK && data != null) {
            String returnResult = data.getStringExtra("result");

            for (int i=0; i < mainLayout.getChildCount(); i++){
                TextView tv = (TextView) mainLayout.findViewWithTag("NewID"+i);
                if(tv == null) {
                    break;
                } else {
                    tv.setText(returnResult);
                }
            }
        }
    }

Solution

  • In your click listener of TextView you can change:

    Intent intent = new Intent(activity, MctCameraActivity.class);
    activity.startActivityForResult(intent, 1);
    

    to

    Intent intent = new Intent(activity, MctCameraActivity.class);
    intent.putExtra("text_view_tag", tag);
    activity.startActivityForResult(intent, 1);
    

    And then sent it back in result of MctCameraActivity, so you can use it in onActivityResult with getStringExtra. This way you will know tag of TextView which started activity.

    Alternatively you can use requestCode that you pass to startActivityForResult to differentiate between text views. For example pass request code 1 for text view 1, request code 2 for text view 2 etc and use this requestCode in onActivityResult.