Android custom dialog example
The following steps are for creating a basic custom dialog in Android by extending the Dialog class.
1. This is optional, but here is a drawable layout for making round corners for the custom dialog. dialog_shape.xml
2. Create the layout file for the custom dialog. custom_dialog.xml
3. Create the custom class which extends from the Dialog class for the custom dialog.
public class CustomDialog extends Dialog implements android.view.View.OnClickListener { public Activity activity; public Button btnYes, btnNo; public CustomDialog(Activity activity) { super(activity); this.activity = activity; } @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); requestWindowFeature(Window.FEATURE_NO_TITLE); setContentView(R.layout.custom_dialog); btnYes = (Button) findViewById(R.id.btn_yes); btnNo = (Button) findViewById(R.id.btn_no); btnYes.setOnClickListener(this); btnNo.setOnClickListener(this); } @Override public void onClick(View v) { switch (v.getId()) { case R.id.btn_yes: activity.finish(); break; case R.id.btn_no: dismiss(); break; default: break; } dismiss(); } }
4. Call the Custom Dialog.
CustomDialog customDialog = new CustomDialog(MainActivity.this); customDialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT)); customDialog.show();
5. Modify the custom layout to whatever fits your need.
Search within Codexpedia
Custom Search
Search the entire web
Custom Search
Related Posts