2

Hi I'm developing an app in which I'm using a dialog on an activity. The dialog will just display a picture and when the user touch on the picture the dialog should dismiss and the activity which started the dialog should come in front. The Dialog doesn't have any buttons.

My problem in this app is that I can't get the touch events on the dialog, I tried searching on the internet for a solution for this problem but I couldn't find a proper way to implement it. So can any one please suggest a way of doing this...

Bob
  • 881
  • 2
  • 10
  • 16
  • Thanks and I've chosen the correct answers for all my questions. – Bob Apr 13 '12 at 15:32
  • nice.... because that even makes it more appealing for users to answer your question..... and for this question?? still any problem? – 5hssba Apr 13 '12 at 15:33

3 Answers3

3

Try like this..

  final Dialog dialog = new Dialog(context);
        dialog.setContentView(R.layout.mylayout);
   //craete a layout with imageview
        dialog.setTitle("Title...");

        ImageView image = (ImageView) dialog.findViewById(R.id.image);
        image.setImageResource(R.drawable.yourimage);

        image.setOnClickListener(new View.OnClickListener(){
         public void onClick(View View3) {
             //your onclick functionality
         } });

        });

        dialog.show();
5hssba
  • 8,079
  • 2
  • 33
  • 35
  • It works perfectly, and can you suggest me that how to display a Dialog in full screen with out title bar. – Bob Apr 13 '12 at 16:00
  • See this link.. i think it will help.. http://stackoverflow.com/questions/1362723/how-can-i-get-a-dialog-style-activity-window-to-fill-the-screen – 5hssba Apr 13 '12 at 16:02
  • The solution in this question is for an activity, but in my app I need a full screen for a Dialog. – Bob Apr 13 '12 at 16:10
0

I believe settings a View.OnClickListener on the main layout element in your Dialog would do the trick :-)

Example code:

    public class DialogActivity extends Activity implements OnClickListener
    {
        @Override
        protected void onCreate(Bundle savedInstanceState) 
        {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.dialog);
            LinearLayout linearlayout = (LinearLayout)findViewById(R.id.dialogMainLayout);
            linearlayout.setOnClickListener(this);
        }

        public void onClick(View v)
        {
            finish();
        }
    }

dialog.xml:

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical" 
        android:background="#FFA500"
        android:id="@+id/dialogMainLayout"
        >
        <TextView 
            android:layout_height="250dp"
            android:layout_width="fill_parent"
            android:text="TEST"
            android:background="#FFFFF0"
            />
    </LinearLayout>

First i tried just making an activtiy that would start another themed as a dialog. Click the dialog themed activity did absolutely nothing. So i thought that setting an onclicklistener on the root layout element in the xml-file used in the dialog themed activity might solve the problem.

I got the above code working. Hope it fixes your problem :-)

  • Thanks for the reply and the code, but I want them in a Dialog instead of an Activity like Dialog. – Bob Apr 13 '12 at 15:49
0

Android: Close dialog window on touch

This question might provide an answer to your question. Alternatively, you can use LayoutInflater to create a custom view that contains an ImageButton or ImageView with an OnClickListener and AlertDialog.Builder and its setView method to make this view the body of your Dialog.

Community
  • 1
  • 1
MattDavis
  • 5,158
  • 2
  • 23
  • 35