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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T07:37:57+00:00 2026-06-09T07:37:57+00:00

I have dialog layout xml file which stores button, and I have set android:onClick=doSomething

  • 0

I have dialog layout xml file which stores button, and I have set android:onClick="doSomething", but I dont have any class files attached to that dialog. So practically dialog_layout.xml is the only file that I have in my project. I am able to show it and able to interact with it, but I can’t make onClick listener for the button in dialog.
I tried making the Activity that launches my dialog an owner of that dialog and then in that Activity create public void doSomething(View view) but it still doesnt seem to work. I get this error:

08-03 23:39:23.085: E/AndroidRuntime(6757): java.lang.IllegalStateException: Could not find a method doSomething(View) in the activity class android.view.ContextThemeWrapper for onClick handler on view class android.widget.Button with id 'button2'

any idea what am I doing wrong?

EDIT
here is the code:

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    addPreferencesFromResource(R.xml.settings);

    findViewById(R.id.button2).setOnClickListener(new OnClickListener() {
        public void onClick(View v) {
        //Do something.
        } 
    });




    Preference keyP = (Preference) findPreference("keyP");
    keyP.setOnPreferenceClickListener(new OnPreferenceClickListener() {
        public boolean onPreferenceClick(Preference preference) {



            Dialog dialog = new Dialog(SettingsActivity.this);
            dialog.setContentView(R.layout.draws_dialog);
            dialog.setTitle("Title");
            dialog.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-06-09T07:37:58+00:00Added an answer on June 9, 2026 at 7:37 am

    Your code looks good except you need to add button from code as it is recommended. After you loaded dialog layout the following code needs to be added. Note that you need to get rid of buttons in your xml layout, in this case android will create buttons with current theme style.

    dialog.setButton(Dialog.BUTTON_POSITIVE, "Do something", 
                     new DialogInterface.OnClickListener() {
    
        @Override
        public void onClick(DialogInterface dialog, int which) {
            // TODO do something here
        }
    });
    return dialog;
    

    If you want your button has a specific design or features then you can use following:

    dialog.setContentView(R.layout.draws_dialog);
    Button myDialogButton = (Button)dialog.findViewById(R.id.my_button);
    myDialogButton.setOnClickListener(new View.OnClickListener() {
    
        @Override
        public void onClick(View v) {
        // TODO Auto-generated method stub
        }
    });
    

    Click listeners can be moved out of dialog generation body.

    private DialogInterface.OnClickListener myButtonClickListener = new
        DialogInterface.OnClickListener(DialogInterface dialog, int which) {
        // TODO: do something here with click
    });
    

    And dialog generation body will look like this:

    dialog.setButton(Dialog.BUTTON_POSITIVE, "Do something", myButtonClickListener);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a faux dialog which uses this layout: <?xml version=1.0 encoding=utf-8?> <FrameLayout xmlns:android=http://schemas.android.com/apk/res/android
I have a class that extends android.app.Dialog, the layout is done in an xml
I have the following layout for a dialog: <?xml version=1.0 encoding=utf-8?> <TableLayout xmlns:android=http://schemas.android.com/apk/res/android android:id=@+id/categorylist
I have a custom dialog that uses an xml file to setup the layout,
I have the layout of a configuration dialog in an XML like this: <?xml
I want to add a custom Dialog, but I have problems creating a xml
I have extended my class with a dialog in which I have set content
I have been attempting to use an android.preference.DialogPreference inflated from XML, but the documentation
I have created a custom dialog and a layout xml: <?xml version=1.0 encoding=utf-8?> <LinearLayout
i am using a layout file for showing a dialog. My xml file is

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.