Bob
Bob

Reputation: 881

TouchEvent in a Dialog Android

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...

Upvotes: 2

Views: 4109

Answers (3)

MattDavis
MattDavis

Reputation: 5178

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.

Upvotes: 0

5hssba
5hssba

Reputation: 8079

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();

Upvotes: 3

patrickwlarsen
patrickwlarsen

Reputation: 168

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 :-)

Upvotes: 0

Related Questions