Custom Dialog Showing using Button Click
To create custom dialog in android with help of android studio ide
Another Activity to Call :-
btn_next = (Button)findViewById(R.id.btn_next); btn_next.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { CustomDialogFragment cdf = new CustomDialogFragment(); cdf.show(getSupportFragmentManager(),"CustomDialogFragment"); } });
Custom Dialog Fragment class ===> java Class
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; } }
custom_dialog.xml file
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_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_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="Message Dialog Content Showing Here Internet connection"
android:textSize="16sp"
android:textColor="#333333" />
<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> <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>
==============drawable folder xml files ============================
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_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>
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>
Result Image
No comments:
Post a Comment