I am inflating layout in onCreate() which is already declared in my activity:
layout = (RelativeLayout) findViewById(R.id.layout);
Assume id is declared as an int in my activity.
Later, after some event I am adding an imageview to this layout
id = (int) Calendar.getInstance().getTimeInMillis();
ImageView imageView = new ImageView(context);
imageView.setImageResource(R.drawable.ic_launcher);
imageView.setId(id);
layout.addView(imageView);
Later somewhere, I want to get imageView from the id we have set earlier:
ImageView imageView = (ImageView) findViewById(id);
if (imageView == null)
Log.e("Test", "imageview is null");
All code runs successfully and Imageview always returns null as printed in log.
Note: I can't able to keep the object of imageview itself because I have many number of different views in real project. Here I have described my problem using single imageview. I have stored all the ids of the views but can't able to get all those views using findViewById(). The reason why I have used Calendar.getInstance().getTimeInMillis() to generate id is because I want a unique id everytime. I have stored all the layout data including ids for later use. If user again opens this activity anytime, he will get from where he left off. So while adding any new imageview the id must not be repeated which is generated earlier.
Keypoint: If I set the device date-time 2-3 or more days earlier then it is worknig properly. I think the issue is with generating the id using calendar.