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

                    }
            });
                        

5 comments:

  1. great site, i luv your clearity, pls keep the good work going

    ReplyDelete
  2. good site for examples...keep posting some more examples and...give some tutorials for programming in java in android platform

    ReplyDelete
  3. please do i create a dialog message that synchronises with time alarm

    ReplyDelete
  4. How to create a dialog message to pop up at stipulated time of the day in android

    ReplyDelete