school_guy
school_guy

Reputation: 300

getText() in EditText is not working

I am currently trying to get a string out of an EditText field.

Here is my Code:

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.app.DialogFragment;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;

public class FirstRunDialog extends DialogFragment {

    public static final String DEBUG_TAG = FirstRunDialog.class.getSimpleName();
    private static final String PREFS_NAME = "MyPrefsFile";
    private LayoutInflater inflater;
    private View dialogView;
    private EditText input;
    private DialogInterface.OnClickListener listener;
    private SharedPreferences settings;
    private SharedPreferences.Editor editor;

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        if (activity instanceof DialogInterface.OnClickListener) {
            listener = (DialogInterface.OnClickListener) activity;
        }
    }

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {

        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
        builder.setTitle(R.string.app_name);
        builder.setView(R.layout.first_run_dialog);
        builder.setCancelable(false);
        builder.setPositiveButton("Key-Testen", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int whichButton) {
                settings = getActivity().getSharedPreferences(PREFS_NAME, 0);
                dialogView = getActivity().getLayoutInflater().inflate(R.layout.first_run_dialog, null);
                input = (EditText) dialogView.findViewById(R.id.editText);
                editor = settings.edit();
                editor.putString("apiKey", input.getText().toString());
                editor.commit();
                Log.d(DEBUG_TAG, "apiKey Input: " + input.getText().toString());
                Log.d(DEBUG_TAG, "apiKey: " + settings.getString("apiKey", ""));
                dialog.dismiss();
            }
        });
        builder.setNegativeButton("Key-Erstellen", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int whichButton) {
                //Open Browser, then go to: https://account.arena.net/applications
                Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("https://account.arena.net/applications"));
                startActivity(browserIntent);
            }
        });
        return builder.create();
    }

}

This code is not working! According to the answers how to retrieve text from an EditText I did nothing wrong. The EditText is located in an AlertFragment.

The current output for the Log.d(...) is (and the same goes for the second):

apiKey Input:

Thx in advance.

Upvotes: 0

Views: 120

Answers (2)

rojiTOCH
rojiTOCH

Reputation: 29

try this

take out this line from the onclick

input = (EditText) dialogView.findViewById(R.id.editText);

and inflate the view

LayoutInflater inflater = this.getLayoutInflater();
View dialogView = inflater.inflate(R.layout.alert_label_editor, null);
builder .setView(dialogView);

EditText input = (EditText) dialogView.findViewById(R.id.editText);

then input.gettext().tostring in the onclick, sorry for my english and I hope to help

Upvotes: 0

Juan Cruz Soler
Juan Cruz Soler

Reputation: 8254

Do not inflate a new view when the button is pressed.
Try using getDialog():

builder.setPositiveButton("Key-Testen", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int whichButton) {
            settings = getActivity().getSharedPreferences(PREFS_NAME, 0);
            input = (EditText) getDialog().findViewById(R.id.editText);
            editor = settings.edit();
            editor.putString("apiKey", input.getText().toString());
            editor.commit();
            Log.d(DEBUG_TAG, "apiKey Input: " + input.getText().toString());
            Log.d(DEBUG_TAG, "apiKey: " + settings.getString("apiKey", ""));
            dialog.dismiss();
        }
    });

Upvotes: 1

Related Questions