Here is my dialog code, there is a ListView
in a dialog, I wanna close this dialog when I click on ListView
:
public void createSearchDialog(final String[] Memo){
LayoutInflater factory = LayoutInflater.from(this);
View searchView = factory.inflate(R.layout.seach_dialog, null);
lv = (ListView) searchView.findViewById(R.id.search_list);
lv.setAdapter(new MyPerformanceArrayAdapter(this, Memo, memo_PW));
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> arg0, View view, int position,long arg3) {
// TODO Auto-generated method stub
/// close dialog
}
});
searchBuilder = new AlertDialog.Builder(this);
searchBuilder.setTitle("Search")
.setView(searchView)
.setNegativeButton("Back", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface arg0, int arg1) {
// TODO Auto-generated method stub
}
})
.show();
}
I tried to put this code in onItemClick()
searchBuilder.create().dismiss();
searchBuilder.create().cancel();
But it's not working.
AlertDialog generally consists of the main title, the message, and two buttons, technically termed as a positive button and a negative button. Both positive and negative buttons can be programmed to perform various actions. By default, the negative button lets close the AlertDialog without any additional lines of code.
AlertDialog is a lightweight version of a Dialog. This is supposed to deal with INFORMATIVE matters only, That's the reason why complex interactions with the user are limited. Dialog on the other hand is able to do even more complex things .
Android AlertDialog can be used to display the dialog message with OK and Cancel buttons. It can be used to interrupt and ask the user about his/her choice to continue or discontinue. Android AlertDialog is composed of three regions: title, content area and action buttons.
Change .show()
to dialog = searchBuilder.show();
then put dialog.dismiss()
to onItemClick()
.
Complete Code For this problem
public void showDialog(){
final AlertDialog.Builder dialog = new AlertDialog.Builder(getContext());
dialog.setTitle("Choose App");
dialog.setCancelable(true);
View view = ((Activity)getContext()).getLayoutInflater().inflate(R.layout.custom_dialog_all_app, null);
list = (ListView) view.findViewById(R.id.AllAppList);
AllAppPckName = getPackages();
AllAppListAdapter adapter= new AllAppListAdapter(getContext(), R.layout.app_item, AllAppPckName);
list.setAdapter(adapter);
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
appIcon.setVisibility(View.VISIBLE);
appIcon.setImageDrawable(getPackageIcon(getContext(), AllAppPckName.get(i)));
appNameBtn.setText(getAppNameFromPkgName(getContext(), AllAppPckName.get(i)));
dialogg.dismiss();
}
});
dialog.setView(view);
dialogg = dialog.show();
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With