0

I am going through a very weird problem of Button not responding to click events. I have a dialog box that is displayed and then when the user clicks on the positive button in the dialog, it opens up a layout with video , textbox and buttons on it. Whenever I click the button on the layout, it is unresponsive! Please help.

Layout file :

     <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/layout_root"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@drawable/roundedcorners"
    android:orientation="vertical"
    android:padding="0dp" >

   <LinearLayout
       android:id="@+id/video_panel"
       android:layout_width="match_parent"
       android:layout_height="175dp"
       android:layout_above="@+id/bottom_panel"
       android:layout_alignParentTop="true"
       android:layout_gravity="top"
       android:gravity="top"
       android:orientation="vertical" >

    </LinearLayout>

   <RelativeLayout
       android:id="@+id/bottom_panel"
       android:layout_width="match_parent"
       android:layout_height="70dp"
       android:layout_alignParentBottom="true"
       android:layout_alignParentLeft="true"
       android:layout_gravity="bottom"
       android:background="@color/milky_white"
       android:gravity="bottom"
       android:orientation="vertical" >

      <TextView
           android:id="@+id/agentname"
           android:layout_width="match_parent"
           android:layout_height="35dp" 
           android:background="@color/video_username"
           android:layout_alignParentTop="true"
           android:textStyle="bold"
           android:text="name"/>



       <Button
           android:id="@+id/disconn"
           android:layout_width="510dp"
           android:layout_height="37dp"
           android:layout_alignParentBottom="true"
           android:layout_alignParentRight="true"
           android:clickable="true"
           android:text="@string/disconnect" />

       </RelativeLayout>


    </RelativeLayout>

The code for the onClick is given as :

    alertVideoChatBuilder.setMessage(message);
    alertVideoChatBuilder.setPositiveButton("Answer", new OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            StatusJson sj = new StatusJson("User accepted.",1);
            outCmd.sendJsonCommand(getNodeId(), "videoChatStatus", sj);
            handler.post(new Runnable() {
                @Override
                public void run() {
                    try {
                        pc = pcf.createPeerConnection(iceServers, pcConstraints, new PCObserver(nodeId));
                    } catch (UnsupportedOnThisDeviceException e) {
                        Log.e("MeshAgent", "Video chat not supported");
                        return;
                    }
                    MediaConstraints audio = new MediaConstraints();
                    audio.mandatory.add(new KeyValuePair("googEchoCancellation", "true"));
                    audio.mandatory.add(new KeyValuePair("googAutoGainControl", "true"));
                    //audio.mandatory.add(new KeyValuePair("noiseSuppression", "false"));
                    MediaStream ms = pcf.createLocalMediaStream("device");
                    AudioSource as = pcf.createAudioSource(audio);
                    AudioTrack at = pcf.createAudioTrack("devicea0",as);



                    ms.addTrack(at);
                    pc.addStream(ms, audio);

                    videoView = new VideoStreamsView(context, R.drawable.tech_mute, R.drawable.tech_connect);


                    LayoutInflater li = LayoutInflater.from(context);
                    outerVideoView = (RelativeLayout) li.inflate(R.layout.chat_float, null); 
                    LinearLayout vView = (LinearLayout) outerVideoView.findViewById(R.id.video_panel);

                    TextView agent_name = (TextView) outerVideoView.findViewById(R.id.agentname);
                    agent_name.setText(name);



                    //RelativeLayout bottomView = (RelativeLayout) outerVideoView.findViewById(R.id.bottom_panel);

                    Button discon_but = (Button) outerVideoView.findViewById(R.id.disconn);
                    discon_but.setClickable(true);

                    discon_but.setOnClickListener(new View.OnClickListener() 
                    {
                        @Override
                        public void onClick(View v) 
                        {
                            //System.out.println("mesh: button clicked disconnect!!");
                            Toast.makeText(context, "Video Connection disconnected", Toast.LENGTH_LONG).show();

                            outCmd.sendJsonCommand(getNodeId(), "videoChatStatus", new StatusJson("User disconnected the call.",1));
                            coord.disconnect();

                        }
                    });
                    vView.addView(videoView);

                    wm.addView(outerVideoView, params);
Uday
  • 21
  • 4

3 Answers3

0

Please check your code, in Positive button's onclick method, you are inflating the new view which is not set on Dialog.. It is not referenced to any view on dialog. You can set listener by using the same view which was set on dialog. View set on dialog is different than the view on which you are setting onclick listener. I think the problem is, you are setting onClick listener inside the onClick of Positive button. Hence, click listener on disconnect button is not being set unless you click on Positive button of dialog. Try setting onclick listener on disconnect button outside of onClick of positive button. As shown in below sample code. Hope This Helps!

AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle("test title");
        builder.setMessage("test message");
        LayoutInflater inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View view = inflater.inflate(R.layout.test_layout, null);
        builder.setView(view);
        Button testButton = (Button) view.findViewById(R.id.test_button);
        testButton.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                Toast.makeText(context, "Hello", Toast.LENGTH_SHORT).show();
            };
        });
        builder.setNegativeButton("Cancel",
                new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        // Logic on negative button click
                    }
                });
        builder.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                // logic on positive button click
            }
        });
        AlertDialog dialog = builder.create();
        dialog.show();

How to set onclick listener in on click of positive button:

AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle("test title");
        builder.setMessage("test message");
        LayoutInflater inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        // This is the view we are setting on dialog. You can use only this view
        // to access view present on dialog. Using this reference, we can set
        // onclick listener in positive button's onclick event
        final View view = inflater.inflate(R.layout.test_layout, null);
        builder.setView(view);

        builder.setNegativeButton("Cancel",
                new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        // Logic on negative button click
                    }
                });
        builder.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                // logic on positive button click
                // Set onclick listener whenever positive button is clicked.
                Button testButton = (Button) view
                        .findViewById(R.id.test_button);
                testButton.setOnClickListener(new OnClickListener() {
                    public void onClick(View v) {
                        Toast.makeText(context, "Hello", Toast.LENGTH_SHORT)
                                .show();
                    };
                });
            }
        });
        AlertDialog dialog = builder.create();
        dialog.show();
srs
  • 647
  • 3
  • 11
  • Thanks Santosh. I will check it. But when I click on the positive button on the Dialog, shouldnt the onClick on the button inside the chat layout get set? I always press the positive button. – Uday Aug 22 '14 at 05:59
  • Please check your code, in Positive button's onclick method, you are inflating the new view which is not set on Dialog.. It is not referenced to any view on dialog. You can set listener by using the same view which was set on dialog. View set on dialog is different than the view on which you are setting onclick listener. – srs Aug 22 '14 at 06:03
  • Can you please let me know how do we do that? Meanwhile the onClickLister outside the poitive button and still the onClick is not invoked :( – Uday Aug 22 '14 at 06:31
  • Use the sample code I posted.. If you want to set click listener only on positive button click, take the reference of the view set on dialog and use it. Added sample code in the original answer to show how we can set onclick listener on button when positive button is clicked – srs Aug 22 '14 at 06:33
  • Thanks I did that and I get an exception in the following line. wm.addView(outerVideoView, params); How do we find the parent view for the layout? E/AndroidRuntime(16635): FATAL EXCEPTION: main E/AndroidRuntime(16635): java.lang.RuntimeException: view android.widget.RelativeLayout{212aa410 V.E..... ......ID 0,0-556,617 #7f0e0086 app:id/layout_root} being added, but it already has a parent E/AndroidRuntime(16635): at android.view.View.assignParent(View.java:12121) E/AndroidRuntime(16635): at android.view.ViewRootImpl.setView(ViewRootImpl.java:838) – Uday Aug 22 '14 at 07:08
  • What is wm? What exception you are getting? Is the sample code working for you? – srs Aug 22 '14 at 07:10
  • If wm is root layout of your dialog. It is a RelativeLayout. Make sure that params object in addView is of the type RelativeLayout.LayoutParams. http://developer.android.com/reference/android/widget/RelativeLayout.LayoutParams.html – srs Aug 22 '14 at 07:14
  • As per logs, it seems that outerVideoView already has a parent and you are trying to add it again. Please check. I think wm.addView(outerVideoView, params); is not required. outerVideoView already has parent. – srs Aug 22 '14 at 07:16
  • wm is a WindowManager. I need to add the parent view on that to get the video layout. – Uday Aug 22 '14 at 08:51
  • If my understanding is correct, you are trying to add outerVideoView on dialog. If yes, then addView is not required. If its not dialog part then inflate a new view and add to WindowManager. Also make sure that params in addView should be off the type WindowManager.LayoutParams – srs Aug 22 '14 at 08:59
  • Ok. In that case, we are back to square one. What do I need to do in the original code to make it work? Appreciate your comments. – Uday Aug 22 '14 at 09:34
0
 videoView = new VideoStreamsView(context, R.drawable.tech_mute, R.drawable.tech_connect);


                    LayoutInflater li = LayoutInflater.from(context);
                    outerVideoView = (RelativeLayout) li.inflate(R.layout.chat_float, null); 
                    LinearLayout vView = (LinearLayout) outerVideoView.findViewById(R.id.video_panel);

                    TextView agent_name = (TextView) outerVideoView.findViewById(R.id.agentname);
                    agent_name.setText(name);



                    //RelativeLayout bottomView = (RelativeLayout) outerVideoView.findViewById(R.id.bottom_panel);

                    Button discon_but = (Button) outerVideoView.findViewById(R.id.disconn);
                    discon_but.setClickable(true);

                    discon_but.setOnClickListener(new View.OnClickListener() 
                    {
                        @Override
                        public void onClick(View v) 
                        {
                            //System.out.println("mesh: button clicked disconnect!!");
                            Toast.makeText(context, "Video Connection disconnected", Toast.LENGTH_LONG).show();

                            outCmd.sendJsonCommand(getNodeId(), "videoChatStatus", new StatusJson("User disconnected the call.",1));
                            coord.disconnect();

                        }
                    });

All of this code should be run on UI thread, you can't put this code inside run(). Instead, find a way to use runOnUiThread()

Community
  • 1
  • 1
Marco Altran
  • 376
  • 2
  • 9
0

Looks like finally the issue was fixed. Sorry I had not posted the full code. I change the WindowManager params from the TYPE_SYSTEM_OVERLAY to TYPE_PHONE and it worked. Looks like TYPE_SYSTEM_OVERLAY prevented any touch events from being received due to security issue.

Thanks, Uday

Uday
  • 21
  • 4