RSS

Code to create alert box that accept the inputs in Android

15 Aug

Note : You can create your view directly from the Layout Inflater, you only need to use the name of your layout XML file and the ID of the layout in file.

Your XML file should have an ID like this:

<?xml version=”1.0″ encoding=”utf-8″?>
<LinearLayout xmlns:android=”http://schemas.android.com/apk/res/android&#8221;
android:id=”@+id/dialog_layout_root”
android:orientation=”vertical”
android:layout_width=”fill_parent”
android:layout_height=”wrap_content”
android:padding=”10dp”
>

And then you can set your layout on the builder with the following:

LayoutInflater inflater = getLayoutInflater();
View dialoglayout = inflater.inflate(R.layout.dialog_layout, (ViewGroup) findViewById(R.id.dialog_layout_root));
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setView(dialoglayout);
LayoutInflater factory = getLayoutInflater();
final View textEntryView = factory.inflate(R.layout.username_password_dialog, (ViewGroup) findViewById(R.id.linearLayout1));
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle(“MyWindow”);
builder.setView(textEntryView);
builder.setPositiveButton(“Ok”, new DialogInterface.OnClickListener() {

@Override
public void onClick(DialogInterface dialog, int whichButton) {
EditText etUsername = (EditText)textEntryView.findViewById(R.id.txtUsername);
EditText etPassword = (EditText)textEntryView.findViewById(R.id.txtPassword);
String strUsername = etUsername.getText().toString();
String strPassword = etPassword.getText().toString();
if(“nikhil”.equals(strUsername) && “puppy”.equals(strPassword))
{
setContentView(R.layout.username_password_dialog);
}
}
});
builder.setNegativeButton(“Cancel”, new DialogInterface.OnClickListener() {

@Override
public void onClick(DialogInterface dialog, int whichButton) {
dialog.cancel();
}
});
builder.show();
}

Advertisements
 
Leave a comment

Posted by on August 15, 2011 in Android Source Code

 

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

 
%d bloggers like this: