I want to use a list separator in my .xml file, but everytime i do that, the text style changes, it becomes bigger and bolder.
Below are links of the screenshots. The picture on the left is what shows up from my code, and on the right is what i really want.
Picture for output
I only want to know how to stop the text from getting bigger and bolder, and at the same time how to put a horizontal line below the <TextView>.
Below is the link for the code
Code
Building from #John Kalimeris' answer, i have a better way, in which no foriegn resource will be needed. This should work for you.
<TextView
android:layout_width="match_parent"
android:layout_height="5dp"
style="?android:attr/listSeparatorTextViewStyle"
android:layout_marginBottom="3dp"/>
for the horizontal line you can use this code:
<TextView
android:id="#+id/tevi_divide"
android:layout_width="match_parent"
android:layout_height="0.5dp"
android:layout_margin="3dp"
android:background="#color/detail_line_divider"
android:text="#string/str_empty" />
you put this below your textview. You change the values of height and margin according to your needs.
I'm curious about the difference setText() and append() are creating. I'm writing a very basic editor with line numbers. I have a TextView to hold line numbers on the left, paired with an EditText on the right to hold the data. Here's the XML:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="top">
<TextView
android:id="#+id/line_numbers"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="0dip"
android:gravity="top"
android:textSize="14sp"
android:textColor="#000000"
android:typeface="monospace"
android:paddingLeft="0dp"/>
<EditText
android:id="#+id/editor"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:inputType="text|textMultiLine|textNoSuggestions"
android:imeOptions="actionNone"
android:gravity="top"
android:textSize="14sp"
android:textColor="#000000"
android:typeface="monospace"/>
</LinearLayout>
Ignoring some of the other things I'm doing, the most curious thing I came across was the extra spacing that showed up when I used append() (assuming things have been initialized and all that).
This below, in combination with the XML, sets a flush border between the TextView and EditText.
theEditor = (EditText) findViewById(R.id.editor);
lineNumbers = (TextView) findViewById(R.id.line_numbers);
theLineCount = theEditor.getLineCount();
lineNumbers.setText(String.valueOf(theLineCount)+"\n");
Change the last line to this, though, and suddenly each line in the TextView has padding on the right before the EditText.
lineNumbers.append(String.valueOf(theLineCount)+"\n");
It's not the end of the world. but I was curious what was causing this behavior. Since I'm new to the language, the only thing I could think of was maybe, when append throws the Editable on there, it adds the padding. If I can get an answer, I get to replace all of these nasty lines with simpler appends:
lineNumbers.setText(lineNumbers.getText().toString()+String.valueOf(newLineCount)+"\n");
lineNumbers.setText("It is test,");
//Here lineNumbers have It is test
lineNumbers will have "It is test,". After that, if you use setText again, text will completely change
lineNumbers.setText("It is second test,");
//Here you'll lose first text and lineNumbers text will be "It is
second test,"
After that, if you use append, lets see what will happen..
lineNumbers.append("It is third test,");
// Here you will not lose lineNumbers text.. It will be like this
"It is second test,It is third test"
setText(): Destroys the buffer content by filling the text to be set.
append(): Adds a text to a buffer and then prints the result.
Example: example.setText("Hello"); would print Hello on the output screen. If you then execute example.append("World"); you would get HelloWorld as the output.
setText will replace the existing text with new text.
From Android doc:
Sets the text that this TextView is to display (see setText(CharSequence)) and also sets whether it
is stored in a styleable/spannable buffer and whether it is editable.
append will keep the old text and add the new one more like concatenating.
From Android Doc
Convenience method: Append the specified text to the TextView's display buffer, upgrading it to
BufferType.EDITABLE if it was not already editable.
I think changing BufferType to EDITABLE by append method caused the unexpected padding.
If you want to use append method instead of setText method and remove that padding,
you can try to remove it by using
textView.setincludeFontPadding(false)
or adding this line to your textview in your xml file
android:includeFontPadding="false"
Hope this helps.
The basic difference is that setText() replaces all the text from the existing one and append() adds your new value to existing one. Hope i helped.
<EditText
android:id="#+id/drawer_search"
android:layout_width="match_parent"
android:layout_height="?android:attr/listPreferredItemHeightSmall"
android:background="#color/white"
android:drawableLeft="#android:drawable/ic_menu_search"
android:hint="#string/drawer_search_hint"
android:imeOptions="actionSearch"
android:maxLines="1"
android:textColor="#color/dark_blue"
android:textColorHint="#color/dark_blue"
/>
So... all of the attributes work except imeOptions and maxlines. I want the text view to be only one line and the keyboard to not have a return key to go to the next line. It needs to submit/search what ever is in the text view.
So this is the text view. cropped for space.
If you press enter/return it goes to the next line(which there should only be one line).
Why isn't the textview using all of the attributes?
Is there a better way to make it so the keyboard's return button is a submit button rather than next line?
The layout file is declared like this.
View headerRoot = inflater.inflate(R.layout.drawer_header, null);
Attribute android:maxLines corresponds to the maximum height of the EditText or TextView. Use android:singleLine=true for one line input.
)
I am having a bit of a problem with my app. I am using a multiline edit text.
I want its content to have a bit of a left space (not the edittext, but the text in it), because of a drawable i set as backgroud for the edittext.
Just like padding works for layout alignment, is there anything to align text?
Try:
<EditText
.....
android:paddingLeft="50dp"
.....
/>
If you want to have a padding,
You can simply use android:paddingLeft="20dp"
or android:padding="20dp" for your EditText in your XML.(i don't know exactly what you want, just try).
If you want to align your text,
Try to use Android:Gravity in order to align your text in your EditText.
Here is the code : android:gravity="right" (in order to have right align for your text)
You can find more information about what you can do with this here : Android Gravity
Does any one know how to wrap text in TextView in Android platform. i.e if the text in textview exceed the screen length it should be displayed in the second line.
I have searched and tried the following:
android:scrollHorizontally="false",
android:inputType="textMultiLine",
android:singleLine="false"
But none work..
Can anyone suggest how can I do it.
Constraint Layout
<TextView
android:id="#+id/some_textview"
android:layout_width="0dp"
android:layout_height="wrap_content"
app:layout_constraintLeft_toLeftOf="#id/textview_above"
app:layout_constraintRight_toLeftOf="#id/button_to_right"/>
Ensure your layout width is zero
left / right constraints are defined
layout height of wrap_content allows expansion up/down.
Set android:maxLines="2" to prevent vertical expansion (2 is just an e.g.)
Ellipses are prob. a good idea with max lines android:ellipsize="end"
0dp width allows left/right constraints to determine how wide your widget is.
Setting left/right constraints sets the actual width of your widget, within which your text will wrap.
Constraint Layout docs
For me this issue only occurred on Android < 4.0
The combination of parameters I used were:
android:layout_weight="1"
android:ellipsize="none"
android:maxLines="100"
android:scrollHorizontally="false"
The maxLines count seemed to be the random final piece that made my TextView wrap.
For the case where the TextView is inside a TableLayout, the solution is to set android:shrinkColumns="1" on the TableLayout. (Replace 1 with the column number the TextView you want to wrap is in. (0-indexed))
AFAICT, no other attributes are needed on the TextView.
For other cases, see the other answers here.
FWIW, I had initially gotten it to sort of work with
<TextView
android:id="#+id/inventory_text"
android:layout_width="fill_parent"
android:layout_weight="1"
android:width="0dp"
but that resulted in some extra empty space at the bottom of the Dialog it was all in.
Use app:breakStrategy="simple" in AppCompatTextView, it will control over paragraph layout.
It has three constant values
balanced
high_quality
simple
Designing in your TextView xml
<android.support.v7.widget.AppCompatTextView
android:id="#+id/textquestion"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:scrollHorizontally="false"
android:text="Your Question Display Hear....Your Question Display Hear....Your Question Display Hear....Your Question Display Hear...."
android:textColor="#android:color/black"
android:textSize="20sp"
android:textStyle="bold"
app:breakStrategy="simple" />
If your current minimum api level is 23 or more then in Coding
yourtextview.setBreakStrategy(Layout.BREAK_STRATEGY_SIMPLE);
For more refrence refer this BreakStrategy
You must use 2 parameters :
android:ellipsize="none" : the text is not cut on textview width
android:scrollHorizontally="false" the text wraps on as many lines as necessary
This should fix your problem: android:layout_weight="1".
By setting android:maxEms to a given value together with android:layout_weight="1" will cause the TextView to wrap once it reaches the given length of the ems.
OK guys the truth is somewhere in the middle cause you have to see the issue from the parent's view and child's. The solution below works ONLY when spinner mode = dialog regardless of Android version (no problem there.. tested it in VD and DesireS with Android =>2.2) :
.Set you spinner's(the parent) mode like :
android:spinnerMode="dialog"
Set the textview's(child custom view) properties to :
android:layout_weight="1"
android:ellipsize="none"
android:maxLines="100"
android:scrollHorizontally="false"
I hope this works for you also.
In Android Studio 2.2.3 under the inputType property there is a property called textMultiLine. Selecting this option sorted out a similar problem for me. I hope that helps.
Just was working on a TextView inside a layout inside a RecyclerView. I had text getting cut off, ex, for Read this message, I saw: Read this. I tried setting android:maxLines="2" on the TextView, but nothing changed. However, android:lines="2" resulted in Read this on first line and message on the 2nd.
Try #Guykun's approach
android:layout_weight="1"
android:ellipsize="none"
android:maxLines="100"
android:scrollHorizontally="false"
Also, make sure that parents width is not set to wrap content. This is the thing that I was missing.
I had the same problem. Following change made it work -
android:layout_width="wrap_content"
The ellipsis, maxLines, or layout_weight - all didn't make any difference.
Note - The parent width is also set as wrap_content.
All you have to do is to set your textview width.
android:layout_width="60dp"
you can change the width to your choice. Just type long sentence to check if it working like this
android:text="i want to be among world class software engineer"
I am using Android 2.2 and my textview will automatically goto the next line if it exceeds the screen.
If you would like to have the text goto the next line before the end of the screen, just add in (just put in your own dp value). This will be useful if you have a picture on the right of the text.
android:layout_marginRight="52dp"
Strange enough - I created my TextView in Code and it wrapped - despite me not setting anything except standard stuff - but see for yourself:
LinearLayout.LayoutParams childParams = new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT);
childParams.setMargins(5, 5, 5, 5);
Label label = new Label(this);
label.setText("This is a testing label This is a testing label This is a testing label This is a testing labelThis is a testing label This is a testing label");
label.setLayoutParams(childParams);
As you can see from the params definition I am using a LinearLayout. The class Label simply extends TextView - not doing anything there except setting the font size and the font color.
When running it in the emulator (API Level 9) it automatically wraps the text across 3 lines.
Just set layout_with to a definate size, when the text fills the maximum width it will overflow to the next line causing a wrap effect.
<TextView
android:id="#+id/segmentText"
android:layout_marginTop="10dp"
android:layout_below="#+id/segmentHeader"
android:text="You have the option to record in one go or segments(if you swap options
you will loose your current recordings)"
android:layout_width="300dp"
android:layout_height="wrap_content"/>
The trick is with the textView width, try to make it dedicated number like:
<TextView
android:layout_width="300dp"
android:layout_height="wrap_content"/>
I've tried many solutions without any result, I've tried:
android:ellipsize="none"
android:scrollHorizontally="false"
the only one thing triggred the wrap option is the dedicated width
You need to add your TextView in a ScrollView with something like this :
<ScrollView android:id="#+id/SCROLL_VIEW"
android:layout_height="150px"
android:layout_width="fill_parent">
<TextView
android:id="#+id/TEXT_VIEW"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="This text view should act as header This text view should act as header This text view should act as header This text view should act as header This text view should act as header This text view should act as header This text view should act as header" />
</ScrollView>