detno29
detno29

Reputation: 2231

how to bind a view to alertdialog and use the children in this view in android?

I have a View like this:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<ScrollView 
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <RelativeLayout 
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical">
        <TextView
            android:id="@+id/tvDialogTitle"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:text="Information" 
            android:padding="6dip"
            android:textStyle="bold"/>
        <TextView
            android:id="@+id/tvDialogEmail"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Email: " 
            android:padding="6dip"
            android:textStyle="bold"
            android:layout_below="@id/tvDialogTitle"/>
        <EditText
            android:id="@+id/etDialogEmail"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:padding="6dip"
            android:layout_below="@id/tvDialogTitle"
            android:layout_toRightOf="@id/tvDialogEmail"/>
        <TextView
            android:id="@+id/tvDialogPassword"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Password: " 
            android:padding="6dip"
            android:textStyle="bold"
            android:layout_below="@id/etDialogEmail"/>
        <EditText
            android:id="@+id/etDialogPassword"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:padding="6dip"
            android:layout_below="@id/etDialogEmail"
            android:layout_toRightOf="@id/tvDialogPassword"/>
        <TextView 
            android:id="@+id/tvDialogNumberRe"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Re- number: " 
            android:padding="6dip"
            android:textStyle="bold"
            android:layout_below="@id/etDialogPassword"/>
        <EditText
            android:id="@+id/etDialogNumberRe"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:padding="6dip"
            android:layout_below="@id/etDialogPassword"
            android:layout_toRightOf="@id/tvDialogNumberRe"/>
        <TextView 
            android:id="@+id/tvDialogEmailRe"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Re- email: " 
            android:padding="6dip"
            android:textStyle="bold"
            android:layout_below="@id/etDialogNumberRe"/>
        <EditText
            android:id="@+id/etDialogEmailRe"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:padding="6dip"
            android:layout_below="@id/etDialogNumberRe"
            android:layout_toRightOf="@id/tvDialogEmailRe"/>
        <TextView 
            android:id="@+id/tvDialogVerifyCode"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Verify code: " 
            android:padding="6dip"
            android:textStyle="bold"
            android:layout_below="@id/etDialogEmailRe"/>
        <EditText
            android:id="@+id/etDialogVerifyCode"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:padding="6dip"
            android:layout_below="@id/etDialogEmailRe"
            android:layout_toRightOf="@id/tvDialogVerifyCode"/>
    </RelativeLayout>
</ScrollView>

Now I want to bind this view to a AlertDialog. I just know that I can use the .setView method. Nevertheless, I do not know how to use the children such as EditText, TextView in the binded View. For example, I want to get the text in some TextView after clicking OK button in the AlertDialog.

Could anybody help me please. Thanks in advance.

Upvotes: 0

Views: 1113

Answers (2)

jeet
jeet

Reputation: 29199

I am assuming that you have inflated your view into view object, as below:

LayoutInflater mInflater= mContext.getSystemService(mContext.Layout_Inflater_Service);

View view=mInflater.inflate(R.layout.dialog);
dialog.setViw(view);

you can fetch children views by using ids of that view.:

TextView txt=(TextView)view.findViewById(R.id.tvDialogTitle);

Upvotes: 1

waqaslam
waqaslam

Reputation: 68177

you need to inflate your layout into dialog's view and retrieve TextViews/EditTexts from the inflated view:

LayoutInflater li = getLayoutInflater();// LayoutInflater.from(getBaseContext());
View view = li.inflate(R.layout.your_dialog_layout, null);

EditText email = (EditText) view.findViewById(R.id.etDialogEmail);
TextView title = (TextView) view.findViewById(R.id.tvDialogTitle);
//and your other stuff....

AlertDialog.Builder bldr = new AlertDialog.Builder(getBaseContext());
bldr.setView(view);
AlertDialog diag = bldr.create();

Upvotes: 1

Related Questions