Add multiple text to a TextView - java

I have created a TextView with similar details:
<TextView
android:id="#+id/tAge"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/tHeadline"
android:layout_centerHorizontal="true"
android:layout_marginTop="4dp"
android:gravity="center"
android:hint="Age"
android:textSize="18sp"
android:textColor="#000" />
I would like to set text for multiple strings. such as
mUserAgeRetrieved.setText("Age:");
mUserAgeRetrieved.setText(String.valueOf(objects.get(i).getInt("UserAge")));
Hence, the result of the above would be Age: (age that is generated from Parse).
Another Instance
mUserHeadlineRetrieved = (TextView) findViewById(R.id.tHeadline);
mUserAgeRetrieved.setText("Headline:");
mUserHeadlineRetrieved.setText(objects.get(i).get("Headline")
.toString());
Hence, the result of the above would be Headline: (Headline that is generated from Parse).
The problem I have is that It chose the last setText, and does not combined both of them.
Lets say I wanted to have one of them bold like Age: (bold) 25 (non-bold)
If you need any clarification, let me know.

Of course it doesn't! setText sets new text.
The solution is very simple.
textView.setText("This: ");
textView.append("works");
textView should say "This: works"
Hope this helps,
good luck :)

You can set both texts to a single string and then set it, like this:
mUserAgeRetrieved.setText(Html.fromHtml("<b>" + "Age:" + "</b>" + String.valueOf(objects.get(i).getInt("UserAge"))));

mUserAgeRetrieved.setText("Age: " + String.valueOf(objects.get(i).getInt("UserAge")));
or
mUserHeadlineRetrieved.setText("Headline: " + objects.get(i).get("Headline").toString());
You just need to use regular string concatenation.

Related

How can I force my textview to display the full text on a single line?

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!

How to update textView properly?

Hello I am following a book tutorial on learning Android but the book is outdated. I'm getting a "String literal cannot be translated" and "Do not concatenate text with setText". I am wondering what is the correct method to update a TextView? I think that's what I essentially want to do.
<TextView
android:id="#+id/textScore"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Score: 999"
android:textSize="36sp"
android:layout_alignParentBottom="true"
android:layout_toLeftOf="#+id/textOperator"
android:layout_toStartOf="#+id/textOperator" />
<TextView
android:id="#+id/textLevel"`enter code here`
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Level: 4"
android:textSize="36sp"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_marginRight="11dp"
android:layout_marginEnd="11dp" />
and here is the Java.
textObjectScore.setText("Score:" + currentScore);
textObjectLevel.setText("Level:" + currentLevel);
Both of these have to do with translations.
"String literal cannot be translated". This is a warning telling you that this string will be in english no matter what language the user changes their phone to. For a professional app, you would define your string in strings.xml and use that string in your app. That allows the string to be chosen for the phone's locale, assuming you've provided translation files.
"Do not concatenate text with setText". There's no actual problem with doing this in your native language. The problem is that in other languages, what you're doing may not make grammatical sense. Instead you should define a string with input variables in strings.xml and use getString to fill in those variables.
But for throwing something together quickly that isn't going for international release you're fine.
Follow the guidelines (https://developer.android.com/guide/topics/resources/string-resource.html). For example specify your score-related string like this:
<string name="score">Score: %1$d</string>
And set it like this:
Resources res = getResources();
String text = res.getString(R.string.score, currentScore);
textObjectScore.setText(text);
This way you can translate strings, provide plurals and format safely.

maxLength property is not working on EditText

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"

How to display the numeric keyboard directly in the Android [duplicate]

I just basically want to switch to the number pad mode as soon a certain EditText has the focus.
You can configure an inputType for your EditText:
<EditText android:inputType="number" ... />
To do it in a Java file:
EditText input = new EditText(this);
input.setInputType(InputType.TYPE_CLASS_NUMBER);
I found this implementation useful, shows a better keyboard and limits input chars.
<EditText
android:inputType="phone"
android:digits="1234567890"
...
/>
Additionally, you could use android:maxLength to limit the max amount of numbers.
To do this programmatically:
editText.setInputType(InputType.TYPE_CLASS_PHONE);
KeyListener keyListener = DigitsKeyListener.getInstance("1234567890");
editText.setKeyListener(keyListener);
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
...
android:inputType="number|phone"/>
will show the large number pad as dialer.
You can do it 2 ways
Runtime set
EditText input = new EditText(this);
input.setInputType(InputType.TYPE_CLASS_NUMBER);
Using XML
<EditText
...
android:inputType="number" />
If you need fractional number, then this is the answer for you:
android:inputType="numberDecimal"
If you are using your EditText in Dialog or creating dynamically and You can't set it from xml then this example will help you in setting that type of key board while you are using dynamic Edit Text etc
myEditTxt.setInputType(InputType.TYPE_CLASS_NUMBER);
where myEditTxt is the dynamic EDIT TEXT object(name)
editText.setRawInputType(InputType.TYPE_CLASS_NUMBER);
Use the below code in java file
editText.setRawInputType(Configuration.KEYBOARD_QWERTY);
Below code will only allow numbers "0123456789”, even if you accidentally type other than "0123456789”, edit text will not accept.
EditText number1 = (EditText) layout.findViewById(R.id.edittext);
number1.setInputType(InputType.TYPE_CLASS_NUMBER|InputType.TYPE_CLASS_PHONE);
number1.setKeyListener(DigitsKeyListener.getInstance("0123456789”));
More input types and details found here on google website
Define this in your xml code
android:inputType="number"
EditText number1 = (EditText) layout.findViewById(R.id.edittext);
number1.setInputType(InputType.TYPE_CLASS_NUMBER);

Android text justification issue within checkbox

In one of the activity of my application, I am displaying user names with their points in front of the names.
The name may have different number of characters, but I want the strings to end at the same place.
I am able to provide correct number of spaces and the strings are equal in character lengths but the characters take uneven space and ruin the symmetry of the strings.
Is there any work around for this?
Here's the code :
private String separateNamePoints{
String text="";
//text is separated by ,
String[]splittedRawText=rawText.split(",");
String name=(splittedRawText)[0];
String points=(splittedRawText)[1];
int pointsLength=points.length();
int reqSpaceLength=40-name.length();
String space="";
for(int i=0;i<reqSpaceLength;i++){
space+=" ";
}
if(pointsLength==1)
space+=" ";
else if(pointsLength==2)
space+=" ";
else if(pointsLength==3)
space+=" ";
text=name+space+points;
return text;
}
And here's the image :
There are ways you can handle this..
Your approach
In here, you can try using String.format() to format as per requirement .. here read this for ref
You can use 2 textViews and use alignment to solve your problem..
Try to use this xml for list item and write adapter for this:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:id="#+id/name"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="4"/>
<TextView
android:id="#+id/points"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"/>
</LinearLayout>
Android do not provide complete text justification.
In this case, need to create a custom control containing separate edittexts to display the data.
Your code will work fine. I think your input string having some space after the number.
please try with this
String[]splittedRawText=rawText.trim().split(",");

Categories

Resources