user47
user47

Reputation: 415

Inflating view inside an alert dialog

I need to ask as how should I inflate three buttons in an alert dialog. Basically a list view is available , and when user long presses on the list items , an alert dialog should appear, the dialog should have edit, delete and some other button, the buttons should perform their tasks when pressed, I'd really appreciate it, if someone could tell me as how I should inflate the alert dialog with the buttons.Thanks

Upvotes: 0

Views: 1224

Answers (5)

Angel
Angel

Reputation: 11

The next code capture a view override setView in a custom dialog class extends alertdialog:

class example extends Activity{


private void onClick_show_dialog( View v )
{


    LayoutInflater lainf = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    final View frmFileSystem = lainf.inflate(R.layout.dlg_filesystem, (ViewGroup) findViewById(R.id.lilaFileSystem));

    dlg_filesystem = new DlgFileSystem(this);
    dlg_filesystem.setView(frmFileSystem);
    dlg_filesystem.setCancelable(true);
    dlg_filesystem.setButton( 'aceptar' , new DialogInterface.OnClickListener() { public void onClick( DialogInterface dialog, int which )
    {
        alert( dlg_filesystem.prueba );
    }});
    dlg_filesystem.setButton2( 'cancelar' , new DialogInterface.OnClickListener() { public void onClick( DialogInterface dialog, int which )
    {
    }});
    dlg_filesystem.show();
}

}

and

public class DlgFileSystem extends AlertDialog{
public String prueba;
private Context context;
private View frmFileSystem;

public DlgFileSystem(Context context)
{ 
    super(context);
    this.context = context;
}

@Override
public void setView( View frmFileSystem )
{
    super.setView(frmFileSystem);
    this.frmFileSystem = frmFileSystem;
    TextView txprueba = (TextView)frmFileSystem.findViewById(R.id.txPrueba);
    txprueba.setText("adios");
}   
}

Upvotes: 0

Savas Adar
Savas Adar

Reputation: 4262

i am doing it like this, also you can

    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    builder.setTitle("Seçenekler");
    builder.setAdapter( /*#your adapter here#*/, new DialogInterface.OnClickListener() {

                        @Override
                        public void onClick(DialogInterface dialog, int which) {

//do something
                        }
                    });

Upvotes: 0

Steelight
Steelight

Reputation: 3357

You can use the context menu, Like so:

final OnCreateContextMenuListener occml = new OnCreateContextMenuListener() {

    @Override
    public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo)
    {
        AdapterContextMenuInfo info = (AdapterContextMenuInfo) menuInfo;
        menu.setHeaderTitle(pi.getBrowserIdentifier());
        menu.add(R.id.actionBarMenu_Settings, R.string.one, 1, getString(R.string.one));
        menu.add(R.id.actionBarMenu_Settings, R.string.two, 2, getString(R.two));
        menu.add(R.id.actionBarMenu_Settings, R.string.three, 3, getString(R.string.three));
    };
_listView= (ListView) findViewById(R.id.list);
_listView.setOnCreateContextMenuListener(occml);

Upvotes: 0

CaseyB
CaseyB

Reputation: 25058

If you use the AlertDialog.Buider you can add three buttons.

new AlertDialog.Builder(this)
    .setPositiveButton("Edit", new OnClickListener()
        {
            // Code Here
        })
    .setNeutralButton("Delete", new OnClickListener()
        {
            // Code Here
        })
    .setNegativeButton("Delete", new OnClickListener()
        {
            // Code Here
        })
    .create()
    .show();

Upvotes: 1

Anders Metnik
Anders Metnik

Reputation: 6237

No need to invent the wheel twice.. Its called a context menu: http://developer.android.com/guide/topics/ui/menus.html

Upvotes: 1

Related Questions