Hi all, does anyone know a good tutorial that expl...
# android
d
Hi all, does anyone know a good tutorial that explains how a view can be created programmatically. I have managed to create a view (EditText) however, I cannot set the layout_height or layout_width to anything other than MATCH_PARENT or WRAP_CONTENT. Also, I would like to add a scrollbar with minLines and maxLines. However, none of them appears to work even though they work in the XML resource file.
stackoverflow 4
a
Try setting these attributes in edittext xml
Copy code
android:minLines="2"
Copy code
android:maxLines="3"
Copy code
android:scrollbars="vertical"
Copy code
android:inputType="textMultiLine"
Detailed explaination 🙂 <!-- android:minLines Makes the TextView be at least this many lines tall. When used on an editable text, the inputType attribute's value must be combined with the textMultiLine flag for the minLines attribute to apply. android:maxLines Makes the TextView be at most this many lines tall. When used on an editable text, the inputType attribute's value must be combined with the textMultiLine flag for the maxLines attribute to apply. android:scrollbars Defines which scrollbars should be displayed on scrolling or not. Must be one or more (separated by '|') of the following constant values. none : No scrollbar is displayed. horizontal : Displays horizontal scrollbar only. vertical : Displays vertical scrollbar only. android:inputType The type of data being placed in a text field, used to help an input method decide how to let the user enter text. textMultiLine : Can be combined with text and its variations to allow multiple lines of text in the field. If this flag is not set, the text field will be constrained to a single line. Corresponds to _TYPE_TEXT_FLAG_MULTI_LINE._ --> <!-- android:minLines="2" It will initially display minimum 2 lines on EditText. android:maxLines="3" It will display maximum 3 lines on App interface. android:scrollbars="vertical" It will display a vertical scrollbar if the EditText text exceed 3 lines (android:maxLines). -->
d
Thanks Aness, would you know how this could be done programmatically in Kotlin. I tried to add factDeleteEditTest.minLines = 3 and your maxLines, scrollbars etc ... but they do not seem to work.
Copy code
val factDeleteEditText = EditText(this)
            factDeleteEditText.layoutParams = LinearLayout.LayoutParams(
                LinearLayout.LayoutParams.WRAP_CONTENT,
                LinearLayout.LayoutParams.WRAP_CONTENT
            ).apply { gravity = Gravity.CENTER }
            factDeleteEditText.setHint(R.string.enter_fact)
            factDeleteEditText.setPadding(20, 20, 20, 20)
            binding.section3.addView(factDeleteEditText)
a
I have an appcompact edittext in my xml and its working fine if I set min/max lines through code. I have my input type set in xml. Also I would prefer creating edittext through xml.