Skip to content

Instantly share code, notes, and snippets.

@SouravKumarPandit
Created December 19, 2018 13:51
Show Gist options
  • Save SouravKumarPandit/f3485e589ab9151f18e08086c6c440b7 to your computer and use it in GitHub Desktop.
Save SouravKumarPandit/f3485e589ab9151f18e08086c6c440b7 to your computer and use it in GitHub Desktop.
package com.example.madhav.milikevault.dash;
import android.graphics.Color;
import android.graphics.Point;
import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.GradientDrawable;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewParent;
import android.widget.FrameLayout;
import android.widget.HorizontalScrollView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.ScrollView;
import android.widget.TextView;
import android.widget.Toast;
import java.util.ArrayList;
//created by madhav
public class DashScrollFocus extends AppCompatActivity implements /*View.OnTouchListener, */View.OnClickListener {
String[] scolors=new String[]{"#4236dd"
,"#dc5d4e"
,"#d07da4"
,"#333333"
,"#ffbb43"
,"#feb3ff"
,"#167672"
,"#f19623"
,"#5c0067"
,"#0048e0"};
String[] sGraphNames=new String[]{"Pie chart",
"Bar Graph",
"Report",
"Bar Graphs",
"Boxplots",
"Histogram",
"Line Graphs",
"Pie Graphs",
"Stemplots",
"Plot Graph"};
ScrollView clscrollView;
private ArrayList<String> visibleWidgets=new ArrayList<>();
private float height;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(getFocusAwareView());
}
private View getFocusAwareView() {
RelativeLayout clRelativeLayout=new RelativeLayout(this);
clRelativeLayout.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,ViewGroup.LayoutParams.MATCH_PARENT));
clscrollView=new ScrollView(this);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
clscrollView.setOnScrollChangeListener(new View.OnScrollChangeListener() {
@Override
public void onScrollChange(View v, int scrollX, int scrollY, int oldScrollX, int oldScrollY) {
handleScrollAction(oldScrollY>scrollY);
}
});
}
// clscrollView.setOnTouchListener(this);
clRelativeLayout.addView(clscrollView);
clRelativeLayout.addView(getFooterLayout());
clscrollView.setFillViewport(true);
clscrollView.setLayoutParams(new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,ViewGroup.LayoutParams.MATCH_PARENT));
LinearLayout clLinearLayout=new LinearLayout(this);
clscrollView.addView(clLinearLayout);
clLinearLayout.setLayoutParams(new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,ViewGroup.LayoutParams.MATCH_PARENT));
clLinearLayout.setOrientation(LinearLayout.VERTICAL);
for (int i=300;i<300+sGraphNames.length;i++){
clLinearLayout.addView(getView(sGraphNames[i-300], i,Color.parseColor(scolors[i-300])));
}
return clRelativeLayout;
}
private View getFooterLayout() {
HorizontalScrollView clHorizontalView=new HorizontalScrollView(this);
clHorizontalView.setPadding(0,0,0,0);
clHorizontalView.setFillViewport(true);
RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, 80);
layoutParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
clHorizontalView.setLayoutParams(layoutParams);
LinearLayout clLinearLayout=new LinearLayout(this);
clLinearLayout.setPadding(20,8,8,8);
clLinearLayout.setBackgroundDrawable(new ColorDrawable(Color.parseColor("#80000000")));
clHorizontalView.addView(clLinearLayout);
clLinearLayout.setLayoutParams(new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,ViewGroup.LayoutParams.WRAP_CONTENT));
clLinearLayout.setOrientation(LinearLayout.HORIZONTAL);
for (int i=0;i<scolors.length;i++){
clLinearLayout.addView(addDrawable(Color.parseColor(scolors[i]),i+1000));
}
return clHorizontalView;
}
private View addDrawable(int iColor,int iId) {
View clView=new View(this);
if (iId<1300) {
clView.setOnClickListener(this);
}
clView.setId(iId);
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(30, 30);
layoutParams.setMargins(8,0,8,0);
clView.setLayoutParams(layoutParams);
GradientDrawable gradientDrawable=new GradientDrawable();
gradientDrawable.setCornerRadii(new float[]{10,10,10,10,10,10,10,10});
gradientDrawable.setColor(iColor);
gradientDrawable.setStroke(1,Color.parseColor("#dfe1e5"));
clView.setBackgroundDrawable(gradientDrawable);
return clView;
}
private View getView(String s,int id,int iColor) {
LinearLayout linearLayout=new LinearLayout(this);
linearLayout.setId(id);
linearLayout.setTag(id+700);
linearLayout.setOrientation(LinearLayout.HORIZONTAL);
View view = addDrawable(iColor,id+1000);
LinearLayout.LayoutParams layoutParams1 = new LinearLayout.LayoutParams(45,45);
layoutParams1.setMargins(20,20,0,0);
view.setLayoutParams(layoutParams1);
linearLayout.addView(view);
GradientDrawable gradientDrawable=new GradientDrawable();
gradientDrawable.setStroke(1,Color.parseColor("#dfe1e5"));
gradientDrawable.setColor(Color.parseColor("#ffffff"));
linearLayout.setBackgroundDrawable(gradientDrawable);
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, 400);
layoutParams.setMargins(15,15,15,15);
linearLayout.setLayoutParams(layoutParams);
linearLayout.setOrientation(LinearLayout.VERTICAL);
TextView textView=new TextView(this);
linearLayout.addView(textView);
textView.setText(s);
textView.setGravity(Gravity.CENTER);
textView.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,ViewGroup.LayoutParams.MATCH_PARENT));
return linearLayout;
}
/*float oldfX=0,oldfY=0,newfX=0,newfY=0;
float distance=0;
@Override
public boolean onTouch(View view, MotionEvent event) {
switch (event.getActionMasked()) {
case MotionEvent.ACTION_DOWN:
oldfX=event.getRawX();
oldfX =event.getRawY();
break;
case MotionEvent.ACTION_UP:
break;
case MotionEvent.ACTION_MOVE:
newfX=event.getRawX();
newfY=event.getRawY();
if((newfY-oldfY)>0)//up
{
distance=newfY-oldfY;
// setSwipe(distance, true,150);
//newfY-oldfY
handleScrollAction(true);
}
else if((oldfY-newfY)>0)
{
distance=oldfY-newfY;
// setSwipe(distance, false,150);
handleScrollAction(false);
}
oldfX=newfX;
oldfY=newfY;
break;
default:
return false;
}
return true;
}*/
/*@Override
public boolean onTouch(View v, MotionEvent event) {
int action = event.getAction();
float height = event.getY();
if(action == MotionEvent.ACTION_DOWN){
this.height = height;
}
else if(action==MotionEvent.ACTION_MOVE)
{
if (this.height < height) {
// Toast.makeText(this, "Scrolled Up", Toast.LENGTH_SHORT).show();
handleScrollAction(true);
} else if (this.height > height) {
// Toast.makeText(this, "Scrolled Down", Toast.LENGTH_SHORT).show();
handleScrollAction(false);
}
}
else if(action == MotionEvent.ACTION_SCROLL) {
if (this.height < height) {
// Toast.makeText(this, "Scrolled Up", Toast.LENGTH_SHORT).show();
handleScrollAction(true);
} else if (this.height > height) {
// Toast.makeText(this, "Scrolled Down", Toast.LENGTH_SHORT).show();
handleScrollAction(false);
}
}
*//*switch (event.getAction()) {
case MotionEvent.ACTION_CANCEL:
case MotionEvent.ACTION_UP:
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
handleScrollAction();
}
}, 150);
break;
}*//*
return false;
}*/
@Override
public void onWindowFocusChanged(boolean hasFocus) {
super.onWindowFocusChanged(hasFocus);
//Here you can get the size!
handleScrollAction(false);
}
private void handleScrollAction(boolean b) {
ViewGroup scrollViewRootChild = (ViewGroup) clscrollView.getChildAt(0);
int childCount = scrollViewRootChild.getChildCount();
//Scroll view location on screen
int[] scrollViewLocation = {0, 0};
clscrollView.getLocationOnScreen(scrollViewLocation);
//Scroll view height
int scrollViewHeight = clscrollView.getHeight();
for (int i = 0; i < childCount; i++) {
View child = scrollViewRootChild.getChildAt(i);
if (child != null && child.getVisibility() == View.VISIBLE) {
int[] viewLocation = new int[2];
child.getLocationOnScreen(viewLocation);
int viewHeight = child.getHeight();
getViewVisibilityOnScrollStopped(scrollViewLocation, scrollViewHeight, viewLocation, viewHeight, String.valueOf(child.getTag()), (childCount - (i + 1)),b);
}
}
}
/**
* getViewVisibilityOnScrollStopped
* @param scrollViewLocation location of scroll view on screen
* @param scrollViewHeight height of scroll view
* @param viewLocation location of view on screen, you can use the method of view claas's getLocationOnScreen method.
* @param viewHeight height of view
* @param tag tag on view
* @param childPending number of views pending for iteration.
* @param b
*/
void getViewVisibilityOnScrollStopped(int[] scrollViewLocation, int scrollViewHeight, int[] viewLocation, int viewHeight, String tag, int childPending, boolean b) {
float visiblePercent = 0f;
int viewBottom = viewHeight + viewLocation[1]; //Get the bottom of view.
if(viewLocation[1] >= scrollViewLocation[1]) { //if view's top is inside the scroll view.
visiblePercent = 100;
int scrollBottom = scrollViewHeight + scrollViewLocation[1]; //Get the bottom of scroll view
if (viewBottom >= scrollBottom) { //If view's bottom is outside from scroll view
int visiblePart = scrollBottom - viewLocation[1]; //Find the visible part of view by subtracting view's top from scrollview's bottom
visiblePercent = (float) visiblePart / viewHeight * 100;
}
}else{ //if view's top is outside the scroll view.
if(viewBottom > scrollViewLocation[1]){ //if view's bottom is outside the scroll view
int visiblePart = viewBottom - scrollViewLocation[1]; //Find the visible part of view by subtracting scroll view's top from view's bottom
visiblePercent = (float) visiblePart / viewHeight * 100;
}
}
if(visiblePercent > 0f){
visibleWidgets.add(tag); //List of visible view.
}
if(childPending == 0){
//Do after iterating all children.
if (b)
addPreviewViews();
else
removePreviewView();
}
}
private void addPreviewViews() {
String s = visibleWidgets.get(visibleWidgets.size()-1);
FrameLayout childAt=null;
Integer value = Integer.valueOf(s);
for (int i=value;i<=1009;i++){
RelativeLayout parent = (RelativeLayout) clscrollView.getParent();
childAt= (FrameLayout) parent.getChildAt(1);
View viewById = childAt.findViewById(Integer.parseInt(s));
// if (viewById!=null&&viewById.getVisibility()!=View.VISIBLE)
// {
viewById.setVisibility(View.VISIBLE);
// }
}
if (childAt != null)
childAt.invalidate();
visibleWidgets.clear();
}
private void removePreviewView() {
FrameLayout childAt=null;
for (int i=0;i<visibleWidgets.size();i++)
{
String sTag = visibleWidgets.get(i);
RelativeLayout parent = (RelativeLayout) clscrollView.getParent();
childAt= (FrameLayout) parent.getChildAt(1);
View viewById = childAt.findViewById(Integer.parseInt(sTag));
viewById.setVisibility(View.GONE);
// childAt1= (LinearLayout) childAt.getChildAt(0);
// childAt1.removeView(viewById);
}
if (childAt != null)
childAt.invalidate();
visibleWidgets.clear();
}
/**
* Used to scroll to the given view.
*
* @param scrollViewParent Parent ScrollView
* @param view View to which we need to scroll.
*/
private void scrollToView(final ScrollView scrollViewParent, final View view) {
// Get deepChild Offset
Point childOffset = new Point();
getDeepChildOffset(scrollViewParent, view.getParent(), view, childOffset);
// Scroll to child.
scrollViewParent.smoothScrollTo(0, childOffset.y);
}
/**
* Used to get deep child offset.
* <p/>
* 1. We need to scroll to child in scrollview, but the child may not the direct child to scrollview.
* 2. So to get correct child position to scroll, we need to iterate through all of its parent views till the main parent.
*
* @param mainParent Main Top parent.
* @param parent Parent.
* @param child Child.
* @param accumulatedOffset Accumulated Offset.
*/
private void getDeepChildOffset(final ViewGroup mainParent, final ViewParent parent, final View child, final Point accumulatedOffset) {
ViewGroup parentGroup = (ViewGroup) parent;
accumulatedOffset.x += child.getLeft();
accumulatedOffset.y += child.getTop();
if (parentGroup.equals(mainParent)) {
return;
}
getDeepChildOffset(mainParent, parentGroup.getParent(), parentGroup, accumulatedOffset);
}
@Override
public void onClick(View v) {
int id = v.getId();
invisibleBeforeViews(id);
if (id>=1000){
int i = v.getId() ;
View viewById = findViewById(i + 300);
scrollToView(clscrollView,(LinearLayout)viewById.getParent());
}
handleScrollAction(false);
}
private void invisibleBeforeViews(int id) {
FrameLayout childAt = null;
for (int i = id; i >= 1000; i--) {
RelativeLayout parent = (RelativeLayout) clscrollView.getParent();
childAt = (FrameLayout) parent.getChildAt(1);
View viewById = childAt.findViewById(i);
viewById.setVisibility(View.GONE);
}
if (childAt != null)
childAt.invalidate();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment