I have the following XML layout file, HELPME.XML
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/fVal"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:textStyle="bold"
android:textColor="#CC0000"
android:text="Function:" />
<TextView
android:id="@+id/eVal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textStyle="bold"
android:textColor="#CC0000"
android:text="Error Control:" />
<TextView
android:id="@+id/hDesc"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="BLAH" />
<TextView
android:layout_width="wrap_content"
android:layout_height="12dp"
android:text="" />
<Button
android:id="@+id/closeButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:text="Close" />
</LinearLayout>
And the bit from the MainFile.java which calls this XML layout in a Dialog box:
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case 1:
//Toast.makeText(MainActivity.this, "Help is Here...", Toast.LENGTH_SHORT).show();
//openHelpWindow();
final Dialog d = new Dialog (MainActivity.this);
d.requestWindowFeature(Window.FEATURE_LEFT_ICON);
d.setContentView(R.layout.helpme);
d.setFeatureDrawableResource(Window.FEATURE_LEFT_ICON, R.drawable.ic_help);
d.setTitle("Color Finder Help");
d.setCancelable(true);
// **********************************************************
// EDIT - FIXED CODE
// This closes just the Dialog window...
Button btn = (Button) d.findViewById(R.id.closeButton);
btn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
d.dismiss();
}
});
//******************************************************************
d.show();
break;
case 2:
Toast.makeText(MainActivity.this, "About this app...", Toast.LENGTH_SHORT).show();
break;
//case 3:
// Toast.makeText(MainActivity.this, "Exiting app...", Toast.LENGTH_SHORT).show();
// System.exit(0);
// break;
}
return true;
}
Because i have my custom “Close Window” button in the xml layout, and because its being displayed as part of the layout in the Dialog Window which opens up from a menu option choice… how can i create an action for the button which will dismiss the dialog box?
You still need to override the onclick listener for the button. Then you can close the dialog like normal.