Is there a way to set the lines of an EditText in Java? Example
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:lines="5"
android:gravity="top|left"
android:inputType="textMultiLine"
android:scrollHorizontally="false"
/>
So far I have this
EditText et = new EditText(getActivity());
et.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT));
obviously I need still need to set
android:lines="5"
android:gravity="top|left"
android:inputType="textMultiLine"
android:scrollHorizontally="false" <-- may not be important, to be decided
Setting an Id is not important in this scenario, however making it appear as an EditText "Box" or "Area" is. I searched quite a bit for some information on this and found nothing. Either I searched for the wrong terms or this isn't possible. Any help is appreciated.
Not sure if this is what you are after. But you can set the attributes in Java as follows...
android:lines="5" as setLines(5);
android:gravity="top|left" as setGravity(Gravity.TOP | Gravity.LEFT)
android:inputType="textMultiLine" as setInputType(InputType.TYPE_TEXT_FLAG_MULTI_LINE)
android:scrollHorizontally="false" as setHorizontallyScrolling(false)
See TextView docs
Is there a way to set the lines of an EditText in Java?
Yes there is. Try setLines(int lines).
Gravity: setGravity(int gravity)
InputType: setRawInputType (int type)
Related
I have a TextView in my android app that I want to display all the text on a single line. Right now, it is displayed like this:
this is som
e text
When I set setSingleLine(), I get this:
this is som e text
(notice the space within some)
When I use setMaxLines(1), I get this:
this is som
What I want is this:
this is some text
How can I force the text to display fully on a single line?
You can try this on your textview's xml:
android:maxLines="1"
You can make scroll text using following code if you want.
<TextView
android:id="#+id/txtBigLongTitle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ellipsize="marquee"
android:marqueeRepeatLimit="marquee_forever"
android:scrollHorizontally="true"
android:singleLine="true"
android:textAlignment="center"
android:textSize="18sp"
android:textStyle="bold"
tools:text="long text goes here......................"/>
The answers above provide a solution to my issue.
The problem was the previous programmer had implemented logic which automatically started a new line after 20 characters.'
Thanks for your help everyone!
I am having some problems to underline an EditText in AndroidStudio.
This is what I am looking for (this is just a photo, not my real text):
But I do not really know any property to do that.
My code right now is really simple. Just the "normal" one:
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textPersonName"
android:ems="10"
android:layout_margin="16dp"
android:id="#+id/tx_titulo"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:text="#string/pt_titulo"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:allowUndo="true"
/>
I do not want to underline just the text (in this case "TÃtulo"), but the whole PlainText
Does anybody know it?
Greets.
It is default, but try to check your styles. Maybe there is a style that will override the style in the whole application. If there is, just remove it
To do this programatically, you could find an answer in this post.
To set the underline color:
editText.getBackground().setColorFilter(color, PorterDuff.Mode.SRC_IN);
To remove the underline color:
editText.getBackground().clearColorFilter();
To do it from the XML, you could add background property to do the same thing in your layout.xml with any drawable resource as a background by adding:
android:background="#drawable/mydrawable"
If you have a couple of EditText with the same style configuration, to avoid setting the attribute to each EditText, you could override EditText's default attributes, defining a custom style like it is done here.
I have the following EditText where I have set maximum character limit to 10, but somehow it is not working. It accepts more than 10 characters.
<EditText
android:id="#+id/txtName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:gravity="left|center"
android:hint="Enter Full Name *"
android:maxLength="10"
android:padding="5dip" />
Please helps, thanks in advance.
Make sure that you don't have any InputFilter set to the EditText by code.
You should comment this line of code.
editText.setFilters(new InputFilter[] { filter });
You can try with this. This is the simple hack around this problem if you are setting it from xml.
Internally if your suggestion is switched on then as per dictionary its not able to judge the word character length. So If it has been switched off then maxlength will work and Android will understand it as own written word,where no dictionary should be applicable.
android:inputType="textNoSuggestions|textVisiblePassword"
android:maxLength="10"
Okay, so the thing is that I'm trying to display text but it won't let me. the code is :
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/app_name"
android:textAlignment="center"
android:textColor="#000000"
android:textColorLink="#android:color/black"
android:textStyle="normal" />
What have I done wrong? (the text won't appear on screen)
At your string resources app_name might be empty or null.
There is another view that prevents your TextView to be displayed at your screen, take a look at your layout file.
The value for TextView modified to a null/empty value in your code.
Try this
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:gravity="center"
android:text="Flights"
android:textColor="#android:color/white" />
Your code is correct as it is working for me. The probable mistake you have done is that your String resource variable "app_name" is empty or you can provide the full xml code for us to have a better look at this problem.
Is it possible to change the view appearance order programmaticaly?
Example.
<EditText
android:id="#+id/edit_txt_address_location_street"
android:layout_width="0dip"
android:layout_height="36dp"
android:layout_marginBottom="4dp"
android:layout_marginRight="4dp"
android:layout_marginTop="8dp"
android:layout_weight="8"
android:background="#drawable/shape_edit_text"
android:inputType="textNoSuggestions"
android:textColor="#color/black"
android:textCursorDrawable="#null" />
<EditText
android:id="#+id/edit_txt_address_location_street_nr"
android:layout_width="0dip"
android:layout_height="36dp"
android:layout_marginBottom="4dp"
android:layout_marginLeft="4dp"
android:layout_marginRight="4dp"
android:layout_marginTop="8dp"
android:layout_weight="2"
android:background="#drawable/shape_edit_text"
android:inputType="textNoSuggestions"
android:textColor="#color/black"
android:textCursorDrawable="#null" />
The xml about show the normal appearance order.
1. edit_txt_address_location_street
2. edit_txt_address_location_street_nr
For some conditions i muss invert the appearance order programmaticaly.
E.g.:
1. edit_txt_address_location_street_nr
2. edit_txt_address_location_street
Most, if not all, of the xml commands have their corresponding programmatic calls. Assuming what you mean "change" is adding or removing a view after it is inflated with xml, then the answer is yes, but it is does not mean you should.
If you want to add/remove views dynamically , you would probably just better off creating the whole layout dynamically. Trust me it is not that hard compared to xml layout once you know how. I've read some where that there could be potentially some problems if you mix both xml and dynamic layout, I can't prove that statement, but what I have done is to inflate an empty linearlayout with xml, then dynamically add all the views I want.
EditText location_street = new EditText(this);
location_street.setBackgroundResource(R.drawable.shape_edit_text);
EditText location_street_nr = new EditText(this);
location_street_nr.setBackgroundResource(R.drawable.shape_edit_text);
LinearLayout ll = (LinearLayout)findViewById(R.id.textlayout);
LayoutParams lp = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
ll.addView(location_street, lp);
ll.addView(location_street_nr, lp);