Thursday, April 18, 2013

Creating Dialog To Collect User Input

EditText in Dialog

We can create a dialog with Edittext   and other views like Button, CheckBoxes, RadioButtons etc.

For this we need to Create A xml layout and and  inflate it in AlertDialog



                               

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >
   
   
    <EditText
        android:id="@+id/editTextKeywordsToBlock"
        android:hint="Enter 1 or more keywords. Use space berween two keywords"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" >

        <requestFocus />
    </EditText>

    <LinearLayout
                 
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
           
            android:layout_marginTop="10dp">
                 

     <Button
         android:id="@+id/buttonBlockByKeyword"
         android:layout_marginTop="15dp"
         android:layout_weight="1"
         android:layout_width="fill_parent"
         android:layout_height="wrap_content"
         android:text="SAVE"
         />
    
      <Button
         android:id="@+id/buttonCancelBlockKeyword"
         android:layout_marginTop="15dp"
         android:layout_weight="1"
         android:layout_width="fill_parent"
         android:layout_height="wrap_content"
         android:text="Cancel"
         />
   
     </LinearLayout>
   

</LinearLayout>


And inflate this Layout at run time    like following



final Dialog dialog = new Dialog(this);

                    dialog.setContentView(R.layout.block_by_keyword);
                    dialog.setTitle("Keyword To Block");

                    final EditText editTextKeywordToBlock=(EditText)dialog.findViewById(R.id.editTextKeywordsToBlock);
                    Button btnBlock=(Button)dialog.findViewById(R.id.buttonBlockByKeyword);
                    Button btnCancel=(Button)dialog.findViewById(R.id.buttonCancelBlockKeyword);
                    dialog.show();


Get The DATA:

String input =  editTextKeywordToBlock.getText().toString();

We can set ClickListiner on Buttons As Well

 btnBlock.setOnClickListener(new View.OnClickListener() {
                  
                    @Override
                    public void onClick(View v)
                    {
                         //  Your Code

                    }
            });
                        

7 comments: