0

I am trying to create an alert dialog which lists all the currently installed launchers, when one is pressed, that launcher will be run.

I have managed to get the code working, however whenever I return to my activity, the dialoge is still displayed on the screen. How can I make the alert dialog close/hide/cancel in it's child element's onclick listener?

I cant pass the dialog into the onClickListener because it hasn't been created at the time I'm declaring it

public void btnLaunchers_Click(View v)
{
    AlertDialog.Builder dlgAlert  = new AlertDialog.Builder(this);
    LinearLayout l = new LinearLayout(this);
    l.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
    l.setOrientation(LinearLayout.VERTICAL);

    LayoutInflater inflater = this.getLayoutInflater();

    for (ResolveInfo r : gLstLaunchers) 
   {
        View d = inflater.inflate(R.layout.adapter_psudo_launcheritem, null);
        ImageView i = (ImageView) d.findViewById(R.id.Adapter_LauncherItem_Image);
        TextView t = (TextView) d.findViewById(R.id.Adapter_LauncherItem_Text);


        LinkedList<Object> tTag = new LinkedList<Object>();
        tTag.add(r);

        d.setTag(tTag);
        d.setOnClickListener(new OnClickListener(){

            @Override
            public void onClick(View arg0) 
            {


                LinkedList<Object> o = (LinkedList<Object>) arg0.getTag();

                ResolveInfo r = (ResolveInfo) o.get(0);

                ActivityInfo activity=r.activityInfo;
                ComponentName name=new ComponentName(activity.applicationInfo.packageName,
                                                     activity.name);
                Intent i=new Intent(Intent.ACTION_MAIN);

                i.addCategory(Intent.CATEGORY_LAUNCHER);
                i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
                            Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
                i.setComponent(name);

                startActivity(i);
                //Toast.makeText(arg0.getContext(), intent, Toast.LENGTH_LONG).show();
            }

        });

        i.setImageDrawable(r.loadIcon(this.getPackageManager()));
        t.setText(r.loadLabel(this.getPackageManager()));
        l.addView(d);
   }


    dlgAlert.setView(l);
    dlgAlert.setPositiveButton("Cancel", null);
    dlgAlert.setCancelable(true);

    AlertDialog alrt = dlgAlert.create();
    alrt.show();
}
LairdPleng
  • 765
  • 3
  • 8
  • 27

3 Answers3

1

you can close AlertDialog as on View click:

d.setOnClickListener(new OnClickListener(){
   @Override
    public void onClick(View arg0) 
     {
           //....your code here.....
           if(null !=alrt){
              if(alrt.isShowing())
                alrt.dismiss()
           }      
     }

  });
ρяσѕρєя K
  • 127,886
  • 50
  • 184
  • 206
1

Ok well one way to accomplish it is to make the AlertDialog an activity-wide variable, instead of declaring it in the onClick event. I don't much like doing it that way, because it has no use outside of the event, but unless anybody can think of anything better that's what I'm going to have to go with

private AlertDialog gAlrt;

@Override
protected void onCreate(Bundle savedInstanceState) 
{
    ...
}

@Override
public boolean onCreateOptionsMenu(Menu menu) 
{
            ...
}

public void btnLaunchers_Click(View v)
{
    AlertDialog.Builder dlgAlert  = new AlertDialog.Builder(this);



    LinearLayout l = new LinearLayout(this);


    l.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
    l.setOrientation(LinearLayout.VERTICAL);



    LayoutInflater inflater = this.getLayoutInflater();

    for (ResolveInfo r : gLstLaunchers) 
   {
        View d = inflater.inflate(R.layout.adapter_psudo_launcheritem, null);
        ImageView i = (ImageView) d.findViewById(R.id.Adapter_LauncherItem_Image);
        TextView t = (TextView) d.findViewById(R.id.Adapter_LauncherItem_Text);

        LinkedList<Object> tTag = new LinkedList<Object>();
        tTag.add(r);



        d.setTag(tTag);
        d.setOnClickListener(new OnClickListener(){

            @Override
            public void onClick(View arg0) 
            {


                LinkedList<Object> o = (LinkedList<Object>) arg0.getTag();
                gAlrt.cancel();

                ResolveInfo r = (ResolveInfo) o.get(0);


                ActivityInfo activity=r.activityInfo;
                ComponentName name=new ComponentName(activity.applicationInfo.packageName,
                                                     activity.name);
                Intent i=new Intent(Intent.ACTION_MAIN);

                i.addCategory(Intent.CATEGORY_LAUNCHER);
                i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
                            Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
                i.setComponent(name);

                startActivity(i);

            }

        });

        i.setImageDrawable(r.loadIcon(this.getPackageManager()));
        t.setText(r.loadLabel(this.getPackageManager()));
        l.addView(d);
   }


    dlgAlert.setView(l);
    dlgAlert.setPositiveButton("Cancel", null);
    dlgAlert.setCancelable(true);
    gAlrt = dlgAlert.create();


    gAlrt.show();
}
LairdPleng
  • 765
  • 3
  • 8
  • 27
0

in onclick event of child

onClick(...){
    dialogname.dismiss();
}
karan
  • 8,406
  • 3
  • 39
  • 75