pippo15
pippo15

Reputation: 113

How to get views from AlertDialog?

I build an alertDialog in this way:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <TextView
        android:id="@+id/title"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/title1"
        android:textSize="20sp"
        android:textColor="@color/white" />

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/chars1" />

    <RelativeLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:id="@+id/RelativeLayout1"
        android:background="@drawable/body_white">
        <LinearLayout
            android:orientation="horizontal"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/Layout1"
            android:layout_centerHorizontal="true">
            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="@drawable/img1"
                android:id="@+id/img1"
                android:layout_alignParentTop="true"
                />

            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/img2"
                android:background="@drawable/img2"
                android:layout_alignParentTop="true"
                android:layout_toRightOf="@+id/img1"
                android:layout_toEndOf="@+id/img1" />
        </LinearLayout>
    </RelativeLayout>
</LinearLayout>

I want to do this thing:

img1 = (ImageView) findViewById(R.id.img1);

But doing this I receive a NullPointerException.

I used this:

(EditText)((Dialog) dialog).findViewById(R.id.username);

in a early application where I take value after click. In this case I don't have an object DialogInterface dialog. So I can't use it. How can I fill img1?

I want so set manually padding but I want to do this in JAVA and not in XML.

Thanks

Edit:

This is method that launch alertDialog:

public void createDialog() {
        alertDialog = new AlertDialog.Builder(Instructions.this);
        LayoutInflater inflater = this.getLayoutInflater();
        alertDialog.setView(inflater.inflate(R.layout.alert_dialog_layout, null));
        n1Dialog = (ImageView) findViewById(R.id.img1);
        n1Dialog.setPadding(0,0,0,0);
        alertDialog.show();

    }

I have a simple button that when in onClick() method have

createDialog();

Row with setPadding (that is only a proof, it isn't what I want to do) was mistaken because is NullPointerException

Upvotes: 2

Views: 3688

Answers (3)

sayani
sayani

Reputation: 148

You can create the dialog in the following way also:

public void displayPopup(Context ctx) {
    Dialog cd_display = new Dialog(ctx);
    cd_display.requestWindowFeature(Window.FEATURE_NO_TITLE);
    cd_display.setContentView(R.layout.alert_dialog_layout);
    ImageView n1Dialog = (ImageView) cd_display.findViewById(R.id.img1);
    cd_display.show();
}

Upvotes: 0

Matthieu Lemonnier
Matthieu Lemonnier

Reputation: 122

This is how you should inflate a custom view for a AlertDialog:

Builder myCustomDialog = new AlertDialog.Builder(yourContext);
        LayoutInflater factory = LayoutInflater.from(context);
    final View yourInflatedView = factory.inflate(R.layout.your_xml_layout, null);

    //Here you get your imageView (or TextView, EditText,...)
    ImageView img1 = (ImageView) yourInflatedView.findViewById(R.id.img1);

    // Do stuff with your imageview...

    myCustomDialog.setView(yourInflatedView);
    myCustomDialog.show();

Upvotes: 0

Rathan Kumar
Rathan Kumar

Reputation: 2577

do like this:

 alertDialog = new AlertDialog.Builder(Instructions.this);
            LayoutInflater inflater = this.getLayoutInflater();
            View dialogView=inflater.inflate(R.layout.alert_dialog_layout, null);
            alertDialog.setView(dialogView);
            n1Dialog = (ImageView) dialogView.findViewById(R.id.img1);
            n1Dialog.setPadding(0,0,0,0);
            alertDialog.show();

Upvotes: 1

Related Questions