Saturday, 4 April 2020

Dialog to capture image from the camera or pick from the gallery

  3 comments
<?xml version="1.0" encoding="utf-8"?><!-- res/drawable/rounded_edittext_focused.xml -->
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:padding="10dp"
android:shape="rectangle">
<solid android:color="#FFFFFF" />
<stroke
android:width="1dp"
android:color="@android:color/white" />
<corners
android:bottomLeftRadius="15dp"
android:bottomRightRadius="15dp"
android:topLeftRadius="15dp"
android:topRightRadius="15dp" />
</shape>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/root_view"
android:layout_width="match_parent"
android:orientation="vertical"
android:layout_height="wrap_content"
android:background="@drawable/bg_round_corner">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="@dimen/_50sdp"
android:orientation="horizontal"
android:id="@+id/ll_camera"
android:layout_marginTop="@dimen/_20sdp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent">
<ImageView
android:layout_width="wrap_content"
android:layout_height="@dimen/_40sdp"
android:src="@drawable/ic_camera_take_photo"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="@dimen/_40sdp"
android:textSize="@dimen/_15ssp"
android:gravity="center_vertical"
android:text="Take Photo"
android:textAppearance="@android:style/TextAppearance.Medium"
android:textColor="@android:color/black" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="@dimen/_50sdp"
android:orientation="horizontal"
android:id="@+id/ll_gallery"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toBottomOf="@id/ll_gallery">
<ImageView
android:layout_width="wrap_content"
android:layout_height="@dimen/_40sdp"
android:src="@drawable/ic_gallery"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="@dimen/_40sdp"
android:textSize="@dimen/_15ssp"
android:gravity="center_vertical"
android:text="Choose Photo"
android:textAppearance="@android:style/TextAppearance.Medium"
android:textColor="@android:color/black" />
</LinearLayout>
<TextView
android:id="@+id/txt_dialog_close"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="end"
android:layout_marginRight="@dimen/_15sdp"
android:text="Close"
android:textAllCaps="true"
android:layout_marginBottom="@dimen/_20sdp"
android:textStyle="bold"
android:textSize="@dimen/_15ssp"
android:textColor="@color/colorBlack"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toBottomOf="@id/ll_gallery"/>
</LinearLayout>
class ImagePickerDialog : DialogFragment(), View.OnClickListener {
companion object {
fun newInstance(): ImagePickerDialog {
return ImagePickerDialog()
}
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setStyle(STYLE_NORMAL, R.style.DialogStyle)
}
override fun onActivityCreated(arg0: Bundle?) {
super.onActivityCreated(arg0)
dialog!!.window!!.attributes.windowAnimations = R.style.DialogZoomAnim
}
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
isCancelable = false
return inflater.inflate(R.layout.dialog_image_picker, container, false)
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
setListener()
}
fun setListener() {
ll_camera.setOnClickListener(this)
ll_gallery.setOnClickListener(this)
txt_dialog_close.setOnClickListener {
dismiss()
}
}
override fun onResume() {
super.onResume()
val window = dialog!!.window
val size = Point()
val display = window!!.windowManager.defaultDisplay
display.getSize(size)
val width = size.x
window.setLayout((width * 0.85).toInt(), WindowManager.LayoutParams.WRAP_CONTENT)
window.setGravity(Gravity.CENTER)
}
override fun onClick(view: View?) {
when (view?.id) {
R.id.ll_camera -> {
val values = ContentValues()
values.put(MediaStore.Images.Media.TITLE, "NEW")
val capturedImage = activity?.contentResolver
?.insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values)
val takePicktureIntent = Intent(MediaStore.ACTION_IMAGE_CAPTURE)
takePicktureIntent.putExtra(MediaStore.EXTRA_OUTPUT, capturedImage)
activity?.startActivityForResult(takePicktureIntent, Constants.REQUEST_CAMERA)
dismiss()
}
R.id.ll_gallery -> {
val galleryIntent = Intent(Intent.ACTION_PICK,
MediaStore.Images.Media.EXTERNAL_CONTENT_URI)
activity?.startActivityForResult(galleryIntent, Constants.REQUEST_GALLERY)
dismiss()
}
}
}
}

Simplest way to showing a dialog to choose capture image from the camera or to select image from the gallery of the device.
Image





3 comments :

  1. You can called that dialog by this from the fragment.
    ImagePickerDialog.newInstance().show(fragmentManager!!, "Image Picker")

    ReplyDelete