how to get text from edittext inside a dialog [duplicate] - java

This question already has answers here:
Android Alert Dialog unable to find view
(4 answers)
Android can't get EditText getText().toString() in a Dialog
(1 answer)
Closed 8 years ago.
Like the title said how can I get text out of EditText which is found inside a custom dialog?
This is the onCreateDialog method from my dialogfragment:
public Dialog onCreateDialog(Bundle savedInstanceState) {
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
LayoutInflater inflater = getActivity().getLayoutInflater();
SharedPreferences sharedPref = getActivity().getPreferences(Context.MODE_PRIVATE);
final SharedPreferences.Editor editor = sharedPref.edit();
builder.setView(inflater.inflate(R.layout.name_dialog, null))
.setPositiveButton(R.string.name_ok, new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int id) {
EditText nameEditText = (EditText) getActivity().findViewById(R.id.name_text_view);
editor.putString(getString(R.string.name_key), nameEditText.getText().toString());
editor.commit();
}
})
.setNegativeButton(R.string.name_cancel, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
NameDialogFragment.this.getDialog().cancel();
}
});
return builder.create();
}
I can't get it to work.. I write something in the edittext then i press OK and it just crashes and gives me a null pointer error on this line :
editor.putString(getString(R.string.name_key), nameEditText.getText().toString());

You are looking for the EditText at the wrong View. Its not part of the Activity, its part of the dialog. So check the dialog for the view:
Dialog dialogView = dialog.getDialog();
EditText paymentEt = (EditText) dialogView.findViewById(R.id.edittext_payment);

Instead of using
EditText nameEditText = (EditText) getActivity().findViewById(R.id.name_text_view);
try using this
Dialog dialg = (Dialog) dialog;
EditText et = (EditText) dialg.findViewById(R.id.search_input_text);
val = et.getText().toString();

Related

Centre message in AlertDialog box [duplicate]

This question already has answers here:
Center message in android dialog box
(7 answers)
Closed 4 years ago.
I have created a custom title for my AlertDialog box. But I have not been able to centre the message. It always comes left aligned.
TextView vehicle_no = (TextView) findViewById(R.id.vehicle_no);
String ScoreTitle = getResources().getString(R.string.AlertTitle);
AlertDialog.Builder alrtbuilder = new AlertDialog.Builder(this);
Context mContext = alrtbuilder.getContext();
LayoutInflater mLayoutInflater = LayoutInflater.from(mContext);
View mView = mLayoutInflater.inflate(R.layout.simple_dialog, null);
TextView mTextView = (TextView) mView.findViewById(R.id.title_text);
mTextView.setText(ScoreTitle);
The AlertDialog box has a positive and a negative button
alrtbuilder.setCustomTitle(mView);
alrtbuilder.setMessage(vehicle_no.getText().toString().toUpperCase()+" "+"?")
.setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
insert_timecard();
}
})
.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialogInterface, int i) {
dialogInterface.dismiss();
}
}).show;
Add android:gravity="center" to TextView with id R.id.title_text in your layout R.layout.simple_dialog.
<TextView
android:id="#+id/title_text"
android:layout_width="match_parent"
android:gravity="center"
android:layout_height="wrap_content" />
To center default message TextView you have to get the TextView from dialog after show().
alertDialog.show();
TextView messageView = (TextView)alertDialog.findViewById(android.R.id.message);
messageView.setGravity(Gravity.CENTER);
I do not know if its a good approach but you can also set the custom view to whole dialog then things can be handled easily.
Add:
mTextView.setGravity(Gravity.CENTER_HORIZONTAL);
TextView vehicle_no = (TextView) findViewById(R.id.vehicle_no);
String ScoreTitle = getResources().getString(R.string.AlertTitle);
AlertDialog.Builder alrtbuilder = new AlertDialog.Builder(this);
Context mContext = alrtbuilder.getContext();
LayoutInflater mLayoutInflater = LayoutInflater.from(mContext);
View mView = mLayoutInflater.inflate(R.layout.simple_dialog, null);
TextView mTextView = (TextView) mView.findViewById(R.id.title_text);
mTextView.setGravity(Gravity.CENTER_HORIZONTAL);
mTextView.setText(ScoreTitle);
This should resolve:
how about adding this?
TextView messageView = (TextView)dialog.findViewById(android.R.id.message);
messageView.setGravity(Gravity.CENTER);

android: input dialog not working [duplicate]

This question already has answers here:
How to display Toast in Android?
(22 answers)
Closed 5 years ago.
public void showEditPassword() {
LayoutInflater li = LayoutInflater.from(this);
View promptsView = li.inflate(R.layout.dialog_editpassword, null);
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(this);
// set prompts.xml to alertdialog builder
alertDialogBuilder.setView(promptsView);
final EditText txtOldPass, txtNewPass, txtConfirmPass;
txtOldPass = (EditText) promptsView.findViewById(R.id.txtOldPassword);
txtNewPass = (EditText) promptsView.findViewById(R.id.txtNewPassword);
txtConfirmPass = (EditText) promptsView.findViewById(R.id.txtConfirmPassword);
// set and show dialog edit password
alertDialogBuilder.setCancelable(false)
.setPositiveButton("SAVE",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int id) {
//do the saving here
saveNewPassword(currentPassword, txtOldPass.getText().toString(),
txtNewPass.getText().toString(), txtConfirmPass.getText().toString());
//recreate();
Toast.makeText(MainActivity.this, "Save Password clicked", Toast.LENGTH_LONG);
}
})
.setNegativeButton("CANCEL",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int id) {
dialog.cancel();
}
});
// create alert dialog
AlertDialog alertDialog = alertDialogBuilder.create();
// show it
alertDialog.show();
}
I want to call a input dialog and capture the save button action when click. I have tried putting a Toast message when save button was clicked in the dialog but nothing' happened. Thanks.
You do not call show() on your Toast. That's why it looks like nothing is happening

