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:

7

3 Answers 3

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();
            }
        });   
4
  • Didn´t get errors, but dont do anything when i click it :| Why ? i tried dialog1.show(); but i get error to cast dialog1 o.O Dec 14, 2013 at 6:50
  • Omg, now i get error: Change to alertDialogBuilder, AlertDialog(android app) Dec 14, 2013 at 6:54
  • 1
    @FabianMontossi remove this AlertDialogManager alert = new AlertDialogManager(); line in your codes
    – Hariharan
    Dec 14, 2013 at 6:58
  • 1
    Thanks :D Using your code and with SilentKiller responses i fixed it and works perfectly :D Dec 14, 2013 at 7:16
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>
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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.