Skip to content

Instantly share code, notes, and snippets.

@leonguyen
Created April 10, 2013 04:36
Show Gist options
  • Select an option

  • Save leonguyen/5351822 to your computer and use it in GitHub Desktop.

Select an option

Save leonguyen/5351822 to your computer and use it in GitHub Desktop.
Android Lab: Dialog with Custom Layout - Create Custom Dialog program
package com.example.androidlab;
import android.os.Bundle;
import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class MainActivity extends Activity {
final Context context = this;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button = (Button) findViewById(R.id.buttonCustomDialog);
// Add button listener
button.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
// Custom dialog
final Dialog dialog = new Dialog(context);
dialog.setContentView(R.layout.custom_dialog);
dialog.setTitle("Edit");
Button dialogButton = (Button) dialog.findViewById(R.id.buttonOK);
// if button is clicked, close the custom dialog
dialogButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
dialog.dismiss();
}
});
dialog.show();
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment