gridView.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View v,
int position, long id) {
AlertDialog.Builder builder;
Context mContext = getActivity();
LayoutInflater inflater = (LayoutInflater) mContext
.getSystemService(getActivity().LAYOUT_INFLATER_SERVICE);
View layout = inflater
.inflate(R.layout.alertdiaolog_main, null);
GridView gridView2 = (GridView) layout
.findViewById(R.id.alert_gridview);
gridView2.setAdapter(new AlertImageAdapter(getActivity()));
gridView2.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View v,
int position, long id) {
}
});
builder = new AlertDialog.Builder(mContext);
builder.setNegativeButton("Close",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
});
builder.setView(layout);
builder.show();
}
});
I have this code. when you click gridView item. an alertdialog will be showns with gridview also. How do I close the alertdialog if the user click on the gridView2 items?
When you build the AlertDialog using AlertDialog Builder
AlertDialog.Builder builder = new AlertDialog.Builder(mContext);
Get the instance of AlertDialog using create
Dialog dialog = builder.create();
So when required , you can dismiss by calling
dialog.dismiss();
Related
I have a custom listview , what im trying to do is when a user select a specific value in lisview it will goto editText inside a dialog in the same activity.. but it wont get the value.. here's my code
public void savedNotes(){
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View arg1, final int position,
long arg3) {
LayoutInflater li = LayoutInflater.from(context);
View promt = li.inflate(R.layout.prompt_saved_notes,null);
AlertDialog.Builder alerDialogBuilder = new AlertDialog.Builder(context);
alerDialogBuilder.setView(promt);
final EditText textfield1 = (EditText) promt.findViewById(R.id.edt_textfield);
alerDialogBuilder.setCancelable(false).setPositiveButton("saved",new DialogInterface.OnClickListener(){
public void onClick(DialogInterface dialog,int id){
String itemString = list.getItemAtPosition(position).toString();
textfield1.setText(itemString);
}
}).setNegativeButton("cancel",new DialogInterface.OnClickListener(){
public void onClick(DialogInterface dialog,int id){
dialog.cancel();
}
});
AlertDialog alertDialog = alerDialogBuilder.create();
alertDialog.show();
}
});
}
Try this..
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View arg1, final int position,
long arg3) {
LayoutInflater li = LayoutInflater.from(context);
View promt = li.inflate(R.layout.prompt_saved_notes,null);
AlertDialog.Builder alerDialogBuilder = new AlertDialog.Builder(context);
alerDialogBuilder.setView(promt);
final EditText textfield1 = (EditText) promt.findViewById(R.id.edt_textfield);
String itemString = list.getItemAtPosition(position).toString();
textfield1.setText(itemString);
alerDialogBuilder.setCancelable(false).setPositiveButton("saved",new DialogInterface.OnClickListener(){
public void onClick(DialogInterface dialog,int id){
}
}).setNegativeButton("cancel",new DialogInterface.OnClickListener(){
public void onClick(DialogInterface dialog,int id){
dialog.cancel();
}
});
AlertDialog alertDialog = alerDialogBuilder.create();
alertDialog.show();
}
});
Hope this will help you.
You can't get the value from a custom listview with
String itemString = list.getItemAtPosition(position).toString();
Instead you might have used an array or an arraylist to store the data that you are setting inside the listview. Inside the onItemCLick() type
//For arraylist
String itemString = your_arrayList.get(position);
//For array
String itemString = your_array[position];
By your code, this "itemString" will appear in edittext only when you click/tap the "Saved" button in the dialog.
You are setting value on EditText after clicking the positive("saved") button of dialog, but when your dialog is already visible EditText is not having any value.
First you need to remove the code from here:-
alerDialogBuilder.setCancelable(false).setPositiveButton("saved",new DialogInterface.OnClickListener(){
public void onClick(DialogInterface dialog,int id){
//Remove these two line code from here
String itemString = list.getItemAtPosition(position).toString();
textfield1.setText(itemString);
}
}
Now add these two line just after finding the reference of EditText in alert dialog like :-
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View arg1, final int position,
long arg3) {
LayoutInflater li = LayoutInflater.from(context);
View promt = li.inflate(R.layout.prompt_saved_notes,null);
AlertDialog.Builder alerDialogBuilder = new AlertDialog.Builder(context);
alerDialogBuilder.setView(promt);
final EditText textfield1 = (EditText) promt.findViewById(R.id.edt_textfield);
String itemString = list.getItemAtPosition(position).toString();
textfield1.setText(itemString);
alerDialogBuilder.setCancelable(false).setPositiveButton("saved",new DialogInterface.OnClickListener(){
public void onClick(DialogInterface dialog,int id){
}
}).setNegativeButton("cancel",new DialogInterface.OnClickListener(){
public void onClick(DialogInterface dialog,int id){
dialog.cancel();
}
});
AlertDialog alertDialog = alerDialogBuilder.create();
alertDialog.show();
}
});
}
I have a listView inside a popUp window and I want that when the user clicks on an item in the listview, the popUp window will automatically close. Any idea how I can do that?
public void popUp(){
final LayoutInflater layoutInflater = LayoutInflater.from(Record.this);
final View promptView = layoutInflater.inflate(R.layout.input_language, null);
final AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(Record.this);
alertDialogBuilder.setView(promptView);
String[] languages = {"Arabic","Bulgarian","Catalan"};
ListView list = (ListView) promptView.findViewById(R.id.inputlang);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, languages);
list.setAdapter(adapter);
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
String from_language = ((TextView)view).getText().toString();
Toast.makeText(getBaseContext(), from_language, Toast.LENGTH_LONG).show();
// CLOSE POPUP WINDOW
}
});
// setup a dialog window
alertDialogBuilder.setCancelable(false)
.setNegativeButton("Cancel",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
});
// create an alert dialog
AlertDialog alert = alertDialogBuilder.create();
alert.show();
}
Put this code after alert.show();
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
String from_language = ((TextView)view).getText().toString();
Toast.makeText(getBaseContext(), from_language, Toast.LENGTH_LONG).show();
// CLOSE POPUP WINDOW
alert.dismiss();
}
});
You can do this by changing the order of initialization of the dialog:
public void popUp(){
final ListView promptView = new ListView(this);
final AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(this);
alertDialogBuilder.setView(promptView);
// setup a dialog window
alertDialogBuilder.setCancelable(false)
.setNegativeButton("Cancel",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
});
// create an alert dialog
final AlertDialog alert = alertDialogBuilder.create();
String[] languages = {"Arabic","Bulgarian","Catalan"};
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, languages);
promptView.setAdapter(adapter);
promptView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
String from_language = ((TextView)view).getText().toString();
Toast.makeText(getBaseContext(), from_language, Toast.LENGTH_LONG).show();
// CLOSE POPUP WINDOW
alert.dismiss();
}
});
alert.show();
}
I have an AlertDialog with the GridView to choose a color, now I want to close the AlertDialog when click item . How can I do?
private void Alert_Colori() {
final AlertDialog.Builder customDialog = new AlertDialog.Builder(this);
LayoutInflater inflater = this.getLayoutInflater();
View view = inflater.inflate(R.layout.color_picker, null);
GridView gridViewColors = (GridView) view.findViewById(R.id.gridViewColors);
gridViewColors.setAdapter(new ColorPickerAdapter(this));
gridViewColors.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//here close the dialog
}
});
customDialog.setView(view);
customDialog.setNegativeButton("Annulla", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int arg1) {
dialog.dismiss();
}
});
customDialog.setView(view);
customDialog.show();
}
First Add
final Dialog dialog = customDialog .create();
after the line
final AlertDialog.Builder customDialog = new AlertDialog.Builder(this);
And then add this in your code
dialog.dismiss();
to
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//here close the dialog
}
ie, your code for the ItemClickListener will be as follows
gridViewColors.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
dialog.dismiss();
}
});
Also, please remember to change customDialog.show(); to dialog.show()
Do this - customDialog.dismiss();
And try changing
final AlertDialog.Builder customDialog = new AlertDialog.Builder(this);
to
final AlertDialog customDialog = new AlertDialog.Builder(this);
in your original code like this -
private void Alert_Colori() {
final AlertDialog customDialog = new AlertDialog.Builder(this);
LayoutInflater inflater = this.getLayoutInflater();
View view = inflater.inflate(R.layout.color_picker, null);
GridView gridViewColors = (GridView) view.findViewById(R.id.gridViewColors);
gridViewColors.setAdapter(new ColorPickerAdapter(this));
gridViewColors.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//here close the dialog
customDialog.dismiss();
}
});
customDialog.setView(view);
customDialog.setNegativeButton("Annulla", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int arg1) {
dialog.dismiss();
}
});
customDialog.setView(view);
customDialog.show();
}
As describe here: https://stackoverflow.com/a/23372134 Get the instance of AlertDialog using create
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;
}
}
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();
}