CodePrimate
CodePrimate

Reputation: 6666

How do I make this DialogBox pop up on a list item click?

I got an assignment where I have to implement a custom call forwarding system and was looking for ways of doing so. I decided that Androids own call forwarding looked great and decided that I wanted to prompt the user on an item click on the listview. Basically I want this to pop up when the user clicks an item.

this

Here's the code in which I'm trying to implement the dialog box :

public class CallForwardActivity extends ListActivity 
{
String[] settingsLabels = {"Viderestillinger", "Altid", "Optaget", "Ingen svar", "Timeout"};
int position;
@Override
public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  ListView lv = getListView();
  lv.setTextFilterEnabled(true);
  setListAdapter(new ArrayAdapter<String>(this, R.layout.callforward_items, R.id.callforward_item_text, settingsLabels));

  lv.setOnItemClickListener(new OnItemClickListener() {
    public void onItemClick(AdapterView<?> parent, View view,
        int position, long id) 
    {
        // Open Dialog Box with an edittext view for setting the correct number to Call forward
        myTextDialog().show() // ???
    }
  });
}


private Dialog myTextDialog() {
    final View layout = View.inflate(this, R.layout.custom_dialog, null);

    final EditText savedText = ((EditText) layout.findViewById(R.id.myEditText));

    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    builder.setIcon(0);

    builder.setPositiveButton("Save", new Dialog.OnClickListener() {
        public void onClick(DialogInterface dialog, int which) {
           String myTextString = savedText.getText().toString().trim();
        }
    });
    builder.setView(layout);
    return builder.create();
 }

How do I go about "calling" this method/showing the dialog?

Upvotes: 1

Views: 5813

Answers (2)

Hesham Saeed
Hesham Saeed

Reputation: 5378

Use it this way to declare the dialog,

final AlertDialog.Builder builder = new AlertDialog.Builder(context);
                         builder.setView(//your view)
                           .setPositiveButton("Update", new DialogInterface.OnClickListener() {
                            public void onClick(final DialogInterface dialog, final int id) {
                                //update click
                            }
                           })
                           .setNeutralButton("Disable", new DialogInterface.OnClickListener() {
                                public void onClick(final DialogInterface dialog, final int id) {
                                    //disable click
                                }
                               })
                           .setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                                public void onClick(final DialogInterface dialog, final int id) {
                                //cancel click
                                }
                               });

And show it in your click listener as follows,

builder.show();

In the function builder.setView() you have to pass for it a view (layout for example) that consist of textview and edittext and button if you want to make it the same as the figure.

Upvotes: 1

soren.qvist
soren.qvist

Reputation: 7416

If you want to build your own dialog (in an onItemClickListener for example) then the AlertDialog.Builder is a good place to start. For example:

  lv.setOnItemClickListener(new OnItemClickListener() {
    public void onItemClick(AdapterView<?> parent, View view,
        int position, long id) 
    {
        new AlertDialog.Builder(view.getContext())
            .setMessage("Something here")
            .setNegativeButton("Close", null).show();

    }
  });

You can take a look at the docs for AlertDialog here: http://developer.android.com/reference/android/app/AlertDialog.Builder.html

Upvotes: 1

Related Questions