Reputation: 71
I have a layout with a CalendarView and EditText and I show them in an AlertDialog, but I can't get the value of EditText (numberDecimal) when I click OK.
private void dialogHoras() {
LayoutInflater inflater = (LayoutInflater)getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
LinearLayout ll= (LinearLayout)inflater.inflate(R.layout.almanaque, null, false);
CalendarView cv = (CalendarView) ll.getChildAt(0);
final EditText input = (EditText)findViewById(R.id.etHoras);
cv.setFirstDayOfWeek(2);
cv.setOnDateChangeListener(new OnDateChangeListener() {
@Override
public void onSelectedDayChange(CalendarView view, int year, int month, int dayOfMonth) {
dia = dayOfMonth; mes = month; anio = year;
}
});
new AlertDialog.Builder(this)
.setTitle("HORAS EXTRAS")
.setIcon(drawable.ic_launcher)
.setView(ll)
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
String res = input.getText().toString();
mensaje("INFO", "Se le deben... " + res + "horas");
}
}).setNegativeButton("Volver", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
// Do nothing.
}
} ).show();
}
Upvotes: 0
Views: 1347
Reputation: 9326
You are using the AlertDialog inside your main Activity. Basically what is happening is when you use final EditText input = (EditText)findViewById(R.id.etHoras);
Android by default search for the editText in the main Activity where it doesn't exists. So to make android understand that you want the editText from the AlertDialog you have to inflate it's view and then use it to search for the editText.
Since you have already inflated your AlertDialog's view in
LayoutInflater inflater = (LayoutInflater)getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
LinearLayout ll= (LinearLayout)inflater.inflate(R.layout.almanaque, null, false);
you will just have to tell android to search for editText present in ll
. This you can use by the following
final EditText input = (EditText)ll.findViewById(R.id.etHoras);
Upvotes: 0
Reputation: 18933
You are inflating your view so you have to pass reference of that view to find the id of element. So change
final EditText input = (EditText)findViewById(R.id.etHoras);
to
final EditText input = (EditText)ll.findViewById(R.id.etHoras);
Also if you want to get the value of editText then use this:
String myValue = yourEdittext.getText().toString():
Upvotes: 3
Reputation: 2891
EditText txtPwd = (EditText) findViewById(R.id.user_password);
String passWord = txtPwd.getText().toString().trim();
Upvotes: 2
Reputation: 11131
replace
final EditText input = (EditText)findViewById(R.id.etHoras);
with
final EditText input = (EditText)ll.findViewById(R.id.etHoras);
the first one searches for the EditText in your Activity layout and second one searches in LinearLayout which is part of Dialog View
Upvotes: 1