Android - Best way to layout some text on an activity - android

I have an activity called medical and I just want to layout some text that explains what to do in a medical emergency. It will just be hard coded in, doesn't need to come from a db or anything complicated.
I current have just a textview like this:
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Do not move the patient,unless it is necessary, to reduce the risk of possible further injury"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintHorizontal_bias="0.134"
app:layout_constraintVertical_bias="0.032" />
But I need to write it out so it looks like this:
Do not move the patient,unless it is necessary, to reduce the risk of possible further injury
Render assistance and call 911
Contact Gary Edwards
Despatch staff
It would be really good if I could make the link to the phone number work as well.
Thanks everyone

You can simply add a newline character to add line breaks :)
"First Line \nSecond Line"
You can also have the control auto-detect phone numbers so long as they are in the correct format:
android:autoLink="phone"
You should also be able to use \u2022 for a bullet.

Related

Use multiple #string statements in android xml

I have the following code:
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="25dp"
android:inputType="text"
android:maxLength="10"
android:textColorHighlight="#7cff88"
android:hint='#string/nome'
android:textColor="#ff0000"
android:textAlignment="center"
android:layout_weight="1"
>
></EditText>
Is there a way to set a double string in the hint? Something like #string/age & #string/name or #string/age :
I am trying to add ´:´ without adding it to the string.xml file.
You shouldn't do that. Why? Localization. To you, as an English speaker, "name:age" might make a lot of sense. It may not make sense in other languages. Other languages have syntactic rules that differ and may make it nonsensical or worse. Or it may have different words for the same concept in different contexts, and this will cause it to use the wrong one.
In general, you should never concatenate strings for display. You should always use a single parameterized string, and allow the translators to translate the entire thing in context. This minimizes the chance of a mistake.
Now if this is a personal app that will never be translated- no there's no ability to do that in xml, unless possibly via databinding. But I wouldn't suggest using databinding like that unless its an architectural decision that your entire app is going to invest into databainding.

Android studio layouts howto make prompt/hint before EdiText

I am working in a kotlin project, and have been searching for some documentation about the screen layout.
What i want to do is very rudimentairy i guess. I want is to put a label/prompt/text before a EditText.
In html i would program something like this:
<form>
<label for="fname">First name:</label>
<input type="text" id="fname" name="fname"><br><br>
</form>
And get the a result like this:
First name: ___________
All i can find is a "android:hint=". But it only fills the View if there is nothing in it.
Should one add extra (plainText) elements for each label ? and how should one contstrain it to the EditText ? or is there some kind of grouping ?
Update after received answers
After reading the answers i understand that you have to roll your own solution. (I am still fighting with androidstudio because it sorts the xml elements so they are not always where i put them.)
I do not use a TextInputLayout (i hope this is allowed) which makes it all quite simple. So this is my solution for now:
We link the TextView ("Date of Birth") to the parent layout:
<TextView
android:id="#+id/dobLabel2"
android:layout_width="92dp"
android:layout_height="23dp"
android:layout_marginStart="76dp"
android:layout_marginBottom="112dp"
android:labelFor="#id/dobInputText"
android:text="Date of Birth"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent" />
and we link the EditText to the TextView
<com.google.android.material.textfield.TextInputEditText
android:id="#+id/dobInputText"
android:layout_width="240dp"
android:layout_height="54dp"
android:hint="Day/Month/Year"
app:layout_constraintStart_toStartOf="#+id/dobLabel2"
app:layout_constraintTop_toBottomOf="#+id/dobLabel2" />
If you play with the layout_contraints you can position the the EditText also to the left of the label.
Thanks for all the input, i think i can solve my problem now.
Android EditText (now often used as a combination of TextInputLayout containing one (and only one) TextInputEditText) can display a Hint, but only while the view has no focus/content.
If you want to provide a better description on what a particular EditText is for, for many reasons (accessibility, often neglected, is not the only one), you may want to provide an extra TextView positioned anywhere you consider it ok to add the extra information needed to better describe the EditText.
The main thing to keep in mind, is to provide this TextView with the labelFor attribute, as described in the Android documentation.
If you're reading this and wondering but why do I have to provide an extra Textview to describe, why not just use the hint, android is horrible!!!, keep in mind that the Hint is good for different reasons, but not for describing what the field is about.
E.g.: Imagine you're asking for a Date of Birth. You may be tempted to write this: (note this is a simplified version obviously):
<com.google.android.material.textfield.TextInputLayout
android:id="#+id/dobInputLayout"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<com.google.android.material.textfield.TextInputEditText
android:id="#+id/dobInputText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Date of Birth" />
</com.google.android.material.textfield.TextInputLayout>
And you'd be mostly ok, but then your designer comes in and says, well, we also want to show the Format that we accept, for e.g.: DAY/MONTH/YEAR...
Now you're going to change the hint to be:
android:hint="Date of Birth (DD/MM/YYYY)
And you'd again, be ok, but for accessibility users... this doesn't read very efficiently nor is very clear. You also get back from your designer who says: "but I don't want the (DD/MM/YYYY) part to be visible after the user focuses or types something..."
And so on and so forth.
The correct (according to Google, Material Design, and who knows what), is to provide an extra TextView that accompanies the TextInput combos:
(again, keep in mind this is pseudo-code, when in doubt, read the documentation)
<TextView
android:id="#+id/dobLabel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Date of Birth"
android:labelFor="#id/dobInputText />
<com.google.android.material.textfield.TextInputLayout
android:id="#+id/dobInputLayout"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<com.google.android.material.textfield.TextInputEditText
android:id="#+id/dobInputText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Day/Month/Year" />
</com.google.android.material.textfield.TextInputLayout>
Do not provide contentDescription on those views because they will likely interfere with TalkBack/Accessibility. These are the conventions that Android set in place, you may or may not like them, but this is how it's expected to be done.
Do I think EditText should be a better widget and handle this better for you? Yes. Does it matter what I think? Nope.
Yes, you have to add one more textview before adding edit text. The hint is used for displaying messages in edittext.

Remove the underline when typing word in EditText

I know, the question has already been asked many times on this forum, but no answer worked for me...
When I write in one of my EditTexts, the word being written is underlined, and I would like it not to be underlined... I've already seen answers like "You have to change the android:background", or "You have to put the textNoSuggestions attribute in android:inputType"... But I've already done all that - see the code just below - (and I even keep them by default) and it doesn't work.
A really huge thank you to everyone who will take the time to answer this question!
XML EditText :
<EditText
android:id="#+id/editP1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_margin="4dp"
android:autofillHints="username"
android:background="#drawable/background_card"
android:gravity="center_horizontal"
android:hint="#string/player_name"
android:imeOptions="flagNoFullscreen"
android:inputType="textFilter|textNoSuggestions"
android:maxLength="12"
android:padding="4dp"
android:textSize="25sp" />
<!-- The ' android:imeOptions = "flagNoFullscreen" ' was only necessary to show the underline word since my app is always in landscape mode -->
Pictures :
As outlined at Android edittext is underlined when typing, this may be a function of the keyboard in use, rather than the EditText. Without knowing which specific solutions you've tried and have failed (you say no answer worked for you, but don't list things you tried) it is hard to offer a specific suggestion, but I'd suggest the
android:inputType="textVisiblePassword|textNoSuggestions"
option and see if that works. The password should typically prevent the keyboard from suggesting things (since no suggestions are typically useful for passwords).

Textview limit 140 Characters not working

I know this question already asked multiple times but I could not able to solve my problem with available solutions.
I applied all solutions which are available on other stack overflow threads.
Requirements :
If string length is more than 140 than show only 140 characters and at last add ".." at last.
If string is less than 140 characters then no need to append ".." at last.
What I have tried:
<TextView
android:id="#+id/tvIssueBody"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ellipsize="end"
android:maxLength="140"
android:paddingBottom="5dp"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:text="Small Text"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textSize="#dimen/detail_text_size"/>
I have tried above code but it is not working and I tried all other properties mixture also but It is not working.
Shall I have to do with str.subString(0,141) option? There is no way to limit 140 characters using textview property?
Thanks to all in advanced. Any help will be appreciated.
There's no way to get this to work exactly as you want with a built in TextView. First off, a TextView will not allow anyone to add more than maxLength characters. So if they try to add 200 characters, the last 60 would be dropped. This means even 141 won't work if you want to keep that data. And Elipsize doesn't ellipsize where you want it- it ellipsizes where it needs to in order to fit the text on screen. You can't force it to do so after a given length.
What you want really need is a custom view or a subclass of TextView, so you can override onDraw to draw a different string than the set text (a hand ellipsized version of it).

Hyphen breaks the line into multi line

In a a custom list view, I have a textview showing name and emailId. When the name provided is something like A-TestThisString(testA#test.com), the length of displayed area exceeds,
In that case the string is broken at the place of hyphen like
A-
is shown in the list. This is causing lots of problems. I have found one link which talks about solution bellow
topic: "Android: How do i make nonbreakable block in TextView?"
but, I am not able to understand how to use it. Please suggest.
Thanks for your help. I found the answer. use the inputType as email or singleLine=True, it starts working fine.
I used android:inputType="date", no line breaks and hyphen is displayed nicely
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:maxLines="1"
android:text="2-3 4-5"
android:inputType="date"
android:textSize="25sp" />

Categories

Resources