Last active
October 31, 2022 08:46
-
-
Save hilfritz/af0f54bc8a38503a1626ad60946a4ec1 to your computer and use it in GitHub Desktop.
EditTextViewUtil - util to make edittext look like textview
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import android.content.Context | |
import android.graphics.drawable.Drawable | |
import android.graphics.drawable.ShapeDrawable | |
import android.graphics.drawable.shapes.RectShape | |
import android.support.v4.content.ContextCompat | |
import android.support.v4.widget.NestedScrollView | |
import android.text.method.ScrollingMovementMethod | |
import android.view.Gravity | |
import android.view.MotionEvent | |
import android.view.View | |
import android.widget.EditText | |
import android.widget.ScrollView | |
import android.widget.TextView | |
/** | |
* Created by Hilfritz Camallere on 28/5/19. | |
* to use: EditTextViewUtil.Companion.makeEditTextLookLikeTextView(editText); | |
*/ | |
class EditTextViewUtil { | |
companion object { | |
fun makeEditTextLookLikeTextView(edt: EditText, maxLines: Int =4, showScrollBars: Boolean = true){ | |
edt.setMaxLines(maxLines) | |
edt.setLines(maxLines) | |
edt.setVerticalScrollBarEnabled(true) | |
edt.setMovementMethod(ScrollingMovementMethod()) | |
edt.isFocusableInTouchMode = false | |
edt.setGravity(Gravity.LEFT or Gravity.TOP) | |
if (showScrollBars) { | |
addScrollbarToEditText(edt) | |
} | |
edt.background = null | |
} | |
/** | |
* Edittext needs to have this: android:scrollbars="vertical" | |
*/ | |
fun addScrollbarToEditText(edt:EditText){ | |
try { | |
edt.scrollBarStyle = EditText.SCROLLBARS_INSIDE_INSET | |
edt.overScrollMode = EditText.OVER_SCROLL_ALWAYS | |
edt.isVerticalScrollBarEnabled = true | |
edt.isHorizontalScrollBarEnabled = true | |
edt.scrollBarFadeDuration = 0 | |
//changeScrollbarColor(edt, colorId, edt.context) | |
}catch (e:Exception){ | |
e.printStackTrace() | |
} | |
} | |
fun changeScrollbarColor(Scroll: View, color: Int, cxt: Context) { | |
//https://stackoverflow.com/questions/9713909/how-to-set-scrollbar-thumb-programmatically-in-android | |
try { | |
val mScrollCacheField = View::class.java.getDeclaredField("mScrollCache") | |
mScrollCacheField.isAccessible = true | |
val mScrollCache = mScrollCacheField.get(Scroll) | |
val scrollBarField = mScrollCache.javaClass.getDeclaredField("scrollBar") | |
scrollBarField.isAccessible = true | |
val scrollBar = scrollBarField.get(mScrollCache) | |
val method = scrollBar.javaClass.getDeclaredMethod("setVerticalThumbDrawable", Drawable::class.java) | |
method.isAccessible = true | |
val layers = arrayOfNulls<Drawable>(1) | |
val sd1 = ShapeDrawable(RectShape()) | |
sd1.getPaint().setColor(ContextCompat.getColor(cxt, color)) | |
//sd1.setIntrinsicWidth(Math.round(cxt.getResources().getDimension(R.dimen.margin_a))) | |
layers[0] = sd1 | |
method.invoke(scrollBar, layers) | |
} catch (e: Exception) { | |
e.printStackTrace() | |
} | |
} | |
fun makeEditTextOverrideParentScrollView(edt: EditText){ | |
edt.setOnTouchListener(object : View.OnTouchListener { | |
override fun onTouch(view: View, event: MotionEvent): Boolean { | |
// TODO Auto-generated method stub | |
if (view.id == edt.id) { | |
view.parent.requestDisallowInterceptTouchEvent(true) | |
when (event.action and MotionEvent.ACTION_MASK) { | |
MotionEvent.ACTION_UP -> view.parent.requestDisallowInterceptTouchEvent(false) | |
} | |
} | |
return false | |
} | |
}) | |
} | |
fun enableTextViewScrollInsideScrollView(editText: TextView, scrollView: NestedScrollView) { | |
editText.setOnTouchListener { view, motionEvent -> | |
scrollView.requestDisallowInterceptTouchEvent(true) | |
false | |
} | |
} | |
fun enableTextViewScrollInsideScrollView(editText: EditText, scrollView: NestedScrollView) { | |
editText.setOnTouchListener { view, motionEvent -> | |
scrollView.requestDisallowInterceptTouchEvent(true) | |
false | |
} | |
} | |
fun enableTextViewScrollInsideScrollView(editText: TextView, scrollView: ScrollView) { | |
editText.setOnTouchListener { view, motionEvent -> | |
scrollView.requestDisallowInterceptTouchEvent(true) | |
false | |
} | |
} | |
fun enableTextViewScrollInsideScrollView(editText: EditText, scrollView: ScrollView) { | |
editText.setOnTouchListener { view, motionEvent -> | |
scrollView.requestDisallowInterceptTouchEvent(true) | |
false | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment