1

So I'm making my first Android app, using ADT in Eclipse and working with SDK Version 14 (ICS), and I'm trying to create a dialog when the user clicks on an ImageButton, but nothing appears to be happening. I've researched the documentation and searched for online tutorials but there isn't much out there yet for SDK rev 14. Do I need to use FragmentManager? I couldn't figure out how to use that, but as far as I can tell the old dialog method should work fine.

Here is the relevant code (I think)...

Main Activity.java:

ImageButton voiceBlock = (ImageButton) findViewById(R.id.voice_block);

    voiceBlock.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {
            Dialog dialog = new Dialog(getBaseContext());
            dialog.setContentView(R.layout.dialogue);
            dialog.setCancelable(true);
            ImageView voice = (ImageView) dialog.findViewById(R.id.InfoText);
//info is a variable referencing an info image which changes on what is in the app
            voice.setImageResource(info);
            dialog.show();
        }
    });

dialogue.xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:layout_width="wrap_content" android:layout_height="wrap_content">

<ImageView
    android:src="@drawable/coin_text"
    android:id="@+id/InfoText"
    android:layout_width="300px"
    android:layout_height="200px" >

</ImageView>
</RelativeLayout>

Does anyone know what's going on?

1 Answer 1

1

Personally, I have never used the getBaseContext() method. I tried out your code, but replaced the mentioned part with:

Dialog dialog = new Dialog(OuterActivity.this);

where OuterActivity is the Activity in which your Button is located that launches the Dialog. Now it works for me.

Hope that helps.

3
  • Thanks for the suggestion, but unfortunately it did not fix my dialog. Did you test it on ICS? Would it make a difference that I have a second onClickListener for the dialog button that plays a sound?
    – Xavier
    Mar 4, 2012 at 20:11
  • Yes, I tested it on the ICS simulator, API Level 14.
    – Patrick
    Mar 4, 2012 at 20:57
  • The issue in this case is likely that you have multiple ones (see stackoverflow.com/questions/7587299/… for a multi-listener implementation). If you add the sound listener last, it will remove the dialog launching listener.
    – Patrick
    Mar 4, 2012 at 21:00

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.