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

  • Home
  • SEARCH
  • 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 3332218
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T23:39:53+00:00 2026-05-17T23:39:53+00:00

I have a little problem with an android custom dialog. I construct a custom

  • 0

I have a little problem with an android custom dialog.

I construct a custom dialog in the onCreateDialog(int) function:

dialog = new Dialog(this);
dialog.setContentView(R.layout.custom_dialog);
dialog.setTitle("Custom Dialog");

I have a onClick(View) function in the same class:

   public void onClick(View v) {
        switch(v.getId()) {
        case R.id.dialog_button:
            Log.i("pma57","dialog button pressed");
            break;
        case R.id.main_button:
            showDialog(DIALOG_CUSTOM);
            break;
        }       
    }

This is the XML definition:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:orientation="vertical"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"
  android:paddingLeft="20dp"
  android:paddingRight="20dp"
  android:paddingBottom="20dp">
      <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/enter_username" />
      <EditText
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" />
      <Button
        android:id="@+id/dialog_button"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="OK"
        android:onClick="onClick" />

</LinearLayout>

The Dialog shows up. But the Button does not work (the app crashs)- which is quite ok because the onClick-function for the callback is defined in my main activity – and the dialog is a new activity (am I right?).

But I realy don’t know how i implement a button in the dialog – I think this is a fundamental understanding problem of the technic. The long way would be to subclass Dialog and write everything there – but is there another way which I don’t see?

  • 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-17T23:39:53+00:00Added an answer on May 17, 2026 at 11:39 pm

    The way round it that I use, rather than having a switch block is to use onClickListeners for the buttons:

    dialog = new Dialog(this);
    dialog.setContentView(R.layout.custom_dialog);
    dialog.setTitle("Custom Dialog");
    
    
    Button dialog_btn = (Button) dialog.findViewById(R.id.dialog_button);
    dialog_btn.setOnClickListener(new View.OnClickListener() 
    {
        // Perform button logic
    }
    

    Note that you are finding the view from the dialog, not just calling straight to findViewById as that would return a null pointer as there would be not dialog_button on the application view.

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

Sidebar

Related Questions

I have a little problem with a ScrollView. I have a layout for an
I am new to android and only have a little experience with HTML and
i have this little problem, i use the Application settings in VS 2010 to
i have little problem with boost::asio library. My app receive and process data asynchronously,
I have a little problem with a Listview. I can load it with listview
I have a little Java problem I want to translate to Python. Therefor I
I've run on a little problem today: I have a JS drop down menu
I have a little demonstration below of a peculiar problem. using System; using System.Windows.Forms;
I’m having a little Architecture problem. In my project I have a Business Logic
I have a problem with a little .Net web application which uses the Amazon

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.