How can I display a list view in an Android Alert Dialog?
Used below code to display custom list in AlertDialog
AlertDialog.Builder builderSingle = new AlertDialog.Builder(DialogActivity.this);
builderSingle.setIcon(R.drawable.ic_launcher);
builderSingle.setTitle("Select One Name:-");
final ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(DialogActivity.this, android.R.layout.select_dialog_singlechoice);
arrayAdapter.add("Hardik");
arrayAdapter.add("Archit");
arrayAdapter.add("Jignesh");
arrayAdapter.add("Umang");
arrayAdapter.add("Gatti");
builderSingle.setNegativeButton("cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
builderSingle.setAdapter(arrayAdapter, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
String strName = arrayAdapter.getItem(which);
AlertDialog.Builder builderInner = new AlertDialog.Builder(DialogActivity.this);
builderInner.setMessage(strName);
builderInner.setTitle("Your Selected Item is");
builderInner.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog,int which) {
dialog.dismiss();
}
});
builderInner.show();
}
});
builderSingle.show();
Show list in AlertDialog
public void showDialog() {
EntityType en = new EntityType();
ArrayList array = ApplicationController.entities;
final AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Pick one");
ArrayList<String> displayValues=new ArrayList<>();
for (Entity entity : array) {
displayValues.add(entity.name);
}
ArrayAdapter<String> adapter = new ArrayAdapter<String>(getApplicationContext(),android.R.layout.simple_list_item_1,array);
final AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Pick one");
builder.setSingleChoiceItems(displayValues, 0, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Entity selectedItem = array[which];
}
});
builder.show();
}
Want to have a list view in an alert dialog box with both onClick listener and onLongClickListener
// TODO Auto-generated method stub
AlertDialog.Builder alertBuilder = new AlertDialog.Builder(
ListAlertDailog.this);
alertBuilder.setIcon(R.drawable.ic_launcher);
alertBuilder.setTitle("Select Mobile OS:-");
final ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(
ListAlertDailog.this,
android.R.layout.select_dialog_item);
arrayAdapter.add("Android");
arrayAdapter.add("IOS");
arrayAdapter.add("Windows");
arrayAdapter.add("Bada");
arrayAdapter.add("BlackBerry OS");
arrayAdapter.add("Symbian OS");
alertBuilder.setNegativeButton("Cancle",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog,
int which) {
dialog.dismiss();
}
});
alertBuilder.setAdapter(arrayAdapter,
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog,
int which) {
String strOS = arrayAdapter.getItem(which);
Toast.makeText(getApplicationContext(),
"On click selected " + strOS, Toast.LENGTH_SHORT)
.show();
dialog.dismiss();
}
});
final AlertDialog alertDialog = alertBuilder.create();
alertDialog.setOnShowListener(new OnShowListener() {
@Override
public void onShow(DialogInterface dialog) {
// TODO Auto-generated method stub
ListView listView = alertDialog.getListView();
listView.setOnItemLongClickListener(new OnItemLongClickListener() {
@Override
public boolean onItemLongClick(
AdapterView<?> parent, View view,
int position, long id) {
// TODO Auto-generated method stub
String strOS = arrayAdapter.getItem(position);
Toast.makeText(getApplicationContext(),
"Long Press - Deleted Entry " + strOS,
Toast.LENGTH_SHORT).show();
alertDialog.dismiss();
return true;
}
});
}
});
alertDialog.show();
Android Studio Pass data from a ListView to AlertDialog
when user clicks on the row item just get the position and get the item from the list and pass it to the dialog to show in TextView or something. for ex.
get the device name in click listener like below.
listViewDevices.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//or showDeviceInfo(devices[position]);
showDeviceInfo(Names[position]); //whatever array you have for devices just get the item based on position and pass it
}
});
return root;
}
and pass it to the showing dialog function like below.
public void showDeviceInfo(String name){
final AlertDialog.Builder alert = new AlertDialog.Builder(getActivity());
View mView = getLayoutInflater().inflate(R.layout.popup_device, null);
TextView DeviceName = (TextView)mView.findViewById(R.id.deviceName);
DeviceName.setText(name); //setting device name
alert.setView(mView);
final AlertDialog alertDialog = alert.create();
alertDialog.setCanceledOnTouchOutside(true);
alertDialog.show();
}
How to scroll to a specific list position inside AlertDialog
You can use AlertDialog
to get the list and after show you can scroll to a certain position.
Try this way it should work .
fun createAlert(activity: Activity, mandatory: Boolean, provinceSelected: (Int) -> Unit) {
val provinceDialog: AlertDialog.Builder = AlertDialog.Builder(activity)
provinceDialog.setTitle(activity.getString(R.string.select_province_from_list))
provinceDialog.setCancelable(!mandatory)
val arrayAdapter = ArrayAdapter<String>(activity, R.layout.province_list_row_item, SubAdministrativeAreas.provincesList)
provinceDialog.setAdapter(arrayAdapter
) { _, positionSelected ->
provinceSelected(positionSelected)
}
val dialog = provinceDialog.create()
dialog.setOnShowListener {
dialog.listView.smoothScrollToPosition(11)
}
dialog.show()
}
how to add custom ListView in alert dialog android
Within alert dialog builder change below line
ListView lv2 = (ListView) getActivity().findViewById(R.id.toplist);
with
ListView lv2 = (ListView) view.findViewById(R.id.toplist);
Related Topics
How to Get Pixel Color in Android
Set Icon For Android Application
Failed to Resolve: Com.Google.Firebase:Firebase-Core:11.2.0
Generate Sha-1 for Flutter/React-Native/Android-Native App
Change Value of R.String Programmatically
Android: Progressdialog.Show() Crashes with Getapplicationcontext
Gradle: Execution Failed for Task ':Processdebugmanifest'
More Than One File Was Found with Os Independent Path 'Meta-Inf/License'
Solution to Install_Failed_Insufficient_Storage Error on Android
Eclipse Giving Error, Missing R.Java File After Recent Update
Android - Sms Broadcast Receiver
What Android Tools and Methods Work Best to Find Memory/Resource Leaks
Wait Firebase Async Retrieve Data in Android
Differencebetween Min Sdk Version/Target Sdk Version VS. Compile Sdk Version
Android- Going Back to Previous Activity with Different Intent Value