Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get ID for views that are added dynamically

Tags:

android

How to get ID of Views that are added dynamically in layout?

   public class Display 
    {

        static int i;
        Dynamic_UIActivity dyn;
        private Context context;
        View mField= null;
        TextView text;
        protected int mYear;
        protected int mMonth;
        protected int mDay;
        LinearLayout ll;
        Mynewdialog dialognew;

        public Display(Context mcontext)
        {
            context=mcontext;
            ll = new LinearLayout(mcontext);
            ll.setOrientation(LinearLayout.VERTICAL);
            dyn = new Dynamic_UIActivity();
       }

       public void addLabel(String text)
       {
        TextView textview = new TextView(context);
        textview.setText(text);
        ll.addView(textview);       
        }

        public void addField(String Type,String Tag)
        {
            mField.setId(i);
            i++;
            if(Type.equalsIgnoreCase("text"))
           {
                mField= new EditText(context);
                mField.setTag(Tag);
                //int id = mField.getResources().getIdentifier((String) mField.getTag(),"id" ,dyn.getPackageName());
                //EditText edit = (EditText)mField.findViewById(1);
                ll.addView(mField);
            }
            if(Type.equalsIgnoreCase("datepicker"))
            {
                mField=new EditText(context);
                ((EditText) mField).setText("Pick Date");
                ll.addView(mField);
                mField.setOnClickListener(new View.OnClickListener()
                {
                    @Override
                    public void onClick(View v) 
                    {
                        dialognew = new Mynewdialog(context);
                         dialognew.dialog_obj((EditText) mField);
                    }
                });
            } 
        }   

        protected DatePickerDialogWithEditText.OnDateSetListener mdatepickerdialog = new DatePickerDialogWithEditText.OnDateSetListener()
        {
            public void onDateSet(DatePicker view , int year , int month, int date)
             {
                  mYear = year;
                  mMonth = month;
                 mDay = date;
                 dialognew.showdialogtext();

             }
         };
     }

     class DatePickerDialogWithEditText extends DatePickerDialog 
     {
         EditText tv;    

         DatePickerDialogWithEditText(Context c, OnDateSetListener oD, int y, int m, int d, EditText tv) 
        {
             super(c, oD, y, m, d);
             this.tv = tv;
        }

         EditText getAssociatedTextView() 
         {
             return tv;
         }
    }


      class Mynewdialog 
     {
        private Context context;
        Display dis = new Display(context);
        private DatePickerDialogWithEditText dialog;

        public Mynewdialog(Context mcontext)
        {
            context=mcontext;       
        }

        public void dialog_obj(EditText editTextView)
        {
             final Calendar cal = Calendar.getInstance();
             dis.mYear = cal.get(Calendar.YEAR);
             dis.mMonth = cal.get(Calendar.MONTH);
             dis.mDay = cal.get(Calendar.DAY_OF_MONTH);
             dialog = new DatePickerDialogWithEditText(context, dis.mdatepickerdialog, dis.mYear, dis.mMonth, dis.mDay, editTextView);
            dialog.show();
        }

         public void showdialogtext()
        {
             EditText et = dialog.getAssociatedTextView();
             et.setText(new StringBuilder().append(dis.mDay).append("-").append(dis.mMonth).append("-").append(dis.mYear));
         }  
       }

now I want to Set the ID of the views that I get by checking its Type?how can I do that?

like image 932
Geetanjali Avatar asked Jul 19 '11 11:07

Geetanjali


1 Answers

You can do it by using getId() and setId() methods of the View.

For example:

TextView txtView = new TextView(this);
txtView.setId(4);

and you can get the ID by using getId() method:

txtView.getId();
like image 133
Paresh Mayani Avatar answered Oct 23 '22 21:10

Paresh Mayani