Showing posts with label alert Dialog with xml layout. Show all posts
Showing posts with label alert Dialog with xml layout. Show all posts

Wednesday, 10 December 2014

Creating Input Dialog Box with Xml Layout File



Creating Input Dialog Box with Xml Layout File


Call this Method in your Actvity


private void OpenCategroyDialogBox() {

  LayoutInflater layoutInflater = LayoutInflater.from(this);
  View promptView = layoutInflater.inflate(R.layout.addnewcategory, null);
  final AlertDialog.Builder alert = new AlertDialog.Builder(this);
  alert.setTitle("Add New Category");
  alert.setView(promptView);

  final EditText input = (EditText) promptView
    .findViewById(R.id.etCategory);

  input.requestFocus();
  input.setHint("Enter Category");
  input.setTextColor(Color.BLACK);

  alert.setPositiveButton("OK", new DialogInterface.OnClickListener() {
   public void onClick(DialogInterface dialog, int whichButton) {
    String newCategoryName = input.getText().toString();

    // Do something with value!

    if (newCategoryName.equals("")) {
     input.setError("Name Required");
     OpenCategroyDialogBox();
    } else {
     
      Toast.makeText(getApplicationContext(),
        "Ok Clicked", Toast.LENGTH_SHORT).show();

      
     }
    }

   

  });

  alert.setNegativeButton("CANCEL",
    new DialogInterface.OnClickListener() {
     public void onClick(DialogInterface dialog, int whichButton) {
      // Canceled.

      Toast.makeText(getApplicationContext(),
        "Ok Clicked", Toast.LENGTH_SHORT).show();
} }); // create an alert dialog AlertDialog alert1 = alert.create(); alert1.show(); }



addnewcategory.xml


<?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" >

    <EditText
        android:id="@+id/etCategory"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:ems="10"
        android:layout_marginTop="20dp"
        android:layout_marginLeft="24dp"
        android:layout_marginRight="24dp"
         android:layout_marginBottom="20dp"
         android:inputType="textCapSentences" >

        <requestFocus />
    </EditText>

</LinearLayout>


ScreenShot