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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T20:52:31+00:00 2026-05-30T20:52:31+00:00

I am new to Android dev. The way I have been handling clicks has

  • 0

I am new to Android dev. The way I have been handling clicks has been by setting the android:onClick attribute in the manifest file for buttons. What I am wondering is the best way to handle long clicks in general. I have read about implementing onLongClick(), but is there a way to use handlers (like above), rather than having to extend View? It would be very helpful, as I would rather not have to rebuild my entire project with an extended View class.

EDIT

I should clarify. I have a ListView and I want to set what will happen when I long click on an element in the list. Each element in the list is a TextView. As per one of the answers, I have added the below code, and now I get a force close:

public class TwitterActivity extends ListActivity {
    List<String> tweets = new LinkedList<String>();

    @Override
    public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);

            setListAdapter(new ArrayAdapter<String>(this, R.layout.layout, tweets));

            TextView view = (TextView) findViewById(R.id.ListTemplate);
            view.setOnLongClickListener(new OnLongClickListener() {
                @Override
                public boolean onLongClick(View v) {
                    Toast toast = new Toast(TwitterActivity.this);
                    toast.setText("LongClick");
                    toast.show();

                    return true;
                }
            });

    //...
    }
}
  • 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-30T20:52:32+00:00Added an answer on May 30, 2026 at 8:52 pm

    For a ListActivity if you want to respond to long clicks on the list elements do this:

    public class TwitterActivity extends ListActivity {
        List<String> tweets = new LinkedList<String>();
    
        @Override
        public void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
    
                setListAdapter(new ArrayAdapter<String>(this, R.layout.layout, tweets));
                ListView lv = getListView();
                lv.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener(){ 
                       @Override 
                       public boolean onItemLongClick(AdapterView<?> av, View v, int pos, long id) 
                      { 
                           Toast.makeText(TwitterActivity.this, "LongClick", Toast.LENGTH_LONG).show();
                      } 
                 }); 
    
        }
    }
    

    For a regular activity you could do something like this:

    public class MyActivity extends Activity implements View.onLongClickListener {
    
       View myView = null;
    
    
       public void onCreate(Bundle state) {
          super.onCreate(state);
          setContentView(R.layout.my_activity);
          myView = findViewById(r.id.my_view);
          myView.setOnLongClickListener(this);
       }
    
       @Override
       public void onLongClick(View v) {
        //long clicked
       }
    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm brand new to Android dev and have some n00b questions about AVD. Is
I'm new in Android dev and I have a ridiculous question.. I read this
I'm new to Android dev. There's a very pretty state diagram of the MediaPlayer
I'm pretty new to Android dev and still working out a lot of things.
The new Android 2.1 SDK (version 7) has a new class called SignalStrength: http://developer.android.com/reference/android/telephony/SignalStrength.html
I am completely new to android, and pretty much a Java newb. I have
i'm new at Android world, and i have a doubt, is there any method
I just started to play with android dev and java+eclipse is pretty new to
I've been trying to get a handle on Dialogs. I read the android dev
I am quite new to Android dev, but not Java dev, so doing 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.