Reputation:
When i click the save button without entering values the dialog gets disappear.I want to show the dialog box as well as want to do the validation.Please help me if anybody knows.
Here is my code:
TextView tvUserName=new TextView(this);
TextView tvPassword=new TextView(this);
TextView tvURL=new TextView(this);
final EditText etUserName=new EditText(this);
final EditText etPassword=new EditText(this);
final AlertDialog.Builder alert = new AlertDialog.Builder(this);
LinearLayout login= new LinearLayout(this);
login.setOrientation(1); //1 is for vertical orientation
tvUserName.setText(getResources().getString(R.string.username));
tvPassword.setText(getResources().getString(R.string.password));
login.addView(tvURL);
login.addView(etURL);
login.addView(tvUserName);
login.addView(etUserName);
login.addView(tvPassword);
etPassword.setInputType(InputType.TYPE_CLASS_TEXT |InputType.TYPE_TEXT_VARIATION_PASSWORD);
login.addView(etPassword);
alert.setView(login);
alert.setTitle(getResources().getString(R.string.login));
alert.setPositiveButton(getResources().getString(R.string.login), new DialogInterface.OnClickListener() {
public void onClick(final DialogInterface dialog, int whichButton) {
strUserName=etUserName.getText().toString();
XmlUtil.username=strUserName;
strPassword=etPassword.getText().toString();
strhwdXml=etURL.getText().toString();
if((strUserName.length()==0)||(strPassword.length()==0){
Toast.makeText(getBaseContext(),"Please enter username and password", Toast.LENGTH_SHORT).show();
}else {
Toast.makeText(getBaseContext(),"Success", Toast.LENGTH_SHORT).show();
}
Upvotes: 0
Views: 1647
Reputation: 5045
private void showAlertBox() {
AlertDialog alertBox = null;
alertBox = new AlertDialog.Builder(this).create();
alertBox.setTitle("titleText");
alertBox.setMessage("messageText");
alertBox.setCancelable(false);
LayoutInflater inflater = LayoutInflater.from(this);
View alert_webview = inflater.inflate(R.layout.alert_layout, null);
alertBox.setView(alert_webview);
btnfromlayout = (Button) alert_webview.findViewById(R.id.btnclose);
btnfromlayout.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// Whatever you want on click...compare
// if(strUserName.equalsIgnoreCase("") ||
// strPassword.equalsIgnoreCase("")){
// Your toast msg
// }else{
// cAlert.dismiss();
// }
Log.i("Close", "Close");
}
});
alertBox.show();
}
@Override
public void onClick(View v) {
int vId = v.getId();
switch (vId) {
case R.id.btnclose:
//Whatever you want on click...compare
//if(strUserName.equalsIgnoreCase("") || strPassword.equalsIgnoreCase("")){
//Your toast msg
//}else{
// cAlert.dismiss();
//}
Log.i("Close", "Close");
break;
default:
break;
}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/txtcustmessage"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Txt custom Layout For Alert" />
<Button
android:id="@+id/btnclose"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Close Custom" />
</LinearLayout>
/// Like the above layout u can create your own layout for login controls with design
and set click listners as per the layout and required action
Upvotes: 1
Reputation: 34765
better to include button also instead of using setpositivebutton. if yo have button then you can do as you want.
Upvotes: 0
Reputation: 2302
I think the best way to make that kind of AlertDialog, would be to make a new custom layout for it, just like you would make for an activity. If you're using setPositiveButton etc. they close the dialog on default, when pressed.
Upvotes: 0