android setText get text from strings + position - android

I have a viewPager that get its text from the string.xml.
The plan is, depending on the position of ViewPager, it gets the text from strings, for example if the user is in page 3 it should get text from string name="page3".
Here is my beta code that is incorrect:
tv.setText(R.string.page + position);

You can get a resource from its name by using Resources.getIdentifier, eg:
String name = "page" + position;
int id = getResources().getIdentifier(name, "string", getPackageName());
tv.setText(id);

Related

How do I modify the effect of a button with another button in android studio?

I am trying to create an app that adds, at first the value 1 to the textView.
Then I tried creating another button that doubles the value shown in the textview.
Everytime I press the first button, it should increment by 1
and everytime I press the other button it should double that result.
But when I add 1 to the value again, the value goes back to the undoubled stage and increments by one.
I need to cover the operations by making a button that adds value X
and the other button that multiplies that value of the button by 2.
This is the main.kt I made so far
value.setText("" + id)
plusBtn.setOnClickListener {
value.setText("" + ++id)
}
doubleBtn.setOnClickListener {
value.setText("" + 2*id)
}
I think this could solve the problem. You haven't saved the value in the variable. when you set the text to 2*id, the id variable is still the same.
value.setText("" + id)
plusBtn.setOnClickListener {
value.setText("" + ++id)
}
doubleBtn.setOnClickListener {
value.setText("" + 2*id)
id=2*id;
}
You have to get updated value from textView when you click on any button.
Check following statements, I had written in Java but you may able to convert in kotlin.
plusBtn.setOnClickListener {
id = Integer.parseInt(value.getText().toString());
value.setText("" + ++id)
}
doubleBtn.setOnClickListener {
id = Integer.parseInt(value.getText().toString());
value.setText("" + 2*id)
}
This is because you are just setting the double value of id as text to the value, not assigning the double value to the id.
to do this you can either assign the
value.setText("" + 2*id)
id = Integer.parseInt(value.getText().toString());
or
id = 2*id
value.setText(id)
I'm only answering because the other answers so far are convoluted (converting back and forth from Strings) or use needless repetition (calculating the doubled value twice).
When you use ++id, you are using the increment operator, which adds one to the value in id, but also changes id to the new value. It is a shorthand way of writing what would normally take two lines of code. What you're doing is equivalent to
plusBtn.setOnClickListener {
id = id + 1
value.setText("" + id)
}
When you use 2*id, you are calculating a new value, but you are not assigning this new value to id. There isn't a shorthand operator for doing both actions like there is for adding or subtracting one.
The simplest solution is to assign the new value before changing the text. So your second button listener becomes:
doubleBtn.setOnClickListener {
id = id * 2
value.setText("" + id)
}
or, more succinctly:
doubleBtn.setOnClickListener {
id *= 2
value.setText("" + id)
}

Concatenation of int with string to TextView gives a set of Integer

In my android app, I'm trying to set the text to a TextView with integer and a string value. I'm getting the length of the array and going to set it with a string value as below.
if (array.size() > 0) {
textView.setText(array.size() + " " + R.string.lbl_text);
}
When I run the App it give me a set of integers as shown below:
1 2345223232
Then I covert array size to string as follow
if (array.size() > 0) {
textView.setText(Integer.toString(array.size()) + " " + R.string.lbl_text);
}
But the same output. What is the reason for getting Integer value for the string.lbl_text
R.string.xxx is a resource identifier. In order it to convert to a String value you need to call getString() on a Resources or Context instance:
resources.getString(R.string.xxx);
Use getResources().getString(R.string.lbl_text) and then you would be able to access the string text. as #pskink told already

Html Styling in textview goes wrong Android

I am selecting a part of the TextView and on click of a "highlight" button, I am sending the start and the end index of selection to the database. Then I am loading all the start and end indexes from db and changing the color of text between them.
The problem is after once or twice, the app is changing the color of text that is not in selection.. and the selected part remains unchanged.
MY CODE:
When user selects and presses the highlight button
int i=contentText.getSelectionStart();
int j=contentText.getSelectionEnd();
db.insertHiglightIndex(String.valueOf(i),String.valueOf(j));
setHighlightedText();
The setHighlightedText() method..
String fullText=contentText.getText().toString();
for(int i=0; i<db.getAllStartIndex().size();i++){
String a=fullText.substring(Integer.parseInt(db.getAllStartIndex().get(i)),Integer.parseInt(db.getAllEndIndex().get(i)));
fullText = fullText.replace(a, "<font color='red'>"+a+"</font>");
}
contentText.setText(Html.fromHtml(fullText), TextView.BufferType.SPANNABLE);
MY SCREENSHOTS.
The selection:
The Result:
Clearly the selected area is from "Garrick" to "Bart", and the result is from "entity" to "2012"
I am not able to understand why is this happening. I think there is some problem with this <font color='red'>"+a+"</font> line.
Thank you
It got wrong indexed because There is already added <font color='red'> in the beginning, So that in second time This tag is also counted as a part of string, So I suggest creating a new temporary String, assign same text to the String but after replacing the previous font tag it held. Use this syntax to remove previous font tag from originalString
String tempString = originalString.replaceAll("[<](/)?font[^>]*[>]", "");
After that work with only tempString. That means again add every previous font tag you have to tempString and set that text.
In next time again do the same first remove all font tag and again add all of them back in tempString as well as current selection using same loop you are using currently.
You have wrong indexes because you are modifying the fullText content within the loop.
Taking a look at this example you can figure it:
final TextView tv = (TextView) findViewById(R.id.textView);
tv.setText( "abcdefghijklmnopqrstuvwxyz0123456789");
String fullText= tv.getText().toString();
// your first iteration
String a = fullText.substring(1,3);
// a contains "ab"
fullText = fullText.replace(a, "<font color='red'>"+a+"</font>");
After the first iteration full text contains now
a<font color='red'>bc</font>defghijklmnopqrstuvwxyz0123456789"
Then the substring() in the second iteration won't returns the substring base on your initial content.
If you want to be able to have multiple substrings colored in red you can try this:
String fullText = contentText.getText().toString();
StringBuilder result = new StringBuilder();
for(int i=0; i < db.getAllStartIndex().size(); i++){
fullText = applyFont(result, fullText, Integer.parseInt(db.getAllStartIndex().get(i)), Integer.parseInt(db.getAllEndIndex().get(i)));
}
// Add here the remaining content
result.append(fullText);
contentText.setText(Html.fromHtml(result.toString()), TextView.BufferType.SPANNABLE);
private String applyFont(StringBuilder result, String source, int from, int to){
result.append(source.substring(0, from));
result.append("<font color='red'>");
result.append(source.substring(from, to));
result.append("</font>");
return source.substring(to, source.length());
}

android LayoutInflater setText get text from strings + position

I have a viewPager that get its text from the string.xml.
The plan is, depending on the position of ViewPager, it gets the text from strings, for example if the user is in page 3 it should get text from string name="page3".
the important issue is I want to write my code in layoutInflater not in Activity.
here is the code that works just in Activity:
String name = "page" + position;
int id = getResources().getIdentifier(name, "string", getPackageName());
tv.setText(id);
As per your code, you are getting identifier means id of particular string in R.java but not string.
So you can do one thing like this
int[] arr = {R.string.page1,R.string.page2,R.string.page3};
tv.setText(getResources.getString(arr[pos]));

Grab TextView (in a ListVIew Row ) from ContextMenu

I know to get a string of a specific TextView in a ListView, I can do this:
ReviewUser = ((TextView) rowView.findViewById(R.id.labelUser))
.getText().toString();
What if I want to get the TextView itself?
The TextView is an integer and I simply want to get the TextView and add 1 to it.
So you already specified that you know how to get the specific text from your ListView. Since you want to modify that same TextView, the rest is simple. This code is lengthier just to show the steps.
TextView textView = (TextView) rowView.findViewById(R.id.labelUser)
String text = textView.getText().toString();
int num = Integer.valueOf (text).intValue() + 1;
textView.setText (""+num);
Also, if you are working with a String ArrayAdapter and you know the index of the row you want to modify, what you can do is (assuming arrayAdapter is initialized and index is your index variable):
String text = arrayAdapter.get(index);
arrayAdapter.remove (text);
arrayAdapter.insert ((Integer.valueOf (text).intValue() + 1 ) + "", index);

Categories

Resources