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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T19:29:56+00:00 2026-05-26T19:29:56+00:00

I have a custom Dialog that shows a few buttons. The idea is to

  • 0

I have a custom Dialog that shows a few buttons. The idea is to have a scrollable Dialog where one can choose a number or letter from 0 to 9, or 0 to F or 0 to Z …

So my first problem is how do I add these buttons through code and not xml since there is a variable number of buttons each time. Even the simplest code crashes on me so I’m probably not doing anything right.

Also the code I have with a few xml buttons chrashes when I click on the buttons saying it can’t find the onClick function. As you can see I have android:onClick=”onClickDialogbutton” in my button xml and the function does exist in my java code but still it crashes.

Hope someone can take look at the code and help me with adding buttons programatically and get the onClick to work.

Here is my code:

DialogTestActivity.java:

package com.test.dialog;
import android.app.Activity;
import android.app.Dialog;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.LinearLayout;
public class DialogTestActivity extends Activity {
    Dialog dialog;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    }
    public void onClickStartDialog( View view ) {
        dialog = new Dialog( this );
        dialog.requestWindowFeature( Window.FEATURE_NO_TITLE );
        dialog.setContentView( R.layout.dialog );
        dialog.setCancelable( true );
        dialog.show();
        // I here wish to add buttons through code and not xml.
        // This gives an error as it is now.
        Button button = new Button( this );
        ( ( LinearLayout )findViewById( R.id.Buttons ) ).addView( button );
    }
    public void onClickDialogButton( View view ) {
        dialog.dismiss();
    }
}

Main.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:onClick="onClickStartDialog"
        android:text="Start Dialog" />
</LinearLayout>

Dialog.xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="54dip"
    android:layout_height="150dip"
    android:gravity="center" >
    <ScrollView
        android:layout_width="48dip"
        android:layout_height="144dip" >
        <LinearLayout
            android:id="@+id/Buttons"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:orientation="vertical" >
            <Button
                android:id="@+id/ButtonId0"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center_horizontal"
                android:onClick="onClickDialogbutton"
                android:text="0"
                android:textSize="32dip" />
            <Button
                android:id="@+id/ButtonId1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center_horizontal"
                android:onClick="onClickDialogbutton"
                android:text="1"
                android:textSize="32dip" />
            <Button
                android:id="@+id/ButtonId2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center_horizontal"
                android:onClick="onClickDialogbutton"
                android:text="2"
                android:textSize="32dip" />
            </LinearLayout>
    </ScrollView>
</RelativeLayout>
  • 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-26T19:29:56+00:00Added an answer on May 26, 2026 at 7:29 pm

    You probably need to call findViewById on the dialog. Simply calling findViewById inside of your Activity will try to find a view or child view currently being displayed in your activity with the specified id. In this case that is probably returning null, hence the crash. Try it again with dialog.findViewById...

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

Sidebar

Related Questions

I have a custom dialog with one editText view and two buttons ok and
I have a custom dialog in my app that descends from Activity and uses
I have a custom EditButton that shows a dialog for the user to select
I have a custom Dialog that contains only a TextView to display some text
I have an activity that appears as a dialog using the following custom theme:
I have a control derived from a CWnd object that has its custom implemented
I have a custom dialog that I set up as a function: public void
I have a dropdown list that I'm converting over to a custom modal dialog.
I have created Custom Dialog for my application. While i run that application in
I have some custom made dialog that have on it Set Button , I

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.