Skip to content

Instantly share code, notes, and snippets.

@amrishkakadiya
Last active June 10, 2019 09:10
Show Gist options
  • Save amrishkakadiya/fc2173f29a786dede58899a81e938174 to your computer and use it in GitHub Desktop.
Save amrishkakadiya/fc2173f29a786dede58899a81e938174 to your computer and use it in GitHub Desktop.
Android time range picker using dialog fragment and tablayout.

//Use these files and use below code to open timerange picker

class FormActivity: AppCompatActivity(), RangeSetListener {
    override fun onRangeSet(fromHour: Int, fromMinute: Int, toHour: Int, toMinute: Int) {

    }
    private fun openTimeRangePicker() {
        val rangePickerDialog = RangePickerDialog()
        rangePickerDialog.show(supportFragmentManager, "range_picker")
        rangePickerDialog.setRangeListener(this)
   }
}
<?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="wrap_content"
android:orientation="vertical">
<TimePicker
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/timePicker"/>
</LinearLayout>
<?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:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<android.support.design.widget.TabLayout
android:id="@+id/tabLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/colorAccent"
app:tabIndicatorColor="@color/colorPrimary"
app:tabSelectedTextColor="@color/white_1000"
app:tabTextColor="@color/grey_400" />
<in.coronainfotech.punyakam.daterange.WrapContentHeightViewPager
android:id="@+id/masterViewPager"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<Button
android:id="@+id/btnOkay"
style="@style/Widget.AppCompat.Button.Borderless.Colored"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="end"
android:layout_marginRight="8dp"
android:layout_marginBottom="8dp"
android:text="OK" />
</LinearLayout>
</LinearLayout>
package `in`.coronainfotech.punyakam.daterange
interface FromChangeListener {
fun onTimeSet(fromHour:Int,fromMinute:Int)
}
package `in`.coronainfotech.punyakam.daterange
import `in`.coronainfotech.punyakam.R
import android.os.Bundle
import android.support.design.widget.TabLayout
import android.support.v4.app.DialogFragment
import android.support.v4.app.Fragment
import android.support.v4.app.FragmentManager
import android.support.v4.app.FragmentPagerAdapter
import android.support.v4.view.ViewPager
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import kotlinx.android.synthetic.main.dialog_picker_tabs.view.*
class RangePickerDialog : DialogFragment(), FromChangeListener {
var tabLayout: TabLayout? = null
var viewPager: ViewPager? = null
var adapter: PickerTabAdapter? = null
var fromTimePickerFragment: TimePickerFragment? = null
var toTimePickerFragment: TimePickerFragment? = null
private var rangeSetListener: RangeSetListener? = null;
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
return inflater.inflate(R.layout.dialog_picker_tabs, container, false)
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
tabLayout = view.tabLayout
viewPager = view.masterViewPager;
adapter = PickerTabAdapter(childFragmentManager)
fromTimePickerFragment = TimePickerFragment()
toTimePickerFragment = TimePickerFragment()
fromTimePickerFragment!!.setChangeListener(this)
adapter!!.addFragment("From", fromTimePickerFragment!!)
adapter!!.addFragment("TO", toTimePickerFragment!!)
viewPager!!.adapter = adapter
tabLayout!!.setupWithViewPager(viewPager)
view.btnOkay.setOnClickListener {
if (rangeSetListener != null) {
val fromHour = fromTimePickerFragment!!.getHour()
val fromMinute = fromTimePickerFragment!!.getMinute()
val toHour = toTimePickerFragment!!.getHour()
val toMinute = toTimePickerFragment!!.getMinute()
rangeSetListener!!.onRangeSet(
fromHour,
fromMinute,
toHour,
toMinute
)
}
if (dialog != null)
dialog.dismiss()
}
}
fun setRangeListener(listener: RangeSetListener) {
this.rangeSetListener = listener
}
override fun onTimeSet(fromHour: Int, fromMinute: Int) {
toTimePickerFragment!!.setMinimumTime(fromHour, fromMinute)
}
inner class PickerTabAdapter(private val fm: FragmentManager) : FragmentPagerAdapter(fm) {
var mFragmentCollection: MutableList<Fragment> = ArrayList()
var mTitleCollection: MutableList<String> = ArrayList()
fun addFragment(title: String, fragment: Fragment) {
mTitleCollection.add(title)
mFragmentCollection.add(fragment)
}
override fun getPageTitle(position: Int): CharSequence? {
return mTitleCollection[position]
}
override fun getItem(position: Int): Fragment {
val frgm = mFragmentCollection[position]
val bundle = Bundle()
bundle.putInt("position", position);
frgm.arguments = bundle;
return frgm
}
override fun getCount(): Int {
return mFragmentCollection.size
}
}
}
package `in`.coronainfotech.punyakam.daterange
interface RangeSetListener {
fun onRangeSet(fromHour:Int,fromMinute:Int,toHour:Int,toMinute:Int)
}
package `in`.coronainfotech.punyakam.daterange
import `in`.coronainfotech.punyakam.R
import android.os.Build
import android.os.Bundle
import android.support.v4.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TimePicker
import kotlinx.android.synthetic.main.dialog_fragment_time_picker.view.*
class TimePickerFragment : Fragment() {
var openedPosition = 0
private var fromChangeListener: FromChangeListener? = null;
var timePicker: TimePicker? = null
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
if (arguments != null) {
openedPosition = arguments!!.getInt("position", 0)
}
return inflater.inflate(R.layout.dialog_fragment_time_picker, container, false)
}
fun setChangeListener(listener: FromChangeListener) {
this.fromChangeListener = listener;
}
public fun setMinimumTime(hour: Int, minute: Int) {
if (timePicker != null) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
timePicker!!.hour = hour
timePicker!!.minute = minute
} else {
timePicker!!.currentHour = hour
timePicker!!.currentMinute = minute
}
}
}
public fun getHour():Int{
return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
timePicker!!.hour
} else {
timePicker!!.currentHour
}
}
public fun getMinute():Int{
return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
timePicker!!.minute
} else {
timePicker!!.currentMinute
}
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
timePicker = view.timePicker
timePicker!!.setOnTimeChangedListener { timePicker, hour, minute ->
if (openedPosition == 0 && fromChangeListener != null) fromChangeListener!!.onTimeSet(
hour,
minute
)
}
}
}
package `in`.coronainfotech.punyakam.daterange
import android.content.Context
import android.support.v4.view.ViewPager
import android.util.AttributeSet
import android.view.View
/* https://gist.github.com/egslava/589b82a6add9c816a007*/
class WrapContentHeightViewPager : ViewPager {
constructor(context: Context) : super(context) {}
constructor(context: Context, attrs: AttributeSet) : super(context, attrs) {}
override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
var heightMeasureSpec = heightMeasureSpec
var height = 0
for (i in 0 until childCount) {
val child = getChildAt(i)
child.measure(widthMeasureSpec, View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED))
val h = child.measuredHeight
if (h > height) height = h
}
heightMeasureSpec = View.MeasureSpec.makeMeasureSpec(height, View.MeasureSpec.EXACTLY)
super.onMeasure(widthMeasureSpec, heightMeasureSpec)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment