code doesn't work alert Dialog - android

I'm just trying to show an android Dialog but my activity crash every times.
i want to show a dialog which contains 2 EditText fields and 2 buttons.
I'm trying like this, what am i doing wrong or may be there is an easiest way to do that?
public class OsmRoadTaskActivity extends Activity {
Button Chercher, Annuler;
EditText startpoint, endpoint;
Context ctx;
#SuppressLint("CutPasteId")
#SuppressWarnings("unused")
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ctx = this.getApplicationContext();
setContentView(R.layout.activity_main);
Chercher = (Button) findViewById(R.id.chercher);
Annuler = (Button) findViewById(R.id.annuler);
final AlertDialog.Builder alert = new AlertDialog.Builder(this);
final AlertDialog alertd = alert.create();
alert.setTitle("Itineraire");
LayoutInflater inflater = this.getLayoutInflater();
View layout = inflater.inflate(R.layout.activity_main, null, false);
/*
* final EditText startpoint = new EditText(OsmRoadTaskActivity.this);
* LinearLayout.LayoutParams lp = new
* LinearLayout.LayoutParams(LinearLayout
* .LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.MATCH_PARENT);
* startpoint.setLayoutParams(lp); alert.setView(startpoint);
*/
// final EditText startpoint =
// (EditText)layout.findViewById(R.id.endpoint);
final EditText startpoint = new EditText(this);
final EditText endpoint = (EditText) layout.findViewById(R.id.endpoint);
alert.setView(endpoint);
alert.setView(startpoint);
alert.setView(layout);
alert.setView(Annuler);
alert.setView(Chercher);
alert.setPositiveButton("Chercher",
new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
new OsmRoadTask(ctx, startpoint.getText(), endpoint
.getText()).execute();
}
});
alert.setNegativeButton("Annuler",
new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
alertd.dismiss();
}
});
alert.show();
}
}

Create the AlertDialog object after initializing the builder.
public class OsmRoadTaskActivity extends Activity {
Button Chercher, Annuler;
EditText startpoint, endpoint;
Context ctx;
#SuppressLint("CutPasteId")
#SuppressWarnings("unused")
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ctx = this.getApplicationContext();
setContentView(R.layout.activity_main);
Chercher = (Button) findViewById(R.id.chercher);
Annuler = (Button) findViewById(R.id.annuler);
final AlertDialog.Builder alert = new AlertDialog.Builder(this);
alert.setTitle("Itineraire");
LayoutInflater inflater = this.getLayoutInflater();
View layout = inflater.inflate(R.layout.activity_main, null, false);
/*
* final EditText startpoint = new EditText(OsmRoadTaskActivity.this);
* LinearLayout.LayoutParams lp = new
* LinearLayout.LayoutParams(LinearLayout
* .LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.MATCH_PARENT);
* startpoint.setLayoutParams(lp); alert.setView(startpoint);
*/
// final EditText startpoint =
// (EditText)layout.findViewById(R.id.endpoint);
final EditText startpoint = new EditText(this);
final EditText endpoint = (EditText) layout.findViewById(R.id.endpoint);
alert.setView(endpoint);
alert.setView(startpoint);
alert.setView(layout);
alert.setView(Annuler);
alert.setView(Chercher);
alert.setPositiveButton("Chercher",
new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
new OsmRoadTask(ctx, startpoint.getText(), endpoint
.getText()).execute();
}
});
alert.setNegativeButton("Annuler",
new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
alertd.dismiss();
}
});
final AlertDialog alertd = alert.create();
alertd.show();
}
}

Related

AlertDialog doesn't wrap my content

