CalendarView inside ScrollView
In my android app, I need to scroll up and down the months of the CalendarView located inside the ScrollView and LinearLayout.
This is my XML structure:
-ScrollView
--LinearLayout
---CalendarView
What should I do to make the calendar scroll up and down (select another month from here)?
The current behavior of the ScrollView seems to disable scrolling on the calendar.
Any help would be appreciated.
source to share
First create a custom calendar view
scrollCalendar.java
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.widget.CalendarView;
import android.content.Context;
import android.view.ViewParent;
import android.view.MotionEvent;
public class scrollCalendar extends CalendarView {
public scrollCalendar(Context context)
{
super(context);
}
public scrollCalendar(Context context, AttributeSet attrs)
{
super(context,attrs);
}
public scrollCalendar(Context context,AttributeSet attrs, int defStyle)
{
super(context,attrs,defStyle);
}
@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
if (ev.getActionMasked() == MotionEvent.ACTION_DOWN) {
ViewParent p= this.getParent();
if (p != null)
p.requestDisallowInterceptTouchEvent(true);
}
return false;
}
}
In Activity.xml file
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="50dp">
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scrollbarThumbHorizontal="@null"
android:scrollbarThumbVertical="@null"
android:isScrollContainer="true">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<com.example.gateway.scrollCalendar
android:id="@+id/calendarDate"
android:layout_width="wrap_content"
android:layout_height="250dp"
android:layout_gravity="center"
android:background="@drawable/edit_bg"
android:visibility="gone">
</com.example.gateway.scrollCalendar>
</LinearLayout>
</ScrollView>
</RelativeLayout>
source to share
This might help future users. Use below custom scroll class. in xml just replace ScrollView
with nameofpackage.VerticalScrollView
. nameofpackage means the fully qualified name of the package where you put the custom scroll view class (for example if it's com.example than nameofpackage.VerticalScrollView = com.example.VerticalScrollView).
public class VerticalScrollView extends ScrollView{
public VerticalScrollView(Context context) {
super(context);
}
public VerticalScrollView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public VerticalScrollView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
final int action = ev.getAction();
switch (action)
{
case MotionEvent.ACTION_DOWN:
Log.i("VerticalScrollview", "onInterceptTouchEvent: DOWN super false" );
super.onTouchEvent(ev);
break;
case MotionEvent.ACTION_MOVE:
return false; // redirect MotionEvents to ourself
case MotionEvent.ACTION_CANCEL:
Log.i("VerticalScrollview", "onInterceptTouchEvent: CANCEL super false" );
super.onTouchEvent(ev);
break;
case MotionEvent.ACTION_UP:
Log.i("VerticalScrollview", "onInterceptTouchEvent: UP super false" );
return false;
default: Log.i("VerticalScrollview", "onInterceptTouchEvent: " + action ); break;
}
return false;
}
@Override
public boolean onTouchEvent(MotionEvent ev) {
super.onTouchEvent(ev);
Log.i("VerticalScrollview", "onTouchEvent. action: " + ev.getAction() );
return true;
}
}
source to share