Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9017139
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T04:11:22+00:00 2026-06-16T04:11:22+00:00

I am trying to add 20 textviews and assign a onclick to dynamically added

  • 0

I am trying to add 20 textviews and assign a onclick to dynamically added textviews. Problem is whenever i try to click any of the dynamic textiview. It always fires up click event of last added textview.

Here is my code:

EditText s;
EditText t;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    RelativeLayout containerLayout = (RelativeLayout) findViewById(R.id.container);

    for (int i = 0; i < 20; i++) {
        TextView dynaText = new TextView(this);

        dynaText.setText("Some text " + i);
        dynaText.setTextSize(30);
        dynaText.setTag("" + i);

        dynaText.setOnClickListener(btnClickListener);
        // Set the location of your textView.

        dynaText.setPadding(0, (i * 30), 0, 0);

        containerLayout.addView(dynaText);
    }

}

OnClickListener btnClickListener = new OnClickListener() {

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        RelativeLayout ll = (RelativeLayout) v.getParent();
        TextView tv = (TextView) ll.getChildAt(2);
        // Integer pos = (Integer) tv.getTag();
        Toast.makeText(v.getContext(), "Toast " + v.getTag(),
                Toast.LENGTH_SHORT).show();
    }

};

}

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T04:11:24+00:00Added an answer on June 16, 2026 at 4:11 am

    Try this:

    OnClickListener btnClickListener = new OnClickListener() {
    
        @Override
        public void onClick(View v) {
              Toast.makeText(v.getContext(), "Toast " + v.getTag(),
                    Toast.LENGTH_SHORT).show();
        }
    
    };
    

    Update:

    The reason why you are clicking the last TextView is because it is just so big.. You are increasing the padding with each loop and when you click somewhere, it will always hit the last View because it is overlaying the others. Try a LinearLayout and remove the padding like so:

    public class MainActivity extends Activity implements OnClickListener {
    
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            LinearLayout containerLayout = (LinearLayout) findViewById(R.id.container);
    
            for (int i = 0; i < 20; i++) {
                TextView dynaText = new TextView(this);
    
                dynaText.setText("Some text " + i);
                dynaText.setTextSize(30);
                dynaText.setTag("" + i);
                dynaText.setOnClickListener(this);
    
                containerLayout.addView(dynaText);
            }
        }
    
        public void onClick(View v) {
            // do stuff
        }
    
    }
    

    But having your problem solved.. I think a ListView would suit your purpose 🙂

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to add dynamic textview to listview items.textviews can be of 1-2
I am trying to dynamically add textviews to a linearLayout, say the textview is
I've been trying to add multiple textviews and buttons when onClick, and this is
I'm trying to add a bunch of TextViews at runtime to a scrollview but
I'm trying to add a RelativeLayout, that contains 2 TextViews, above a ListView as
I am trying add picture boxes dynamically. My code is as PictureBox picture =
I am trying to setup an activity and when I try to assign some
I am trying to add a TextView dynamically into a layout which is part
I am trying to add rows to a table after a click to a
I'm trying to add a little space between lines to my TextViews using android:lineSpacingMultiplier

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.