Search code examples
androidspinnerandroid-alertdialogsetcontentview

setContentView, spinner custom alertdialog in android


I want to create a custom alertdialog with Spinner .So i used the following code.

AlertDialog.Builder alertDialog = new AlertDialog.Builder(ActivityThu.this);

    alertDialog.setTitle("Thêm khoản thu");
    //final LayoutInflater layoutInflater = (LayoutInflater) getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
   // final  View dialogView = layoutInflater.inflate(R.layout.thu_thong_bao_them, null);//Tạo dialogview từ layout thông báo
    View dialogView = getLayoutInflater().inflate(R.layout.thu_thong_bao_them, null);
    spinner=(Spinner)dialogView.findViewById(R.id.spdanhmuc_them);

    MyDatabaseHelper db = new MyDatabaseHelper(getApplicationContext());

    List<String> lables = db.Load_danhmucthu();

    ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item, lables);

    dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

    spinner.setAdapter(dataAdapter);

    alertDialog.setView(dialogView);// đặt dialogview vào alertdialog
    alertDialog.setIcon(R.drawable.add48x48);//đặt icon

    alertDialog.setPositiveButton("Thêm",
            new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int which) {


                    }

            });

    alertDialog.setNegativeButton("Hủy",
            new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int which) {
                    dialog.cancel();
                }
            });

    alertDialog.show();

Layout for DialogAlert thu_thong_bao_them.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
              xmlns:tools="http://schemas.android.com/tools"
              android:layout_width="match_parent"
              android:layout_height="match_parent"
              android:padding="20dp">
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Danh mục"
        android:id="@+id/tvdanhmuc_them"/>
    <Spinner
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_toRightOf="@id/tvdanhmuc_them"
        android:layout_marginLeft="20dp"
        android:id="@+id/spdanhmuc_them"
        android:spinnerMode="dropdown">
    </Spinner>
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Nội dung"
        android:id="@+id/tvnoidung_them"
        android:layout_below="@+id/tvdanhmuc_them"
        android:layout_marginTop="20dp"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"/>
    <EditText
        android:id="@+id/edtnoidung_them"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_toRightOf="@id/tvnoidung_them"
        android:layout_below="@id/spdanhmuc_them"
        android:layout_marginLeft="20dp"
        android:inputType="text" />
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Ngày thu"
        android:id="@+id/tvngaythu_them"
        android:layout_below="@+id/tvnoidung_them"
        android:layout_marginTop="20dp"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"/>
    <EditText
        android:id="@+id/edtngaythu_them"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="20dp"
        android:inputType="text"
        android:layout_toRightOf="@id/tvnoidung_them"
        android:layout_below="@id/edtnoidung_them"/>
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Số tiền"
        android:id="@+id/tvsotien_them"
        android:layout_marginTop="20dp"
        android:layout_below="@+id/tvngaythu_them"

        />
    <EditText
        android:id="@+id/edtsotien_them"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_toRightOf="@id/tvsotien_them"
        android:layout_below="@id/edtngaythu_them"
        android:layout_marginLeft="30dp"
        android:inputType="text" />
</RelativeLayout>

Image Befor I call AlertDialog

Image After call alertdialog (data has been load into spinner but main_layout was replaced by layout of alertdialog and spinner in Dialog hasn't data)

Please help me.


Solution

  • After create custom view , set it to alert dialog view, don't set anything related to dialog like icon,positive buttons etc.