1

So I'm working on my first real attempt at an Android app, just a simple scorekeeper for softball games. I've got it tallying scores, outs, etc, and right now it just displays "Home" and "Away." I'd like to give users the chance to enter in actual team names. To this effect, I added a custom AlertDialog that pops up with an EditText, so when you hit "OK" it'll update the Home/Away team name.

The problem is I've been Googling this for most of the week and I've not found a single way to actually do this. I've tried tagging the fragment's layout XML so I can find the EditText, but it always gives me a null reference and crashes the app. I added a TextWatcher that presumably watched the fragment's text, but once changed and hit "OK," nothing happened. Tried adding the TextWatch to the fragment, that crashed I think, it was about two hours ago and I'm exhausted.

Really, I think I need a way to have the fragment find the TextView with the team name and change it to the value of the EditText when I positive click, but I don't know how to do that. Or maybe I've seen it and don't understand it, I've only been doing this about two months. I'd post my code, but I deleted out all the stuff that didn't work because it was taking up most of the screen real estate. Any ideas?

EDIT: So I followed advice below on defining views, that found the value of the EditText presumably, but hitting "OK" just made it set the TextView to a blank value. I think this is because the EditText's contents went away as the dialog was closed. Either that or this is all wrong. View dialogname = getActivity().getLayoutInflater().inflate(R.layout.fragment_home, null); EditText mEtName = (EditText) dialogname.findViewById(R.id.homeName); View mainAct = getActivity().getLayoutInflater().inflate(R.layout.activity_softball, null); TextView oTextView = (TextView) mainAct.findViewById(R.id.teamOne); newName = mEtName.getText().toString(); oTextView.setText(newName)

  • Hey @Kyle Why don't you put your code as well as log-cat , so we can solve your problem easily. – ULHAS PATIL Sep 23 '15 at 04:41
  • http://stackoverflow.com/questions/10903754/input-text-dialog-android this worked fine for me. Where it mentions `m_Text` you can replace with your data-model and/or the text view that displays it. – Breavyn Sep 23 '15 at 04:45

2 Answers2

1

I think you are doing wrong at time of defining id for EditText. You need to give it dialog reference.

rather than doing

 Edittext edittext = (EditText) findViewById(R.id.editText);

do like

 Edittext edittext = (EditText) dialog.findViewById(R.id.editText);
Avinash Kumar Pankaj
  • 1,700
  • 2
  • 18
  • 27
0

Been through this issue a while ago. I created my own class which extended DialogFragment. When I tried to initialize EditText which was in the dialog, I got null like

mEtName = (EditText)dialog.findViewById(R.id.editText);

So, I initialized it in this way and it worked:

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
    ...
    View dialogName = getActivity().getLayoutInflater().inflate(R.layout.dialog_name, null);
    mEtName = (EditText) dialogName.findViewById(R.id.dialog_etxt_name);
    }

Where R.layout.dialog_name is my custom dialog layout.

Shahzeb
  • 3,696
  • 4
  • 28
  • 47
  • @Kyle Vanhove if this worked, kindly accept answer to close the thread. Thanks – Shahzeb Sep 24 '15 at 14:45
  • If you are getting value, means you solved mentioned issue. Setting textview is another problem. Are you using fragments? Are you setting textview in same fragment from where you started dialog. Pls post your code – Shahzeb Sep 25 '15 at 05:50