I have this working piece of code for selecting a number from a GridView that's inside an AlertDialog:
public void iconSelect (View v){
AlertDialog.Builder builder = new AlertDialog.Builder(this);
GridView gridview = new GridView(this);
List<Integer> mList = new ArrayList<Integer>();
for (int i = 1; i < 10; i++) {
mList.add(i);
}
ArrayAdapter<Integer> adapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, mList);
gridview.setAdapter(adapter);
gridview.setNumColumns(3);
gridview.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(getApplicationContext(), "Chosen: " + position, Toast.LENGTH_SHORT).show();
}
});
builder.setView(gridview);
builder.setTitle("Icon selector");
builder.show();
}
My goal is that when the user selects a number from the gridview then the dialog get closed. Right now it lets the user click the number on the gridview and show the toast many times as the user wants until he presses back button.
What should I do?
I'm looking for a method similar to builder.close() or builder.dismiss() to be executed inside the listener. (Making builder a final variable)
You should use builder.show() or builder.create() to hold an instance of AlertDialog in order to dismiss it after that
Try the following code:
public void iconSelect (View v){
AlertDialog.Builder builder = new AlertDialog.Builder(this);
GridView gridview = new GridView(this);
List<Integer> mList = new ArrayList<Integer>();
for (int i = 1; i < 10; i++) {
mList.add(i);
}
ArrayAdapter<Integer> adapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, mList);
gridview.setAdapter(adapter);
gridview.setNumColumns(3);
builder.setView(gridview);
builder.setTitle("Icon selector");
final AlertDialog dialog = builder.show();
gridview.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(getApplicationContext(), "Chosen: " + position, Toast.LENGTH_SHORT).show();
dialog.dismiss();
}
});
}
Related
I have an alertDialog window which shows the simple array list. Each item is the specific command and one of them is "Delete" option. I want the app to show another dialog which asks to confirm the decision made. How can I do that?
Here is my code:
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
builder.setCancelable(true);
ArrayAdapter<String> adapter2 = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, items);
ListView lv = new ListView(this);
lv.setAdapter(adapter2);
builder.setView(lv);
final AlertDialog alert = builder.create();
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
if (position == 0) {
Intent intent = new Intent(MainActivity.this, EditSavedEventActivity.class);
intent.putExtra("EventPosition", pos);
startActivity(intent);
}
else if (position == 1) {
/**************************
I want to show the second alertDialog if this option is selected
***************************/
}
alert.dismiss();
onPause();
onResume();
}
});
alert.show();
i got 2 dynamically created spinners in 1 Activity.
private void populateSpinner() {
AlertDialog.Builder adb2 = new AlertDialog.Builder(this);
LayoutInflater adbInflater2 = LayoutInflater.from(this);
View SpinnerLayout = adbInflater2.inflate(R.layout.spinner, null);
adb2.setView(SpinnerLayout);
adb2.setTitle("Kostenstelle auswählen:");
spinnerKOST = (Spinner) SpinnerLayout.findViewById(R.id.spinner);
List<String> lables = new ArrayList<String>();
lables.add("");
spinnerKOST.setSelection(1, false);
for (int i = 0; i < KostList.size(); i++) {
lables.add(KostList.get(i).getKost());
}
ArrayAdapter<String> spinnerAdapter = new ArrayAdapter<String>
(this,android.R.layout.simple_spinner_dropdown_item, lables);
spinnerAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
// attaching data adapter to spinner
spinnerKOST.setAdapter(spinnerAdapter);
// use .create to get the AlertDialog
AlertDialog dialog = adb2.create();
// set an OnShowListener
dialog.setOnShowListener(new DialogInterface.OnShowListener() {
public void onShow(DialogInterface dialog) {
spinnerKOST.setOnItemSelectedListener(StaffActivity.this);
}
});
dialog.show();
}
and the second:
private void populateSpinner2() {
AlertDialog.Builder adb2 = new AlertDialog.Builder(this);
LayoutInflater adbInflater2 = LayoutInflater.from(this);
View SpinnerLayout = adbInflater2.inflate(R.layout.spinner, null);
adb2.setView(SpinnerLayout);
adb2.setTitle("Box auswählen:");
spinnerBox = (Spinner) SpinnerLayout.findViewById(R.id.spinner);
List<String> lables = new ArrayList<String>();
lables.add("");
spinnerBox.setSelection(1, false);
for (int i = 0; i < BoxesList.size(); i++) {
lables.add(BoxesList.get(i).getBoxer_mail());
}
ArrayAdapter<String> spinnerAdapter = new ArrayAdapter<String>
(this,android.R.layout.simple_spinner_dropdown_item, lables);
spinnerAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinnerBox.setAdapter(spinnerAdapter);
AlertDialog dialog = adb2.create();
dialog.setOnShowListener(new DialogInterface.OnShowListener() {
public void onShow(DialogInterface dialog) {
spinnerBox.setOnItemSelectedListener((OnItemSelectedListener) StaffActivity.this);
}
});
dialog.show();
}
they are pretty the same, as you see.
Normally I use for every spinner 1 public void onItemSelected(AdapterView<?> parent, View view, int position, long id)
Before I only had 1 Spinner per Activity and 1 public void onItemSelected.
Now I got 2 Spinner in the Activity but still 1 public void onItemSelected.
How I can use both spinners?
Use two spinner and toggle view viible and invisible between them
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
Spinner spinner = (Spinner) parent;
if(spinner.getId() == R.id.spinner1)
{
//spinner1
}
else if(spinner.getId() == R.id.spinner2)
{
//spinner2
}
}
Here is my code:
When I click on textview one dialog displaying list is shown. When i select particular list item dialog is not dismissed. how to dismiss dialog when list item is clicked,
educationtxt=(TextView)findViewById(R.id.education_txt);
String[] educationarray = new String[]{"High School","Som College","Associates Degree","Bachelor Degree","Masters Degree","PHD"};
educationtxt.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
final ListView lv ;
AlertDialog.Builder alertDialog = new AlertDialog.Builder(RegistrationActivity.this);
LayoutInflater inflater = getLayoutInflater();
View convertView = (View) inflater.inflate(R.layout.custom_dialog, null);
alertDialog.setView(convertView);
alertDialog.setTitle("Education");
View convertView1 = (View) inflater.inflate(R.layout.custom_dialog_row, null);
TextView tv =(TextView)convertView1.findViewById(R.id.list_row_txt);
lv = (ListView) convertView.findViewById(R.id.custom_listView1);
lv.setBackgroundColor(Color.WHITE);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(RegistrationActivity.this,R.layout.custom_dialog_row,R.id.list_row_txt,educationarray);
lv.setAdapter(adapter);
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
education_selected = lv.getAdapter().getItem(position).toString();
educationtxt.setText(education_selected);
Toast.makeText(RegistrationActivity.this, "You Clicked at "+education_selected, Toast.LENGTH_SHORT).show();
//here i want dismiss
}
});
alertDialog.show();
}
});
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
education_selected = lv.getAdapter().getItem(position).toString();
educationtxt.setText(education_selected);
Toast.makeText(RegistrationActivity.this, "You Clicked at "+education_selected, Toast.LENGTH_SHORT).show();
//here i want dismiss
alertDialog.dissmiss(); <----- add this lone to dismiss
}
});
and make alertdialoge as a final
Just Call this method and Show Dialog. Also Dismiss Dialog On Click ListItem
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();
}
i'am having a list with textview, many data's are flowing in list and having one textview in xml....problem is i want to update the every textview entry in the list..i want to update the (TAG_QTY) textview in list when every value entry...
ListAdapter adapter = new SimpleAdapter(this, contactList,
R.layout.list_item,
new String[] { TAG_BARCODE, TAG_DIVISION, TAG_MRP,TAG_QTY}, new int[] {
R.id.txt, R.id.txt1, R.id.mrp,R.id.qty1 });
setListAdapter(adapter);
// selecting single ListView item
ListView lv = getListView();
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//String name = ((TextView) view.findViewById(R.id.qty1)).getText().toString();
LayoutInflater li = LayoutInflater.from(context);
View promptsView = li.inflate(R.layout.prompts, null);
//final View textEntryView;
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
context);
// set prompts.xml to alertdialog builder
alertDialogBuilder.setView(promptsView);
fourth = (TextView)findViewById(R.id.qty1);
userInput = (EditText)promptsView.findViewById(R.id.editTextDialogUserInput);
//String ed = userInput.getText().toString();
//final int ed= Integer.parseInt(userInput.getText().toString());
alertDialogBuilder
.setCancelable(false)
.setPositiveButton("OK",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int id) {
String ed = userInput.getText().toString().trim();
fourth.setText(ed);
}
})
.setNegativeButton("Cancel",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int id) {
dialog.cancel();
}
});
// create alert dialog
AlertDialog alertDialog = alertDialogBuilder.create();
alertDialog.show();
}
}
);
In order to get the list view child
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
View c = lv.getChildAt(position);
// c is your list view child which is clicked
final TextView tv = (TextView) c.findViewById(R.id.qty1);
// tv is your textview of whom vwlue you have to change.
//changes the value of textview here and den notify data set changed and refresh the list.
}
});
I have an Alert Dialog which has a list on it, and i want to close onlistclick is it possible?
AlertDialog.Builder builder = new AlertDialog.Builder(this);
final String[] Categories = SQLiteHelper.getAllCategories();//this is where i get the array for my list
ListView myList = new ListView(this);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.alert_dialog_list_view, Categories);
myList.setAdapter(adapter);
myList.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view, int position, long rowId) {
//doing something in here and then close
}
});
builder.setTitle("Please Choose");
builder.setInverseBackgroundForced(true);
builder.setView(myList);
final Dialog dialog = builder.create();
dialog.show();
}
The alert dialog is running perfect i just dont want to put any buttons in it.
If you define the onItemClickListener after the Dialog you can just call dialog.dismiss(); in the onItemClick() method.
check below code
myList.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view, int position, long rowId) {
//doing something in here and then close
dialog.dismiss();
}
});