Close button in dialog

I created my dialog when I click one of the menu buttons: @Override public boolean onCreateOptionsMenu (menu menu) {// Inflate the menu; this adds items to the action bar if present. getMenuInflater (). inflate (R.menu.menu_first__window, menu); return true; }

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();


    switch (id)
    {
        case    R.id.action_settings:
            About_us aboutus = new About_us(this);
            aboutus.show();
            return true;

        case R.id.close: System.exit(0); return true;
    }


    return super.onOptionsItemSelected(item);
}

      

My dialog layout

      <Button
    android:layout_marginTop="10dp"
    android:layout_width="match_parent"
    android:layout_height="40dp"
    android:text="Close"
    android:onClick="ext_btn"

    />

      

My dialogue class

public class mydialog extends Dialog

      

{

public About_us(Context context) {

    super(context);
}

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    setContentView(R.layout.about_us);
}

public void ext_btn(View view) {

    About_us.this.dismiss();
}
}

      

I have tried many codes to close the close button of the dialog box. My application is always crashing. Where is the mistake?

+3


source to share


1 answer


I needed to delete the method I just created and create the following inside the onCreate method



    Button btn_ext = (Button) findViewById(R.id.btn_ext);
    btn_ext.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            dismiss();
        }

    });

      

+1


source







All Articles