ImageView setOnClickListener in a dialog

1.1k Views Asked by At

I create a custom dialog box with this method:

public Dialog onCreateDialog() {
    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    LayoutInflater inflater = this.getLayoutInflater();
    builder.setView(inflater.inflate(R.layout.dialog_rate, null))
            .setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int id) {
                    SharedPreferences.Editor settingsEditor = settings.edit();
                    settingsEditor.putBoolean("rate",false);
                    settingsEditor.apply();
                    Intent intent = new Intent();
                    intent.setAction(Intent.ACTION_VIEW);
                    intent.addCategory(Intent.CATEGORY_BROWSABLE);
                    intent.setData(Uri.parse("https://play.google.com/store/apps/details?id=de.resper.pzcrettungsdienst"));
                    startActivity(intent);
                }
            })
            .setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {
                    SharedPreferences.Editor settingsEditor = settings.edit();
                    settingsEditor.putBoolean("rate",false);
                    settingsEditor.apply();
                }
            })
            .setNeutralButton(R.string.later, new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {
                    SharedPreferences.Editor settingsEditor = settings.edit();
                    settingsEditor.putInt("count",0);
                    settingsEditor.apply();
                }
            });
    return builder.create();
}

I open the dialog like this:

Dialog dialog = onCreateDialog();
dialog.show(); 

This is my layout file:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:padding="10dp">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:text="@string/dialog_message"
        android:id="@+id/textView15"
        android:layout_gravity="center_horizontal"
        android:gravity="center"/>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:gravity="center"
        android:text="@string/resperLink"
        android:layout_margin="20dp"
        android:id="@+id/textView" />


    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="center">
        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/fbImg"
            android:src="@drawable/fb_icon"
            android:layout_margin="5dp"/>
        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/gplusImg"
            android:src="@drawable/gplus_icon"
            android:layout_margin="5dp"/>
        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/twImg"
            android:src="@drawable/tw_icon"
            android:layout_margin="5dp"/>
        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/pinImg"
            android:src="@drawable/pin_icon"
            android:layout_margin="5dp"/>


    </LinearLayout>

</LinearLayout>

In the layout file are images, is it possible to set on the images in the layout "setOnClickListener"? The Buttons working, but I want to add some more "actions" for the images.

1

There are 1 best solutions below

0
On BEST ANSWER

In the layout file are images, is it possible to set on the images in the layout "setOnClickListener"?

yes it is possible. Keep a reference to the inflated view, and use this to findViewById

View view= inflater.inflate(R.layout.dialog_rate, null);
view.findViewById(R.id.yourid).setOnClickListener(...);
builder.setView(view)