I'm using an alert dialog to display a view with spinner to set the period and the scale of a chart
But it doesn't wrap my content and all the tuto I tried failed...
My code
LinearLayout vue = new LinearLayout(context);
vue.setOrientation(LinearLayout.VERTICAL);
vue.addView(diag.getView());
choixPeriode = (Spinner) vue.findViewById(R.id.spinner_periode);
choixEchelle = (Spinner) vue.findViewById(R.id.spinner_echelle);
if(choixEchelle == null)
choixEchelle = new Spinner(context);
final int oldPeriode = DiagramController.PeriodeToPos(this.periode);
final int oldEchelle = DiagramController.echelleToPos(this.echelle);
choixPeriode.setSelection(oldPeriode);
choixEchelle.setSelection(oldEchelle);
LinearLayout containerButton = new LinearLayout(context);
containerButton.setGravity(Gravity.CENTER);
final ImageView croix = ((ImageView) vue.findViewById(R.id.croix));
final Button ok = new Button(context);
ok.setText(R.string.valider);
final Button cancel = new Button(context);
cancel.setText(android.R.string.cancel);
containerButton.addView(ok);
containerButton.addView(cancel);
vue.addView(containerButton);
AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(context);
dialogBuilder.setView(vue);
final AlertDialog alertDialog = dialogBuilder.create();
WindowManager.LayoutParams lp = new WindowManager.LayoutParams();
lp.copyFrom(alertDialog.getWindow().getAttributes());
lp.width = WindowManager.LayoutParams.WRAP_CONTENT;
lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
alertDialog.getWindow().setAttributes(lp);
alertDialog.show();
croix.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
alertDialog.dismiss();
}
});
ok.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
periode = DiagramController.posToPeriode(choixPeriode.getSelectedItemPosition());
echelle = DiagramController.posToEchelle(choixEchelle.getSelectedItemPosition());
changePeriode();
alertDialog.dismiss();
}
});
cancel.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
alertDialog.dismiss();
}
});
diag.getView() return a LinearLayout with the title, the spinners and the chart.
Result in portrait
Result in landscape
Result with custom Dialog
1.) You will need to create a custom xml layout.
2.) Create a new class. And write your code in the onCreate.
public class MyDialog extends Dialog {
private TextView textView;
private Context context;
public MyDialog(Context context) {
super(context);
this.context = context;
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.my_layout);
//Your code
textView = (TextView) findViewById(R.id.textView);
}
}

EditText value in dialog not being stored

I have an application with a listview and a button on screen that opens a dialog box. Dialog box has one EditText field where user can input text and click Add. onClickListener for Add button makes a async call and passes text to api. Problem is EditText value is not being saved. I cant even get it to show in the Log. Im sure Im missing something simple. Your help is imprecated. Here is the code.
SignInActivity.java
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sign_in);
Log.v("SignInActivity","Activity has began");
final DancerAdapter adapter = new DancerAdapter(this,oneDancerArrayList,1);
Log.v("SignInActivity","DancerAdapter");
listView = (ListView) findViewById(R.id.listView);
listView.setAdapter(adapter);
getGirlList();
nameInput = (EditText) findViewById(R.id.cr_room_name);
loginDancer = (Button) findViewById(R.id.addDancer);
loginDancer.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
DialogFragment newFragment = new AddDancerDialog();
newFragment.show(getFragmentManager(), "newDancer");
}
});
static public class AddDancerDialog extends DialogFragment {
#Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setView(R.layout.signin_dialog);
LayoutInflater inflater = LayoutInflater.from(getContext());
final View dialogview = inflater.inflate(R.layout.signin_dialog, null);
//final EditText dancerName = (EditText)dialogview.findViewById(R.id.etDancerName);
builder.setMessage("Login New Dancer")
.setPositiveButton("Add", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
final ProgressBar newProgress = (ProgressBar)dialogview.findViewById(R.id.progressBar);
final EditText dancerName = (EditText)dialogview.findViewById(R.id.etDancerName);
String nameSubmit = dancerName.getText().toString();
Log.v("SignInActivity-Dialog",nameSubmit);
newProgress.setVisibility(View.VISIBLE);
AsyncHttpClient client = new AsyncHttpClient();
RequestParams params = new RequestParams();
params.put("action", "addDancer");
params.put("name",nameSubmit);
Log.v("SignInActivity-Dialog", "Add Dancer Function");
Log.v("SignInActivity-Dialog",dancerName.getText().toString());
client.post("http://peekatu.com/apiweb/girlList.php", params,
new AsyncHttpResponseHandler() {
#Override
public void onSuccess(String response) {
Log.v("response", response);
responseString2 = response;
//parseDancerList(response);
Log.v("SignInActivity",response);
}
#Override
public void onFailure(Throwable error, String content) {
Log.v("response", "response failed network error");
//waitncall(true);
}
});
}
})
.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
}
});
return builder.create();
}
}
}
Problem is here:
Here you set a view to dialog
builder.setView(R.layout.signin_dialog);
Then, you search a EditText in this view:
LayoutInflater inflater = LayoutInflater.from(getContext());
final View dialogview = inflater.inflate(R.layout.signin_dialog, null);
final EditText dancerName = (EditText)dialogview.findViewById(R.id.etDancerName);
That dialogView was inflated but it is not the view that was added to
dialog. They are different..
I think you can fix as follows:
#Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
LayoutInflater inflater = LayoutInflater.from(getContext());
final View dialogview = inflater.inflate(R.layout.signin_dialog, null);
builder.setView(dialogview);
...
}

