Creating multiline Edit view in android with coding - android

I have created an EditText object dynamically but I haven't been able to create a multi-line EditText. I have tried this:
EditText et1 = new EditText(this);
et1.setHint("Enter Your Address");
et1.setSingleLine(false);
et1.setHorizontalScrollBarEnabled(false);
et1.setInputType(android.text.InputType.TYPE_TEXT_VARIATION_POSTAL_ADDRESS);
et1.setLines(7);
Thanks.

Include this in your code:
et1.setMaxLines(maxlines);
Or you can set the specific height for the edit text.

If you want the text to wrap to the next line, add TYPE_CLASS_TEXT to the MULTI_LINE flag:
textArea.setInputType(InputType.TYPE_CLASS_TEXT|InputType.TYPE_TEXT_FLAG_MULTI_LINE);

It is the line:
et1.setInputType(android.text.InputType.TYPE_TEXT_VARIATION_POSTAL_ADDRESS);
that is the problem. Take that out.
setMaxLines doesn't matter much unless you want to set a max number of lines. You should also avoid setting the height to something specific. WRAP_CONTENT works great.
Even changing it to:
et1.setInputType(android.text.InputType.TYPE_TEXT_FLAG_MULTI_LINE);
forces it to a single line edit, which seems odd.
This doesn't work either:
et1.setInputType(android.text.InputType.TYPE_TEXT_FLAG_CAP_SENTENCES);
which is really freakin irritating. Seems like an android bug...
You also might want to set the vertical scroll on and gravity so it can scroll up and down and starts in the top left.
et1.setGravity(Gravity.TOP|Gravity.LEFT);
et1.setVerticalScrollBarEnabled(true);

What worked for me is:
et1.setSingleLine(false);
et1.setHorizontalScrollBarEnabled(false);
et1.setVerticalScrollBarEnabled(true);
et1.setMinLines(minLines);

Related

How do i make the constraintlayout wrap my textview programmatically

I've got a problem and have no idea how to fix it. I'm using a ConstraintLayout in android
I want to set my TextView to wrap_content programmatically but respect my constraints.
Now the issue is that if i set my constriantWidth to WRAP_CONTENT it does not respect the constraints it's given to it.
I've found that there is a solution in xml in it here:
Wrap_content view inside a ConstraintLayout stretches outside the screen
but in this issue no where is it described how to set the property of constrainedwidth to true programmaticly.
I've tried a few things but have not found a solution to my problem:
set.constrainWidth(textView.getId(),ConstraintSet.WRAP_CONTENT)
just wraps the content without keeping in my constriants that i've set.
I've also tried to set the constraintedWidth with the ConstraintLayout.Params but nothing happend.
And i have no clue if en how i can set constrainedWidth in my ConstraintSet.
and
set.constrainWidth(textView.getId(),ConstraintSet.MATCH_CONSTRAINT_WRAP)
Just makes my text a thin line of my text and doesn't show my text anymore.
If someone could help i would be very great full.
PS. Sorry for my english not a native speaker.
Use constrainDefaultWidth:
set.constrainDefaultHeight(textView.getId(), ConstraintSet.MATCH_CONSTRAINT_WRAP);

Android- EditText's text pushing other views down

I have an app in which the main view is a PopupWindow with a couple of ImageViews and an EditText on top of them. My problem is when I enter a very long text and the text box expands down and pushing the image views out of the view and I can't see them, and in order to see them back I need to delete some text.
What is the proper way of overcoming this?
thanks.
try to add into the XML something like that:
android:maxLines = "5"
android:scrollbars = "vertical"
Perhaps enclosing your LinearLayout of the PopupWindow in a ScrollView could help. LinearLayout does not allow for scrolling, if you are using it.
EDIT: In regards to the other answers, these will also work, but if you are intending to allow the user to enter as much text as they want, it may not be the best solution
See This question for more details
This should limit the height of the EditText, so even if the user types a long book in there, the field will keep its height limited to one line.
android:maxLines="1"
To limit the text to just one line, you can use this instead:
android:singleLine="true"

Character spacing for "1" in a textview

I'm using a Digital-7 Typeface for a pair of TextViews. The problem with the TextViews is that whenever I use the character "1" in it, the characters left of the "1" get pushed back and they lose their proper spacing. As seen here:
You can see the 9's are properly in place while the 1's get squished together. Is there any piece of code that can help me fix this or do I need a better Typeface?
As what Der Golem said, switching to a monospace Typeface fixed my issue.
Please try this property in XML, Hope it will work.
android:textScaleX="1.2"
Must be a floating point value, such as "1.2". You can change the value depending on your requirement.

EditText displays text slightly off?

I am using and EditText to display my app's EULA. The EditText is marked singleLine=false and enabled = false.
When I ev.setText='....', the text appears slightly shifted 1 and 1/2 characters to the left plus down 1 and 1/2 lines. That is, the text is not registered/displayed properly to the top left corner. Any ideas?
Why an EditText? Seems like a TextView would be the more proper vehicle to display that.
As for the text position, it sounds like you have padding (or margins, or both) specified in the XML.
The following code works:
myEtLoginEula.setEnabled(false);
myEtLoginEula.setVisibility(View.VISIBLE);
myEtLoginEula.setText(myJS.getString("EULAText").replace("<br>","\n") + "\n");
myEtLoginEula.setScroller(new Scroller(getBaseContext()));
myEtLoginEula.setVerticalScrollBarEnabled(true);
myEtLoginEula.setMovementMethod(new ScrollingMovementMethod());
I was previously setting the text, .setText(myJS...) after the .setMovementMethod. It does not like that. I had originally obtained all this months back from another SO question regarding scrolling. Seems to work now. I will revisit TextViewinstead of EditTextas well. Thanks for your patience as I am newbie to SO responding!

Increase height of TextView on runtime

I have a TextView and I want to increase its height on runtime.
I have used android:layout_height="wrap_content" but it did not gave me the desired result.
I'm using a Relative Layout.
i get text through edittext and i want to set it on textview.
when i do this it shows only one line, and when i click on edittext it get expanded and full message is shown.
Add in Oncreate method of your
Activity class,
((TextView)
findViewById(R.id.YOURTEXTVIEWIDHERE)).setHeight(IntegerValuePixels);
hard to tell without more details - but do you really need to change it on runtime? because obviously you're not happy with the height at startup.
have you thought about using android:layout_height="fill_parent", eventually in combination with the android:minHeight attribute of other components?

Categories

Resources