Hi i am working the first time with dialogs. i want to create a dialog after an item in a listView was clicked:
String[] listItems = {\"Colour\", \"Font Size
just use
public void onItemClick(AdapterView<?> parent, View
view, int position, long id)
{
String[] listItems = {"Colour", "Font Size",};
if(listItems[position].equals("Font Size"))
{
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setTitle("Choose Font Size").show();
}
}
Call this method in your onItemClick method :
public void showMyCustomDialog(Context context, String title, String message)
{
final Dialog dialog = new Dialog(context);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
//tell the Dialog to use the customalertdialog.xml as it's layout description
dialog.setContentView(R.layout.customalertdialog);
TextView titleText = (TextView) dialog.findViewById(R.id.txtTitleAlertDialog);
titleText.setText(title);
TextView txt = (TextView) dialog.findViewById(R.id.txtAlertDialog);
txt.setText(message);
Button dialogButton = (Button) dialog.findViewById(R.id.buttonAlertDialogCancel);
dialogButton.setText("OK");
dialogButton.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v) {
dialog.dismiss();
}
});
dialog.show();
}
Something like below :
lv.setOnItemClickListener(new OnItemClickListener()
{
public void onItemClick(AdapterView<?> parent, View
view, int position, long id)
{
String[] listItems = {"Colour", "Font Size",};
if(listItems[position].equals("Font Size"))
{
public dialog onCreateDialog(Bundle savedInstanceState)
{
showMyCustomDialog(Context context, String title, String message) // pass title, message and activity context in this method.
}
}
}
});
Hope it will help.
move
public dialog onCreateDialog(Bundle savedInstanceState) {
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setTitle("Choose Font Size");
}
int the class scope, and call it from the onItemClick
try this one
String[] listItems = {"Colour", "Font Size",};
ListView lv = (ListView) findViewById(R.id.settings_list);
lv.setAdapter(new ArrayAdapter<String>
(this, android.R.layout.simple_list_item_1, listItems));
lv.setOnItemClickListener(new OnItemClickListener()
{
public void onItemClick(AdapterView<?> parent, View
view, int position, long id)
{
String[] listItems = {"Colour", "Font Size",};
if(listItems[position].equals("Font Size"))
{
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
getApplicationContext());
// set title
alertDialogBuilder.setTitle("Choose Font Size");
//create alert dialog
AlertDialog alertDialog = alertDialogBuilder.create();
// show it
alertDialog.show();
}
}
});