Skip to content Skip to sidebar Skip to footer

How Should I Will Get The Position Of The Dynamic View In Android

Below is the view that I have created dynamically and I have added two rows but I can't get the position of that particular row, so I could I get it. I have created four textviews

Solution 1:

You're using a listview for this, right?

In your activity/fragment...

listview.setOnItemClickListener(new OnItemClickListener() {
    publicvoidonItemClick(AdapterView<?> parent, View view, int position, long id) {
        // do stuff ('position' is the position of the item you've clicked on)
    }
});

Solution 2:

If you just have four view,you can set tag with position when create it.like this:

txtResMemberName.setTag(1); 

and then you can get position by:

intpos = (Integer) txtResMemberName.getTag();

Solution 3:

As we have discussed above you have managed this View using LinearLayout ,this is not the right approach to proceed , You need to implement this task using RecyclerView or ListView which is provided by default in Android . I would suggest you to go through this Link

Solution 4:

I came to the conclusion for the above question and I have implemented it to my project and it works fine for me.

I have created one main linearlayout and for that I have added those views to that layout and the lastly I have added that newly created view to my main linearlayout and hopefully it works.

AddFamilyMemberModelfamilyMemberModel=newAddFamilyMemberModel();
                familyMemberModel.setStrMemberName(strMemberName);
                familyMemberModel.setStrGender(strGender);
                familyMemberModel.setStrMemberRelation(strMemberRelation);
                familyMemberModel.setStrMemberAge(strMemberAge);
                arrAddFamilyMember.add(familyMemberModel);

                LinearLayout.LayoutParamslayoutParams=newLinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
                LinearLayoutlinearLayout=newLinearLayout(getApplicationContext());
                linearLayout.setLayoutParams(layoutParams);
                linearLayout.setOrientation(LinearLayout.VERTICAL);

                LayoutParamslparams=newLayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
                linearLayout.setBackgroundColor(getResources().getColor(android.R.color.white));

                TextViewtxtResMemberName=newTextView(RegistrationPatientActivity.this);
                txtResMemberName.setLayoutParams(lparams);
                txtResMemberName.setText("Member Name: " +familyMemberModel.getStrMemberName());
                txtResMemberName.setTypeface(Typeface.createFromAsset(getAssets(),"Roboto-Regular.ttf"));
                txtResMemberName.setTextSize(14);
                txtResMemberName.setTextColor(Color.parseColor("#9C9C9C"));
                txtResMemberName.setSingleLine(true);
                linearLayout.addView(txtResMemberName);
                lparams.setMargins(50, 5, 40, 0);

                Viewview1=newView(RegistrationPatientActivity.this);
                LayoutParamslparamsView1=newLayoutParams(LayoutParams.WRAP_CONTENT, 2);
                view1.setLayoutParams(lparamsView1);
                view1.setBackgroundColor(Color.parseColor("#DEDDDA"));
                linearLayout.addView(view1);
                lparamsView1.setMargins(50, 10, 0, 0);

                TextViewtxtResGender=newTextView(RegistrationPatientActivity.this);
                txtResGender.setLayoutParams(lparams);
                txtResGender.setText("Member Gender: " +familyMemberModel.getStrGender());
                txtResGender.setTypeface(Typeface.createFromAsset(getAssets(), "Roboto-Regular.ttf"));
                txtResGender.setTextSize(14);
                txtResGender.setTextColor(Color.parseColor("#9C9C9C"));
                linearLayout.addView(txtResGender);
                lparams.setMargins(50, 10, 0, 0);

                Viewview2=newView(RegistrationPatientActivity.this);
                LayoutParamslparamsView2=newLayoutParams(LayoutParams.WRAP_CONTENT, 2);
                view2.setLayoutParams(lparamsView2);
                view2.setBackgroundColor(Color.parseColor("#DEDDDA"));
                linearLayout.addView(view2);
                lparamsView2.setMargins(50, 10, 0, 0);

                TextViewtxtResMemberRelation=newTextView(RegistrationPatientActivity.this);
                txtResMemberRelation.setLayoutParams(lparams);
                txtResMemberRelation.setText("Member Relation: " +familyMemberModel.getStrMemberRelation());
                txtResMemberRelation.setTypeface(Typeface.createFromAsset(getAssets(), "Roboto-Regular.ttf"));
                txtResMemberRelation.setTextSize(14);
                txtResMemberRelation.setTextColor(Color.parseColor("#9C9C9C"));
                linearLayout.addView(txtResMemberRelation);
                lparams.setMargins(50, 10, 0, 0);

                Viewview3=newView(RegistrationPatientActivity.this);
                LayoutParamslparamsView3=newLayoutParams(LayoutParams.WRAP_CONTENT, 2);
                view3.setLayoutParams(lparamsView3);
                view3.setBackgroundColor(Color.parseColor("#DEDDDA"));
                linearLayout.addView(view3);
                lparamsView3.setMargins(50, 10, 0, 0);

                TextViewtxtResMemberAge=newTextView(RegistrationPatientActivity.this);
                txtResMemberAge.setLayoutParams(lparams);
                txtResMemberAge.setText("Member Age: " +familyMemberModel.getStrMemberAge());
                txtResMemberAge.setTypeface(Typeface.createFromAsset(getAssets(), "Roboto-Regular.ttf"));
                txtResMemberAge.setTextSize(14);
                txtResMemberAge.setTextColor(Color.parseColor("#9C9C9C"));
                linearLayout.addView(txtResMemberAge);
                lparams.setMargins(50, 10, 0, 0);

                Viewview=newView(RegistrationPatientActivity.this);
                LayoutParamslparamsView=newLayoutParams(LayoutParams.WRAP_CONTENT, 4);
                view.setLayoutParams(lparamsView);
                view.setBackgroundColor(Color.parseColor("#006D67"));
                linearLayout.addView(view);
                lparamsView.setMargins(0, 10, 0, 0);

                intpos= arrAddFamilyMember.size()-1;
                linearLayout.setTag(pos);
                linearLayout.setOnClickListener(newOnClickListener() {

                    @OverridepublicvoidonClick(View v) {
                        // TODO Auto-generated method stubintposition= Integer.parseInt(v.getTag().toString());
                        AlertDialogUtility.SHOW_TOAST(RegistrationPatientActivity.this, "Position: "+position);
                    }
                });

                llAddMember.addView(linearLayout);

Post a Comment for "How Should I Will Get The Position Of The Dynamic View In Android"