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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T16:05:16+00:00 2026-05-24T16:05:16+00:00

I have decided that I wanted to create some custom dialog classes that could

  • 0

I have decided that I wanted to create some custom dialog classes that could be generically used by different Activities in different situations. To be specific, I created a dialog that contains a single EditText box and a title like this:

public class EditTextDialogFragment extends DialogFragment {

    // Factory method to create a new EditTextDialogFragment 
    public static EditTextDialogFragment newInstance( int title, int defaultText ) {
        EditTextDialogFragment frag = new EditTextDialogFragment( );
        Bundle args = new Bundle( );
        args.putInt( "title", title );
        args.putInt( "defaultText", defaultText );
        frag.setArguments( args );
        return frag;
    }

    // Set title and default text
    @Override
    public Dialog onCreateDialog( Bundle savedInstanceState ) {
        // Set an EditText view to get user input
        EditText inputView = new EditText( getActivity( ) );
        inputView.setHint( getArguments( ).getInt( "defaultText" ) );

        AlertDialog.Builder builder = new AlertDialog.Builder( getActivity( ) )
        .setIcon( android.R.drawable.ic_dialog_alert )
        .setTitle( getArguments( ).getInt( "title" ) )
        .setPositiveButton( R.string.ok, ( DialogInterface.OnClickListener )getActivity( ) )
        .setNegativeButton( R.string.cancel, ( DialogInterface.OnClickListener )getActivity( ) )
        .setView( inputView );

        return builder.create( );
    }
}

Note also that I’m doing this using DialogFragments as suggested in the most recent Android SDK.

Anyway, the idea is that, from any Activity I could make a call like this:

        EditTextDialogFragment etdf = EditTextDialogFragment.newInstance( R.string.add_new_term, R.string.term_name );
        etdf.show( getSupportFragmentManager( ), "new_term" );

And as long as I implement DialogInterface.OnClickListener in the Activity, I’ll get a callback when the user finishes with the dialog. So I have this method in my Activity:

@Override
public void onClick( DialogInterface dialog, int whichButton ) {

    if ( whichButton == DialogInterface.BUTTON_POSITIVE ) {
        Toast.makeText( getApplicationContext( ), "Dialog finished!", Toast.LENGTH_SHORT ).show( );
    }
}

I have used this exact same design for a Yes/No dialog and it has worked great and I love it. I can easily call it from any activity with just two lines of code and show a Yes/No dialog with a customized title. Then the onClick callback contains which button was pressed so I have all the info I need from the dialog.

However, for this EditText dialog, I can’t figure out how to get the data out of the EditText box inside the OnClick method. The only thing passed back is the button pressed and a DialogInterface reference (which as far as I can tell, doesn’t help me get to the EditText data).

The only solutions I have seen involve embedding the OnClickListener code inside the builder code like this:

    builder.setItems(terms, new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            Toast.makeText( getApplicationContext( ), inputView.getText().toString(), Toast.LENGTH_SHORT ).show( );
        }
    });

but that defeats the whole purpose of creating a generic dialog class that can be used from any Activity that needs an EditBox. Am I missing something obvious? Is this impossible?

  • 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-24T16:05:17+00:00Added an answer on May 24, 2026 at 4:05 pm

    This is a very common question and actually has a really easy solution. You just need to use the new Dialog as the view to grab the control from.

    Something like this..

    EditText editTextFromDialog = (EditText)dialog.findViewById(the_editText_ID);
    

    Then in you the onclick handler, as long as you are in scope for the declared control (if not you will have to modify the scope to suit your needs you can just grab the content.

    String dialogInput = editTextFromDialog.getText().toString();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have decided that I really need to get some flowcharts for reverse engineering
I have some commits that I've decided, after the fact, are going to be
I have decided that all my WPF pages need to register a routed event.
I've recently decided that I just have to finally learn C/C++, and there is
I have pretty big background of .net, and I've decided that i want to
I have a largish Mercurial repository that I've decided would be better as several
I decided to teach myself assembly language. I have realized that my program will
Some days ago, I decided that it would be fun to write a streambuf
I am relatively new to game development so I decided I wanted to create
Greetings, everyone. I'm trying to learn some Silverlight basics, and have decided to write

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.