Android - close popUp window - android

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();
}

Related

Proceed to next activity through radio buttons on alert dialog

I have listview ,on click of each list item, it must pop up a alert with radio buttons. Selecting a radio button option and then clicking "ok" button on alert dialog , I must be able to proceed to next activity. (PS i dont want to use positive , negative button ).
Below is my code, listview is working fine , alert dialog pops up and on selecting yes or no , Toast shows .But upon yes it isn't proceeding to next activity. Please help!!
listview = (ListView) findViewById(R.id.mylistview);
final String[] items = new String[]{"IOS", "ANDROID", "WINDOWS"};
ArrayAdapter<String> adapter = new ArrayAdapter<String>(getApplicationContext(), android.R.layout.simple_expandable_list_item_1, items);
listview.setAdapter(adapter);
listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
int itemposition = position;
String itemvalue = (String) listview.getItemAtPosition(position);
final CharSequence[] items1 = {"yes", "no"};
final AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
builder.setTitle("slection confirmation");
builder.setSingleChoiceItems(items1, -1, new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(getApplicationContext(), items1[which], Toast.LENGTH_SHORT).show();
}
});
builder.setNeutralButton("OK", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
switch(items1.toString())
{
case("yes"):
Intent myint=new Intent(MainActivity.this,secondpage.class);
myint.putExtra("act1","");
startActivity(myint);
break;
case("no"):
dialog.cancel();
}
}
});
AlertDialog alert = builder.create();
alert.show();
}
});
}
}
You have problem in below code snippet, As you covert whole string array into string , but you need to get one item at time.
switch(items1.toString())
{
case("yes"):
Intent myint=new Intent(MainActivity.this,secondpage.class);
myint.putExtra("act1","");
startActivity(myint);
break;
case("no"):
dialog.cancel();
}
Please replace this with
String selection;
listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
int itemposition = position;
String itemvalue = (String) listview.getItemAtPosition(position);
final CharSequence[] items1 = {"yes", "no"};
final AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
builder.setTitle("slection confirmation");
builder.setSingleChoiceItems(items1, -1, new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
selection = items1[which]
Toast.makeText(getApplicationContext(), items1[which], Toast.LENGTH_SHORT).show();
}
});
builder.setNeutralButton("OK", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
switch(selection)
{
case("yes"):
Intent myint=new Intent(MainActivity.this,secondpage.class);
myint.putExtra("act1","");
startActivity(myint);
break;
case("no"):
dialog.cancel();
}
}
});
AlertDialog alert = builder.create();
alert.show();
}
});

Android GridView item click Alert Dialog

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

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();
}

Closing alertdialog when gridview was clicked

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();

Categories

Resources