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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T23:10:24+00:00 2026-06-17T23:10:24+00:00

I wrote a listener that does some actions onKeyDown basically I need to include

  • 0

I wrote a listener that does some actions onKeyDown basically I need to include it in all my activities but I’m new to Java and don’t know the best way to do it, I can of course just copy paste the code on every class but it would be a pain to keep every one updated.

@Override
public boolean onKeyDown(int keyCode, KeyEvent event)  {
    if(sharedPrefs.getBoolean("pref_disable_keydowns", true)) {
        System.out.println("Prevent keydown");
        return true; 
        }
    return super.onKeyDown(keyCode, event);
}

How can I include this in different .java files so I only have to modify it once to affect all classes it’s included?

  • 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-17T23:10:25+00:00Added an answer on June 17, 2026 at 11:10 pm

    Do not never under (nearly) no circumstances copy code from one method to another.

    Now to your solution:

    public class MyListerer implement KeyListener {
    
        public void onKeyDown(int keyCode, KeyEvent event){
            Toast.makeText(this, "Hey world!", Toast.LENGTH_LONG).show();
        }
    }
    

    Now you can make

    object.addKeyListener(new MyListener());
    

    I am pretty sure, that the Interface KeyListener may have a different Name, but I think you get the point.

    update

    Is it all about Android Activities? http://developer.android.com/reference/android/app/Activity.html

    In that case you can implement it a different way. There is no event-listener in Android activites. You can do the following:

    class MyActivity extends Activity {
    
        private MyListener delegate= new MyListener();
    
        // your existing code
    
        public boolean onKeyDown(int keyCode, KeyEvent event)  {
            delegate.onKeyDown(keyCode, event);
        }
    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I wrote some code that has a jpanel with a mouse listener and mouse
I wrote some script in a site. The script makes a new spreadsheet and
I'm working on a project that does some intense math calculations (arrays of matrices,
i am trying to implement a simple server application in java. all it does
I need to write a piece of code that will attach a listener to
I have some code that I wrote to implement a vertical swipe on a
Java Socket Server I have a Java process that is creating a listener on
I tried to write some AR app. For now I wrote some code that
I'd like to write an express middleware function that sets up a listener on
I'm developing a JavaScript application using Google Visualization API. I wrote a event listener

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.