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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T03:21:50+00:00 2026-06-12T03:21:50+00:00

public void onCreate(Bundle savedInstanceState) { … btn_client_connect.setOnClickListener(new Button.OnClickListener() { public void onClick(View v) {

  • 0
public void onCreate(Bundle savedInstanceState) {
    ...
    btn_client_connect.setOnClickListener(new Button.OnClickListener() {
        public void onClick(View v) {
            // inline implementation goes here ...
        }
}

I want to move inline implementation to separate function, keeping onCreate function short and readable:

    private void OnBtnConnectClick(View v) {
        // implementation...
    }

Now I need to subscribe to the button click using something like:

btn_client_connect.setOnClickListener(this.OnBtnConnectClick);

But this is not compiled. I only have this ugly solution:

btn_client_connect.setOnClickListener(new Button.OnClickListener() 
{
    public void onClick(View v) 
    {
        OnBtnConnectClick(v);
    }
});

Is there better way to do this?

  • 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-12T03:21:51+00:00Added an answer on June 12, 2026 at 3:21 am

    there are other ways to set Click Listeners like one

    you simply type in xml

    android:onClick="OnBtnConnectClick"

    and other as Keppil suggest

    myButton.setOnClickListener(new myClickListener());
    
    public class myClickListener implements OnClickListener {
    
        @Override
        public void onClick(View arg0) {
            // TODO Auto-generated method stub
    
        }
    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); Button OpenPDF = (Button) findViewById(R.id.button); OpenPDF.setOnClickListener(new View.OnClickListener()
@Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); Button button1 = (Button) findViewById(R.id.button1); button1.setOnClickListener(new
I have code like this @Override public void onCreate(Bundle savedInstanceState) { addItemsOnSpinnerOrgaLevel(); btn_getReport.setOnClickListener(new OnClickListener()
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.picture_choice_activity); Button takePictureButton = (Button)findViewById(R.id.takePictureButton); takePictureButton.setOnClickListener(new OnClickListener()
public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); Init(); backthread tr=new backthread(); tr.execute(0,0,0); } backthread
My timer code is: public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); initializer(); fire.setOnClickListener(this); }
public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); final ThraedDemo objDemo = new ThraedDemo(); Thread
//----AppActivity.java public class AppActivity extends Activity { Button Login; @Override public void onCreate(Bundle savedInstanceState)
public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); loadData(); setContentView(R.layout.preload); Thread thread = new Thread() {
I have this: public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); LinearLayout layout = (LinearLayout) View.inflate(this,

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.