0

I want to do something with dialog in my code and I have a problem with this thing. My problem occurs at here:

btnDialogCancel.setOnClickListener(new View.OnClickListener() 

Can anyone please help me ? thx a lot

Here is my MainActivity class:

public class MainActivity extends AppCompatActivity {

private ProgressBar prgChronemeter;
private TextView txtShowSecond;
private Handler handler = new Handler();

private Button btnSelectMin;
private Button btnDialogSelect;
private Button btnDialogCancel;
private  EditText etMinute;
private  Dialog dialog;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);



    prgChronometer = (ProgressBar) findViewById(R.id.prgChronometer);
    txtShowSecond = (TextView) findViewById(R.id.txtShowSecond);

    btnSelectMin = (Button) findViewById(R.id.btnSelectMin);

    btnSelectMin.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            customDialogCalistir();
        }
    });
}

private void customDialogCalistir(){
    dialog = new Dialog(this);
    dialog.setContentView(R.layout.dialog);
    btnDialogCancel = (Button) findViewById(R.id.btnDialogCancel);
    btnDialogSelect =(Button) findViewById(R.id.btnDialogSelect);
    etMinute = (EditText) findViewById(R.id.etMinute);

    btnDialogCancel.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            dialog.dismiss();
        }
    });
    btnDialogSelect.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            dialog.dismiss();
        }
    });
    dialog.show();
}
}

Here is my dialog.xml file

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

    <Button
        android:id="@+id/btnDialogSelect"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginRight="16dp"
        android:text="Seç"
        android:layout_marginStart="42dp"
        android:layout_marginTop="155dp"
        android:layout_alignParentTop="true"
        android:layout_alignParentStart="true" />

    <EditText
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:inputType="textPersonName"
        android:ems="10"
        android:layout_centerHorizontal="true"
        android:id="@+id/etMinute"
        android:layout_marginTop="50dp"
         />

    <Button
        android:id="@+id/btnDialogCancel"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="20dp"

        android:layout_marginEnd="12dp"
         />

</RelativeLayout>
Harshad Pansuriya
  • 20,189
  • 8
  • 67
  • 95
enginear
  • 29
  • 2
  • 9

2 Answers2

1
private void customDialogCalistir(){
    dialog = new Dialog(this);
    dialog.setContentView(R.layout.dialog);
    btnDialogCancel = (Button) dialog.findViewById(R.id.btnDialogCancel);
    btnDialogSelect =(Button) dialog.findViewById(R.id.btnDialogSelect);
    etMinute = (EditText) dialog.findViewById(R.id.etMinute);

    btnDialogCancel.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            dialog.dismiss();
        }
    });
    btnDialogSelect.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            dialog.dismiss();
        }
    });
    dialog.show();
}

Use this will Solve your problem.

Rishabh Mahatha
  • 1,251
  • 9
  • 19
0
  btnDialogCancel = (Button) findViewById(R.id.btnDialogCancel);
btnDialogSelect =(Button) findViewById(R.id.btnDialogSelect);
etMinute = (EditText) findViewById(R.id.etMinute);

you cant refer directly to Dialog elements directly grom main class using findViewByid method. Instead of reffering to dialog's elements,use this

 btnDialogCancel = (Button) dialog.findViewById(R.id.btnDialogCancel);
btnDialogSelect =(Button) dialog.findViewById(R.id.btnDialogSelect);
etMinute = (EditText) dialog.findViewById(R.id.etMinute);
Vishesh
  • 308
  • 1
  • 9