Sunday, November 17, 2013

customize dialog box in android

STEP 1
create xml layout for the Dialog
example :
file name and path  : /res/layout/my_dlg.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/editText1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:ems="10" >

        <requestFocus />
    </EditText>

    <Button
        android:id="@+id/button1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="Button" />

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:text="Large Text"
        android:textAppearance="?android:attr/textAppearanceLarge" />

</LinearLayout>


STEP 2

Create Dialog box

code given below
public void showDlg(){
//create dialog object and initialize
Dialog dialog=new Dialog(this,android.R.style.Theme_DeviceDefault_Dialog);
//set title of dialog
dialog.setTitle("My Tiltle");
//set Layout of the dialog
dialog.setContentView(R.layout.my_dlg);

//Intialize UI elements inside the layout
final EditText editText=(EditText)dialog.findViewById(R.id.editText1);
Button button=(Button)dialog.findViewById(R.id.button1);
final TextView textView=(TextView)dialog.findViewById(R.id.textView1);

//some actions

button.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
String hi="editText="+editText.getText().toString();

textView.setText(hi);

}
});

//finally show the dialog

dialog.show();
}



STEP 3
thats all...

No comments:

Post a Comment