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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T08:28:49+00:00 2026-05-24T08:28:49+00:00

When selected, a context menu option brings up an AlertDialog. I want the user

  • 0

When selected, a context menu option brings up an AlertDialog. I want the user to enter text into an EditText in the AlertDialog, and when the user presses PositiveButton, the value of EditText is able to be “returned” to the main method. Here is the relevant code from my class:

public class PassPlay extends ListActivity {
    public static final int PENALTY_ID = Menu.FIRST+1;
    public static final int FUMBLE_ID = Menu.FIRST+2;
    public static final int ADDLYDS_ID = Menu.FIRST+3;
    public static final int SAFETY_ID = Menu.FIRST+4;

    EditText ydsFromAlertDialog;

    String penYdsStr;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.passplay);

        ydsFromAlertDialog=(EditText)findViewById(R.id.passYdsLabel);

        registerForContextMenu(getListView());
    }

    public boolean onCreateOptionsMenu(Menu menu) {
        populateMenu(menu);
        return(super.onCreateOptionsMenu(menu));
    }

    public boolean onOptionsItemSelected(MenuItem item) {
        return(applyMenuChoice(item) || super.onOptionsItemSelected(item));
    }

    public boolean onContextItemSelected(MenuItem item) {
        return(applyMenuChoice(item) || super.onContextItemSelected(item));
    }

    private void populateMenu(Menu menu) {
        menu.add(Menu.NONE, PENALTY_ID, Menu.NONE, "Penalty");
        menu.add(Menu.NONE, FUMBLE_ID, Menu.NONE, "Fumble");
        menu.add(Menu.NONE, ADDLYDS_ID, Menu.NONE, "Additional Yards");
        menu.add(Menu.NONE, SAFETY_ID, Menu.NONE, "Safety");
    }

    private boolean applyMenuChoice(MenuItem item) {
        LayoutInflater factory = LayoutInflater.from(this);
        final View textEntryView;
        switch (item.getItemId()) {
        case PENALTY_ID:
            textEntryView = factory.inflate(R.layout.textdialog, null);
            new AlertDialog.Builder(this)
            .setView(textEntryView)
            .setIcon(android.R.drawable.ic_dialog_alert)
            .setTitle(R.string.timeout)
            .setPositiveButton(R.string.offense, new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    //Actions for offensive timeout
                    EditText penaltyYds=(EditText)findViewById(R.id.ydsAssessedLabel);
                    penYdsStr = penaltyYds.getText().toString();
                    ydsFromAlertDialog.setText(penYdsStr);
                }
            })
            .setNeutralButton(R.string.defense, new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    //Actions for defensive timeout  
                }
            })
            .setNegativeButton(R.string.cancel, null)
            .show();
            return true;
        case FUMBLE_ID:
            //Fumble window
            return true;
        case ADDLYDS_ID:
            //Additional Yards window
            return true;
        case SAFETY_ID:
            //Safety window
            return true;
        }

        return(false);
    }
}

The main XML layout (passplay.xml) has your normal TextViews, EditTexts, CheckBoxes, etc. I want to set one of those EditTexts (ydsFromAlertDialog) to be assigned the value entered in the AlertDialog (EditText penaltyYds). The AlertDialog’s XML layout (textdialog.xml) is very simple with one TextView and one EditText.

When I run the program, the following line errors out with “The application has stopped unexpectedly.”

penYdsStr = penaltyYds.getText().toString();

So in summary, I want to press the menu option “Penalty”, have an AlertDialog with an EditText where I enter a number, and when I press PositiveButton, the EditText ydsFromAlertDialog’s value is changed to what was entered in the Dialog.

In reality, I have a database table with 5 columns, 4 of which will be populated by normal fields, but the 5th will be populated by the value entered in the Dialog. I figured if I can “return” it to “be with” the rest of the values, I’ll be able to save it to same table record as the others, too.

Let me know if you need any more information. Thank you!

  • 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-24T08:28:50+00:00Added an answer on May 24, 2026 at 8:28 am

    You have to get the ydsAssessedLabel from the view you inflated

    EditText penaltyYds=(EditText)textEntryView.findViewById(R.id.ydsAssessedLabel);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to add an option to the finder context menu that calls hg
Need to insert selected text on the page into textarea. There must be some
I would like to determine the index of the selected context menu item. But
I have a select menu that looks like this: <select id ="cal-category-select"> <option value="/event-sort/list/2009/9/sports/">Sports
I want to display a different menu option depending on whether a number or
Silverlight 4 now include the option for creating a context menu upon right clicking.
I need to add items to explorer context menu. When files/folder are selected different
I am extracting content selected by the user via this function: function getSelectionHTML() {
I have selected text by visual mode and pressed :. I run the following
I'm using the following code for a drop down menu but I want the

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.