Android tutorial -Custom dialog fragment

Public on October 5, 2014


/**
Would you like to visit my new apps in google play :
"Best Motivational Stories"

https://play.google.com/store/apps/details?id=com.thanhcs.motivationstories

**/




Do you wanna create Android dialog like this :





Follow my android code below and you'll make that dialog :

CustomDialogFragment.java 
package jp.classmethod.android.sample.customdialog;
import android.app.Dialog;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.view.WindowManager;
public class CustomDialogFragment extends DialogFragment {
     
    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
         
        Dialog dialog = new Dialog(getActivity());
     
        dialog.getWindow().requestFeature(Window.FEATURE_NO_TITLE);
        dialog.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN);
        dialog.setContentView(R.layout.dialog_custom);
        dialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        // OK
        dialog.findViewById(R.id.positive_button).setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });
        // Close
        dialog.findViewById(R.id.close_button).setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });
         
        return dialog;
    }
}

dialog_custom.xml


<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >
   
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_below="@+id/title"
        android:layout_marginTop="6dp"
        android:layout_marginLeft="6dp"
        android:layout_marginRight="6dp"
        android:paddingBottom="20dp"
        android:background="@drawable/bg_dialog"
        android:gravity="center_horizontal"
        android:orientation="vertical"
       >

        <TextView
            android:id="@+id/title"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@drawable/bg_dialog_title"
            android:padding="10dp"
            android:text="Title"
            android:textSize="18sp"
            android:textColor="@android:color/white"
            />
        <TextView
            android:id="@+id/message"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_margin="20dp"
            android:lineSpacingExtra="6dp"
            android:text="MessageMessageMessageMessageMessageMessageMessageMessageMessage"
            android:textSize="16sp"
            android:textColor="#333333"
            />
        <!-- OK
        <Button
            android:id="@+id/positive_button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="6dp"
            android:background="@drawable/bt_dialog_positive"
            android:text="OK"
            android:textColor="@android:color/white"
            />
    </LinearLayout>
    <!-- Close
    <Button
        android:id="@+id/close_button"
        android:layout_width="40dp"
        android:layout_height="40dp"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:background="@drawable/bt_dialog_close"
        android:text="×"
        android:textColor="#9acd32"
        />
</RelativeLayout>


bg_dialog.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
     
    <corners android:radius="5dp"/>
     
    <stroke android:width="2dp" android:color="#9acd32"/>
     
    <gradient android:startColor="#ffffff" android:endColor="#dcdcdc" android:angle="90"/>
</shape>


bg_title.xml


<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
     
    <corners android:topLeftRadius="5dp" android:topRightRadius="5dp"/>
     
    <solid android:color="#9acd32"/>
 </shape>

bt_dialog_positive.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
    <corners android:radius="5dp" />
    <solid android:color="#9acd32" />
 </shape>

bt_dialog_close.xml


<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="oval">
     
    <solid android:color="@android:color/white"/>
     
    <stroke
        android:width="3dp"
        android:color="#9acd32"
        />
     </shape>

----------------------------DONE------------------------

//
may something you're interesting in here : 








[Advertising]Faster Thinking - Speed up your brain


Faster Thinking Game



sentiment_satisfied Emoticon