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 7071853
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T05:44:16+00:00 2026-05-28T05:44:16+00:00

Can anyone please guide me on how to setOnClickListener for the TextViews that are

  • 0

Can anyone please guide me on how to setOnClickListener for the TextViews that are generated dynamically.
I have few buttons in my app.Now when a particular button is clicked than some specific text elements are retrieved from the database based on the the number of elements returned corresponding TextViews are generated in order to display the retrieved text elements.
Now the problem that i’m having is that i don’t know how to set onClickListner for the dynamically generated TextViews.Following is the piece of code where i’m creating my TextViews.Please guide me on what should i do.thank you

public void onClick(View v) {
    switch (v.getId()) {
    case R.id.imageButton1:
catagory = "General";
            IndependentDB genData = IndependentDB.getInstance();
            genData.open(this);
            ArrayList<TextHolder> genList = new ArrayList<TextHolder>();
            genList = genData.getAllTextFromGenT();
            genData.close();
            x = genList.size();
            String xstr = new StringBuilder().append(x).toString();
            System.out.println(xstr);
            mainText = new TextView[x];
            textLayout = new LinearLayout[x];
            llseperator = new LinearLayout[x];
            textLayoutContainer
                    .setBackgroundResource(R.color.dark_navy_blue);
            if (!genList.isEmpty()) {
                for (int i = 0; i < x; i++) {
                    TextHolder firstOne = genList.get(i);
                    String text = firstOne.getText();
                    mainText[i] = new TextView(this);
                    mainText[i].setId(i);
                    mainText[i].setText("Text");
                    mainText[i].setLayoutParams(new LayoutParams(
                            LayoutParams.WRAP_CONTENT,
                            LayoutParams.WRAP_CONTENT));
                    mainText[i].setTextSize(25);
                    mainText[i].setGravity(Gravity.CENTER);
                    mainText[i].setText(text);
                    mainText[i].setTextColor(0xFFFFFFFF);
                    mainText[i].setClickable(true);
                    mainText[i].setOnClickListener(this);
                    llseperator[i] = new LinearLayout(this);
                    llseperator[i].setId(i);
                    llseperator[i].setLayoutParams(new LayoutParams(
                            LayoutParams.FILL_PARENT,
                            LayoutParams.WRAP_CONTENT));
                    llseperator[i].setGravity(Gravity.CENTER);
                    llseperator[i]
                            .setBackgroundResource(R.drawable.tv_seperator);

                    textLayout[i] = new LinearLayout(this);
                    textLayout[i].setId(i);
                    textLayout[i].setLayoutParams(new LayoutParams(
                            LayoutParams.FILL_PARENT,
                            LayoutParams.WRAP_CONTENT));
                    textLayout[i].setGravity(Gravity.CENTER);
                    textLayout[i].addView(mainText[i]);
                    textLayoutContainer.addView(textLayout[i]);
                    textLayoutContainer.addView(llseperator[i]);
                }// End of for
                }// End of If
        break;}
  • 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-05-28T05:44:17+00:00Added an answer on May 28, 2026 at 5:44 am

    You already have onClick() method in Activity class (implemented View.setOnClickListener) so I suggest you to create a new class which implements View.setOnClickListener interface to handle click of TextView.

    Try,

    class TextClick implements View.setOnClickListener {
      @Override
      public void onClick(View view) {
         TextView tx=(TextView) view;
         ...
       }
    }
    

    and set click listener,

     TextClick click=new TextClick();
     for (int i = 0; i < x; i++) {
        TextHolder firstOne = genList.get(i);
        String text = firstOne.getText();
        mainText[i] = new TextView(this);      
        mainText[i].setOnClickListener(click);
        ...
     }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the foll xml can anyone please guide me how can i render
can anyone please guide me towards using mailchimp with MVC 3.. I have made
Can anyone please guide me on how to use XElement in Silverlight (C#) to
Can anyone please guide me how to install dnn in local system. How to
can anyone please guide me on How to insert a DATETIME filed from iphone
Can anyone please guide how can I add a javascript block to page header
Can anyone please guide me as to how I can configure log4j to log
Can anyone please guide about how we can get exact url and store in
can anyone please guide me with writing MySQL query for following scenario. The data
I have downloaded the JSON Framework from https://github.com/stig/json-framework . Can anyone please post the

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.