How to disable Scrollview - android

I have ScrollView and inside that a TabLayout. How can I disable the ScrollView from scrolling without effecting on TabLayout scroll?
EDIT:
<?xml version="1.0" encoding="utf-8"?>
<ScrollView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="#+id/scrollview"
android:background="#color/backgroundView"
xmlns:android="http://schemas.android.com/apk/res/android"
android:scrollbars="none"
xmlns:app="http://schemas.android.com/apk/res-auto">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="#dimen/slider_height"
android:background="#drawable/border_light"
>
app:pv_progressStyle="#style/LightInColorCircularProgressView"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true" />
<com.daimajia.slider.library.SliderLayout
android:id="#+id/main_slider"
android:layout_width="match_parent"
android:layout_height="#dimen/slider_height"
/>
<com.daimajia.slider.library.Indicators.PagerIndicator
android:id="#+id/custom_indicator"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
style="#style/AndroidImageSlider_Corner_Oval"
android:gravity="center"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="10dp" />
</RelativeLayout>
.... other stuff
<com.rey.material.widget.TabPageIndicator
style="#style/TabPageIndicator"
android:id="#+id/main_tpi"
android:layout_height="48dp"
android:layout_width="match_parent"
android:paddingLeft="0dp"
android:clipToPadding="true"
app:tpi_mode="scroll"
app:tpi_indicatorColor="#color/colorPrimary"
android:background="#color/overlayView"/>
<adapters.CustomViewPager
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/main_vp"
/>
</ScrollView>

I used this and it worked for me :
// Get the ScrollView
final ScrollView myScroll = (ScrollView) findViewById(R.id.display_scrollview);
// Disable Scrolling by setting up an OnTouchListener to do nothing
myScroll.setOnTouchListener( new OnTouchListener(){
#Override
public boolean onTouch(View v, MotionEvent event) {
return true;
}
});
// Enable Scrolling by removing the OnTouchListner
tvDisplayScroll.setOnTouchListener(null);
happy programming......!!!

Related

Android: EditText inside ScrollView : hide keyboard generates strange scrolling

I have this layout example:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ffffff"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="56dp"
android:background="#FFF880"
android:gravity="center_vertical"
android:orientation="horizontal">
</FrameLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<ScrollView
android:id="#+id/scroll"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_above="#+id/action_bar_layout">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="#+id/emotion"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#android:color/holo_blue_bright" />
<EditText
android:id="#+id/edit"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#android:color/transparent"
android:inputType="textMultiLine|textCapSentences"
android:singleLine="false"
android:text="On sait depuis longtemps "
android:textColor="#android:color/black" />
<FrameLayout
android:id="#+id/photo"
android:layout_width="match_parent"
android:layout_height="350dp"
android:background="#EE00FF">
</FrameLayout>
</LinearLayout>
</ScrollView>
<FrameLayout
android:id="#+id/action_bar_layout"
android:layout_width="match_parent"
android:layout_height="56dp"
android:layout_alignParentBottom="true"
android:background="#990000"
android:gravity="center_vertical"
android:orientation="horizontal">
</FrameLayout>
</RelativeLayout>
</RelativeLayout>
</LinearLayout>
</RelativeLayout>
And my sample Activity:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_edittext);
final ScrollView scrollView = (ScrollView)findViewById(R.id.scroll);
final EditText edittext = (EditText)findViewById(R.id.edit);
// Setup scrollview
scrollView.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
final int action = event.getAction();
switch (action) {
case MotionEvent.ACTION_MOVE:
// Hide keyboard
UIUtils.hideKeyboard(edittext, EditTextActivity.this);
break;
}
return false;
}
});
public static void hideKeyboard(View view, Activity activity) {
if (view != null) {
view.clearFocus();
if (activity != null) {
activity.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
InputMethodManager imm = (InputMethodManager) activity.
getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
}
}
}
So I would like the same behavior like Facebook new post: when you want to scroll the keyboard disappears automatically.
But in my case, there is a strange behavior when you want to scroll when the keyboard has been hide.
Thanks guys for your help!
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/scroll"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_above="#+id/action_bar_layout"
android:fillViewport="true">
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ffffff"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="56dp"
android:background="#FFF880"
android:gravity="center_vertical"
android:orientation="horizontal">
</FrameLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="#+id/emotion"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#android:color/holo_blue_bright" />
<EditText
android:id="#+id/edit"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#android:color/transparent"
android:inputType="textMultiLine|textCapSentences"
android:singleLine="false"
android:text="On sait depuis longtemps "
android:textColor="#android:color/black" />
<FrameLayout
android:id="#+id/photo"
android:layout_width="match_parent"
android:layout_height="350dp"
android:background="#EE00FF">
</FrameLayout>
</LinearLayout>
<FrameLayout
android:id="#+id/action_bar_layout"
android:layout_width="match_parent"
android:layout_height="56dp"
android:layout_alignParentBottom="true"
android:background="#990000"
android:gravity="center_vertical"
android:orientation="horizontal">
</FrameLayout>
</RelativeLayout>
</RelativeLayout>
</LinearLayout>
</RelativeLayout>
</ScrollView>
Try this hope it will solve your problem.
Instead of hiding keyboard on scroll ,you can hide keyboard on outsidetouch of edit text
Make the parent view(content view of your activity) clickable and focusable by adding the following attributes
android:clickable="true"
android:focusableInTouchMode="true"
Implement a hideKeyboard() method
public void hideKeyboard(View view) {
InputMethodManager inputMethodManager =(InputMethodManager)getSystemService(Activity.INPUT_METHOD_SERVICE);
inputMethodManager.hideSoftInputFromWindow(view.getWindowToken(), 0);
}
and set the onFocusChangeListener of your edittext.
edittext.setOnFocusChangeListener(new View.OnFocusChangeListener() {
#Override
public void onFocusChange(View v, boolean hasFocus) {
if (!hasFocus) {
hideKeyboard(v);
}
}
});
or if you want to hide on scroll then you can add on touch listner for your main view instead of scroll view
findViewById(R.id.mainLayout).setOnTouchListener(this)
set android:focusableInTouchMode="true" on main container

Hide RelativeLayout when pull ScrollView

I have RelativeLayoutin top Scroll View, and now i want when pull down RelativeLayoutin hint and pull up RelativeLayoutin show.
How i do?
My 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="fill_parent"
android:orientation="vertical"
tools:context="${relativePackage}.${activityClass}" >
<RelativeLayout
android:id="#+id/Rlayout_top"
android:layout_width="match_parent"
android:layout_height="40dp"
android:layout_marginBottom="5dp"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:layout_marginTop="5dp" />
</RelativeLayout>
<ScrollView
android:id="#+id/Scrollview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#+id/Rlayout_top"
android:paddingBottom="20dp" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
</LinearLayout>
</ScrollView>
</RelativeLayout>
Thank you!
try this :
private float lastPosition=0;
scrollView.getViewTreeObserver().addOnScrollChangedListener(new OnScrollChangedListener() {
#Override
public void onScrollChanged() {
int scrollY = rootScrollView.getScrollY(); //for verticalScrollView
if(scrollY >lastPosition){
// hide your RelativeLayout
}else {
//show your view
}
lastPosition=scrollY
});

Get the height of dynamically added LinearLayouts in Android

I am working in an android application and I am adding a Linear Layout dynamically in my Scollview. Now I want to get the height of each Linear Layout added dynamically . Below is my code and Xml.
LinearLayout layout = (LinearLayout) findViewById(R.id.subscrollLinearLayout);
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
List<String> mSempLog = new ArrayList<String>();
mSempLog.add("TEST1");
mSempLog.add("TEST2");
mSempLog.add("TEST3");
mSempLog.add("TEST4");
mSempLog.add("TEST5");
mSempLog.add("TEST6");
mSempLog.add("TEST7");
mSempLog.add("TEST8");
for (int i = 0; i < mSempLog.size(); i++) {
LinearLayout service_row = (LinearLayout) inflater.inflate(
R.layout.tesffff, null);
TextView dd = ((TextView) service_row.findViewById(R.id.test1));
dd.setText(mSempLog.get(i));
dd.setTag(i);
((TextView) service_row.findViewById(R.id.test2)).setText(mSempLog
.get(i));
layout.addView(service_row, i);
}
ScrollView parentScrollView = (ScrollView) findViewById(R.id.parentscroll);
ScrollView childScrollView = (ScrollView) findViewById(R.id.subscroll);
parentScrollView.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
findViewById(R.id.subscroll).getParent()
.requestDisallowInterceptTouchEvent(false);
return false;
}
});
childScrollView.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
// Disallow the touch request for parent scroll on touch of
// child view
v.getParent().requestDisallowInterceptTouchEvent(true);
return false;
}
});
}
Xml
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/parentscroll"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<ScrollView
android:id="#+id/subscroll"
android:layout_width="match_parent"
android:layout_height="200dp" >
<LinearLayout
android:id="#+id/subscrollLinearLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
</LinearLayout>
</ScrollView>
<TextView
android:id="#+id/test1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="dfsdfsdf" />
<TextView
android:id="#+id/test1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="dfsdfsdf" />
<TextView
android:id="#+id/test1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="dfsdfsdf" />
<TextView
android:id="#+id/test1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="dfsdfsdf" />
<TextView
android:id="#+id/test1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="dfsdfsdf" />
<TextView
android:id="#+id/test1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="dfsdfsdf" />
<TextView
android:id="#+id/test1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="dfsdfsdf" />
<TextView
android:id="#+id/test1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="dfsdfsdf" />
<TextView
android:id="#+id/test1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="dfsdfsdf" />
</LinearLayout>
</ScrollView>
Row xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:padding="15dp"
android:weightSum="2" >
<TextView
android:id="#+id/test1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:clickable="true"
android:onClick="onTest1"
android:textColor="#android:color/black" />
<TextView
android:id="#+id/test2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:clickable="true"
android:gravity="center"
android:onClick="onTest2"
android:textColor="#android:color/black" />
</LinearLayout>
This worked for me
MyListViewObj.post(new Runnable(){
public void run(){
int height = MyListViewObj.getHeight();
}
});
Actually you can get width by few ways.
case 1: yourView.getHeight() (only when the view was already drawn)
case 2: yourView.getLayoutParams().height (depends on LayoutParams)
case 3: http://www.limbaniandroid.com/2014/10/viewtreeobserver-how-to-get-layout.html
case 4: If you have custom layout then you can get height in onMeasure (http://developer.android.com/reference/android/view/View.html#onMeasure(int,
int))

How to stop scrolling when the Custom view has touchlisteners in android?

I am dynamically adding an Custom Views to the relative layouts.
In my custom View,I have an Child view which can be dragged left and right.Its all working fine with only one view in the Relative layout.But when there are multiple views ACTION_UP events are not called for the dragging child views. so, I want to stop the scrolling when user touches the Dragging childView.
Main xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
tools:context=".MainActivity" >
<com.example.dynamicscroll.ObservableScrollView
android:id="#+id/mainScrollView"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<HorizontalScrollView
android:layout_width="wrap_content" android:layout_height="fill_parent">
<RelativeLayout
android:id="#+id/relativeLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
</HorizontalScrollView>
</com.example.dynamicscroll.ObservableScrollView>
</RelativeLayout>
inflatedfeedmodebloops.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#ababab"
android:orientation="vertical" >
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#drawable/transparent"
>
<com.example.dynamicscroll.ConstrainedDragAndDropView
android:id="#+id/dndView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
>
<ImageView
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_marginTop="20dp"
android:background="#drawable/like_small"
android:id="#+id/dummy"
android:visibility="gone"
/>
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:id="#+id/images">
<!-- left circle -->
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:id="#+id/half_left">
<ImageView
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_marginTop="20dp"
android:background="#drawable/like_small"
android:id="#+id/like_image"
/>
<TextView
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_marginTop="10dp"
android:layout_below="#+id/like_image"
android:text="36"
android:textSize="25dp"
android:textColor="#DF013A"
android:id="#+id/likes_count"
/>
</RelativeLayout>
<!-- end of left circle -->
<!-- <ImageView
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_toLeftOf="#+id/img_1"
android:background="#drawable/pinkcircle"
android:id="#+id/full_left"
android:layout_centerVertical="true"
android:visibility="gone"
/>
-->
<!-- right circle -->
<ImageView
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_alignParentRight="true"
android:id="#+id/backbtn"
android:layout_centerInParent="true"/>
<!-- end of right circle -->
<!-- cartoon image -->
<RelativeLayout
android:layout_width="200dp"
android:layout_height="200dp" ----->I want to drag this image,so I want to stop
scrolling when user touches this view.
android:layout_centerHorizontal="true"
android:background="#drawable/img_1"
android:layout_marginTop="20dp"
android:id="#+id/cartoon_image">
<ImageView
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:background="#drawable/like_small"
android:layout_centerInParent="true"
android:id="#+id/ivheart"
android:visibility="gone"
/>
<RelativeLayout
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:background="#drawable/play_btn"
android:orientation="horizontal"
android:layout_centerInParent="true"
android:id="#+id/play_btn"
>
</RelativeLayout>
</RelativeLayout>
<!-- end of cartoon image -->
</RelativeLayout>
</com.example.dynamicscroll.ConstrainedDragAndDropView>
</RelativeLayout>
</LinearLayout>
For adding Views to Relative Layout:
for(int i=0;i<al_new.size();i++)
{
Log.e("inflating the bloops ","....................");
final View view= getLayoutInflater().inflate(R.layout.inflatedfeedmodebloops, null);
iv= (ImageView)view.findViewById(R.id.main_img);
ivLike=(ImageView)view.findViewById(R.id.like_image);
txtlikesCount=(TextView)view.findViewById(R.id.likes_count);
loader.DisplayImage(al_new.get(i).replace(" ", "%20"), iv);
ConstrainedDragAndDropView dndView = (ConstrainedDragAndDropView)view.findViewById(R.id.dndView);
dndView.setDragHandle(view.findViewById(R.id.cartoon_image),view.findViewById(R.id.half_left),heart);
dndView.setAllowVerticalDrag(false,ScrollingFeedmode.this);
rl.addView(view);
if (view.findViewById(R.id.cartoon_image)!= null && myscrollview!= null) {
view.findViewById(R.id.cartoon_image).setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
mainScrollView.getParent()
.requestDisallowInterceptTouchEvent(false);
return false;
}
});
}
}
So,can anyone suggest me how can I stop scrolling when user touches the childview.
Use the following code to stopping parent view scrolling while touching childview
if (childView != null && parentView != null) {
childView.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
findViewById(R.id.parent_view).getParent()
.requestDisallowInterceptTouchEvent(false);
return false;
}
});
}

android masking a layout with other layout

I am using a progress bar in a matched parent layout in my application. Problem is button in the background is still clickable while the progress bar runs. They should not be clickable, so how do I make them non-clickable. The layout is:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/black_with_fifty_percent_alpha"
android:gravity="center"
android:orientation="vertical" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#android:color/white"
android:gravity="center_horizontal"
android:orientation="vertical" >
<ProgressBar
style="?android:attr/progressBarStyleLarge"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/text_tv_progress_bar_bultenler"
android:textAppearance="?android:attr/textAppearanceMedium" />
</LinearLayout>
</LinearLayout>
Try set this to this layout:
OnTouchListener listener = new OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
return true;//consume the touch event
}
};
yourLayout.setOnTouchListener(listener);

Categories

Resources