Instead of extending an Activity we now extend Dialog.
The onCreate method is the same as in an Activity.
But we need a constructor to let the user check or uncheck the 2 checkboxes starting the dialog.
The Listener for the ok button is the same, but we need to qualify the interface OnClickListener by android.view.View.OnClickListener or otherwise the android.content.Dialog.Interface.OnClickListener is taken.
The beerwine.xml is the same as in Second.
Here comes the empty Activity opening our own BeerWineDialog with both checkboxes checked:
Using java Syntax Highlighting
package tut.orial;
import android.app.Activity;
import android.os.Bundle;
public class Third extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
BeerWineDialog dialog = new BeerWineDialog(this, true, true);
dialog.show();
}
}
Parsed in 0.037 seconds, using
GeSHi 1.0.8.4
And this is the BeerWineDialog:
Using java Syntax Highlighting
package tut.orial;
import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
public class BeerWineDialog extends Dialog {
private boolean beer;
private boolean wine;
public BeerWineDialog(Context context, boolean beer, boolean wine) {
super(context);
this.beer = beer;
this.wine = wine;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.beerwine);
setTitle("beer or wine");
CheckBox cbBeer = (CheckBox) findViewById(R.id.checkboxBeer);
cbBeer.setChecked(beer);
CheckBox cbWine = (CheckBox) findViewById(R.id.checkboxWine);
cbWine.setChecked(wine);
Button buttonOK = (Button) findViewById(R.id.buttonOK);
buttonOK.setOnClickListener(new OKListener());
}
private class OKListener implements android.view.View.OnClickListener {
@Override
public void onClick(View v) {
BeerWineDialog.this.dismiss();
}
}
}
Parsed in 0.040 seconds, using
GeSHi 1.0.8.4
Greetings,
DaRolla