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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T21:59:11+00:00 2026-06-01T21:59:11+00:00

I am trying to do something unusual. I have a main activity with a

  • 0

I am trying to do something unusual. I have a main activity with a menu attached to the menu button. One item in the menu opens a dialog to pick a control that is added to the main activity. I have it working so it adds the control, and saves it in a database (so it will be remembered for the next run). I need to set the button’s onClickListener to the main activity’s onClick.

public class MyMainActivity extends Activity 
    implements View.OnClickListener, View.OnTouchListener
{

private Context mContext;

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    mContext = this;
    AbsoluteLayout mMainActivityView = new AbsoluteLayout;
    SetContentView(mMainActivityView);
... populate mMainActivityView from database ...


public void onClick(View v) {
    switch (v.id) {
        case NEW_BUTTON_ID:
         // TODO implement click handler
    }
}
...

...
protected Dialog onCreateDialog(int id) {
    switch (id) {
        case NEW_BUTTON_DIALOG_ID:
...
...    
            builder.setPositiveButton(android.R.string.ok,
                    new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int which) {
                            int XPos = Integer.valueOf(editXPos.getText().toString());
                            int YPos = Integer.valueOf(editYPos.getText().toString());
                            mDataLayer.AddControl(mScreenID, Width, Height, XPos, YPos, editButtonText.getText().toString());
                            Button button = new Button (mContext);
                            button.setLayoutParams(new AbsoluteLayout.LayoutParams(Width, Height, XPos, YPos));
                            button.setText(editButtonText.getText().toString());
                            mMainActivityView.addView(button);
                // How to set Listeners from main activity?
                            button.setOnClickListener(?????);
                            button.setOnTouchListener(?????);
                            MyMainActivity.this.removeDialog(NEW_BUTTON_DIALOG_ID);
                        }
                    });

            builder.setNegativeButton(android.R.string.cancel,
                new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int whichButton) {
                    MyMainActivity.this.removeDialog(NEW_BUTTON_DIALOG_ID);
                    }
                });
        AlertDialog NewButtonDialog = builder.create();
        return NewButtonDialog;
    }
    return null;
}

So, how do I reverence the main activity’s onClick() from withing the AlertDialog’s button?

  • 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-01T21:59:13+00:00Added an answer on June 1, 2026 at 9:59 pm

    As suggested by Selvin in the comments, I needed to set the onClickListener using: MyMainActivity.onClick

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

Sidebar

Related Questions

I want to have one callback function after actions are done, I'm trying something
I'm trying to do something a bit unusual... I have this class Foo :
I'm trying to do something unusual with Powershell parsing. Basically, I have a literal
We were just trying out the Azure Storage Analytics Service, and something very unusual
I have been trying something which may turn to be impossible in the end.
I am trying something in the zend Framework I have create a project with
I am trying something very basic: I have a list of 5 buttons. They
I'm trying to do something unusual. I promise it has a purpose. I'm 90%
I have an unusual use-case I'm trying to code for. The goal is this:
I am new to CSS and I am trying to create something unusual. My

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.