-
Notifications
You must be signed in to change notification settings - Fork 4
/
ViewUtils.java
93 lines (85 loc) · 3.11 KB
/
ViewUtils.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
package com.bytesizebit.androidutils;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.SpannedString;
import android.text.method.LinkMovementMethod;
import android.text.style.ClickableSpan;
import android.text.style.UnderlineSpan;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
import android.widget.TextView;
/***********
* Android Utils
* Created by Shahar Barsheshet on 07/02/2015.
* bytesizebit@gmail.com
* www.bytesizebit.com
***********/
public class ViewUtils {
/**
* Set visibility to sibling views
*
* @param view current view
* @param visibility visibility state
*/
public static void setSiblingsVisibility(View view, int visibility) {
ViewGroup parent = (ViewGroup) view.getParent();
for (int i = 0; i < parent.getChildCount(); i++) {
View child = parent.getChildAt(i);
if (child != view) {
child.setVisibility(visibility);
}
}
}
/**
* Scroll listview to bottom
*
* @param listView the listview to scroll
* @param smoothScroll should scroll smooth or snap
*/
public static void listViewScrollToBottom(final ListView listView, boolean smoothScroll) {
// due to issue with scrolling in listview doesnt reach the end of the list, this small hack is needed - calling the method twice with small delay in between
if (smoothScroll) {
listView.post(new Runnable() {
@Override
public void run() {
listView.smoothScrollToPosition(listView.getCount() - 1);
}
});
listView.postDelayed(new Runnable() {
@Override
public void run() {
listView.smoothScrollToPosition(listView.getCount() - 1);
}
}, 100);
} else {
listView.post(new Runnable() {
@Override
public void run() {
listView.setSelection(listView.getCount() - 1);
}
});
listView.postDelayed(new Runnable() {
@Override
public void run() {
listView.setSelection(listView.getCount() - 1);
}
}, 100);
}
}
/**
* Create an underline text clickable
*
* @param textView the view to handle
* @param onclick the click callback
*/
public static void makeUnderlinedTextClickable(TextView textView, ClickableSpan onclick) {
SpannedString string = (SpannedString) textView.getText();
int underlineStart = string.nextSpanTransition(0, string.length(), UnderlineSpan.class);
int underlineEnd = string.nextSpanTransition(underlineStart, string.length(), UnderlineSpan.class);
SpannableString ss = new SpannableString(string);
ss.setSpan(onclick, underlineStart, underlineEnd, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
textView.setText(ss);
textView.setMovementMethod(LinkMovementMethod.getInstance());
}
}