I am using Two Relative layouts.one relative layout consists of edit boxes and spinner (Lime color layout).The other relative Layout consists of only web view. I wish to use swipe up/down approach to relative layout(Lime color). If the user swipe up the lime colored layout.other layout will be display in full screen. I don't know how to achieve. I need any reference or article to complete.
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:id="#+id/rL"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#C6FF00">
<LinearLayout
android:id="#+id/linear"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_marginTop="20dp"
android:background="#defec8">
<EditText
android:id="#+id/fromDate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="From Date" />
</LinearLayout>
<LinearLayout
android:id="#+id/linear1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="30dp"
android:layout_marginTop="20dp"
android:layout_toEndOf="#+id/linear"
android:layout_toRightOf="#+id/linear"
android:background="#defec8"
android:orientation="horizontal">
<EditText
android:id="#+id/todate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="To Date" />
</LinearLayout>
<LinearLayout
android:id="#+id/linear2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="30dp"
android:layout_marginTop="20dp"
android:layout_toEndOf="#+id/linear1"
android:layout_toRightOf="#+id/linear1"
android:background="#defec8"
android:orientation="horizontal">
<Spinner
android:id="#+id/timespinner"
android:layout_width="80dp"
android:layout_height="42dp" />
</LinearLayout>
<LinearLayout
android:id="#+id/linear4"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/linear"
android:layout_marginLeft="20dp"
android:layout_marginTop="20dp"
android:layout_marginRight="20dp"
android:background="#defec8"
android:orientation="horizontal">
<Spinner
android:id="#+id/name"
android:layout_width="match_parent"
android:layout_height="40dp" />
</LinearLayout>
<LinearLayout
android:id="#+id/linear5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/linear4"
android:layout_marginLeft="20dp"
android:layout_marginTop="20dp"
android:background="#defec8"
android:orientation="horizontal">
<Spinner
android:id="#+id/nametype"
android:layout_width="80dp"
android:layout_height="40dp">
</Spinner>
</LinearLayout>
<LinearLayout
android:id="#+id/linear3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/linear4"
android:layout_marginLeft="40dp"
android:layout_marginTop="20dp"
android:layout_toEndOf="#id/linear5"
android:layout_toRightOf="#id/linear5"
android:background="#defec8"
android:layout_marginBottom="10dp"
android:orientation="horizontal">
<Spinner
android:id="#+id/digitspinner"
android:layout_width="80dp"
android:layout_height="40dp" />
</LinearLayout>
</RelativeLayout>
<LinearLayout
android:id="#+id/linearweb"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#id/rL"
android:layout_marginBottom="10dp">
<WebView
android:layout_width="match_parent"
android:layout_height="match_parent">
</WebView>
</LinearLayout>
</RelativeLayout>
This works perfectly for me
My custom Gesture Detector Class. Copy and paste in appropriate package.
package com.cse.stackoverflow.gesture;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
public abstract class CustomGestureDetector extends android.view.GestureDetector.SimpleOnGestureListener {
private static final String TAG = CustomGestureDetector.class.getSimpleName();
private View view;
private boolean selectionStart;
public CustomGestureDetector(View view) {
this.view = view;
}
//FOR GESTURE
#Override
public boolean onFling(MotionEvent motionEventOne, MotionEvent motionEventTwo, float velocityX, float velocityY) {
if (motionEventOne == null || motionEventTwo == null) {
return false;
} else if (motionEventOne.getPointerCount() > 1 || motionEventTwo.getPointerCount() > 1) {
return false;
} else {
if (isSelectionStart()) {
Log.d(TAG, "ME 1 : X - " + motionEventOne.getX());
Log.d(TAG, "ME 1 : Y - " + motionEventOne.getY());
Log.d(TAG, "ME 2 : X - " + motionEventTwo.getX());
Log.d(TAG, "ME 2 : Y - " + motionEventTwo.getY());
Log.d(TAG, "Velocity Of X - " + velocityX);
Log.d(TAG, "Velocity Of Y - " + velocityY);
} else {
try {
///////////////////////////////////////////////////////////////////////////////
Log.d(TAG, "ME 1 : X - " + motionEventOne.getX());
Log.d(TAG, "ME 1 : Y - " + motionEventOne.getY());
Log.d(TAG, "ME 2 : X - " + motionEventTwo.getX());
Log.d(TAG, "ME 2 : Y - " + motionEventTwo.getY());
Log.d(TAG, "Velocity Of X - " + velocityX);
Log.d(TAG, "Velocity Of Y - " + velocityY);
float mRightToLeftCover = motionEventOne.getX() - motionEventTwo.getX();
float mTopToBottomCover = motionEventTwo.getY() - motionEventOne.getY();
float mVelocityX = velocityX;
float mVelocityY = velocityY;
Log.i(TAG, "mRightToLeftCover : " + mRightToLeftCover);
Log.i(TAG, "mTopToBottomCover : " + mTopToBottomCover);
Log.i(TAG, "mVelocityX : " + mVelocityX);
Log.i(TAG, "mVelocityY : " + mVelocityY);
if (mRightToLeftCover >= 0) {
if (mTopToBottomCover >= 0) {
if (mTopToBottomCover < 100) {
if (mRightToLeftCover > 100) {
Log.d(TAG, "1. R =>> L");
onRightToLeftSwap();
}
} else {
if (mRightToLeftCover < 100) {
Log.d(TAG, "9. T ==>> B");
onTopToBottomSwap();
} else {
Log.d(TAG, "2. T ==>> B, R =>> L");
}
}
} else {
if (mTopToBottomCover > -100) {
if (mRightToLeftCover > 100) {
Log.d(TAG, "3. R =>> L");
onRightToLeftSwap();
}
} else {
if (mRightToLeftCover < 100) {
Log.d(TAG, "10. B ==>> T");
onBottomToTopSwap();
} else {
Log.d(TAG, "4. B ==>> T, R =>> L");
}
}
}
} else if (mRightToLeftCover < 0) {
if (mTopToBottomCover >= 0) {
if (mTopToBottomCover < 100) {
if (mRightToLeftCover > -100) {
Log.d(TAG, "5. L =>> R");
onLeftToRightSwap();
}
} else {
if (mRightToLeftCover > -100) {
Log.d(TAG, "11. T ==>> B");
onTopToBottomSwap();
} else {
Log.d(TAG, "6. T ==>> B, L =>> R");
}
}
} else {
if (mTopToBottomCover > -100) {
if (mRightToLeftCover < -100) {
Log.d(TAG, "7. L =>> R");
onLeftToRightSwap();
}
} else {
if (mRightToLeftCover < -100) {
Log.d(TAG, "12. B ==>> T");
onBottomToTopSwap();
} else {
Log.d(TAG, "8. B ==>> T, L =>> R");
}
}
}
}
return true;
} catch (Exception e) {
e.printStackTrace();
}
}
//////////////////////////////////////////////////////////////////////////////
return false;
}
}
//EXPERIMENTAL PURPOSE
public abstract void onLeftToRightSwap();
public abstract void onRightToLeftSwap();
public abstract void onTopToBottomSwap();
public abstract void onBottomToTopSwap();
public abstract void onLeftToRightTopToBottomDiagonalSwap();
public abstract void onLeftToRightBottomToTopDiagonalSwap();
public abstract void onRightToLeftTopToBottomDiagonalSwap();
public abstract void onRightToLeftBottomToTopDiagonalSwap();
//SINGLE AND DOUBLE TABS
#Override
public boolean onSingleTapConfirmed(MotionEvent e) {
Log.d(TAG, "On Single Tap");
Log.d(TAG, "Selection Start : " + selectionStart);
Log.d(TAG, "ME 1 : X - " + e.getX());
Log.d(TAG, "ME 1 : Y - " + e.getY());
onSingleTap();
return super.onSingleTapConfirmed(e);
}
#Override
public boolean onDoubleTap(MotionEvent e) {
Log.d(TAG, "On Double Tap");
onDoubleTap();
return super.onDoubleTap(e);
}
public abstract void onSingleTap();
public abstract void onDoubleTap();
public boolean isSelectionStart() {
return selectionStart;
}
public void setSelectionStart(boolean selectionStart) {
this.selectionStart = selectionStart;
}
#Override
public void onLongPress(MotionEvent e) {
onLongPressPerformed(e);
super.onLongPress(e);
}
public abstract void onLongPressPerformed(MotionEvent e);
}
activity_main.xml just small modification i.e. I set id "webView" to your webView. (Copy and paste this xml code into your xml file).
<RelativeLayout android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:android="http://schemas.android.com/apk/res/android">
<RelativeLayout
android:id="#+id/rL"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#C6FF00">
<LinearLayout
android:id="#+id/linear"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_marginTop="20dp"
android:background="#defec8">
<EditText
android:id="#+id/fromDate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="From Date" />
</LinearLayout>
<LinearLayout
android:id="#+id/linear1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="30dp"
android:layout_marginTop="20dp"
android:layout_toEndOf="#+id/linear"
android:layout_toRightOf="#+id/linear"
android:background="#defec8"
android:orientation="horizontal">
<EditText
android:id="#+id/todate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="To Date" />
</LinearLayout>
<LinearLayout
android:id="#+id/linear2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="30dp"
android:layout_marginTop="20dp"
android:layout_toEndOf="#+id/linear1"
android:layout_toRightOf="#+id/linear1"
android:background="#defec8"
android:orientation="horizontal">
<Spinner
android:id="#+id/timespinner"
android:layout_width="80dp"
android:layout_height="42dp" />
</LinearLayout>
<LinearLayout
android:id="#+id/linear4"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/linear"
android:layout_marginLeft="20dp"
android:layout_marginTop="20dp"
android:layout_marginRight="20dp"
android:background="#defec8"
android:orientation="horizontal">
<Spinner
android:id="#+id/name"
android:layout_width="match_parent"
android:layout_height="40dp" />
</LinearLayout>
<LinearLayout
android:id="#+id/linear5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/linear4"
android:layout_marginLeft="20dp"
android:layout_marginTop="20dp"
android:background="#defec8"
android:orientation="horizontal">
<Spinner
android:id="#+id/nametype"
android:layout_width="80dp"
android:layout_height="40dp">
</Spinner>
</LinearLayout>
<LinearLayout
android:id="#+id/linear3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/linear4"
android:layout_marginLeft="40dp"
android:layout_marginTop="20dp"
android:layout_toEndOf="#id/linear5"
android:layout_toRightOf="#id/linear5"
android:background="#defec8"
android:layout_marginBottom="10dp"
android:orientation="horizontal">
<Spinner
android:id="#+id/digitspinner"
android:layout_width="80dp"
android:layout_height="40dp" />
</LinearLayout>
</RelativeLayout>
<LinearLayout
android:id="#+id/linearweb"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#id/rL"
android:layout_alignParentBottom="true">
<WebView
android:id="#+id/webView"
android:layout_width="match_parent"
android:layout_height="match_parent">
</WebView>
</LinearLayout>
</RelativeLayout>
MainActivity code (Copy and paste all methods and call initialiseView() method in onCreate())
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initialiseView();
}
RelativeLayout upperLayout;
LinearLayout lowerLayout;
WebView mWebView;
private void initialiseView() {
upperLayout = (RelativeLayout) findViewById(R.id.rL);
lowerLayout = (LinearLayout) findViewById(R.id.linearweb);
mWebView = (WebView) findViewById(R.id.webView);
CustomGestureDetector mCustomGestureDetectorForUpperLayout = new CustomGestureDetector(upperLayout) {
#Override
public void onLeftToRightSwap() {
}
#Override
public void onRightToLeftSwap() {
}
#Override
public void onTopToBottomSwap() {
//Toast.makeText(MainActivity.this, "onTopToBottomSwap", Toast.LENGTH_SHORT).show();
showUpperLayout();
}
#Override
public void onBottomToTopSwap() {
//Toast.makeText(MainActivity.this, "onBottomToTopSwap", Toast.LENGTH_SHORT).show();
hideUpperLayout();
}
#Override
public void onLeftToRightTopToBottomDiagonalSwap() {
}
#Override
public void onLeftToRightBottomToTopDiagonalSwap() {
}
#Override
public void onRightToLeftTopToBottomDiagonalSwap() {
}
#Override
public void onRightToLeftBottomToTopDiagonalSwap() {
}
#Override
public void onSingleTap() {
}
#Override
public void onDoubleTap() {
}
#Override
public void onLongPressPerformed(MotionEvent e) {
}
};
final GestureDetector mGestureDetectorUpperLayout = new GestureDetector(this, mCustomGestureDetectorForUpperLayout);
CustomGestureDetector mCustomGestureDetectorForLowerLayout = new CustomGestureDetector(lowerLayout) {
#Override
public void onLeftToRightSwap() {
}
#Override
public void onRightToLeftSwap() {
}
#Override
public void onTopToBottomSwap() {
showUpperLayout();
}
#Override
public void onBottomToTopSwap() {
}
#Override
public void onLeftToRightTopToBottomDiagonalSwap() {
}
#Override
public void onLeftToRightBottomToTopDiagonalSwap() {
}
#Override
public void onRightToLeftTopToBottomDiagonalSwap() {
}
#Override
public void onRightToLeftBottomToTopDiagonalSwap() {
}
#Override
public void onSingleTap() {
}
#Override
public void onDoubleTap() {
}
#Override
public void onLongPressPerformed(MotionEvent e) {
}
};
final GestureDetector mGestureDetectorLowerLayout = new GestureDetector(this, mCustomGestureDetectorForLowerLayout);
upperLayout.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View view, MotionEvent motionEvent) {
mGestureDetectorUpperLayout.onTouchEvent(motionEvent);
return true;
}
});
lowerLayout.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View view, MotionEvent motionEvent) {
mGestureDetectorLowerLayout.onTouchEvent(motionEvent);
return true;
}
});
mWebView.loadUrl("https://www.google.co.in/");
CustomGestureDetector mCustomGestureDetectorForWebView = new CustomGestureDetector(mWebView) {
#Override
public void onLeftToRightSwap() {
}
#Override
public void onRightToLeftSwap() {
}
#Override
public void onTopToBottomSwap() {
showUpperLayout();
}
#Override
public void onBottomToTopSwap() {
hideUpperLayout();
}
#Override
public void onLeftToRightTopToBottomDiagonalSwap() {
}
#Override
public void onLeftToRightBottomToTopDiagonalSwap() {
}
#Override
public void onRightToLeftTopToBottomDiagonalSwap() {
}
#Override
public void onRightToLeftBottomToTopDiagonalSwap() {
}
#Override
public void onSingleTap() {
}
#Override
public void onDoubleTap() {
}
#Override
public void onLongPressPerformed(MotionEvent e) {
}
};
final GestureDetector mGestureDetectorForWebView = new GestureDetector(this, mCustomGestureDetectorForWebView);
mWebView.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View view, MotionEvent motionEvent) {
mGestureDetectorForWebView.onTouchEvent(motionEvent);
return true;
}
});
}
public void hideUpperLayout() {
upperLayout.setVisibility(View.GONE);
}
public void showUpperLayout() {
upperLayout.setVisibility(View.VISIBLE);
}
public void toggleUpperLayout() {
if (upperLayout.getVisibility() == View.VISIBLE) {
hideUpperLayout();
} else {
showUpperLayout();
}
}
}
This is optional(to see google home page on your webview).
//Add internet permission in AndroidMenifest.xml
<uses-permission android:name="android.permission.INTERNET" />
Update As Per Comment For Smooth Scroll
To Achieve Smooth Scrolling you need need to use AppBarLayout inside parent layout(Parent layout may be anything for easy use Coordinator layout).
First in your style.xml create themes entry like below or just copy and paste it.
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">#color/colorPrimary</item>
<item name="colorPrimaryDark">#color/colorPrimaryDark</item>
<item name="colorAccent">#color/colorAccent</item>
</style>
<style name="AppTheme.NoActionBar">
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
</style>
<style name="AppTheme.AppBarOverlay" parent="ThemeOverlay.AppCompat.Dark.ActionBar" />
<style name="AppTheme.PopupOverlay" parent="ThemeOverlay.AppCompat.Light" />
</resources>
For colours need to create colors.xml (If you created app its have default entries)
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="colorPrimary">#3F51B5</color>
<color name="colorPrimaryDark">#303F9F</color>
<color name="colorAccent">#FF4081</color>
</resources>
Now your activity_main.xml is like below.
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
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:fitsSystemWindows="true"
tools:context="com.cse.scrolltoolbar.ScrollingActivity">
<android.support.design.widget.AppBarLayout
android:id="#+id/app_bar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fitsSystemWindows="true"
android:theme="#style/AppTheme.AppBarOverlay">
<android.support.design.widget.CollapsingToolbarLayout
android:id="#+id/toolbar_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
app:contentScrim="?attr/colorPrimary"
app:layout_scrollFlags="scroll|exitUntilCollapsed">
<RelativeLayout
android:id="#+id/rL"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#C6FF00"
android:paddingTop="100dp"
app:layout_collapseMode="pin"
app:popupTheme="#style/AppTheme.PopupOverlay">
<LinearLayout
android:id="#+id/linear"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_marginTop="20dp"
android:background="#defec8">
<EditText
android:id="#+id/fromDate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="From Date" />
</LinearLayout>
<LinearLayout
android:id="#+id/linear1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="30dp"
android:layout_marginTop="20dp"
android:layout_toEndOf="#+id/linear"
android:layout_toRightOf="#+id/linear"
android:background="#defec8"
android:orientation="horizontal">
<EditText
android:id="#+id/todate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="To Date" />
</LinearLayout>
<LinearLayout
android:id="#+id/linear2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="30dp"
android:layout_marginTop="20dp"
android:layout_toEndOf="#+id/linear1"
android:layout_toRightOf="#+id/linear1"
android:background="#defec8"
android:orientation="horizontal">
<Spinner
android:id="#+id/timespinner"
android:layout_width="80dp"
android:layout_height="42dp" />
</LinearLayout>
<LinearLayout
android:id="#+id/linear4"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/linear"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:layout_marginTop="20dp"
android:background="#defec8"
android:orientation="horizontal">
<Spinner
android:id="#+id/name"
android:layout_width="match_parent"
android:layout_height="40dp" />
</LinearLayout>
<LinearLayout
android:id="#+id/linear5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/linear4"
android:layout_marginLeft="20dp"
android:layout_marginTop="20dp"
android:background="#defec8"
android:orientation="horizontal">
<Spinner
android:id="#+id/nametype"
android:layout_width="80dp"
android:layout_height="40dp">
</Spinner>
</LinearLayout>
<LinearLayout
android:id="#+id/linear3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/linear4"
android:layout_marginBottom="10dp"
android:layout_marginLeft="40dp"
android:layout_marginTop="20dp"
android:layout_toEndOf="#id/linear5"
android:layout_toRightOf="#id/linear5"
android:background="#defec8"
android:orientation="horizontal">
<Spinner
android:id="#+id/digitspinner"
android:layout_width="80dp"
android:layout_height="40dp" />
</LinearLayout>
</RelativeLayout>
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="#color/colorPrimary"
app:layout_collapseMode="pin"
app:popupTheme="#style/AppTheme.PopupOverlay" />
</android.support.design.widget.CollapsingToolbarLayout>
</android.support.design.widget.AppBarLayout>
<!--<include layout="#layout/content_scrolling" />-->
<WebView
android:id="#+id/webView"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="#string/appbar_scrolling_view_behavior">
</WebView>
</android.support.design.widget.CoordinatorLayout>
Add internet permission in Manifest file and add below method to load google home page to your web view and call this method from onCreate after setContentView.
private void initialiseView() {
WebView mWebView = (WebView) findViewById(R.id.webView);
mWebView.loadUrl("https://www.google.co.in/");
}
Related
I have a frame layout call view_snap_tabs.xml that have image and view like below
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="184dp"
xmlns:tools="http://schemas.android.com/tools"
tools:layout_gravity="bottom"
tools:background="#color/light_purple">
<ImageView
android:id="#+id/vst_bottom_image"
android:layout_width="32dp"
android:layout_height="32dp"
android:layout_marginBottom="48dp"
android:layout_gravity="center|bottom"
android:src="#drawable/small_circle"/>
<View
android:id="#+id/vst_indicator"
android:layout_width="48dp"
android:layout_height="4dp"
android:layout_gravity="bottom|center"
android:layout_marginBottom="44dp"
android:background="#drawable/indicator_background"/>
</FrameLayout>
I have included this layout in my activity_main.xml like below
<include
layout="#layout/view_snap_tabs"
android:id="#+id/am_snap_tabs"
android:layout_alignParentBottom="true"
android:layout_height="wrap_content"
android:layout_width="match_parent"/>
I have created a method call SetUpViewPager and I'm trying to add that method in main activity like below
SnapTabView snapTabView = findViewById(R.id.am_snap_tabs);
snapTabView.setUpWithViewPager(viewPager);
But when I run the application It will crash from there and give a ClassCastExeception how can I fix this?
EDITED====================================================================
public class SnapTabView extends FrameLayout implements ViewPager.OnPageChangeListener {
private ImageView mCenterImage;
private ImageView mStartImage;
private ImageView mBottomImage;
private ImageView mEndImage;
private View mIndicator;
private ArgbEvaluator mArgbEvaluator;
private int mCenterColor;
private int mSideColor;
private int mEndViewsTranslationX;
private int mIndicatorTranslationX;
private int mCenterTransationY;
public SnapTabView(#NonNull Context context) {
this(context, null);
}
public SnapTabView(#NonNull Context context, #Nullable AttributeSet attrs) {
this(context, attrs, 0);
}
public SnapTabView(#NonNull Context context, #Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init();
}
private void init() {
LayoutInflater.from(getContext()).inflate(R.layout.view_snap_tabs, this, false);
mCenterImage = (ImageView) findViewById(R.id.vst_center_image);
mBottomImage = (ImageView) findViewById(R.id.vst_bottom_image);
mEndImage = (ImageView) findViewById(R.id.vst_end_image);
mStartImage = (ImageView) findViewById(R.id.vst_start_image);
mIndicator = (View) findViewById(R.id.vst_indicator);
mCenterColor = ContextCompat.getColor(getContext(), R.color.white);
mSideColor = ContextCompat.getColor(getContext(), R.color.dark_grey);
mArgbEvaluator = new ArgbEvaluator();
mIndicatorTranslationX = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 80, getResources().getDisplayMetrics());
mBottomImage.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
#Override
public void onGlobalLayout() {
mEndViewsTranslationX = (int) ((mBottomImage.getX() - mStartImage.getX()) - mIndicatorTranslationX);
mBottomImage.getViewTreeObserver().removeOnGlobalLayoutListener(this);
mCenterTransationY = getHeight() - mBottomImage.getBottom();
}
});
}
#Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
if(position == 0) {
setColor(1 - positionOffset);
moveViews(1 - positionOffset);
mIndicator.setTranslationX((positionOffset - 1) * mIndicatorTranslationX);
moveAndScaleCenter(1 - positionOffset);
} else if(position == 1) {
setColor(positionOffset);
moveViews(positionOffset);
mIndicator.setTranslationX(positionOffset * mIndicatorTranslationX);
moveAndScaleCenter(positionOffset);
}
}
public void setUpWithViewPager(final ViewPager viewPager) {
viewPager.addOnPageChangeListener(this);
mStartImage.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View view) {
if(viewPager.getCurrentItem() != 0)
viewPager.setCurrentItem(0);
}
});
mEndImage.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View view) {
if(viewPager.getCurrentItem() != 2)
viewPager.setCurrentItem(2);
}
});
}
#Override
public void onPageSelected(int position) {
}
#Override
public void onPageScrollStateChanged(int state) {
}
private void setColor(float fractionFromCenter) {
int color = (int) mArgbEvaluator.evaluate(fractionFromCenter, mCenterColor, mSideColor);
mCenterImage.setColorFilter(color);
mStartImage.setColorFilter(color);
mEndImage.setColorFilter(color);
}
private void moveViews(float fractionFromCenter) {
mStartImage.setTranslationX(fractionFromCenter * mEndViewsTranslationX);
mEndImage.setTranslationX(-fractionFromCenter * mEndViewsTranslationX);
mIndicator.setAlpha(fractionFromCenter);
mIndicator.setScaleX(fractionFromCenter);
}
private void moveAndScaleCenter(float fractionFromCenter) {
float scale = .7f + ((1 - fractionFromCenter) * .3f);
mCenterImage.setScaleX(scale);
mCenterImage.setScaleY(scale);
int translation = (int) (fractionFromCenter * mCenterTransationY);
mCenterImage.setTranslationY(translation);
mBottomImage.setTranslationY(translation);
mBottomImage.setAlpha(1 - fractionFromCenter);
}
}
view_snap_tabs.xml file like below
<?xml version="1.0" encoding="utf-8"?>
<com.example.crowderia.chat.view.SnapTabView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="184dp"
tools:layout_gravity="bottom"
tools:background="#color/light_purple"
android:id="#+id/snaps">
<ImageView
android:id="#+id/vst_center_image"
android:layout_width="88dp"
android:layout_height="88dp"
android:layout_gravity="center|bottom"
android:src="#drawable/large_circle"
android:layout_marginBottom="96dp"/>
<ImageView
android:id="#+id/vst_start_image"
android:layout_width="32dp"
android:layout_height="32dp"
android:layout_marginBottom="56dp"
android:layout_marginStart="24dp"
android:layout_gravity="start|bottom"
android:src="#drawable/ic_chat_bubble_24dp"/>
<ImageView
android:id="#+id/vst_end_image"
android:layout_width="32dp"
android:layout_height="32dp"
android:layout_marginBottom="56dp"
android:layout_marginEnd="24dp"
android:layout_gravity="end|bottom"
android:src="#drawable/ic_group_work_24dp"/>
<ImageView
android:id="#+id/vst_bottom_image"
android:layout_width="32dp"
android:layout_height="32dp"
android:layout_marginBottom="48dp"
android:layout_gravity="center|bottom"
android:src="#drawable/small_circle"/>
<View
android:id="#+id/vst_indicator"
android:layout_width="48dp"
android:layout_height="4dp"
android:layout_gravity="bottom|center"
android:layout_marginBottom="44dp"
android:background="#drawable/indicator_background"/>
</com.example.crowderia.chat.view.SnapTabView>
do this:
<?xml version="1.0" encoding="utf-8"?>
<com.example.crowderia.chat.view.SnapTabView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="184dp"
android:id="#+id/snaps"
xmlns:tools="http://schemas.android.com/tools">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="184dp"
tools:layout_gravity="bottom"
tools:background="#color/light_purple">
<ImageView
android:id="#+id/vst_bottom_image"
android:layout_width="32dp"
android:layout_height="32dp"
android:layout_marginBottom="48dp"
android:layout_gravity="center|bottom"
android:src="#drawable/small_circle"/>
<View
android:id="#+id/vst_indicator"
android:layout_width="48dp"
android:layout_height="4dp"
android:layout_gravity="bottom|center"
android:layout_marginBottom="44dp"
android:background="#drawable/indicator_background"/>
</FrameLayout>
</com.example.crowderia.chat.view.SnapTabView>
in the activity do this:
View view=findViewById(R.id.am_snap_tabs);
SnapTabView snap=(SnapTabView)view.findViewById(R.id.snaps);
snap.setUpWithViewPager(viewPager);
//am_snap_tabs it is the id of the include in the question
It's because you are trying to assign a view with FrameLayout to SnapTabView, modify your xml as below
view_snap_tabs.xml
<?xml version="1.0" encoding="utf-8"?>
<com.example.crowderia.chat.view.SnapTabView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="184dp"
xmlns:tools="http://schemas.android.com/tools">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="184dp"
tools:layout_gravity="bottom"
tools:background="#color/light_purple">
<ImageView
android:id="#+id/vst_bottom_image"
android:layout_width="32dp"
android:layout_height="32dp"
android:layout_marginBottom="48dp"
android:layout_gravity="center|bottom"
android:src="#drawable/small_circle"/>
<View
android:id="#+id/vst_indicator"
android:layout_width="48dp"
android:layout_height="4dp"
android:layout_gravity="bottom|center"
android:layout_marginBottom="44dp"
android:background="#drawable/indicator_background"/>
</FrameLayout>
</com.example.crowderia.chat.view.SnapTabView>
This question already has answers here:
How to group RadioButton from different LinearLayouts?
(21 answers)
Closed 5 years ago.
I have a group of radiobuttons in a radiogroup that refuses to behave the way I'd like. The radiobuttons need to be two lines of two, and one line of one, with the last button having an edittext instead of a label. The layout looks correct, but the radiobuttons allow more than one to be selected and won't deselect any.
Below is my XML:
<RadioGroup
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/rgDesignation">
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/llVT1">
<RadioButton
android:text="#string/pressure_equipment"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/rbPE"
android:layout_weight="1" />
<RadioButton
android:text="#string/assemblies"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/rbAss"
android:layout_weight="1" />
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/llVT2">
<RadioButton
android:text="#string/unheated"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/rbUnh"
android:layout_weight="1" />
<RadioButton
android:text="#string/heated"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/rbH"
android:layout_weight="1" />
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:id="#+id/llVT3">
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/rbOwnSel"
android:layout_weight="0" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="text"
android:ems="10"
android:id="#+id/etOwnChoice"
android:layout_weight="1"
android:textColor="#android:color/black"
android:background="#android:color/white"
android:elevation="5dp" />
</LinearLayout>
</RadioGroup>
Make custom RadioGroup class like this:
public class RadioGroup extends LinearLayout {
private int mCheckedId = -1;
private CompoundButton.OnCheckedChangeListener mChildOnCheckedChangeListener;
private boolean mProtectFromCheckedChange = false;
private OnCheckedChangeListener mOnCheckedChangeListener;
private PassThroughHierarchyChangeListener mPassThroughListener;
public RadioGroup(Context context) {
super(context);
setOrientation(VERTICAL);
init();
}
public RadioGroup(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
private void init() {
mChildOnCheckedChangeListener = new CheckedStateTracker();
mPassThroughListener = new PassThroughHierarchyChangeListener();
super.setOnHierarchyChangeListener(mPassThroughListener);
}
#Override
public void setOnHierarchyChangeListener(OnHierarchyChangeListener listener) {
mPassThroughListener.mOnHierarchyChangeListener = listener;
}
#Override
protected void onFinishInflate() {
super.onFinishInflate();
if (mCheckedId != -1) {
mProtectFromCheckedChange = true;
setCheckedStateForView(mCheckedId, true);
mProtectFromCheckedChange = false;
setCheckedId(mCheckedId);
}
}
#Override
public void addView(View child, int index, ViewGroup.LayoutParams params) {
if (child instanceof RadioButton) {
final RadioButton button = (RadioButton) child;
if (button.isChecked()) {
mProtectFromCheckedChange = true;
if (mCheckedId != -1) {
setCheckedStateForView(mCheckedId, false);
}
mProtectFromCheckedChange = false;
setCheckedId(button.getId());
}
}
super.addView(child, index, params);
}
public void check(#IdRes int id) {
// don't even bother
if (id != -1 && (id == mCheckedId)) {
return;
}
if (mCheckedId != -1) {
setCheckedStateForView(mCheckedId, false);
}
if (id != -1) {
setCheckedStateForView(id, true);
}
setCheckedId(id);
}
private void setCheckedId(#IdRes int id) {
mCheckedId = id;
if (mOnCheckedChangeListener != null) {
mOnCheckedChangeListener.onCheckedChanged(this, mCheckedId);
}
}
private void setCheckedStateForView(int viewId, boolean checked) {
View checkedView = findViewById(viewId);
if (checkedView != null && checkedView instanceof RadioButton) {
((RadioButton) checkedView).setChecked(checked);
}
}
#IdRes
public int getCheckedRadioButtonId() {
return mCheckedId;
}
public void clearCheck() {
check(-1);
}
public void setOnCheckedChangeListener(OnCheckedChangeListener listener) {
mOnCheckedChangeListener = listener;
}
#Override
public LayoutParams generateLayoutParams(AttributeSet attrs) {
return new LayoutParams(getContext(), attrs);
}
#Override
protected boolean checkLayoutParams(ViewGroup.LayoutParams p) {
return p instanceof RadioGroup.LayoutParams;
}
#Override
protected LinearLayout.LayoutParams generateDefaultLayoutParams() {
return new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
}
#Override
public CharSequence getAccessibilityClassName() {
return RadioGroup.class.getName();
}
public static class LayoutParams extends LinearLayout.LayoutParams {
public LayoutParams(Context c, AttributeSet attrs) {
super(c, attrs);
}
public LayoutParams(int w, int h) {
super(w, h);
}
public LayoutParams(int w, int h, float initWeight) {
super(w, h, initWeight);
}
public LayoutParams(ViewGroup.LayoutParams p) {
super(p);
}
public LayoutParams(MarginLayoutParams source) {
super(source);
}
#Override
protected void setBaseAttributes(TypedArray a,
int widthAttr, int heightAttr) {
if (a.hasValue(widthAttr)) {
width = a.getLayoutDimension(widthAttr, "layout_width");
} else {
width = WRAP_CONTENT;
}
if (a.hasValue(heightAttr)) {
height = a.getLayoutDimension(heightAttr, "layout_height");
} else {
height = WRAP_CONTENT;
}
}
}
public void onCheckedChanged(RadioGroup group, #IdRes int checkedId);
}
private class CheckedStateTracker implements CompoundButton.OnCheckedChangeListener {
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
// prevents from infinite recursion
if (mProtectFromCheckedChange) {
return;
}
mProtectFromCheckedChange = true;
if (mCheckedId != -1) {
setCheckedStateForView(mCheckedId, false);
}
mProtectFromCheckedChange = false;
int id = buttonView.getId();
setCheckedId(id);
}
}
private class PassThroughHierarchyChangeListener implements
ViewGroup.OnHierarchyChangeListener {
private ViewGroup.OnHierarchyChangeListener mOnHierarchyChangeListener;
#RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN_MR1)
public void traverseTree(View view) {
if (view instanceof RadioButton) {
int id = view.getId();
// generates an id if it's missing
if (id == View.NO_ID) {
id = View.generateViewId();
view.setId(id);
}
((RadioButton) view).setOnCheckedChangeListener(
mChildOnCheckedChangeListener);
}
if (!(view instanceof ViewGroup)) {
return;
}
ViewGroup viewGroup = (ViewGroup) view;
if (viewGroup.getChildCount() == 0) {
return;
}
for (int i = 0; i < viewGroup.getChildCount(); i++) {
traverseTree(viewGroup.getChildAt(i));
}
}
/**
* {#inheritDoc}
*/
#RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN_MR1)
public void onChildViewAdded(View parent, View child) {
traverseTree(child);
if (parent == RadioGroup.this && child instanceof RadioButton) {
int id = child.getId();
// generates an id if it's missing
if (id == View.NO_ID) {
id = View.generateViewId();
child.setId(id);
}
((RadioButton) child).setOnCheckedChangeListener(
mChildOnCheckedChangeListener);
}
if (mOnHierarchyChangeListener != null) {
mOnHierarchyChangeListener.onChildViewAdded(parent, child);
}
}
/**
* {#inheritDoc}
*/
public void onChildViewRemoved(View parent, View child) {
if (parent == RadioGroup.this && child instanceof RadioButton) {
((RadioButton) child).setOnCheckedChangeListener(null);
}
if (mOnHierarchyChangeListener != null) {
mOnHierarchyChangeListener.onChildViewRemoved(parent, child);
}
}
}
}
and add in your layout like this:
<LinearLayout 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:orientation="vertical"
android:padding="#dimen/activity_vertical_margin"
>
<com.example.admin.myapplication.RadioGroup
android:id="#+id/radio_group_plus"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="60dp">
<RadioButton
android:id="#+id/rb_latte"
android:text="radio1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true" />
<RadioButton
android:text="radio2"
android:id="#+id/rb_latte1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true" />
</RelativeLayout>
<View
android:layout_width="match_parent"
android:layout_height="2dp"
android:background="#E0E0E0" />
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="60dp">
<ImageView
android:id="#+id/iv_mocha"
android:layout_width="40dp"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:src="#mipmap/ic_launcher" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_toRightOf="#+id/iv_mocha"
android:paddingLeft="20dp"
android:text="Mocha" />
<RadioButton
android:id="#+id/rb_mocha"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true" />
</RelativeLayout>
<View
android:layout_width="match_parent"
android:layout_height="2dp"
android:background="#E0E0E0" />
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="60dp">
<ImageView
android:id="#+id/iv_americano"
android:layout_width="40dp"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:src="#mipmap/ic_launcher" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_toRightOf="#+id/iv_americano"
android:paddingLeft="20dp"
android:text="Americano" />
<RadioButton
android:id="#+id/rb_americano"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true" />
</RelativeLayout>
<View
android:layout_width="match_parent"
android:layout_height="2dp"
android:background="#E0E0E0" />
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="60dp">
<ImageView
android:id="#+id/iv_espresso"
android:layout_width="40dp"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:src="#mipmap/ic_launcher" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_toRightOf="#+id/iv_espresso"
android:paddingLeft="20dp"
android:text="Espresso" />
<RadioButton
android:id="#+id/rb_espresso"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true" />
</RelativeLayout>
<View
android:layout_width="match_parent"
android:layout_height="2dp"
android:background="#E0E0E0" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="60dp"
android:orientation="horizontal">
<RelativeLayout
android:layout_width="0dp"
android:layout_height="60dp"
android:layout_weight="1">
<ImageView
android:id="#+id/iv_orange"
android:layout_width="40dp"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:src="#mipmap/ic_launcher" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_toRightOf="#+id/iv_orange"
android:paddingLeft="20dp"
android:text="Orange" />
<RadioButton
android:id="#+id/rb_orange"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true" />
</RelativeLayout>
<View
android:layout_width="2dp"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp"
android:layout_marginTop="10dp"
android:background="#E0E0E0" />
<RelativeLayout
android:layout_width="0dp"
android:layout_height="60dp"
android:layout_weight="1"
android:paddingLeft="10dp">
<ImageView
android:id="#+id/iv_butter"
android:layout_width="40dp"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:src="#mipmap/ic_launcher" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_toRightOf="#+id/iv_butter"
android:paddingLeft="20dp"
android:text="Butter" />
<RadioButton
android:id="#+id/rb_butter"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true" />
</RelativeLayout>
</LinearLayout>
</com.example.admin.myapplication.RadioGroup>
<Button
android:layout_width="wrap_content"
android:layout_height="48dp"
android:layout_gravity="center"
android:layout_marginTop="20dp"
android:background="#138BE8"
android:gravity="center"
android:onClick="onOrderClicked"
android:paddingLeft="20dp"
android:paddingRight="20dp"
android:text="Order Drink"
android:textColor="#android:color/white" />
Sorry for my english. I spend many times but i cant fix my problem. I have listView and i want set setOnItemClickListener him. This is my listView:
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id = "#+id/listScene"
android:clickable="true"
android:cacheColorHint="#00000000"
android:divider="#adb8c2"
android:dividerHeight="1dp"
android:scrollingCache="false"
android:smoothScrollbar="true"
>
</ListView>
Its my code
public void outputListView(ArrayList<String> list) {
Adapter adapter = new Adapter(getApplicationContext(), R.layout.item, list);
adapter.notifyDataSetChanged();
listView.setAdapter(adapter);
listView.setItemsCanFocus(false);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
Toast.makeText(getApplicationContext(), String.valueOf(position), Toast.LENGTH_SHORT).show();
}
});
}
but when i click in item its nothing happens
UPD
My item.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
android:focusable="false"
android:focusableInTouchMode="false"
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<service.SwipeLayout
android:layout_marginTop="5dp"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:orientation="horizontal"
android:background="#32777777"
android:layout_width="match_parent"
android:layout_height="70dp">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#fff"
android:layout_weight="0.7"
>
<TextView
android:id="#+id/conditions"
android:layout_marginTop="10dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="22dp"
android:text=""
android:textColor="#25ac36"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true" />
</RelativeLayout>
<RelativeLayout
android:layout_weight="0.3"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="#+id/description"
android:layout_marginLeft="10dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="18dp"
android:text=""
android:textColor="#222222"
android:layout_centerVertical="true"
/>
</RelativeLayout>
</LinearLayout>
<RelativeLayout
android:background="#66777777"
android:layout_width="match_parent"
android:layout_height="40dp">
<TextView
android:id="#+id/rooms"
android:layout_marginLeft="10dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="18dp"
android:text=""
android:textColor="#fff"
android:layout_centerVertical="true"
/>
</RelativeLayout>
</LinearLayout>
<LinearLayout
android:layout_width="250dp"
android:layout_height="match_parent">
<RelativeLayout
android:id="#+id/open_b"
android:clickable="true"
android:layout_width="78dp"
android:layout_marginRight="5dp"
android:layout_marginLeft="5dp"
android:layout_height="match_parent"
android:background="#4fcc54">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="18dp"
android:text="Open"
android:textColor="#fff"
android:id="#+id/openText"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
/>
</RelativeLayout>
<RelativeLayout
android:id="#+id/disable_b"
android:clickable="true"
android:layout_width="78dp"
android:layout_marginRight="5dp"
android:layout_height="match_parent"
android:background="#8e8e8e">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="18dp"
android:text="Disable"
android:textColor="#fff"
android:id="#+id/disableT"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
/>
</RelativeLayout>
<RelativeLayout
android:id="#+id/delete_b"
android:clickable="true"
android:layout_width="78dp"
android:layout_height="match_parent"
android:background="#de5454">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="18dp"
android:text="Delete"
android:textColor="#fff"
android:id="#+id/deleteText"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
/>
</RelativeLayout>
</LinearLayout>
</service.SwipeLayout>
</LinearLayout>
UPD: simple listView
activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context=".MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/hello_world" />
<ListView
android:id="#+id/listView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:cacheColorHint="#00000000"
android:divider="#adb8c2"
android:dividerHeight="1dp"
android:scrollingCache="false"
android:smoothScrollbar="true" />
</RelativeLayout>
my main
ArrayList<String> asd = new ArrayList<>();
for(int i = 0; i < 10; i++) {
asd.add(String.valueOf(i));
}
ListView listView = (ListView) findViewById(R.id.listView);
Adapter adapter = new Adapter(getApplicationContext(), R.layout.item, asd);
listView.setAdapter(adapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
Toast.makeText(getBaseContext(), String.valueOf(position), Toast.LENGTH_LONG).show();
}
});
and item.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Large Text"
android:id="#+id/textView" />
</LinearLayout>
Its good, its work when i click in item listView, Toast is show position. But i need use SwipeLayout then i change code item.xml and add this:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
android:descendantFocusability="blocksDescendants"
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.bottom.smart.detetethis.SwipeLayout
android:layout_marginTop="5dp"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:orientation="horizontal"
android:background="#32777777"
android:layout_width="match_parent"
android:layout_height="70dp">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#fff"
android:layout_weight="0.7"
>
<TextView
android:id="#+id/conditions"
android:layout_marginTop="10dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="22dp"
android:text="Active"
android:textColor="#25ac36"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true" />
</RelativeLayout>
<RelativeLayout
android:layout_weight="0.3"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="#+id/description"
android:layout_marginLeft="10dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="18dp"
android:text="Turn light on \n when people arrive"
android:textColor="#222222"
android:layout_centerVertical="true"
/>
</RelativeLayout>
</LinearLayout>
<RelativeLayout
android:background="#66777777"
android:layout_width="match_parent"
android:layout_height="40dp">
<TextView
android:id="#+id/rooms"
android:layout_marginLeft="10dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="18dp"
android:text="Livingroom, Kitechen, Rest room"
android:textColor="#fff"
android:layout_centerVertical="true"
/>
</RelativeLayout>
</LinearLayout>
<LinearLayout
android:layout_width="250dp"
android:layout_height="match_parent">
<RelativeLayout
android:id="#+id/open_b"
android:layout_width="78dp"
android:layout_marginRight="5dp"
android:layout_marginLeft="5dp"
android:layout_height="match_parent"
android:background="#4fcc54">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="18dp"
android:text="Open"
android:textColor="#fff"
android:id="#+id/openText"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
/>
</RelativeLayout>
<RelativeLayout
android:id="#+id/disable_b"
android:layout_width="78dp"
android:layout_marginRight="5dp"
android:layout_height="match_parent"
android:background="#8e8e8e">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="18dp"
android:text="Disable"
android:textColor="#fff"
android:id="#+id/disableT"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
/>
</RelativeLayout>
<RelativeLayout
android:id="#+id/delete_b"
android:layout_width="78dp"
android:layout_height="match_parent"
android:background="#de5454">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="18dp"
android:text="Delete"
android:textColor="#fff"
android:id="#+id/deleteText"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
/>
</RelativeLayout>
</LinearLayout>
</com.bottom.smart.detetethis.SwipeLayout>
</LinearLayout>
got sick, its not working when i click item listView. But when I get a finger between the clearance item, its work:
to blame SwipeLayout, code SwipeLayout:
public class SwipeLayout extends LinearLayout {
private ViewDragHelper viewDragHelper;
private View contentView;
private View actionView;
private int dragDistance;
private final double AUTO_OPEN_SPEED_LIMIT = 400.0;
private int draggedX;
public SwipeLayout(Context context) {
this(context, null);
}
public SwipeLayout(Context context, AttributeSet attrs) {
this(context, attrs, -1);
}
public SwipeLayout(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
viewDragHelper = ViewDragHelper.create(this, new DragHelperCallback());
}
#SuppressLint("MissingSuperCall")
#Override
protected void onFinishInflate() {
contentView = getChildAt(0);
actionView = getChildAt(1);
actionView.setVisibility(GONE);
}
#Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
dragDistance = actionView.getMeasuredWidth();
}
private class DragHelperCallback extends ViewDragHelper.Callback {
#Override
public boolean tryCaptureView(View view, int i) {
return view == contentView || view == actionView;
}
#Override
public void onViewPositionChanged(View changedView, int left, int top, int dx, int dy) {
draggedX = left;
if (changedView == contentView) {
actionView.offsetLeftAndRight(dx);
} else {
contentView.offsetLeftAndRight(dx);
}
if (actionView.getVisibility() == View.GONE) {
actionView.setVisibility(View.VISIBLE);
}
invalidate();
}
#Override
public int clampViewPositionHorizontal(View child, int left, int dx) {
if (child == contentView) {
final int leftBound = getPaddingLeft();
final int minLeftBound = -leftBound - dragDistance;
final int newLeft = Math.min(Math.max(minLeftBound, left), 0);
return newLeft;
} else {
final int minLeftBound = getPaddingLeft() + contentView.getMeasuredWidth() - dragDistance;
final int maxLeftBound = getPaddingLeft() + contentView.getMeasuredWidth() + getPaddingRight();
final int newLeft = Math.min(Math.max(left, minLeftBound), maxLeftBound);
return newLeft;
}
}
#Override
public int getViewHorizontalDragRange(View child) {
return dragDistance;
}
#Override
public void onViewReleased(View releasedChild, float xvel, float yvel) {
super.onViewReleased(releasedChild, xvel, yvel);
boolean settleToOpen = false;
if (xvel > AUTO_OPEN_SPEED_LIMIT) {
settleToOpen = false;
} else if (xvel < -AUTO_OPEN_SPEED_LIMIT) {
settleToOpen = true;
} else if (draggedX <= -dragDistance / 2) {
settleToOpen = true;
} else if (draggedX > -dragDistance / 2) {
settleToOpen = false;
}
final int settleDestX = settleToOpen ? -dragDistance : 0;
viewDragHelper.smoothSlideViewTo(contentView, settleDestX, 0);
ViewCompat.postInvalidateOnAnimation(SwipeLayout.this);
}
}
#Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
if(viewDragHelper.shouldInterceptTouchEvent(ev)) {
return true;
}
return super.onInterceptTouchEvent(ev);
}
#Override
public boolean onTouchEvent(MotionEvent event) {
viewDragHelper.processTouchEvent(event);
return true;
}
#Override
public void computeScroll() {
super.computeScroll();
if(viewDragHelper.continueSettling(true)) {
ViewCompat.postInvalidateOnAnimation(this);
}
}
}
I can not delete it, because I need it in the project. I do not know how to fix this situation
Sometimes the ListView click doesn't work. And at this case you might have to add one more attribute.
android:descendantFocusability="blocksDescendants"
Add this attribute to the top most layout of your XML where you have provided the ListView elements.
In your ListView remove the xml attribute android:clickable="true"
This is taking your click event and not sending it down to the child views.
Edit
You are passing in getApplicationContext() which is the wrong context. The listener does work, but the Toast is not displaying in the right context. You need to pass in getContext(). However, since this is an Activity, you can just pass in this instead.
public void outputListView(ArrayList<String> list) {
Adapter adapter = new Adapter(this, R.layout.item, list);
adapter.notifyDataSetChanged();
listView.setAdapter(adapter);
listView.setItemsCanFocus(false);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
Toast.makeText(this, String.valueOf(position), Toast.LENGTH_SHORT).show();
}
});
}
I wanna make footer buttons in android one way is to simply make buttons and align them to bottom but I want the footer like in Facebook android app whenever we drag screen down three buttons appears for status , photo , checkin.
How to do this ??
To get a list like the following image, create a layout.xml as follows after the sample image
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/root"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/white" >
<RelativeLayout
android:id="#+id/headerLayout"
android:layout_width="match_parent"
android:layout_height="50dp"
android:background="#drawable/header" >
<LinearLayout
android:id="#+id/BtnSlide"
android:layout_width="wrap_content"
android:layout_height="50dp"
android:layout_alignParentLeft="true"
android:background="#drawable/button_bg_drawable" >
<ImageView
android:id="#+id/imageView0"
android:layout_width="wrap_content"
android:layout_height="50dp"
android:layout_gravity="center_vertical|left"
android:background="#drawable/button_bg_drawable"
android:paddingBottom="10dp"
android:paddingLeft="15dp"
android:paddingRight="15dp"
android:paddingTop="10dp"
android:src="#drawable/back_btn_small" />
</LinearLayout>
<EditText
android:id="#+id/headerText"
android:layout_width="fill_parent"
android:layout_height="40dp"
android:layout_centerVertical="true"
android:layout_marginRight="10dp"
android:layout_toRightOf="#id/BtnSlide"
android:background="#android:drawable/editbox_background_normal"
android:editable="false"
android:textColor="#color/black"
android:textSize="18sp"
android:textStyle="normal" />
<AutoCompleteTextView
android:id="#+id/filterNewProject"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginRight="5dp"
android:layout_toRightOf="#id/BtnSlide"
android:background="#drawable/bg_input_blue"
android:completionThreshold="1"
android:hint="Search for a locality, developer or project"
android:textColor="#color/black"
android:textSize="14sp"
android:visibility="gone" />
<Button
android:id="#+id/clearAutoCompleteList"
android:layout_width="18dp"
android:layout_height="18dp"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_marginRight="10dp"
android:background="#drawable/custom_button_clear"
android:paddingLeft="20dp"
android:visibility="gone" />
<Button
android:id="#+id/searchButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_marginRight="15dp"
android:background="#drawable/seaerch_glass" />
<View
android:id="#+id/sep_header"
android:layout_width="fill_parent"
android:layout_height="2dp"
android:layout_below="#id/tabBar"
android:background="#d5d5d5"
android:visibility="visible" />
</RelativeLayout>
<include
android:id="#+id/footerLayout"
layout="#layout/post_requirement_footer"
android:visibility="gone" />
<ListView
android:id="#+id/projectsList"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="#id/footerLayout"
android:layout_below="#id/headerLayout"
android:divider="#color/white"
android:dividerHeight="1.5dp" >
</ListView>
<RelativeLayout
android:id="#+id/zeroResultsLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="#id/footerLayout"
android:layout_below="#id/headerLayout"
android:visibility="gone" >
<ImageView
android:id="#+id/emptyIllustration"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:src="#drawable/no_results_illustration" />
</RelativeLayout>
<com.housing.utils.QuickReturnRelativeLayoutFooter
android:id="#+id/frame"
android:layout_width="match_parent"
android:layout_height="60dp"
android:layout_alignParentBottom="true"
android:layout_alignParentTop="true"
android:background="#color/transparent" >
<RelativeLayout
android:id="#+id/bottomListViewContainer"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_alignParentBottom="true"
android:background="#drawable/footer_bg" >
<ImageButton
android:id="#+id/filterButtonFooter"
android:layout_width="wrap_content"
android:layout_height="45dp"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:background="#drawable/button_bg_drawable"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:src="#drawable/filter_icon" />
<ImageButton
android:id="#+id/subscribeButtonFooter"
android:layout_width="wrap_content"
android:layout_height="45dp"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:background="#drawable/button_bg_drawable"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:src="#drawable/subscribe_iphone"
android:visibility="visible" />
<TextView
android:id="#+id/resultsText"
android:layout_width="wrap_content"
android:layout_height="45dp"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:ellipsize="end"
android:paddingTop="10dp"
android:scrollHorizontally="false"
android:singleLine="false"
android:text=""
android:textColor="#color/black"
android:textSize="15dp"
android:visibility="visible" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toLeftOf="#id/resultsText"
android:paddingRight="5dp"
android:paddingTop="15dp"
android:src="#drawable/filter_normal"
android:visibility="gone" />
</RelativeLayout>
</com.housing.utils.QuickReturnRelativeLayoutFooter>
</RelativeLayout>
Here is the Class com.housing.utils.QuickReturnRelativeLayoutFooter
PS : Replace com.housing.utils with your package name
package com.housing.utils;
import android.content.Context;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.view.View;
import android.widget.AbsListView;
import android.widget.AbsListView.OnScrollListener;
import android.widget.RelativeLayout;
public class QuickReturnRelativeLayoutFooter extends RelativeLayout implements
OnScrollListener {
private class ChildDescriptor {
public int index;
public int total;
public int drawable;
public ChildDescriptor(int index) {
this.index = index;
}
public int getApproximateScrollPosition() {
return index * total + (total - drawable);
}
}
public int MAX_HEIGHT_DP =60;
public int MIN_HEIGHT_DP = 0;
public static final int SCROLL_DIRECTION_INVALID = 0;
public static final int SCROLL_DIRECTION_UP = 1;
public static final int SCROLL_DIRECTION_DOWN = 2;
private int direction = SCROLL_DIRECTION_INVALID;
private ChildDescriptor lastchild;
private OnScrollListener onscrolllistener;
private int maxheight;
private int minheight;
public QuickReturnRelativeLayoutFooter(Context context, AttributeSet attrs,
int defStyle) {
super(context, attrs, defStyle);
calculateMinMax();
}
public QuickReturnRelativeLayoutFooter(Context context, AttributeSet attrs) {
super(context, attrs);
calculateMinMax();
}
public QuickReturnRelativeLayoutFooter(Context context) {
super(context);
calculateMinMax();
}
private void calculateMinMax() {
DisplayMetrics metrics = getResources().getDisplayMetrics();
maxheight = (int) (metrics.density * (float) MAX_HEIGHT_DP);
minheight = (int) (metrics.density * (float) MIN_HEIGHT_DP);
}
private void adjustHeight(int howmuch, int max, int min) {
if ((howmuch < 0) && (direction != SCROLL_DIRECTION_UP)) {
direction = SCROLL_DIRECTION_UP;
return;
} else if ((howmuch > 20) && (direction != SCROLL_DIRECTION_DOWN)) {
direction = SCROLL_DIRECTION_DOWN;
return;
}
int current = getHeight();
current += howmuch;
if (current < min) {
current = min;
} else if (current > max) {
current = max;
}
RelativeLayout.LayoutParams f = (RelativeLayout.LayoutParams) getLayoutParams();
if (f.height != current) {
f.height = current;
setLayoutParams(f);
}
if (direction == SCROLL_DIRECTION_UP
&& Math.abs(f.topMargin) <= current) {
// if (f.topMargin != howmuch) {
//
// f.topMargin = howmuch + f.topMargin;
//
// if (f.topMargin > 0) {
// f.topMargin = -f.topMargin - 10;
// }
//
// }
// mBottomListViewContainer.setVisibility(View.GONE);
f.bottomMargin = -100;
setLayoutParams(f);
} else if (direction == SCROLL_DIRECTION_DOWN) {
if (f.bottomMargin != 0) {
f.bottomMargin = 0;
setLayoutParams(f);
}
}
}
#Override
public void onScroll(AbsListView view, int firstVisibleItem,
int visibleItemCount, int totalItemCount) {
ChildDescriptor currentchild = getFirstChildItemDescriptor(view,
firstVisibleItem);
try {
adjustHeight(lastchild.getApproximateScrollPosition()
- currentchild.getApproximateScrollPosition(), maxheight,
minheight);
lastchild = currentchild;
} catch (NullPointerException e) {
lastchild = currentchild;
} catch (Exception e) {
}
if (onscrolllistener != null) {
onscrolllistener.onScroll(view, firstVisibleItem, visibleItemCount,
totalItemCount);
}
}
private ChildDescriptor getFirstChildItemDescriptor(AbsListView view,
int index) {
ChildDescriptor h = new ChildDescriptor(index);
try {
Rect r = new Rect();
View child = view.getChildAt(0);
child.getDrawingRect(r);
h.total = r.height();
view.getChildVisibleRect(child, r, null);
h.drawable = r.height();
return h;
} catch (Exception e) {
}
return null;
}
#Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
if (onscrolllistener != null) {
onscrolllistener.onScrollStateChanged(view, scrollState);
}
}
public void attach(AbsListView view) {
view.setOnScrollListener(this);
}
public void setOnScrollListener(OnScrollListener l) {
onscrolllistener = l;
}
}
I Have made it as a widget and Now finally to add this to your List View use
frame = (QuickReturnRelativeLayoutFooter) newProjectsView
.findViewById(R.id.frame);
frame.attach(projectsList);
where projectList is your List View
example
ListView projectList =(ListView)findViewById(R.id.projectList);
and Voila Cheers Completed Smooth as Heaven .......
I am using JakeWharton's ViewPageIndicator. I want the height of the pager and the indicator to span around 300dp so that i can more content below it and it can scroll.
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="#+id/scrollView"
android:fillViewport="true" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="300dp"
android:orientation="vertical" >
<android.support.v4.view.ViewPager
android:id="#+id/pager"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:paddingTop="2dp" />
<com.viewpagerindicator.CirclePageIndicator
android:id="#+id/indicator"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="5dp"
android:padding="10dip" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Testing something cool" />
</LinearLayout>
</LinearLayout>
</ScrollView>
Things Work Perfect. But i am not able to Slide the Viewpagper and it gets really stuck and doesnt slide to the next one. Any one know how to make it work in a confined height ?
******Edit******
Trying to use the solution from Here. Am not very sure if its the right way to Sub-Class Scroll View.
public class PlaceDetailsFragment extends SherlockFragment {
PlaceSlidesFragmentAdapter mAdapter;
ViewPager mPager;
PageIndicator mIndicator;
public static final String TAG = "detailsFragment";
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_place_details,
container, false);
mAdapter = new PlaceSlidesFragmentAdapter(getActivity()
.getSupportFragmentManager());
mPager = (ViewPager) view.findViewById(R.id.pager);
mPager.setAdapter(mAdapter);
mIndicator = (CirclePageIndicator) view.findViewById(R.id.indicator);
mIndicator.setViewPager(mPager);
// ((CirclePageIndicator) mIndicator).setSnap(true);
mIndicator
.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
#Override
public void onPageSelected(int position) {
Toast.makeText(PlaceDetailsFragment.this.getActivity(),
"Changed to page " + position,
Toast.LENGTH_SHORT).show();
}
#Override
public void onPageScrolled(int position,
float positionOffset, int positionOffsetPixels) {
}
#Override
public void onPageScrollStateChanged(int state) {
}
});
return view;
}
public class CustomScrollView extends ScrollView {
private GestureDetector mGestureDetector;
View.OnTouchListener mGestureListener;
public CustomScrollView(Context context, AttributeSet attrs) {
super(context, attrs);
mGestureDetector = new GestureDetector(context, new YScrollDetector());
setFadingEdgeLength(0);
}
#Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
return super.onInterceptTouchEvent(ev) && mGestureDetector.onTouchEvent(ev);
}
// Return false if we're scrolling in the x direction
class YScrollDetector extends SimpleOnGestureListener {
#Override
public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
if(Math.abs(distanceY) > Math.abs(distanceX)) {
return true;
}
return false;
}
}
}
}
Have to create a Custom Scroll View and use that
public class VerticalScrollView extends ScrollView {
View.OnTouchListener mGestureListener;
private VelocityTracker mVelocityTracker;
private int mMaximumVelocity;
public VerticalScrollView(Context context, AttributeSet attrs) {
super(context, attrs);
setFadingEdgeLength(0);
final ViewConfiguration configuration = ViewConfiguration.get(context);
mMaximumVelocity = configuration.getScaledMaximumFlingVelocity();
}
private boolean doNotInterceptUntilUp = false;
#Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
// System.out.println("onInterceptTouchEvent");
int action = ev.getAction();
int index = MotionEventCompat.getActionIndex(ev);
int mActivePointerId = MotionEventCompat.getPointerId(ev, index);
if (action == MotionEvent.ACTION_DOWN) {
if (mVelocityTracker == null) {
mVelocityTracker = VelocityTracker.obtain();
}
mVelocityTracker.addMovement(ev);
} else if (action == MotionEvent.ACTION_MOVE) {
if (doNotInterceptUntilUp) {
return false;
}
mVelocityTracker.addMovement(ev);
mVelocityTracker.computeCurrentVelocity(1000, mMaximumVelocity);
int initialVelocity = (int) VelocityTrackerCompat.getXVelocity(
mVelocityTracker, mActivePointerId);
int initialYVelocity = (int) VelocityTrackerCompat.getYVelocity(
mVelocityTracker, mActivePointerId);
if (Math.abs(initialVelocity) > 100) {
// 140 is the minimum threshold it seems.
if (!(Math.abs(initialYVelocity) > 140))
doNotInterceptUntilUp = true;
return false;
}
} else if (action == MotionEvent.ACTION_UP) {
doNotInterceptUntilUp = false;
if (mVelocityTracker != null) {
mVelocityTracker.clear();
}
}
boolean val = super.onInterceptTouchEvent(ev);
return val;
}
}
XML
<?xml version="1.0" encoding="utf-8"?>
<com.m7.nomad.VerticalScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res/com.m7.nomad"
android:id="#+id/scrollView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:fillViewport="true" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="300dp"
android:orientation="vertical" >
<android.support.v4.view.ViewPager
android:id="#+id/pager"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1" />
<com.viewpagerindicator.CirclePageIndicator
android:id="#+id/indicator"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="5dp"
android:paddingBottom="10dip"
android:paddingLeft="10dip"
android:paddingTop="1dip" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Testing something cool" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Testing something cool" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Testing something cool" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Testing something cool" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Testing something cool" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Testing something cool" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Testing something cool" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Testing something cool" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Testing something cool" />
</LinearLayout>
</LinearLayout>
</com.m7.nomad.VerticalScrollView>