I have a TextView with fixed width as follows:
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:textSize="12sp"
tools:text="Health Department"
android:layout_marginTop="4dp"
android:maxLines="2"
/>
... But it appears like:
|Health Dep-|
| artment |
... While I want it to be:
| Health |
|Department|
What XML attribute can I use to do this?
From api 23 you can choose the break strategy and that should fix your issue.
By default android for TextView use BREAK_STRATEGY_HIGH_QUALITY and that is causing the words breaking using hyphens.
If you want to avoid that, you can set the break strategy to BREAK_STRATEGY_SIMPLE.
You can find more information in the api doc.
I hope that helps you.
Try using this in TextView, this works for me
android:breakStrategy="simple"
This is a new thing in android 6 version(Marshmallow).
Try adding the code below to your TextView in xml layout
android:hyphenationFrequency="none"
I wanted to apply the break strategy mentioned in this answer for all my textviews throughout the app and added a new v23/styles.xml containing:
<resources xmlns:android="http://schemas.android.com/apk/res/android">
<style name="Base.Widget.TextView" parent="android:Widget.TextView">
<item name="android:breakStrategy">simple</item>
</style>
</resources>
And I put the following in my themes.xml
<item name="android:textViewStyle">#style/Base.Widget.TextView</item>
This answer is for those wanting to prevent text being broken into individual words before truncation to fit the available space.
(This was how I interpreted the actual question before reading further.) Suppose:
textView.setText("word1 word2") // User sees 'word1 word2'
textView.setText("word1 word2islonger ") // User sees only 'word1'
In the latter case, if you are wondering why you see only 'word1' instead of (perhaps) 'word1 word2islo' then the peculiarly-named ellipsize attribute is probably the culprit.
In your TextView XML, for example, try specifying
android:ellipsize="marquee"
You will then at least see as much of the intended text as will actually fit the available space.
Related
I'm setting the text on a text view to a String that is 14,000,000 characters long.
This takes anywere from 1 to 5 minutes to load on screen(depending on device). Any Ideas how I can speed this up?
Here is the relevant code:
responseView.text = requestResponsePair.second
.....................
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
tools:context="com.salesrabbit.android.sales.universal.features.lumberjack.TextDisplayFragment">
<TextView
android:id="#+id/request_label"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/request_label_string"
android:textSize="20sp"/>
<TextView
android:id="#+id/text_view_request"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<TextView
android:id="#+id/response_label"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/response_label_string"
android:textSize="20sp"/>
<TextView
android:id="#+id/text_view_response"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
Approach One
Recently, Google announced the PrecomputedText which is available for SDK level 28 and above. Along with that, the PrecomputedTextCompat has been released which is available for SDK 21 to 27. I haven't try it yet but there is a Medium article from Chris Craik called "Prefetch Text Layout in RecyclerView" which explains how to make use of it and how much workload gets off the UI thread without missing any frames.
Snippet from article
Precomputing
val precomputedText : Spannable = PrecomputedTextCompat.create(expensiveText, params)
Set precomputed text to TextView
TextViewCompat.setPrecomputedText(myTextView, precomputedText)
Make sure to read the docs, as some posting to a background thread is required.
Approach Two
Split your text into small chunks or paragraphs. Put it into a list and use a simple RecyclerView to show each item in the list. It's simple and you will get for free good performance as RecyclerView will render and process the items that are visible to the user.
Also, make sure to listen to the Episode 90: Spanspanspanspan of the Android Developers Backstage podcast, it is hosted by developers of the Android Engineering team.
Use PrecomputedText
Using PrecomputedText is one of the best approaches here. Read an answer by Giorgos Neokleous.
Turn off hyphenation
Turn off hyphenation also helps to improve performance significantly. You can do that by setting android:hyphenationFrequency="none" for a specific TextView or overriding that property for the whole application:
<!-- res/values/styles.xml -->
<style name="AppTheme" parent="Theme.AppCompat.Light">
<!-- ... -->
<item name="android:textViewStyle">#style/AppTextView</item>
</style>
<style name="AppTextView" parent="BaseAppTextView">
<item name="android:hyphenationFrequency">none</item>
</style>
<style name="BaseAppTextView" parent="android:Widget.TextView"/>
<!-- res/values-21/styles.xml -->
<style name="BaseAppTextView" parent="android:Widget.Material.TextView"/>
Also take a look at few links:
Use Android Text Like a Pro presentation by Siyamed Sinir from the Android Dev Summit '18 where he describes both approaches with some details.
Prefetch Text Layout in RecyclerView article in Android Developers blog that can help make your RecyclerView faster.
That text is far too long. You should divide them by \n (divide into paragraphs) and put each paragraph into a textview in a recyclerview.
Tradeoff is that users cannot select all of the text or in-between paragraphs but i believe at the end it will be a better UX.
Then you should apply Precomputedtext.
Yesterday I was looking for sliders in Android and found this website with the Google search: https://material.io/guidelines/components/sliders.html#sliders-discrete-slider
I know that I can use a SeekBar in Android to implement sliders. However, Google seems to have very nice examples of discrete sliders but I cannot find any code examples.
I already implemented a normal SeekBar that is looking like this:
How can I make it look like this?
(Difference: When I move my slider, there is no big drop that shows the current value)
I think I might just have missed the code documentation for these design guidelines. Does anyone know where to find it? Or is the design difference because I got Android 5.0.2 on my phone?
sadly google just provided how it should look like, but there seems to be no class provided by the android support libraries :(
but for now you can try this library: https://github.com/AnderWeb/discreteSeekBar
or this for even more material elements:
https://github.com/navasmdc/MaterialDesignLibrary
hopefully google adds this in later releases...
Now you can use the official Slider in Material Components Library.
Just use something like:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:clipChildren="false"
android:clipToPadding="false">
<com.google.android.material.slider.Slider
android:id="#+id/slider"
android:layout_gravity="center"
app:labelBehavior="withinBounds"
android:value="60"
android:valueFrom="0"
android:valueTo="100"
..../>
</LinearLayout>
NOTE: it requires the version 1.2.0 (currently 1.2.0-beta01) of the library.
If you want to customize the tooltip shape with a circle marker instead of the default label you can use the labelStyle attribute:
<com.google.android.material.slider.Slider
app:labelStyle="#style/tooltip"
with:
<style name="tooltip" parent="Widget.MaterialComponents.Tooltip">
<item name="shapeAppearanceOverlay">#style/tooltipShOverylay</item>
<item name="backgroundTint">#color/....</item>
</style>
<style name="tooltipShOverylay">
<item name="cornerSize">50%</item>
</style>
AnderWeb's discrete seekbar has a few problems. And for the other one(MDL), you may not want to compile the entire material design library just for a descrete seekbar/slider.
But there is a nice github repository you may find useful: BubbleSeekBar
I tried to find a nice solution for the same problem. In my case I was also trying to find a seekbar that will always show the bubble. After two hours of research I found BubbleSeekBar library, which provides every single attribute you can think of. It was hard to find this library since the readme file doesn't even use the word "material".
EDIT:
After six months, now there is another good Discrete Seek Bar repo that you may find useful. IndicatorSeekBar seems to have everything covered, except a few Issues. You can check it here.
Continuous slider
Continuous sliders allow users to make meaningful selections that don’t require a specific value.
<com.google.android.material.slider.Slider
android:id="#+id/slider"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="32dp"
android:layout_gravity="center"
android:value="8.09"
android:valueFrom="0.0"
android:valueTo="11.0" />
Discrete slider
Discrete sliders display a numeric value label upon pressing the thumb, which allows a user to input an exact value.
<com.google.android.material.slider.RangeSlider
android:id="#+id/range_slider"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="32dp"
android:layout_gravity="center"
app:values="#array/initial_slider_values"
android:valueFrom="0.0"
android:valueTo="10.0"
/>
<!--array.xml-->
<array name="initial_slider_values">
<item>4.0</item>
<item>8.0</item>
</array>
So I would like to see my layout preview with the fields filled with something like default placeholders but if I use bindings the settext attribute is already used and the fields are showing empty since there is no info from the models yet.
<TextView
android:id="#+id/tv_user_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_margin="5dp"
android:gravity="center"
**android:text="#{showBlueportSpotViewModel.name}"**
android:textAllCaps="true"
android:textSize="20sp"
android:textStyle="bold"/>
I tried this:
android:text="#{showBlueportSpotViewModel.name ?? #string/blueport_placeholder_name}"
but I still see the view empty.
Do you guys any workaround? I guess once a workaround is found, it can be used to ImageView and src for example and etc..
Thank you!
You can use the tools attribute to define properties that will appear in the layout preview but will not appear when you run the app.
Add the following to your root view:
xmlns:tools="http://schemas.android.com/tools"
Then use the tools attribute to define text that will only appear in the layout preview:
tools:text="placeholder text"
The tools attribute is very useful when mocking up views in the editor. All of the tools attributes are stripped when the app is packaged. More information here: http://tools.android.com/tech-docs/tools-attributes
I found a workaround
I added
xmlns:bind="http://schemas.android.com/apk/res/android"
to the layout
and just having duplicated declarations in the view like:
android:text="#string/blueport_placeholder_name"
bind:text="#{showBlueportSpotViewModel.name}"
or
android:src="#{showBlueportSpotViewModel.blueportImageDrawable}"
bind:src="#drawable/android_menu_header"
I don't really know if this has secondary wrong consequences so I won't accept this answer until somebody can comment and say if it is okay.. Thanks!
I have an EditText input in Android 4.0 and the Cursor is not showing inside it.
What can make the cursor not appear in the input field?
Make android:cursorVisible="true"
and
If you have used android:textColor then set the android:textCursorDrawable attribute to #null.
Happy coding ;)
I had a similar problem but it was because the cursor is actually white and I had a white background. I needed to be able to change the cursor to black in code somehow and used this approach.
I created a layout resource called textbox.axml which contained this
<?xml version="1.0" encoding="utf-8"?>
<EditText xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="This is a template"
android:background="#ffffff"
android:textColor="#000000"
android:cursorVisible="true"
android:textCursorDrawable="#null" />
I then applied this layout in code (C# because I am using Xamarin) thus
EditText txtCompletionDate = (EditText)LayoutInflater.Inflate(Resource.Layout.textbox, null);
but it is similar in Java.
Add this line for your edit text in the xml file.
android:textCursorDrawable="#null"
I happened quite same problem - cursor was showing up only after user types some characters. I tried solutions listed here, but without any success on my device. What actually work for me, is setting blank text to my edittext:
EditText editText = findViewById(R.id.edit_text);
editText.setText("");
This "fakes" the user input, and cursor appears.
My issue was that I was using the AppCompat theme, but I had some custom view classes that extended EditText that needed to extend AppCompatEditText in order for the AppCompat style to be applied correctly.
Add this line for your edit text in the xml file.
android:cursorVisible="true"
Just adding my own personal fix to anyone it might help. I had tried everything here but forcing android:background="#null" was causing a very tiny cursor only at the end of my right aligned TextEdit (it was right working fine elsewhere).
Simply adding android:padding="1dp" in my TextEdit solved the issue.
As mentioned above, here's the actual line
android:textCursorDrawable="#null"
<EditText
android:textCursorDrawable="#null"
android:imeOptions="actionNext"
android:id="#+id/edSMobile"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#drawable/edit_corner"
android:inputType="phone" />
I found what was causing it to happen to me.
You need to inherit it from the application's theme. I'm not sure what the line item needs to be exactly, but android:Theme has it so inheriting that will do that trick.
Using the default AppBaseTheme will work (it has android:Theme.Light as it's parent).
To use AppBaseTheme put android:theme="#style/AppBaseTheme" into your application tag in the manifest. You can also use a custom style and multiple levels of inheritance so long as one of them has parent="android:Theme" in the style tag.Like I said it may be possible to have it without that, just using certain line item(s) but I don't know what those would be.
If you don't need a custom theme you can just use
android:theme="#android:style/Theme"
In My case the cursor is visible if user language is English but if he change his language to Arabic then its not visible.
To fix this I have created on custom drawable for cursor.
Cursur shap at drawable/black_cursor.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle" >
<solid android:color="#a8a8a8"/><!-- This is the exact color of android edit text Hint -->
<size android:width="1dp" />
</shape>
Edit Text:
<EditText
android:id="#+id/user_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20sp"
android:textCursorDrawable="#drawable/black_cursor"
/>
If the EditText has a Background drawable with a border, the cursor is displaying in the border and appears to be invisible. To rectify the problem set padding in the EditText to a small amount e.g. 5dp
If you want to show the cursor then just do
android:textCursorDrawable="#null".
I like the title bar style from the Android preference category.
In my Activity (not a PreferenceActivity) How can I use the same style?
Since I just spent the last few hours trying to answer this old question, I'll do it here for anyone else.
It turns out the resource the preference category style is using is listSeparatorTextViewStyle.
You use it like this:
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Hello, World"
style="?android:attr/listSeparatorTextViewStyle"/>
Using style="?android:attr/preferenceCategoryStyle" didn't work.
The main layout is most likely a ScrollView with a LinearLayout. As for the individual layout, I believe (just guessing after looking at the documentation) that you can use the various attributes in android.R.attr - look here: http://developer.android.com/reference/android/R.attr.html. There are attributes like preferenceCategoryStyle, preferenceStyle, etc. You can apply any style to any of your views.