I have a TextView with a lot of text. This TextView has maxLines set, so it only shows the first 8 or so lines. I also have a "Read More" button so I handle expanding the TextView on my own.
My problem is that sometimes the TextView scrolls a little (just half a line at a time), even though I never specified any scroll bars. This issue is made worse because the TextView is inside a ListView, so when the user scrolls the main ListView, the TextView sometimes scrolls a little, like this:
How do I prevent the TextView from scrolling?
I have the same problem,My solution is create a NoScrollTextView extends TextView like this
public class NoScrollTextView extends TextView {
public NoScrollTextView(Context context) {
super(context);
}
public NoScrollTextView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public NoScrollTextView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
#TargetApi(Build.VERSION_CODES.LOLLIPOP)
public NoScrollTextView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
}
#Override
public void scrollTo(int x, int y) {
//do nothing
}
}
set scrollTo do nothing
In Kotlin:
class NonScrollingTextView : TextView {
constructor(context: Context) : super(context) {}
constructor(context: Context, attrs: AttributeSet) : super(context, attrs) {}
constructor(context: Context, attrs: AttributeSet, defStyleAttr: Int) : super(context, attrs, defStyleAttr) {}
#TargetApi(Build.VERSION_CODES.LOLLIPOP)
constructor(context: Context, attrs: AttributeSet, defStyleAttr: Int, defStyleRes: Int) : super(context, attrs, defStyleAttr, defStyleRes) {
}
override fun scrollTo(x: Int, y: Int) {
//do nothing
}
}
So I did a little research and I don't think it's as simple as just disabling scrolling, but there are a few things you can do/try.
The first is setEnabled(false) but this will disable links and alter the text color.
The second, which I suggest trying, is using the scrollTo(int x, int y) method. Just scrollTo(0,0) after setting the text of the TextView, my guess is the large text is the only thing causing the scrolling so this should be able to take care of it.
The third answer I found that you can try is a bit more complicated and not exactly your question but it may work for you can be found here.
public class LinkMovementMethodOverride implements View.OnTouchListener{
#Override
public boolean onTouch(View v, MotionEvent event) {
TextView widget = (TextView) v;
Object text = widget.getText();
if (text instanceof Spanned) {
Spanned buffer = (Spanned) text;
int action = event.getAction();
if (action == MotionEvent.ACTION_UP
|| action == MotionEvent.ACTION_DOWN) {
int x = (int) event.getX();
int y = (int) event.getY();
x -= widget.getTotalPaddingLeft();
y -= widget.getTotalPaddingTop();
x += widget.getScrollX();
y += widget.getScrollY();
Layout layout = widget.getLayout();
int line = layout.getLineForVertical(y);
int off = layout.getOffsetForHorizontal(line, x);
ClickableSpan[] link = buffer.getSpans(off, off,
ClickableSpan.class);
if (link.length != 0) {
if (action == MotionEvent.ACTION_UP) {
link[0].onClick(widget);
} else if (action == MotionEvent.ACTION_DOWN) {
// Selection only works on Spannable text. In our case setSelection doesn't work on spanned text
//Selection.setSelection(buffer, buffer.getSpanStart(link[0]), buffer.getSpanEnd(link[0]));
}
return true;
}
}
}
return false;
}
}
"After that apply it to the target textview as touch listener: -
textview.setOnTouchListener(new LinkMovementMethodOverride());"
I needed feature like see more in my project and I used ellipsizing textview from this SO post. It works like a charm and also provides an interface for checking if the text was ellipsized. This should do the trick.
Just use setMinLines() to always display whole text
Try these lines of code in xml
android:isScrollContainer="false"
android:ellipsize="end"
Related
In my project I use a RecyclerView that I only want to scroll by calling the startSmoothScroll() method of the LayoutManager:
private fun next(){
val layoutManager = pager.layoutManager as BattlePageLayoutManager
layoutManager.startSmoothScroll(smoothScroller(layoutManager.findFirstVisibleItemPosition() + 1))
layoutManager.finishScroll()
}
I do not want the user to be able to scroll manually, e. g. by swiping.
I already tried to achieve this through overriding the method onInterceptTouchEvent() of the parent FrameLayout.
override fun onInterceptTouchEvent(ev: MotionEvent): Boolean {
if (ev.actionMasked == MotionEvent.ACTION_DOWN){
startClickTime = System.currentTimeMillis()
startX = ev.x
startY = ev.y
}
val allowEvent = (System.currentTimeMillis() - startClickTime) < 1000 && (startX-ev.x).absoluteValue < 15 && (startY-ev.y).absoluteValue < 15
return !allowEvent
}
That worked basically, but it occured that after double-tapping the View users are able to scroll by themselves.
Do you have any other ideas to approach this?
Did you try overriding canScrollVertically() method in the LayoutManager?
mLayoutManager = new LinearLayoutManager(getActivity()) {
#Override
public boolean canScrollVertically() {
return false;
}
};
Edit:
Create your own implementation of RecyclerView which it disables the touch event while scrolling is performing. Then you have to change the RecyclerView class in the xml file and Fragment/Activity with it.
Find here an example in Kotlin
class MyRecyclerView : RecyclerView {
constructor(context: Context) : super(context) {}
constructor(context: Context, attrs: AttributeSet?) : super(context, attrs) {}
constructor(context: Context, attrs: AttributeSet?, defStyle: Int) : super(context, attrs, defStyle) {}
override fun onInterceptTouchEvent(e: MotionEvent): Boolean {
return if (scrollState != RecyclerView.SCROLL_STATE_IDLE) false else super.onInterceptTouchEvent(e)
}
}
And in Java
public class MyRecyclerView extends RecyclerView {
public MyRecyclerView(Context context) {
super(context);
}
public MyRecyclerView(Context context, #Nullable AttributeSet attrs) {
super(context, attrs);
}
public MyRecyclerView(Context context, #Nullable AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
#Override
public boolean onInterceptTouchEvent(MotionEvent e) {
if(getScrollState() != SCROLL_STATE_IDLE)
return false;
return super.onInterceptTouchEvent(e);
}
}
You might want to block the user interaction with RecyclerView, not with FrameLayout itself.
Check RecyclerView.OnItemTouchListener.
In your RecyclerView, you can implement OnItemTouchListener and override every method to do nothing.
That will block the user interaction with RecyclerView, making scroll not happen.
I want to have a EditText in a scrollview and this EditText needs to be scrollable. So I made a customized one as follow:
public class myEditText extends EditText {
public myEditText(Context context) {
super(context);
}
public myEditText(Context context, AttributeSet attrs) {
super(context, attrs);
}
public myEditText(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
#Override
public boolean onTouchEvent(MotionEvent ev) {
this.getParent().requestDisallowInterceptTouchEvent(true);
return super.onTouchEvent(ev);
}
}
This EditText has only one problem, that is when the text gets really long, the Scrollview containing it jumps for some reason. This makes me unable to edit text after a certain length. Is anyone know why that happens?
I think I solved it.
The issue was when there is a cursor in the EditText, the bringPointIntoView(int offset) method will make EditText scroll to the position where the cursor is at, so that the cursor will be in your sight and you can edit the text. Usually that's how it works, but when the EditText is in a ScrollView, instead of scroll the EditText, it makes the ScrollView scrolls as well. That's the reason I saw the ScrollView jumps.
The solution is simply override the bringPointIntoView method.
So if anyone wants to use a scrollable EditText in a ScrollView, I think this may help:
public class myEditText extends EditText {
public myEditText(Context context) {
super(context);
}
public myEditText(Context context, AttributeSet attrs) {
super(context, attrs);
}
public myEditText(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
#Override
public boolean onTouchEvent(MotionEvent ev) {
this.getParent().requestDisallowInterceptTouchEvent(true);
return super.onTouchEvent(ev);
}
#Override
public boolean bringPointIntoView(int offset) {
return false;
}
}
On Android phone, I get the information of co-ordinates that, user touch on his/her phone screen or device. How do i then, use/pass these X-Y coordinates to run a specific function of mine, and use it in my code
reference from the post you can get the co-ordinates if the user touched the screen on your app. After you implement that on touch listener then in the if statement MotionEvent.ACTION_UP put this there
float myX = event.getX();
float myY = event.getY();
// now you can then pass myX,myY in your method or function as parameters
if you happen to be targeting Api 14+ then you can use onhover with the same approach as to the first one from that post, instead of using setOnTouchListener usegetWindow().getDecorView().setOnHoverListener() that's about it.
A side note is using the firs one is better because onHover can not be trusted to work,actually it doesn't work to me, thats just btw
hope i answered your question.
You can create custom view by extending android view and overriding onTouchEvent method. For example,
public class NotTouchableWebView extends WebView {
private Context context;
public NotTouchableWebView(Context context) {
super(context);
this.context=context;
}
public NotTouchableWebView(Context context, AttributeSet attrs) {
super(context, attrs);
this.context=context;
}
public NotTouchableWebView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
this.context=context;
}
#TargetApi(Build.VERSION_CODES.LOLLIPOP)
public NotTouchableWebView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
this.context=context;
}
#Override
public boolean onTouchEvent(MotionEvent event) {
if (some condition){
//your specific function
return false;
}
return super.onTouchEvent(event);
}
}
Is there a way to determine the current scroll offset or scroll position of a GridView?
View.getScrollY() // Underlying var is not updated during a scroll.
I have tried setting an OnScrollListener but the onScroll callback is not fine grained enough for my purposes.
Here is the how I'm attempting to determine the scroll offset using an OnScrollListener.
private int getScrollY() {
int top = 0;
if (mGridView.getChildCount() > 0) {
final View firstView = mGridView.getChildAt(0);
top = firstView.getTop();
}
return top;
}
The issue with this code is that the returned y offset is inaccurate when scrolling upwards; the top view is recycled and hence, the y offset seems to jump;
Is there a nice way of calculating the scroll offset of a GridView? I can't seem to find a good solution.
Use this.
public class CustomGridView extends GridView {
public CustomGridView(Context context) {
super(context);
}
public CustomGridView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public CustomGridView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
/* ADD THIS */
#Override
public int computeVerticalScrollOffset() {
return super.computeVerticalScrollOffset();
}
}
Returns an int value when called
You can use GridView.getFirstVisiblePosition().
http://developer.android.com/reference/android/widget/AdapterView.html#getFirstVisiblePosition()
I have a dialog box that contains a Scrollview, which contains a layout with two TimePickers.
The timepickers are the newer style ones, what's in ICS.
The problem is that they seem to fight for focus when you change the time by dragging the wheel, or flicking it. It will change the time just a little, and then the layout will scroll instead.
Any ideas?
Thanks in advance.
I had the same problem when using the Holo theme, and here is where I found the solution: https://groups.google.com/forum/?fromgroups#!topic/android-developers/FkSfJI6dH8w
You must implement your custom DatePicker or TimePicker and override the following method:
#Override
public boolean onInterceptTouchEvent(MotionEvent ev)
{
if (ev.getActionMasked() == MotionEvent.ACTION_DOWN)
{
ViewParent p = getParent();
if (p != null)
p.requestDisallowInterceptTouchEvent(true);
}
return false;
}
Because the link from Klemens Zleptnig is broken, here is a complete example. This fix helps with the scroll of a TabLayout too btw. I excluded the area around the big numbers in the top of the TimePicker because they don't need the scroll event anyway.
xml:
<com.name.app.MyTimePicker
android:id="#+id/timePicker"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
.../>
java:
public class MyTimePicker extends TimePicker {
public MyTimePicker(Context context) {
super(context);
}
//This is the important constructor
public MyTimePicker(Context context, AttributeSet attrs) {
super(context, attrs);
}
public MyTimePicker(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
public MyTimePicker(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
}
#Override
public boolean onInterceptTouchEvent(MotionEvent ev)
{
if (ev.getActionMasked() == MotionEvent.ACTION_DOWN) {
//Excluding the top of the view
if(ev.getY() < getHeight()/3.3F)
return false;
ViewParent p = getParent();
if (p != null)
p.requestDisallowInterceptTouchEvent(true);
}
return false;
}
}