Customized Dialog

Web Hosting
In this tutorial, I will show you how to create a customized dialog in android. This is good because you can minimized the design and develop what you want.

See Code Sample below:

public void date_to(){
    final Dialog dialog1 = new Dialog(context);

//Getting the screen resolution
dialog1.requestWindowFeature(Window.FEATURE_LEFT_ICON);
dialog1.setContentView(R.layout.send_money_date_layout);
if((getResources().getConfiguration().screenLayout &
Configuration.SCREENLAYOUT_SIZE_LARGE) ==
Configuration.SCREENLAYOUT_SIZE_LARGE){
   dialog1.getWindow().setLayout(350, 270);
}else{
}
//end getting the screen resolution
dialog1.setFeatureDrawableResource(Window.FEATURE_LEFT_ICON, R.drawable.ml_logo);
dialog1.getWindow().setBackgroundDrawableResource(R.drawable.dialog_title_background);
dialog1.setTitle("Select Date To:");

Button btn_dateto = (Button) dialog1.findViewById(R.id.back);
btn_dateto.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
DatePicker dp_date_to = (DatePicker) dialog1.findViewById(R.id.dp_date);

int   day_to  = dp_date_to.getDayOfMonth();
int   month_to= dp_date_to.getMonth()+1;
int   year_to = dp_date_to.getYear();

String date_to = Integer.toString(month_to)+" "+Integer.toString(day_to)+", "+Integer.toString(year_to);
String cdate_to = Integer.toString(year_to)+Integer.toString(month_to)+Integer.toString(day_to);
SavePreferences("compare_to", cdate_to);
SimpleDateFormat dateFormat = new SimpleDateFormat(
           "MM dd, yyyy");
   Date myDate = null;
  
       try {
myDate = dateFormat.parse(date_to);
} catch (java.text.ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

   SimpleDateFormat timeFormat = new SimpleDateFormat("MMM dd, yyyy");
   String fdate_to = timeFormat.format(myDate); 
   SavePreferences("date_to", fdate_to);
et_dateto.setText(LoadPreferences("date_to", ""));
dialog1.dismiss();
}
});

dialog1.show();
    }


I put the dialog code inside the function, so i just call the function when i click a button or whatsoever. In this example i put the datepicker view inside the dialog. Put this code into your Activity.java.

To call the function in a button for example. In your onCreate Method or whatsoever. Here's the code.

Button your_button_name = (Button)findViewById(R.id.button_id);
your_button_name.setOnClickListener(new OnClickListener(){
date_to();
});

Next is the XML for the customized dialog. See code below.

dialog.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="match_parent"
    android:layout_gravity="center_horizontal"
    android:background="#FFFFFF"
    android:paddingTop="5dp" >

    <LinearLayout
        android:id="@+id/header"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">

        <DatePicker
            android:id="@+id/dp_date"
            android:layout_gravity="center_horizontal"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
        
    </LinearLayout>


    <LinearLayout
        android:id="@+id/ll_date_ok"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentRight="true"
        android:layout_below="@id/header"
        android:layout_marginTop="14dp"
        android:gravity="center"
        android:orientation="horizontal"
        android:paddingBottom="5dp"
        android:weightSum="100" >

        <Button
            android:id="@+id/back"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="10dp"
            android:layout_weight="48"
            android:background="@drawable/btn_style"
            android:gravity="center"
            android:padding="10dp"
            android:shadowRadius="0.6"
            android:drawableLeft="@drawable/back"
            android:text="OK"
            android:textColor="#FFFFFF" />
    </LinearLayout>


</RelativeLayout>

btn_style.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
     <item android:state_pressed="true" >
         <shape android:shape="rectangle"  >
             <corners android:radius="3dip" />
             <stroke android:width="1dip" android:color="#000000" />
             <gradient  android:angle="-90"  android:startColor="#800000" android:endColor="#FF0000"  />            
         </shape>
     </item>
    <item android:state_focused="true">
         <shape android:shape="rectangle"  >
             <corners android:radius="3dip" />
             <stroke android:width="1dip" android:color="#000000" />
             <solid  android:color="#8a7246"/>       
         </shape>
     </item>  
    <item >
        <shape android:shape="rectangle"  >
             <corners android:radius="3dip" />
             <stroke android:width="1dip" android:color="#000000" />
             <gradient  android:angle="-90"  android:startColor="#FF0000" android:endColor="#800000" />            
         </shape>
     </item>

</selector>

Here's the sample output below:


Web Hosting
That's All. Enjoy Coding...!

No comments:

Post a Comment