Vlad.mir
Vlad.mir

Reputation: 695

How to acces EditText value from AlertDialog Builder from another layout?

I have this code in my Config activity and the dialog is activated when i press a add_count TextView, my problem seems to be that the value i get on get_cont is null, maybe because the inflater doesn't work right ? (usernames and passwords are ArrayLists)

add_cont.setOnClickListener(new View.OnClickListener() {

    @Override
    public void onClick(View v) {

         AlertDialog.Builder builder = new AlertDialog.Builder(Config.this);
         LayoutInflater inflater = Config.this.getLayoutInflater();
         final View dialog_layout = inflater.inflate(R.layout.dialog_add_cont, null);
         builder.setTitle("Add cont.");
         builder.setView(inflater.inflate(R.layout.dialog_add_cont, null))
         .setPositiveButton(R.string.create_cont, new DialogInterface.OnClickListener() {
               @Override
               public void onClick(DialogInterface dialog, int id) {
                   EditText get_cont = (EditText)dialog_layout.findViewById(R.id.username);
                   EditText get_pass = (EditText)dialog_layout.findViewById(R.id.password);

                   String test_cont = get_cont.getText().toString();
                   String text_pass = getpass.getText().toString();

                   usernames.add(3, get_cont.getText().toString());
                   passwords.add(3, get_pass.getText().toString());
               }
           })
           .setNegativeButton(R.string.cancel_cont, new DialogInterface.OnClickListener() {
               public void onClick(DialogInterface dialog, int id) {
                  dialog.cancel();
               }
           }); 
         AlertDialog alertDialog = builder.create();
         alertDialog.show();

    }
});

and my dialog_add_cont layout looks like this:

<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/username"
    android:inputType="textEmailAddress"
    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"
    android:hint="username" />

<EditText
    android:id="@+id/password"
    android:inputType="textPassword"
    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"
    android:fontFamily="sans-serif"
    android:hint="password"/>

Did i got the code somewhere wrong ?

Upvotes: 0

Views: 73

Answers (2)

Bidhan
Bidhan

Reputation: 10687

Have you tried doing this?

@Override
           public void onClick(DialogInterface dialog, int id) {
               Dialog d = (Dialog) dialog;
               EditText get_cont = (EditText)d.findViewById(R.id.username);
               EditText get_pass = (EditText)d.findViewById(R.id.password);

               String test_cont = get_cont.getText().toString();
               String text_pass = get_cont.getText().toString();

               usernames.add(3, get_cont.getText().toString());
               passwords.add(3, get_pass.getText().toString());
           }

Upvotes: 1

Mr.Me
Mr.Me

Reputation: 9276

You are inflating dialog_layout and never using it since you are inflating the xml again and setting it directly for the dialog.

your code should be like this:

  AlertDialog.Builder builder = new AlertDialog.Builder(Config.this);
  LayoutInflater inflater = Configurare.this.getLayoutInflater();
  final View dialog_layout = inflater.inflate(R.layout.dialog_add_cont, null);
  builder.setTitle("Add cont.");
  builder.setView(dialog_layout)
....

Upvotes: 0

Related Questions