Tobias Moe Thorstensen
Tobias Moe Thorstensen

Reputation: 8981

More than one EditText inside a EditTextPreference

As the title says, I wonder if its possible to have more than one EditText inside a EditTextPreference and save the content of these EditText fields in separate "keys" inside a SharedPreference

enter image description here

Upvotes: 4

Views: 1816

Answers (2)

Name is Nilay
Name is Nilay

Reputation: 2783

Yes it is possible. Hope this will give you the idea:

 LayoutInflater factory = LayoutInflater.from(OptionList.this);            
 final View textEntryView = factory.inflate(R.layout.newgroup, null);
 AlertDialog.Builder alert = new AlertDialog.Builder(OptionList.this); 

  alert.setTitle("Add Group"); 
  alert.setMessage("Enter Group Name"); 
  // Set an EditText view to get user input  
  alert.setView(textEntryView); 
  AlertDialog loginPrompt = alert.create();

  final EditText input1 = (EditText) textEntryView.findViewById(R.id.et1);
  final EditText input2 = (EditText) textEntryView.findViewById(R.id.et2);

  alert.setPositiveButton("Create", new DialogInterface.OnClickListener() { 
  public void onClick(DialogInterface dialog, int whichButton) { 

              //Logic Here

 }); 

  alert.setNegativeButton("Cancel", new DialogInterface.OnClickListener() { 
  public void onClick(DialogInterface dialog, int whichButton) { 
              dialog.cancel();
                  } 
  }); 

  alert.show(); 

}

Upvotes: 2

Frank Sposaro
Frank Sposaro

Reputation: 8531

I'm guessing that you can only call onAddEditTextToDialogView once? The easy way would be to try calling this twice. However, it may just overwrite what you already have. The documentation did not say what happens when this method is called multiple times.

EditTextPreference comes from a DialogPreference. You can create your own DialogPreference with multiple TextFields and when you click OK save them off their corresponding preferences. This should be the way to go.

There is also a onBindDialogView(View) inside of the EditTextPreference. You may be able to get away with trying to add your second EditText here, but it may be problematic. Last resort.

Upvotes: 3

Related Questions