0

I am newbie, when I click a banner, i need an AlertDialog with a ListView with 2 choices spanish and english to appear. And when I click one I need go to a website, and the option to back or cancel of dialog.

If this help, all that I have (is not with ListView)

public class MainActivity extends Activity {


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    final AdView adView = (AdView) findViewById(R.id.adView);
    adView.loadAd(new AdRequest());

     // Internet Connection detector
    ConnectionDetector cd;

    // Alert Dialog Manager
    AlertDialogManager alert = new AlertDialogManager();

    final ImageView Banner = (ImageView) findViewById(R.id.imageView2);
    Banner.setOnClickListener(new OnClickListener(){




        @Override
        public void onClick(View arg0) {

            // custom dialog
            final Dialog dialog = new Dialog(context);
            dialog.setContentView(R.layout.custom);
            dialog.setTitle("Title...");

            // set the custom dialog components - text, image and button
            TextView text = (TextView) dialog.findViewById(R.id.text);
            text.setText("Android custom dialog example!");
            ImageView image = (ImageView) dialog.findViewById(R.id.image);
            image.setImageResource(R.drawable.ic_launcher);

            Button dialogButton = (Button) dialog.findViewById(R.id.dialogButtonOK);
            // if button is clicked, close the custom dialog
            dialogButton.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    dialog.dismiss();
                }
            });

            dialog.show();
          }
        });
    }
}



            //al apretar click en el banner abre el browser con la pág: las.leagueoflegends.com

            //Intent browserIntent = 
            //  new Intent(Intent.ACTION_VIEW, 
                //  Uri.parse("http://www.facebook.com"));
        //   startActivity(browserIntent);
        }});

PD: i tried different codes from people but i get different errors D:

vipul mittal
  • 17,343
  • 3
  • 41
  • 44
Fabian Montossi
  • 189
  • 3
  • 16

3 Answers3

3

Try this..

Banner.setOnClickListener(new View.OnClickListener(){
            public void onClick(View v) {                   

                String[] MenuItems = {"spanish", "english"};
                AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(Activity.this);
                // set title
                alertDialogBuilder.setTitle("Select");
                // set dialog message
                alertDialogBuilder
                    .setCancelable(true)
                    .setSingleChoiceItems(MenuItems, 0, new DialogInterface.OnClickListener() {

                        @Override
                        public void onClick(DialogInterface dialog1, int pos) {
                            // TODO Auto-generated method stub

                            if(pos == 0) {
                                 // Do Your choice
                            } else if(pos == 1) {
                                 // Do Your choice
                            }
                            dialog1.cancel();
                        }
                    })
                    .setPositiveButton("Cancel",new DialogInterface.OnClickListener() {
                        @SuppressLint("NewApi")
                        public void onClick(DialogInterface dialog1,int id) {
                            dialog1.cancel();
                        }
                      });

                    // create alert dialog
                    AlertDialog alertDialog = alertDialogBuilder.create();
                    // show it
                    alertDialog.show();
            }
        });   
SilentKiller
  • 6,944
  • 6
  • 40
  • 75
Hariharan
  • 24,741
  • 6
  • 50
  • 54
1

Its perfectly achieveble what is mentioned by Tamilan and Vipul but in future if you need better control overr alert dialog its bette r to use customAlertDialog, just Create Layput as usual; you do it in your Activity .

AlertDialog dialog;
public void myDialog(){

AlertDialog.Builder builder=new AlertDialog.Builder(this);
builder.setTitle("Dialog Demo");
LayoutInflater inflater=getLAyoutInflater();
final View dialogView=inflater.inflate(R.layout.cutomdialog,null);
builder.setView(dialogView);
dialog=builder.create();
dialog.show();
}

cutomdialog.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="+@id/myLayout
android:layout_width="match_parent"
android:layout_hight="match_parent"
android:orientation="vertical">

<ListView
android:id="+@id=myListView'
android:layout_width="fill_parent"
android:layout_height="fill_parent"/>
<Button
android:id="+@id/myOkButton"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="OK"/>

</LinearLayout>
Abhijit Chakra
  • 3,201
  • 37
  • 66
0
final CharSequence[] items = {"Foo", "Bar", "Baz"};

AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Make your selection");
builder.setItems(items, new DialogInterface.OnClickListener() {
    public void onClick(DialogInterface dialog, int item) {
         // Do something with the selection
    }
});
AlertDialog alert = builder.create();
alert.show();

Source:

ListView in AlertDialog

Community
  • 1
  • 1
vipul mittal
  • 17,343
  • 3
  • 41
  • 44