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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T01:54:26+00:00 2026-06-01T01:54:26+00:00

The question is : How do I manage to put a listener on elements

  • 0

The question is : How do I manage to put a listener on elements inside a LinearLayout, itself on a ListView of LinearLayout.

Concretely, I have a ListView, with a list of LinearLayout in it, and each contains a Button and a TextView. I simply want to know if the user is clicking on the Button or the TextView and in which row.

I’m not especially looking for code, but so far, I have a OnItemListener on my ListView and when I click in, it returns the LinearLayout, not the components in.

  • 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-01T01:54:28+00:00Added an answer on June 1, 2026 at 1:54 am

    You’ll have to implement a custom adapter and manually set your listeners for those views(the Button and TextView) in the getView() method. To find the row where the Button or the TextView was clicked you could first set as a tag the position(the parameter you get in the getView() method) and then in the listener find out with which row you are dealing with based on the view’s tag. The method onItemClick will tell you only what row was clicked in the list.

    Here is some sample code with the custom adapter:

    class CustomCursorAdapter extends CursorAdapter {
    
        private LayoutInflater mInflater;
        private Context ctx;
    
        public CustomCursorAdapter(Context context, Cursor c,
                boolean autoRequery) {
            super(context, c, autoRequery);
            mInflater = LayoutInflater.from(context);
            ctx = context;
        }
    
        @Override
        public void bindView(View view, final Context context, Cursor cursor) {
            ViewHolder holder = (ViewHolder) view.getTag();
            String txt = cursor.getString(cursor.getColumnIndex("name"));
            holder.text.setText(txt);
            holder.button.setText(txt);
            int currentPosition = cursor.getPosition();
            holder.text.setTag(new Integer(currentPosition));
            holder.button.setTag(new Integer(currentPosition));
    
            OnClickListener listener = new OnClickListener() {
    
                @Override
                public void onClick(View v) {
                    int thePosition = (Integer) v.getTag();
                    if (v.getId() == R.id.button1) {
                        Toast.makeText(ctx,
                                "You clicked the Button from the row "
                                        + thePosition + "!", Toast.LENGTH_SHORT).show();
                    } else if (v.getId() == R.id.textView1) {
                        Toast.makeText(ctx,
                                "You clicked the TextView from the row "
                                        + thePosition + "!", Toast.LENGTH_SHORT).show();
                    }               
                }
            };
            holder.text.setOnClickListener(listener);
            holder.button.setOnClickListener(listener);
        }
    
        @Override
        public View newView(Context context, Cursor cursor, ViewGroup parent) {
            View v = mInflater.inflate(R.layout.adapters_cursor_adapter_clicks,
                    null);
            ViewHolder holder = new ViewHolder();
            holder.text = (TextView) v.findViewById(R.id.textView1);
            holder.button = (Button) v.findViewById(R.id.button1);
            v.setTag(holder);
            return v;
        }
    
        class ViewHolder {
            TextView text;
            Button button;
        }
    
    }
    

    I used a single OnClickListener for both the views, you could use 2 listeners one for each view followinf the same principle. The full class + layout file could be found here: http://pastebin.com/uM0mb4mq (with gist available here: git://gist.github.com/2625797.git)

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

Sidebar

Related Questions

I have a question about the proper, best way to manage the model. I
(This question is asked on Maven User mailing list too) I have recently faced
I know the difference between manage and unmanaged DirectX. My question is if I
Last couple of days we were discussing at another question the best to manage
I managed to get some help from a previous question and I have the
Skip to bottom for question, but first, a little context. So I have been
I have a somewhat complicated question related to MySQL. This is the table I
I have only a vague idea on phrasing this, so question as needed: I
Let's say I want to design a REST store used to manage a list.
I am completely new to javascript programming and I have a question that I

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.