Most Android developers are fairly well familiar with the findViewById function our activity inherits from the Activity class. This function receives the ID number of a specific View resource the activity user interface includes.
If you ever try to call that function before calling the setContentView function (our activity inherits from the Activity class) then you must already know that an exception will be thrown. That exception is thrown because as long as we don’t call the setContentView function our activity knows nothing about the very specific view we refer its ID.
When creating a custom dialog based on xml layout document that describes its user interface we must call the findViewById function that was defined in View. We must invoke it on the very specific View object our code inflates from the xml layout document. Calling the findViewById function that was defined in Activity won’t get us the very specific view we want. The activity is not connected with that view.
The following code sample shows a small demo for creating a popup dialog that has its own customized user interface.
package com.abelski.samples; import android.app.Activity; import android.app.AlertDialog; import android.content.DialogInterface; import android.os.Bundle; import android.util.Log; import android.view.LayoutInflater; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.EditText; public class DialogActivitySample extends Activity { private EditText tf1 = null; private EditText tf2 = null; private EditText tf3 = null; private Button bt = null; private EditText tfDialog; public void setNum(double number) { tf2.setText(String.valueOf(number)); } @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); tf1 = (EditText) findViewById(R.id.editText1); tf2 = (EditText) findViewById(R.id.editText2); bt = (Button) findViewById(R.id.button1); tf3 = (EditText) findViewById(R.id.editText3); LayoutInflater li = LayoutInflater.from(this); View view = li.inflate(R.layout.promptdialoglayout, null); tfDialog = (EditText)view.findViewById(R.id.editTextDialog); final AlertDialog.Builder builder = new AlertDialog.Builder(this); builder.setTitle("d i a l o g"); builder.setView(view); android.content.DialogInterface.OnClickListener listener = new android.content.DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { // TODO Auto-generated method stub double num = Double.parseDouble(tfDialog.getText().toString()); setNum(num); } }; builder.setPositiveButton("Set", listener); bt.setOnClickListener(new OnClickListener() { public void onClick(View v) { // TODO Auto-generated method stub double num1 = Double.parseDouble(tf1.getText().toString()); double num2 = Double.parseDouble(tf2.getText().toString()); if (num2 == 0) { AlertDialog dialog = builder.create(); dialog.show(); } else { tf3.setText(String.valueOf((num1 / num2))); } } }); } }