Spinner from editext android

My Scenario:
When I click the top (+)icon there is a dialog displayed with editext and If I enter some text and click ok button the text should be added to my spinner which I am unable to do it.
Here is what I mean to say:
This is what I have done:
protected void showInputDialog() {
// get prompts.xml view
LayoutInflater layoutInflater = LayoutInflater.from(MainActivity.this);
View promptView = layoutInflater.inflate(R.layout.input_dialog, null);
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
MainActivity.this);
alertDialogBuilder.setView(promptView);
// setup a dialog window
alertDialogBuilder
.setCancelable(false)
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// Spinner element
listsp = (Spinner) findViewById(R.id.listspinner);
listtext = (EditText) findViewById(R.id.list_text);
list = new ArrayList<String>();
list.add(listtext.getText().toString());
listadapter = new ArrayAdapter<String>(getApplicationContext(),
android.R.layout.simple_spinner_item, list);
listadapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
listsp.setAdapter(adapter);
}
})
.setNegativeButton("Cancel",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
});
// create an alert dialog
AlertDialog alert = alertDialogBuilder.create();
alert.show();
}
Try to update the adapter outside the onClick :
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//Whatever else
listsp = (Spinner) findViewById(R.id.listspinned);
list = new ArrayList<String>();
listadapter = new MyArrayAdapter(getApplicationContext(), android.R.layout.simple_spinner_item, list);
listadapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
listsp.setAdapter(adapter);
}
protected void showInputDialog() {
// get prompts.xml view
LayoutInflater layoutInflater = LayoutInflater.from(MainActivity.this);
View promptView = layoutInflater.inflate(R.layout.input_dialog, null);
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
MainActivity.this);
alertDialogBuilder.setView(promptView);
// setup a dialog window
alertDialogBuilder
.setCancelable(false)
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
listtext = (EditText) findViewById(R.id.list_text);
updateAdapter(listtext.getText().toString());
}
})
.setNegativeButton("Cancel",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
});
// create an alert dialog
AlertDialog alert = alertDialogBuilder.create();
alert.show();
}
protected void updateAdapter(String input) {
list.add(input);
listadapter.notifyDataSetChanged();
}
EDIT : Here's how to implement your custom adapter (I made it private so it'd use the same dataList. Therefore, you don't need to call any updateData() function, just to notify the adapter that the data has changed with notifyDataSetChanged()) :
private class MyArrayAdapter extends BaseAdapter implements SpinnerAdapter {
#Override
public int getCount() {
return list.size();
}
#Override
public Object getItem(int position) {
return list.get(position);
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(int position, View view, ViewGroup parent) {
TextView text = new TextView(lexs);
text.setText(list.get(position).getName());
return text;
}
}

dialog.dismiss() doesn't work on item click inside alertDialog

Why dialog.dismiss() doesn't work after click on item inside the AlertDialog ?
I have also tried alert.dismiss but i get the error dialog cannot be resolved. How can i solve that?
Here's the dialog
protected void myMarkersDialog() {
final String name = prefs.getString("Name", "");
String nameTwoo = prefs.getString("NameTwoo", "");
final AlertDialog.Builder builder = new AlertDialog.Builder(this);
LayoutInflater inflater = getLayoutInflater();
View convertView = (View) inflater.inflate(R.layout.my_markers_listview, null);
builder.setView(convertView);
builder.setTitle("List");
lv = (ListView) convertView.findViewById(R.id.listView1);
if (prefs.contains("Name") || prefs.contains("NameTwoo"))
{
String[] values = new String[] {name,nameTwoo};
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,values);
lv.setAdapter(adapter);
}
lv.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long arg) {
String selected;
selected = lv.getItemAtPosition(position).toString();
if(selected.equalsIgnoreCase(name))
{
mapView.getMapViewPosition().setCenter(myMarkerGeopoint);
dialog.dismiss();
}
}
});
builder.setNeutralButton(getResources().getString(R.string.no_dialog), new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss(); // I get error!
}
});
AlertDialog alert = builder.create();
alert.show();
}
Declare the AlertDialog before build it and use it to dismiss.
AlertDialog alert;
protected void myMarkersDialog() {
final String name = prefs.getString("Name", "");
String nameTwoo = prefs.getString("NameTwoo", "");
final AlertDialog.Builder builder = new AlertDialog.Builder(this);
LayoutInflater inflater = getLayoutInflater();
View convertView = (View) inflater.inflate(R.layout.my_markers_listview, null);
builder.setView(convertView);
...
builder.setNeutralButton(getResources().getString(R.string.no_dialog), new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
alert.dismiss();
}
});
alert = builder.create();
alert.show();
}

unable to retrieve edittext value

i am trying to fetch the value in src and dest once the user clicks the navigate button but its always null. can some one plz help
static String source = "currentLocation";
static String destination = null;
private void getSrcDest(){
LayoutInflater factory = LayoutInflater.from(this);
final View textEntryView = factory.inflate(R.layout.srcdest, null);
final AlertDialog.Builder alert = new AlertDialog.Builder(this);
alert.setIcon(R.drawable.srcdest_icon);
alert.setTitle("Enter Source & Destination");
alert.setView(textEntryView);
final EditText sourceText = (EditText) findViewById(R.id.sourcetext);
final EditText destinationText = (EditText) findViewById(R.id.desttext);
alert.setPositiveButton("Navigate", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
try{
Toast.makeText(getApplicationContext(), sourceText.getText().toString()+destinationText.getText().toString(),
Toast.LENGTH_SHORT).show();
source = sourceText.getText().toString();
destination = destinationText.getText().toString();
System.out.println("----------------Source:"+source);
System.out.println("-----------Destination:"+destination);
}
catch(Exception e){
System.out.println("----------------Source:"+source);
System.out.println("-----------Destination:"+destination);
//if(destination.equals(null)){
final AlertDialog.Builder wrongAddressAlert = new AlertDialog.Builder(MainActivity.this);
wrongAddressAlert.setTitle("Destination Address Cannot be Null");
wrongAddressAlert.setPositiveButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
getSrcDest();
}
});
wrongAddressAlert.show();
//}
}
}
});
alert.setNegativeButton("Cancel",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
dialog.cancel();
}
});
alert.show();
}
I am assuming that you are having those edit text fields in srcdest.xml Layout. If yes, then you are initializing your edit texts in a wrong way. Try to initialize them this way:
LayoutInflater factory = LayoutInflater.from(this);
final View textEntryView = factory.inflate(R.layout.srcdest, null);
final EditText sourceText = (EditText) textEntryView.findViewById(R.id.sourcetext);
final EditText destinationText = (EditText) textEntryView.findViewById(R.id.desttext);

Categories

Resources