I am creating a alert box in android studio

I am trying to build a alert box in android studio, i have 2 xml and 1 activity class where on click of a button i want to show the other layout.
I am getting a error, here is my code of MainActivity
cancelBookingButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
LayoutInflater inflater = getLayoutInflater();
View alertLayout = inflater.inflate(R.layout.alert_cancel_confirm, null);
final TextView disAgree = (TextView) alertLayout.findViewById(R.id.TextDisagree);
final TextView Agree = (TextView) alertLayout.findViewById(R.id.TextAgree);
AlertDialog.Builder alert = new AlertDialog.Builder(this);
alert.setTitle("Current Booking");
// This will set the view from XML inside ALertDialog
alert.setView(alertLayout);
// disabling cancel of AlertDialog on click of back button and outside touch
alert.setCancelable(false);
AlertDialog dialog = alert.create();
dialog.show();
}
});
In this line i am getting error
AlertDialog.Builder alert = new AlertDialog.Builder(this);
In Builder cannot be applied
Any idea why i am getting this.
AlertDialog.Builder alert = new AlertDialog.Builder(YourActivity.this);
alert.setTitle("Current Booking");
// This will set the view from XML inside ALertDialog
alert.setView(alertLayout);
// disabling cancel of AlertDialog on click of back button and outside touch
alert.setCancelable(false);
AlertDialog dialog = alert.create();
dialog.show();

android keep dialogbox open until user click on button [duplicate]

i am having an custom alert view which pop ups on a button click event.all the things are going fine.but the problem is:
if user clicks outside alert dialog it disappear.i want to restrict user for clicking out side.I am giving him the choice of cancel/cross button to close alert dialog.
so how to restrict user clicking outside the alert box?
code:
the code in onCreate for button click where i am calling show dialog:
final Button cdButton = (Button) findViewById(R.id.denonCdImage);
cdButton.setOnClickListener(new Button.OnClickListener(){
public void onClick(View v)
{
showDialog(CD_CATG_ID);
}
});
protected Dialog onCreateDialog(int id) {
AlertDialog.Builder builder;
Context mContext = this;
LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(LAYOUT_INFLATER_SERVICE);
View layout = inflater.inflate(R.layout.categorydialog,(ViewGroup) findViewById(R.id.layout_root));
GridView gridview = (GridView)layout.findViewById(R.id.gridview);
gridview.setAdapter(new ImageAdapter(this));
/** Check the id for the device type for image tobe change */
switch(id) {
case 1 : // for the cd image
gridview.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,final int position, long id) {
Toast.makeText(view.getContext(), "Image selected for CD", 3000).show();
cdImageId = getImageId(position);
int elementId = getApplicationContext().getResources().getIdentifier(cdImageId, "drawable", getPackageName());
cdImageView.setImageResource(elementId);
Log.d("CdImageid", ""+cdImageId);
closeDialog(view);
}
});
builder = new AlertDialog.Builder(mContext);
builder.setView(layout);
dialog = builder.create();
break;
default:
dialog = null;
}
/** onclick listner for the close button */
ImageView close = (ImageView) layout.findViewById(R.id.close);
close.setOnClickListener(new View.OnClickListener() {
public void onClick(View v){
dialog.dismiss();
}
});
return dialog;
}
any suggestions?
thanks!
There are two methods concerning this behaviour: setCancelable() and setCanceledOnTouchOutside() as you can see in the reference.

Cannot set EditText? Issue From dialog in a fragment - Strange - Android Java

I've come across an issue which I've never had before. I have a Fragment containing a button. This button (ViewBookingButton) shows a popup dialog. When the dialog opens I would like to set a string in an EditText (AllBooking). Unfortunately it crashes and shows NullPointerExecption. What is going wrong? Here is my code:
ViewBookingButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v)
{
AlertDialog.Builder ViewBookingHelpBuilder = new AlertDialog.Builder(getActivity());
ViewBookingHelpBuilder.setTitle("view booking:");
LayoutInflater inflater = getActivity().getLayoutInflater();
View DialogLayout = inflater.inflate(R.layout.view_booking, null);
ViewBookingHelpBuilder.setView(DialogLayout);
TextView AllBooking = (TextView)view.findViewById(R.id.AllBooking);
AllBooking.setText("Hello World");//WHEN I REMOVE THIS THE DIALOG WORKS
ViewBookingHelpBuilder.setPositiveButton("Done", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which)
{
}
});
AlertDialog helpDialog = ViewBookingHelpBuilder.create();
helpDialog.show();
}
});
Change this
TextView AllBooking = (TextView)view.findViewById(R.id.AllBooking);
to
TextView AllBooking = (TextView)DialogLayout.findViewById(R.id.AllBooking);
TextView belongs to the inflated layout and `findViewById looks for the view in the current view hierarchy.

Categories