Mark Henry
Mark Henry

Reputation: 265

How to get onclick of a view in custom dialog in an activity in android?

How to get onclick of a view in custom dialog in an activity in android?

My XML is:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/default_green"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/tvSetNotificationsHeading"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_margin="5dp"
        android:text="Set Notifications On"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:textColor="@color/white" />

    <RadioButton
        android:id="@+id/rbFriendRequest"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="10dp"
        android:layout_marginTop="5dp"
        android:text="Friend Request"
        android:textColor="@android:color/white" />

    <RadioButton
        android:id="@+id/rbMessages"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="10dp"
        android:layout_marginTop="5dp"
        android:text="Messages"
        android:textColor="@android:color/white" />

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_margin="5dp"
        android:onClick="saveSetNotificationsDialog"
        android:text="Save" />

</LinearLayout>

My code is:

 public void showSetNotificationsDialog(View v) {

            dialog = new Dialog(Activity_Settings.this);
            dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
            dialog.setContentView(R.layout.dialog_setnotificationsdialog);
            rbFriendRequest = (RadioButton)  findViewById(R.id.rbFriendRequest);
            rbMessages = (RadioButton) findViewById(R.id.rbMessages);
            dialog.show();
        }

I am able to show dialog view but no able to get on click of dialog save button in activity class. Error was no method found exception.

Upvotes: 3

Views: 593

Answers (2)

Anand Phadke
Anand Phadke

Reputation: 531

use as following code

 dialog = new Dialog(Activity_Settings.this);
    dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
    View view = View.inflate(getActivity(), R.layout.dialog_setnotificationsdialog, null);
    dialog.setContentView(view);
    //always find your view view.findViewbyid
     yourView= (RadioButton)  view.findViewById(R.id.rbFriendRequest);
    //then you can add on click listner to any of your view i.e. 
yourView.setonClickListener(this);(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    // Close dialog
                    dialog.dismiss();
                }
            });

Upvotes: 0

Android Developer
Android Developer

Reputation: 466

use instance of dialog for findViewById()

Button declineButton = (Button) dialog.findViewById(R.id.declineButton);

            declineButton.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    // Close dialog
                    dialog.dismiss();
                }
            });

Upvotes: 2

Related Questions