Thursday, January 3, 2013

Creating Customized Dialogs in Android

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();

No comments:

Post a Comment