I use this code to build an AlertDialog with an EditText:
AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setTitle("Title");
builder.setView(LayoutInflater.from(context).inflate(R.layout.dialog_view, null));
builder.setNegativeButton("Cancel", null);
builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
mInput = ((EditText) LayoutInflater.from(context).inflate(R.layout.dialog_view, null).findViewById(R.id.etxtDialog)).getText().toString();
}
});
builder.show();
When I run this code though, the mInput.length() == 0, so the string is empty. The line mInput = ((EditText) LayoutInflater.from(context).inflate(R.layout.dialog_view, null).findViewById(R.id.etxtDialog)).getText().toString(); is executed though and the EditText does contain some characters. Why isn't this code working?
You are doing it wrong... Hold the instance of your inflated view and use it later. For example:
AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setTitle("Title");
final View v = LayoutInflater.from(context).inflate(R.layout.dialog_view, null);
builder.setView(v);
builder.setNegativeButton("Cancel", null);
builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
mInput = ((EditText)v.findViewById(R.id.etxtDialog)).getText().toString();
}
});
builder.show();
This is because you are creating a new view each time you click on the positiveButton (inflate is called every time). You should do it like this:
AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setTitle("Title");
View v=LayoutInflater.from(context).inflate(R.layout.dialog_view, null);
builder.setView(v);
builder.setNegativeButton("Cancel", null);
builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
mInput = ((EditText) v.findViewById(R.id.etxtDialog)).getText().toString();
}
});
builder.show();
use this way
ContextThemeWrapper cw = new ContextThemeWrapper( this, R.style.AlertDialogTheme );
AlertDialog.Builder builder= new AlertDialog.Builder( cw );
LayoutInflater inflater = (LayoutInflater) cw.getSystemService(LAYOUT_INFLATER_SERVICE);
View layout = inflater.inflate(R.layout.dialog_view,null);
AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setTitle("Title");
builder.setView(layout);
mInput = (EditText)layout.findViewById(R.id.etxtDialog);
builder.setNegativeButton("Cancel", null);
builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
String value = mInput.getText().toString();
}
});
builder.show();
Here: R.style.AlertDialogTheme is your application theme
Try this. Your full and final solution. This is how i did:
LayoutInflater li = LayoutInflater.from(this);
View promptsView = li.inflate(R.layout.prompts, null);
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(this);
alertDialogBuilder.setView(promptsView);
final EditText userInput = (EditText) promptsView.findViewById(R.id.editTextDialogUserInput);
SimpleDateFormat sdf = new SimpleDateFormat("yyyy_MM_dd_HH.mm.ss");
String currentDateandTime = sdf.format(new Date());
userInput.setText(currentDateandTime);
alertDialogBuilder.setCancelable(false);
alertDialogBuilder.setPositiveButton("OK", new DialogInterface.OnClickListener()
{
public void onClick(DialogInterface dialog,int id)
{
if(userInput.getText()+"" != "")
{
Intent i = new Intent(MainActivity.this,AskForTextFile.class);
i.putExtra("userInput",userInput.getText()+"");
startActivity(i);
}
else
{
Toast.makeText(context, "Please enter backup name to go further.", Toast.LENGTH_LONG).show();
}
}
}
);
alertDialogBuilder.setNegativeButton("Cancel", new DialogInterface.OnClickListener()
{
public void onClick(DialogInterface dialog,int id)
{
dialog.cancel();
}
}
);
AlertDialog alertDialog = alertDialogBuilder.create();
alertDialog.show();
Try this---
Replace this line---
mInput = ((EditText) LayoutInflater.from(context).inflate(R.layout.dialog_view, null).findViewById(R.id.etxtDialog)).getText().toString();
With--
mInput = ((EditText) builder.findViewById(R.id.etxtDialog));
String input= mInput.getText().toString();
Related
Here is the method where I need to get the text from the EditText of AlertDialog
public void checkButton() {
leggspillere = (Button) findViewById(R.id.leggspillere);
final LayoutInflater inflater = this.getLayoutInflater();
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setView(inflater.inflate(R.layout.create_user,null));
builder.setMessage("Lag en spiller");
builder.setCancelable(true);
builder.setPositiveButton("Ok",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// trying to get the EditText-string inside alertdialogbox input here.
final String Name = UserName.
Person person = new Person(Name, 0);
dialog.dismiss();
spillere.setText(person.getName() + " " + person.getScore());
}
}
);
//sette andre knappen "cancel"
builder.setNegativeButton("no",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
}
);
AlertDialog alert = builder.create();
alert.show();
}
XML:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageView
android:layout_width="match_parent"
android:layout_height="64dp"
android:scaleType="center"
android:background="#FFFFBB33"
android:contentDescription="#string/app_name" />
<EditText
android:id="#+id/UserName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="4dp"
android:layout_marginLeft="4dp"
android:layout_marginRight="4dp"
android:layout_marginTop="16dp"
android:inputType="textEmailAddress" />
</LinearLayout>
Trying to get the input/string from EditText.xml to make a person object.
Always referencing to a null object. I can't find any solution, point me somewhere.
How are you trying to access the editText inside DialogInterface.OnClickListener?
Try getting the inflated view in a View object and do findViewById on that View object variable. This code below should work fine:
LayoutInflater inflater = this.getLayoutInflater();
AlertDialog.Builder builder = new AlertDialog.Builder(this);
final View dialogView = inflater.inflate(R.layout.dialog_alert, null);
builder.setView(dialogView);
builder.setMessage("Lag en spiller");
builder.setCancelable(true);
builder.setPositiveButton("Ok",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
EditText editText = (EditText) dialogView.findViewById(R.id.d_alert_et);
Log.v("tag", "Edit text value: " + editText.getText());
}
});
builder.setNegativeButton("no",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
});
AlertDialog alert = builder.create();
alert.show();
Basically get the inflated view in a View Object:
final View dialogView = inflater.inflate(R.layout.dialog_alert, null);
And then do findViewById on that View Object:
EditText editText = (EditText) dialogView.findViewById(R.id.d_alert_et);
editText.getText()
Pls. refer this code : https://www.mkyong.com/android/android-custom-dialog-example/
// custom dialog
final Dialog dialog = new Dialog(context);
dialog.setContentView(R.layout.custom);
dialog.setTitle("Title...");
// set the custom dialog components - text, image and button
TextView text = (TextView) dialog.findViewById(R.id.text);
text.setText("Android custom dialog example!");
ImageView image = (ImageView) dialog.findViewById(R.id.image);
**EditText edtName = (EditText) dialog.findViewById(R.id.edtName);**
image.setImageResource(R.drawable.ic_launcher);
Button dialogButton = (Button)
dialog.findViewById(R.id.dialogButtonOK);
// if button is clicked, close the custom dialog
dialogButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
**String s = edtName.getText().toString();**
dialog.dismiss();
}
});
dialog.show();
}
i prefer to use Material Dialog library because it's very easy to use.
you can use custom view or use the simple input
1.simple input example from my project:
new MaterialDialog.Builder(this)
.title(R.string.input)
.content(R.string.input_content)
.inputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD)
.input(R.string.input_hint, R.string.input_prefill, new MaterialDialog.InputCallback() {
#Override
public void onInput(MaterialDialog dialog, CharSequence input) {
// Do something
}
}).show();
2.custom view example :
View view = LayoutInflater.from(context).inflate(R.layout.dialog_new_queue, null, false);
MaterialDialog dialog = new MaterialDialog.Builder(context)
.customView(view, false).build();
AutoCompleteTextView tv = view.findViewById(R.id.dialog_add_q_tv);
Button btn = view.findViewById(R.id.dialog_add_queue_btn);
btn.setOnClickListener(v -> {
String input = tv.getText().toString().trim();
Observable
.create(emitter -> {
if (input.isEmpty()) {
emitter.onError(new IllegalArgumentException("اسم صف خالی است"));
} else if (dao.getQueue(pref.getLastUsername(), input) != null) {
emitter.onError(new IllegalArgumentException("این اسم تکراری است!"));
} else {
QueueModel q = new QueueModel(input, pref.getLastUsername(), System.currentTimeMillis());
dao.insert(q);
emitter.onComplete();
}
})
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(o -> {},e ->{
tv.setError(e.getMessage());
tv.requestFocus();
},() -> {
Toast.makeText(context, "صف جدید ساخته شد", Toast.LENGTH_SHORT).show();
dialog.dismiss();
loadData();
});
});
dialog.show();
Replace the 2 lines:
final String Name = UserName.
Person person = new Person(Name, 0);
By
EditText editText = (EditText) dialogView.findViewById(R.id.UserName);
Person person = new Person(editText.getText(), 0);
Try this
public void checkButton() {
leggspillere = (Button) findViewById(R.id.leggspillere);
AlertDialog.Builder builder = new AlertDialog.Builder(this);
LayoutInflater inflater = this.getLayoutInflater();
builder.setView(inflater.inflate(R.layout.create_user, null));
builder.setMessage("Lag en spiller");
builder.setCancelable(true);
builder.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
//Change here
Dialog myDialog = (Dialog) dialog;
EditText editText = (EditText) myDialog.findViewById(R.id.UserName);
Person person = new Person(editText.getText(), 0);
}
})
//sette andre knappen "cancel"
builder.setNegativeButton("no", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.dismiss();
}
});
builder.create().show();
}
I am using a slightly different approach in order to keep the dialog open when a button is pressed:
AlertDialog.Builder builder = new AlertDialog.Builder(NewTableActivity.this);
builder.setTitle(R.string.addComponent);
final EditText titleText = new EditText(NewTableActivity.this);
titleText.setHint(R.string.title);
builder.setView(titleText);
builder.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener(){
#Override
public void onClick(DialogInterface dialogInterface, int i){
dialogInterface.cancel();
}
});
builder.setPositiveButton(R.string.ok, null);
final AlertDialog alertDialog = builder.create();
alertDialog.setOnShowListener(new DialogInterface.OnShowListener(){
#Override
public void onShow(DialogInterface dialogInterface){
Log.i("TEst", "Doung");
Button button = alertDialog.getButton(AlertDialog.BUTTON_POSITIVE);
}
}
The dialog is opened a little lower (this works too), but the Log is never called
AlertDialog.Builder builder = new AlertDialog.Builder(NewTableActivity.this);
builder.setTitle(R.string.addComponent);
final EditText titleText = new EditText(NewTableActivity.this);
titleText.setHint(R.string.title);
builder.setView(titleText);
builder.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialogInterface, int i) {
dialogInterface.cancel();
}
});
builder.setPositiveButton(R.string.ok, null);
final AlertDialog alertDialog = builder.create();
alertDialog.setOnShowListener(new DialogInterface.OnShowListener() {
#Override
public void onShow(DialogInterface dialogInterface) {
Log.e("TEst", "Doung");
Button button = alertDialog.getButton(AlertDialog.BUTTON_POSITIVE);
}
});
alertDialog.show();
Just a quick question. I have an alert dialog popup that gives the user a short list of options.
I would like one of these items displayed bold. This is the basic code I'm using for the option list. I want "option1" to be bold, and all other options regular. Is there any way to achieve this without using custom textviews or something?
CharSequence options[] = new CharSequence[] {"option1", "option2", "option3"};
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Pick an option");
builder.setItems(options, new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
// my actions
}
});
builder.show();
You could do with SpannableString:
String b = "boldOption";
SpannableString boldOption = new SpannableString(b);
boldOption.setSpan(new StyleSpan(android.graphics.Typeface.BOLD), 0, b.length(),0);
CharSequence options[] = new CharSequence[] {boldOption, "option2", "option3"};
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Pick an option");
builder.setItems(options, new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
// my actions
}
});
builder.show();
You can use Html.fromHtml in your option1: Html.fromHtml("<b>option1</b>")
CharSequence options[] = new CharSequence[] {Html.fromHtml("<b>option1</b>"), "option2", "option3"};
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Pick an option");
builder.setItems(options, new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
// my actions
}
});
builder.show();
This is my GoogleMapAPI, I wish user can type the Marker information first, and then use that to make the marker.
But LogCat always say "String MarkerInfo = markerInfo.getText().toString();" Fail.
Sorry I'm a newbie for coding. Please help me.
public void onMapLongClick(final LatLng point) {
AlertDialog.Builder builder1 = new AlertDialog.Builder(this);
builder1.setTitle(R.string.funtion);
builder1.setItems(choice, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int selectedItem) {
Toast.makeText(MainActivity.this,"You Select Letter "+choice[selectedItem],Toast.LENGTH_SHORT).show();
dialog.dismiss();
if(selectedItem==1){
AlertDialog.Builder builder2;
Context mContext = MainActivity.this;
LayoutInflater inflater = getLayoutInflater();
builder2 = new AlertDialog.Builder(mContext);
markerInfo = (EditText)findViewById(R.id.markerInfo);
builder2.setView(inflater.inflate(R.layout.markerinfo, null))
.setPositiveButton(R.string.OK, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface marker, int id) {
String MarkerInfo = markerInfo.getText().toString();
map.addMarker(new MarkerOptions()
.position(point)
.title(MarkerInfo)
.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_RED)));
}
})
.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface marker, int id) {
}
});
builder2.setTitle(R.string.typeinfo);
AlertDialog marker = builder2.create();
marker.show();
}
}
});
AlertDialog alert = builder1.create();
alert.show();
}
Looks like R.id.markerInfo is declared insido markerinfo.xml, so you have to use the "inflated" version of markerinfo.xml to retrieve the EditText
View view = inflater.inflate(R.layout.markerinfo, null);
markerInfo = (EditText)view.findViewById(R.id.markerInfo);
builder2.setView(view);
I have an AlertDialog having the text "For more information, Please Call 1-800-200-1000".
Here is my Code for the Alert Dialog Display while clicking on the ListView Item :
final AlertDialog.Builder builder = new AlertDialog.Builder(this);
final SpannableString s = new SpannableString("1-800-200-1000");
Linkify.addLinks(s, Linkify.ALL);
ListView.setOnItemClickListener(new OnItemClickListener(){
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
builder.setMessage("For more information, Please Call "+s)
.setCancelable(true)
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.dismiss();
}
});
AlertDialog alert = builder.create();
alert.show();
}});
Here i wish to hyperlink the "1-800-200-1000" and while clicking on this, another dialog for call function should be implemented.
Here is my AlertDialog for Call Function:
final Builder builder = new AlertDialog.Builder(this);
builder.setMessage("Do you want to Call?");
builder.setCancelable(false);
builder.setPositiveButton("Call", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
Intent dial = new Intent();
dial.setAction("android.intent.action.DIAL");dial.setData(Uri.parse("tel:"+ Phone));
startActivity(dial);
}
});
builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// Toast.makeText(getApplicationContext(),"Activity will continue",Toast.LENGTH_LONG).show();
dialog.cancel();
}
});
AlertDialog dialog = builder.create();
dialog.show();
Please help me in two problems:
1.How to hyperlink the Phone number in the First Dialog?
2.how to embed the Call AlertDialog while clicking the Hyperlinked Phone Number?
Thanks in advance.
try this one:
Spannable spans = (Spannable) text;
ClickableSpan clickSpan = new ClickableSpan() {
#Override
public void onClick(View widget) {
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://"+ url));
}
public void updateDrawState(TextPaint ds) {
//override link-centric text appearance
}
};
int index = (text.toString()).indexOf(url);
spans.setSpan(clickSpan, index, url.length() + index,Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);