I'm trying to get the text from editText and input it into my database. The problem I am having is a NullPointerException once i try to submit the text. Any help would be appreciated.
ERROR:
java.lang.NullPointerException: Attempt to invoke virtual method 'android.text.Editable android.widget.EditText.getText()' on a null object reference
This is from my MainActivity:
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()){
case R.id.action_add_task:
final EditText text = (EditText)findViewById(R.id.editText);
AlertDialog dialog = new AlertDialog.Builder(this)
.setTitle("Add New Task")
.setMessage("what do you want to do next")
.setView(R.layout.custom_view)
.setPositiveButton("Add", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
String task;
task = text.getText().toString();
dbHelper.insertNewTask(task);
if(getFragmentRefreshListener()!=null) {
getFragmentRefreshListener().onRefresh();
}
loadTaskList();
}
})
.setNegativeButton("Cancel",null)
.create();
dialog.show();
return true;
}
return super.onOptionsItemSelected(item);
}
my custom_view.xml:
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter a task"
android:id="#+id/editText" />
my activity_main.xml:
<android.support.design.widget.TabLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/tablayout"
android:background="#color/colorPrimaryDark"
android:minHeight="?attr/actionBarSize"
>
</android.support.design.widget.TabLayout>
<android.support.v4.view.ViewPager
android:id="#+id/pager"
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:backgroundTint="#android:color/darker_gray"
android:visibility="visible">
<ListView
android:id="#+id/lstTask"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:text="#string/tasks"
android:textSize="30sp" />
</android.support.v4.view.ViewPager>
Well since I cant see the xml for MainActivity layout, here it's what most likely it's causing the NPE.
final EditText text = (EditText)findViewById(R.id.editText);
Your searching your edit text in you activity view hierarchy when (assuming from what is shown in the Dialog creation) you should be searching it in the Dialog view hierarchy.
.setView(R.layout.custom_view)
There you are setting that view as the view of the dialog and thats probably what is causing the exception. Try fining the reference to the edit text in the onClick method like this:
AlertDialog dialog = new AlertDialog.Builder(this)
.setTitle("Add New Task")
.setMessage("what do you want to do next")
.setView(R.layout.custom_view)
.setPositiveButton("Add", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
final EditText text = ((AlertDialog)dialog).findViewById(R.id.editText);
String task;
task = text.getText().toString();
dbHelper.insertNewTask(task);
if(getFragmentRefreshListener()!=null) {
getFragmentRefreshListener().onRefresh();
}
loadTaskList();
}
})
.setNegativeButton("Cancel",null)
.create();
dialog.show();
Here is what fixed my problem:
I changed this line:
final EditText text = ((AlertDialog)dialog).findViewById(R.id.editText);
To this:
final EditText text = (EditText) ((AlertDialog)dialog).findViewById(R.id.editText);
Related
So what I'm trying to do is to have an AlertDialog where the user can add their own groceries to the database. I want the program to give suggestions while the user is typing. I want to combine an AlertDialog with an AutoCompleteTextView. I followed this guide for the AutoComplete section https://developer.android.com/reference/android/widget/AutoCompleteTextView.html, but I can't seem to get it right.
In main:
public void add(View caller) {
MaterialAlertDialogBuilder mBuilder = new MaterialAlertDialogBuilder(mainActivity.this);
mBuilder.setTitle(R.string.dialog_title);
mBuilder.setView(R.layout.add_items_dialog);
mBuilder.setMessage("Enter a grocery");
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_dropdown_item_1line, GROCERIES);
AutoCompleteTextView textView = (AutoCompleteTextView) findViewById(R.id.name_text_field);
textView.setAdapter(adapter);
mBuilder.setPositiveButton("Add", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialogInterface, int which) {
}
});
mBuilder.setNegativeButton(R.string.dismiss_label, new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialogInterface, int i) {
dialogInterface.dismiss();
}
});
AlertDialog mDialog = mBuilder.create();
mDialog.show();
add_items_dialog.xml:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto">
<com.google.android.material.textfield.TextInputLayout
android:id="#+id/name_text_field"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="#dimen/dp_16"
android:layout_marginEnd="#dimen/dp_16"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:hint="#string/enter_text"
android:textColorHint="#color/colorDarkGreen"
style="#style/Widget.MaterialComponents.TextInputLayout.OutlinedBox">
<com.google.android.material.textfield.TextInputEditText
android:id="#+id/et_input"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:inputType="text"
android:singleLine="true" />
</com.google.android.material.textfield.TextInputLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
Error at the line textView.setAdapter(adapter):
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.AutoCompleteTextView.setAdapter(android.widget.ListAdapter)' on a null object reference
I guess that it has something to do with setting the adapter to the wrong textview but it can also be something entirely different. Any suggestions?
You need to change mbuilder.addView(R.layout.add_itemes_dialog)
by
View view =LayoutInflater.from(this).inflate(R.layout.add_itemes_dialog, null);
builder.setView(view);
// and to find your AutoCompleteTextView
AutoCompleteTextView textView = (AutoCompleteTextView) view.findViewById(R.id.et_input);
and also in your xml file change TextInputEditText by MaterialAutoCompleteTextView and it should work
I have Settings class. And I use preference inside. In the preference I use AlertDialog which has EditText and radioButton in it.
I want to get the values of the edit text and the radio button from my settings activity, but everything I see online is just about preference OR AlertDialog and I don't understand what to do if they are combined.
my Settings.java:
public class Settings extends AppCompatActivity {
private RadioGroup radioGroup;
AlertDialog.Builder builder;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.settings_activity);
getSupportFragmentManager()
.beginTransaction()
.replace(R.id.fragment_setting, new MySettingsFragment())
.commit();
}
public static class MySettingsFragment extends PreferenceFragmentCompat {
#Override
public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
setPreferencesFromResource(R.xml.preference, rootKey);
}
#Override
public boolean onPreferenceTreeClick(Preference p){
final EditText input = new EditText(this.getContext());
if(p.getKey().equals("appointment type")){
return true;
}
if(p.getKey().equals("time frame")){
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
LayoutInflater inflater = requireActivity().getLayoutInflater();
builder.setView(inflater.inflate(R.layout.set_time, null)).
setPositiveButton(R.string.save_settings, new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
**Here i dont know how to take the value**
}
})
.setNegativeButton(R.string.cancel_settings, new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
AlertDialog dialog = builder.create();
dialog.show();
}
my Prefernce.xml :
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen
xmlns:android="http://schemas.android.com/apk/res/android">
<EditTextPreference
android:key="appointment type"
android:title="Set appointment types"
android:icon="#drawable/appoitment_type"
/>
<Preference
android:id="#+id/timeFrame"
android:key="time frame"
android:title="Allow change meeting"
android:summary="Time frame where clients can't change a meeting"
android:onClick = "popUpWindow"
android:icon="#drawable/no_meetings_changes"
/>
<Preference
android:id="#+id/remindTime"
android:key="remind time"
android:title="Meetings reminder"
android:summary="How much time to remind client before a meeting"
android:icon="#drawable/send_reminder" />
</PreferenceScreen>
my setTime.xml (the dialog):
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/colorPrimaryLight">
<EditText
android:id="#+id/num_input"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="#string/default_num"
android:inputType="text" />
<RadioGroup
android:id="#+id/radio_group"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<RadioButton
android:id="#+id/minutes_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/minutes" />
<RadioButton
android:id="#+id/hours_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text = "#string/hours"/>
<RadioButton
android:id="#+id/days_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text = "#string/days"/>
<RadioButton
android:id="#+id/weeks_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text = "#string/weeks"/>
</RadioGroup>
</LinearLayout>
In your OnPreferenceTreeClick function in MySettingsFragment:
#Override
public boolean onPreferenceTreeClick(Preference p){
final EditText input = new EditText(this.getContext());
if(p.getKey().equals("appointment type")){
return true;
}
if(p.getKey().equals("time frame")){
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
LayoutInflater inflater = requireActivity().getLayoutInflater();
View dialogView = inflater.inflate(R.layout.set_time, null) // add this line
builder.setView(dialogView).
setPositiveButton(R.string.save_settings, new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
// take the value from selected radio button
RadioGroup radioGroup = (RadioGroup) dialogView.findViewById(R.id.radio_group);
int selectedId = radioGroup.getCheckedRadioButtonId();
// find the radiobutton by returned id
RadioButton radioButton = (RadioButton) findViewById(selectedId);
// get edittext value
EditText edittext = (EditText) dialogView.findViewById(R.id.num_input);
String edittextValue = edittext.getText().toString();
}
})
.setNegativeButton(R.string.cancel_settings, new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
AlertDialog dialog = builder.create();
dialog.show();
}
I am having troubles setting the two buttons on the lower part of the alert what I am trying to do is get a similar look to the IOS look of the buttons.
here is my current code:
public void openInstructions() {
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
final AlertDialog dialog = builder.setNegativeButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
setNewDateOnView();
dialog.cancel();
}
}).setPositiveButton("CANCEL", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
}).create();
TextView myMsg = new TextView(getActivity());
myMsg.setText("ExampleText ");
myMsg.setTextSize(15);
myMsg.setGravity(Gravity.CENTER_HORIZONTAL);
dialog.setView(myMsg);
dialog.setTitle("Confirm Date of Purchase");
dialog.setOnShowListener( new DialogInterface.OnShowListener() {
#Override
public void onShow(DialogInterface arg0) {
Button negativeButton = dialog.getButton(AlertDialog.BUTTON_NEGATIVE);
negativeButton.setTextColor(0xFFFF0000);
Button positiveButton = dialog.getButton(AlertDialog.BUTTON_POSITIVE);
positiveButton.setTextColor(0xFF0000FF);
}
});
dialog.show();
}
Example of how it looks right now:
How I wanted it to look:
To design Dialog like ios , we can create custom dialog using custom layout like this :
dialog_custom.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#android:color/transparent"
android:orientation="vertical"
app:layout_constraintHeight_max="wrap">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:background="#drawable/custom_bg">
<TextView
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_below="#+id/title"
android:gravity="center"
android:text="Confirm Date of Purchase"
android:textAlignment="center"
android:textColor="#android:color/black"
android:textSize="17sp" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_above="#+id/back_ll"
android:orientation="vertical"
android:padding="5dp">
<TextView
android:id="#+id/rebooking_single_tv"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="10dp"
android:paddingBottom="10dp"
android:textAlignment="center"
android:text="#string/dummuy"
android:textColor="#android:color/black"
android:textSize="17sp" />
</LinearLayout>
<LinearLayout
android:id="#+id/back_ll"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_below="#+id/end_time_recycle"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:background="#8F8F8F" />
<LinearLayout
android:id="#+id/rebooking_back_button_ll"
android:layout_width="match_parent"
android:layout_height="50dp"
android:orientation="horizontal"
android:gravity="center">
<TextView
android:id="#+id/ok_btn"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight=".49"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:text="Ok"
android:textAlignment="center"
android:textColor="#FF6363"
android:textSize="17sp"
android:textStyle="bold" />
<View
android:layout_width="0dp"
android:layout_weight=".01"
android:layout_height="match_parent"
android:layout_below="#+id/end_time_recycle"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:background="#8F8F8F" />
<TextView
android:id="#+id/cancel_btn"
android:layout_width="0dp"
android:layout_weight=".5"
android:layout_height="wrap_content"
android:paddingLeft="10dp"
android:textAlignment="center"
android:paddingRight="10dp"
android:text="Cancle"
android:textColor="#7BC5FF"
android:textSize="17sp"
android:textStyle="bold" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
</RelativeLayout>
Main_Activity
final Dialog dialog=new Dialog(FirstActivity.this);
dialog.setCancelable(false);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
dialog.setContentView(R.layout.dialog_custom);
TextView cancel_btn=dialog.findViewById(R.id.cancel_btn);
cancel_btn.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View view){
dialog.dismiss();
}
});
TextView ok_btn=dialog.findViewById(R.id.ok_btn);
ok_btn.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View view){
dialog.dismiss();
}
});
dialog.show();
Well, as a start, google recommends you use the Android Design Guidelines when creating Android apps, really you should be sticking to those as that's what your users will be used to (not Apple's).
Also there's no reason to be assigning the dialog a TextView if all you're doing is showing text, the AlertDialog provides this functionality with setMessage.
If that hasn't convinced you to stick to the normal design rules, the only other option would be creating a Custom View and assign it to the AlertDialog with setView(similar to how you're doing it with the TextView).
Here's a tutorial on creating a Custom View
You can create a custom view such as the one sent and inflate it
Example:
AlertDialog.Builder mBuilder = new AlertDialog.Builder(getActivity());
View mView=getActivity().getLayoutInflater().inflate(R.layout.custom_layout, null);
mBuilder.setView(mView);
Button close = (Button) mView.findViewById(R.id.close);
Button OK = (Button) mView.findViewById(R.id.ok);
close.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//close the dialog
hideDialog1();
}
});
OK.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//do something
}
});
dialog = mBuilder.create();
dialog.setCanceledOnTouchOutside(false);
dialog.show();
Use .setNeutralButton that make place left most
TextView textview;
Button button;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button =(Button)findViewById(R.id.button1);
textview = (TextView)findViewById(R.id.textView1);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
new AlertDialog.Builder(MainActivity.this).setIcon(R.drawable.ic_launcher_background)
.setTitle("Alert Dialog Box Title")
.setMessage("Are you sure( Alert Dialog Message )")
.setPositiveButton("YES", new DialogInterface.OnClickListener()
{
#Override
public void onClick(DialogInterface dialog, int which)
{
Toast.makeText(MainActivity.this, "You Clicked on Yes", Toast.LENGTH_SHORT).show();
}
})
.setNegativeButton("Cancel", new DialogInterface.OnClickListener()
{
#Override
public void onClick(DialogInterface dialog, int which)
{
Toast.makeText(MainActivity.this, "You Clicked on Cancel", Toast.LENGTH_SHORT).show();
}
})
.setNeutralButton("NO", new DialogInterface.OnClickListener()
{
#Override
public void onClick(DialogInterface dialog, int which)
{
Toast.makeText(MainActivity.this, "You Clicked on NO", Toast.LENGTH_SHORT).show();
}
})
.show();
}
});
}
===============================================
I am working with an application that involves to dialog boxes that should appear one after the other. What I mean is that, when I click on the positive button of the first dialog box it should then bring up another second dialog box. The issue I am facing is with the second dialog box, the second one will not pop up and it causes the application to crash.
I get an error that says "java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first."
public static void showBusinessOrPrivateStartDialog(final Context context, final CallLogEntry call)
{
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M)
{
// If new version of android, then check permission
if (!Settings.canDrawOverlays(context))
{
Log.e(TAG, "Permission Denied: Draw overlays (for popup)");
return;
}
}
LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
//View view = inflater.inflate(R.layout.dialog_call_type, null);
AlertDialog.Builder builder = new AlertDialog.Builder(context, Theme_Material_Light_Dialog_Alert);
builder.setTitle("Was this a private or a business call?");
builder.setMessage("Please select a call type");
builder.setIcon(R.drawable.ic_deviceinsight);
builder.setCancelable(true);
//builder.setView(view);
builder.setOnCancelListener(new DialogInterface.OnCancelListener() {
#Override
public void onCancel(DialogInterface dialog) {
// Occurs when user cancels the dialog, or clicks somewhere else in the screen or presses the back button
Log.i(TAG, "cancelled");
saveCallEntry(context, call, CLASSIFICATION_TYPE_BUSINESS, "", true, "");
}
});
builder.setPositiveButton("Business", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
CallClassification.showBusinessCallPopup(context, call);
}
});
builder.setNegativeButton("Private", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
saveCallEntry(context, call, CLASSIFICATION_TYPE_PRIVATE, "", false, "");
}
});
AlertDialog dialog = builder.create();
// Rather use something like TYPE_SYSTEM_ALERT, if possible.
dialog.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT); // TYPE_SYSTEM_ERROR
dialog.show();
}
This is the second method for the second dialog box and my app crashes on dialog.show
public static void showBusinessCallPopup(final Context context, final CallLogEntry call)
{
// NEW NRF
if (AppPreferences.isNRF(context)) {
CallItem callItem = saveCallEntry(context, call, CLASSIFICATION_TYPE_BUSINESS, "", false, "");
// SHOW NEW FORM
Intent intent = new Intent(context, CallClassificationFormActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
EventBus.getDefault().postSticky(callItem);
context.startActivity(intent);
return;
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M)
{
// If new version of android, then check permission
if (!Settings.canDrawOverlays(context))
{
Log.e(TAG, "Permission Denied: Draw overlays (for popup)");
return;
}
}
LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.layout.dialog_call_details, null);
AlertDialog.Builder builder = new AlertDialog.Builder(context, Theme_Material_Light_Dialog_Alert);
String toOrFrom = call.Type == CallLog.Calls.INCOMING_TYPE ? "from" : "to";
//builder.setTitle("Business call");
builder.setCustomTitle(view);
//builder.setMessage("Please enter reference details for the call " + toOrFrom + " " + call.Number);
//builder.setIcon(R.drawable.ic_deviceinsight);
builder.setCancelable(true);
builder.setView(view);
final EditText etReference = (EditText)view.findViewById(R.id.etReference);
final EditText etComment = (EditText)view.findViewById(R.id.etComment);
builder.setPositiveButton("Confirm", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
String reference = etReference.getText().toString();
String comment = etComment.getText().toString();
saveCallEntry(context, call, CLASSIFICATION_TYPE_BUSINESS, reference, false, comment);
}
});
builder.setOnCancelListener(new DialogInterface.OnCancelListener() {
#Override
public void onCancel(DialogInterface dialog) {
Log.i(TAG, "cancelled");
}
});
AlertDialog dialog = builder.create();
dialog.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT); // TYPE_SYSTEM_ERROR
dialog.show();
}
and here is my xml. I have included the xml in this post because I read somewhere that the way the height and width of my textboxes is defined
might be causing an issue.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:paddingLeft="5dp"
android:paddingRight="5dp"
android:paddingTop="5dp"
tools:context=".calls.CallClassification">
<TextView
android:id="#+id/tvMessage"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="5dp"
android:padding="5dp"
android:text="Please enter reference details for the call "
android:textColor="#color/colorPrimaryDark"
android:textSize="16sp"/>
<AutoCompleteTextView
android:id="#+id/etReference"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:backgroundTint="#color/colorAccent"
android:hint="Matter Code *"
android:imeOptions="actionNext"
android:inputType="text"
android:maxLength="255"
android:text=""
android:textColor="#color/colorPrimary"
android:textColorHint="#color/colorTextHint"
android:textSize="14sp"/>
<CheckBox
android:id="#+id/cbBillable"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:button="#null"
android:buttonTint="#color/colorAccent"
android:checked="true"
android:drawableRight="?android:attr/listChoiceIndicatorMultiple"
android:paddingBottom="3dp"
android:paddingTop="3dp"
android:text="Billable"
android:textColor="#color/colorPrimaryDark"
android:textSize="14sp"/>
<EditText
android:id="#+id/etComment"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:backgroundTint="#color/colorAccent"
android:hint="Enter comment"
android:imeOptions="actionDone"
android:inputType="text"
android:lines="1"
android:maxLength="1000"
android:maxLines="4"
android:minLines="1"
android:text=""
android:textColor="#color/colorPrimary"
android:textColorHint="#color/colorTextHint"
android:textSize="14sp"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<Button
android:id="#+id/bCancel"
style="#style/Widget.AppCompat.Button.Borderless.Colored"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Cancel"/>
<Button
android:id="#+id/bOk"
style="#style/Widget.AppCompat.Button.Borderless.Colored"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="GO"/>
</LinearLayout>
</LinearLayout>
After hearing to your experience, I could think of one more solution. Rather than declaring AlertDialog variable inside each of the methods, declare one global variable so that you can make sure that only one Dialog is shown at any instance.
While declaring, make
AlertDialog dialog = null;
in class
Then in,
showBusinessOrPrivateStartDialog() {
if(dialog == null) {
dialog = build.create();
}
}
builder.setPositiveButton("Business", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
CallClassification.showBusinessCallPopup(context, call);
}
});
builder.setOnDismissListener(new OnDismissListener() {
public void onDismiss() {
dialog = null;
}
}
Then again in showBusinessCallPopup(),
if(dialog == null) {
dialog = build.create();
}
Hope this helps you.
Before calling
CallClassification.showBusinessCallPopup(context, call);
you need to call
dialog.dismiss();
within the same onClick().
Hope this solves your case.
In your second code you are adding view twice, maybe you should remove one of them.
builder.setCustomTitle(view); -> Here
builder.setCancelable(true);
builder.setView(view); --> Here
:)
Sorry for bothering again, but in my Android keyboard I have
public class zyz extends InputMethodService
implements KeyboardView.OnKeyboardActionListener {
private LinearLayout mInputView;
#Override public View onCreateInputView() {
mInputView = (LinearLayout) getLayoutInflater().inflate(R.layout.main, null);
AddKeys("/layout.txt");
return mInputView;
}
...
final Dialog dialog = new Dialog(this,R.style.myBackgroundStyle);
...
linLayBtn.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
...
dialog.setContentView(R.layout.dialog);
dialog.show();
...
Which is supposed to show a dialog when a button is presses. Yet it crashes the application...
08-30 17:04:41.554: ERROR/AndroidRuntime(15712): android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application
Any ideas how to fix it? Thanks!
dialog.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/layout_root"
android:layout_height="wrap_content" android:layout_width="wrap_content" android:orientation="horizontal">
<ImageView android:layout_height="wrap_content" android:layout_width="wrap_content" android:src="#drawable/frame_left" android:id="#+id/frameLeft"></ImageView>
<LinearLayout android:layout_width="wrap_content" android:layout_height="match_parent" android:background="#drawable/frame_center" android:id="#+id/LLdialog4letter">
<ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="#drawable/bun_r" android:id="#+id/ivDialogLetter" android:layout_marginTop="3px" android:layout_marginRight="9px" android:layout_marginLeft="9px"></ImageView>
</LinearLayout>
<ImageView android:layout_height="wrap_content" android:src="#drawable/frame_right" android:layout_width="wrap_content" android:id="#+id/frameRight"></ImageView>
</LinearLayout>
This will work for you Enjoy
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Make your selection");
builder.setItems(items, new DialogInterface.OnClickListener()
{
public void onClick(DialogInterface dialog, int item)
{
// Do something with the selection
}
});
AlertDialog alert = builder.create();
Window window = alert.getWindow();
WindowManager.LayoutParams lp = window.getAttributes();
lp.token = mInputView.getWindowToken();
lp.type = WindowManager.LayoutParams.TYPE_APPLICATION_ATTACHED_DIALOG;
window.setAttributes(lp);
window.addFlags(WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM);
alert.show();