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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T19:05:59+00:00 2026-06-12T19:05:59+00:00

I am trying to create custom AlertDialog with two EditText fields for input. Here

  • 0

I am trying to create custom AlertDialog with two EditText fields for input. Here is the code:

void newItemInput(){

    AlertDialog.Builder builder = new AlertDialog.Builder(this);

    LayoutInflater inflater = this.getLayoutInflater();

    builder.setView(inflater.inflate(R.layout.dialog_signin, null));

    builder.setTitle("");
    builder.setMessage("");

    builder.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int whichButton) {

            EditText item_name = (EditText)findViewById(R.id.item_name);
            EditText item_price =(EditText)findViewById(R.id.item_price);

            String text = item_name.getText().toString();
            String text_price = item_price.getText().toString();
            int price = Integer.parseInt(text_price);

            // Do something with value!
            products.add(new Product(text, price, R.drawable.unread, false));

        }
    });
    builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int whichButton) {

        }
    });
    builder.show();
}

AlertDialog shows up and I can input data. But when I press OK, while passing text and price as arguments to products.add(new Product(text, price, R.drawable.unread, false)), app crashes down.
Here is the layout file:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">
    <EditText
        android:id="@+id/item_name"
        android:inputType="text"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="16dp"
        android:layout_marginLeft="4dp"
        android:layout_marginRight="4dp"
        android:layout_marginBottom="4dp"/>
    <EditText
        android:id="@+id/item_price"
        android:inputType="number"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="4dp"
        android:layout_marginLeft="4dp"
        android:layout_marginRight="4dp"
        android:layout_marginBottom="16dp"/>
</LinearLayout>

Please let me know, what am I doing wrong. When I do not use custom Dialog and use only one text field it works fine. But I need two fields, and I am assuming that this crash because of LayoutInflater.

  • 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-12T19:06:00+00:00Added an answer on June 12, 2026 at 7:06 pm

    Firstly, in the future, when you receive LogCat errors, please post them.

    Best solution is to cast the dialog to an AlertDialog, then use that.

    AlertDialog aDialog = (AlertDialog) dialog; // This will ONLY work if (dialog instanceof AlertDialog)
    EditText item_name = (EditText) aDialog.findViewById(R.id.item_name);
    EditText item_price =(EditText) aDialog.findViewById(R.id.item_price);
    

    Alternatively, if the above doesn’t work, you can save the View you give to the builder, then use that instead.

    Like so, using a final modifier to access within the OnClickListener:

    final View v = inflater.inflate(R.layout.dialog_signin, null);
    builder.setView(v);
    

    Then, access v.findViewById within the OnClickListener to find the views.

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

Sidebar

Related Questions

I'm trying to create a custom AlertDialog , and there are two buttons in
I am trying to create a custom User library in CodeIgniter. Inside this library
I'm trying to create a custom form input that utilizes some images, it should
I'm trying to create a DialogFragment using a custom view in an AlertDialog .
I am trying to create custom bullet points and it is not working. This
I'm new to Objective-C and trying to create custom cells. In the storyboard (under
I'm trying to create custom AlertDialog with an image text and buttons. When I
I'm trying to create custom view that draws image downloaded from Url. The code
I am trying create my custom Style spinner with help of this site. But
When trying to create custom performance counters as explained in this MSDN article ,

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.