I have a screen with 24 TextViews that I want to change values of. It doesn't matter in which order I just need to change the value from every text field. Right now my code is as follows:
textView = (TextView) findViewById(R.id.textView1);
textView.setText(value1);
textView = (TextView) findViewById(R.id.textView2);
textView.setText(value2);
textView = (TextView) findViewById(R.id.textView3);
textView.setText(value3);
textView = (TextView) findViewById(R.id.textView4);
textView.setText(value4);
etc...
The number of TextViews is constant at 24. I am looking for a loop solution however the issue I run into is dynamically getting the R.id.textViewX value id. Is there a simple way to accomplish this so that I can use it in the following manner:
// Somehow get textViewArray of textView id's //
for(int i=0;i<textViewArray.length;i++) {
textView = (TextView) findViewById(textViewArray[i]);
textView.setText(value[i])
}
I'm open to the idea of dynamically creating the textfields in the Activity class but am looking for an XML solution. I'm not sure if this can be done using the arrays class. I've seen this done for #drawables but never for id's
Important to note is that the textView's are NOT in a list.
Use the getIdentifier() method:
for (int i = 0; i < 24; i++) {
int id = getResources().getIdentifier("textView" + i, "id", getPackageName());
TextView textView = (TextView) findViewById(id);
textView.setText("Set text");
}
Related
Hello I got two Strings and a TextView:
TextView test = (TextView) findViewById(R.id.eins);
String one = "DynamicText";
String two = "Title";
test.setText(two+one);
Now I would to set the two Strings in one TextView but I like to formate the String two text align to center and the String one normal (left). How is this possible?
TextView txtOne = (TextView) findViewById(R.id.txtOne );
TextView txtTwo = (TextView) findViewById(R.id.txtOne);
textOne.setText("DynamicText");
txtTwo.setText("Title");
txtTwo.setGravity(Gravity.CENTER);
I'm trying to generate random ids for views as shown in following screenshot.
But it didn't work. It got null.
How should I findViewById ?
use textView.setId(View.generateViewId()) introduced in API 17.
TextView tv = new TextView(this);
This means you're creating the TextView dynamically. So you don't need to do findViewById.
findViewById is used when the view with id is present in xml file.
Remove the TextView cloneTextView = (TextView) findViewById(randomNo) line. Your question is vague, I tried to explain.
Best practices for unique identifiers
Java
String uniqueID = UUID.randomUUID().toString();
Kotlin
var uniqueID = UUID.randomUUID().toString()
I got my own solution...
It should be like that..
Random r = new Random();
randomNo = r.nextInt(1000+1);
TextView textView = new TextView(this);
textView.setId(randomNo);
linearLayout.addView(textView);
int childCount = linearLayout.getChildCount();
for(int i=0;i<childCount;i++){
if(linearLayout.getChildAt(i).getId()==randomNo){
TextView cloneTextView = (TextView) linearLayout.getChildAt(i);
cloneTextView.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
cloneTextView.setText("I'm a clone...!");
linearLayout.removeAllViews();
linearLayout.addView(cloneTextView);
}
}
It works and that's what I want. Thank you all.
Something like this may work.
But I'm not sure about possible performance and memory issues, since it will return an instance of a view (if found). During a little test sequence it never hit an existing id, with other words the first random number was always ok.
private int createUniqueId() {
int id = RandomUtils.nextInt();
while(findViewById(id) != null) {
//id is not unique, try another one...
id = RandomUtils.nextInt();
}
//return unique id
return id;
}
You can create UUID (universal unique identifier) as follow :
String id= UUID.randomUUID().toString();
Hi in my app i am reading the values from the database thru cursor and displaying in textview
my cursor contains the value 1.01 now i wanna display 101 in my text view..doing the following
TextView tv2 = (TextView)view.findViewById(R.id.acValue);
int ach = Integer.parseInt(topcursor.getString(6));
tv2.setText(ach +"");
i am getting the float value as 1.01, now i wanna show the percentage in textview i.e, 101% .how can i do that
But iam getting numberformatexception. Any help is appreciated.
1.01 is not an integer value, that is why the conversion is failing. Also be careful with locale when you use the parse* methods
Try this
TextView tv2 = (TextView)view.findViewById(R.id.acValue);
String ach = topcursor.getString(6);
tv2.setText(ach);
There is no need to parse as it returns the string.
TextView tv2 = (TextView)view.findViewById(R.id.acValue);
String mResult = topcursor.getString(6);
tv2.setText(mResult);
Thank You all ...Did as follows it work fine..
float ach = Float.parseFloat(topcursor.getString(6));
String kj = String.valueOf(ach*100+"%");
Using replace() will do the trick
TextView tv2 = (TextView)view.findViewById(R.id.acValue);
int ach = Integer.parseInt(topcursor.getString(6).replace(".", ""));
tv2.setText(ach +"%");
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);
can anybody help me? I'm writing an application for Android that parses data from internet and writes them into TextView.
TextView[] dva = new TextView[sitesList.getDva().size()];
// Set the result text in textview and add it to layout
for (int i = 0; i < sitesList.getDva().size(); i++) {
dva[i].new TextView(this);
dva[i].setText("KOSILO: "+sitesList.getDva().get(i));
layout.addView(dva[i]);
Under dva[i] I parse 5 objects that I would like them to display them under one TextView.
TextView textView1 = (TextView) findViewById(R.id.kosilodva);
textView1.setText("KOSILO: "+sitesList.getDva().get(i));
How can I do this? Thanks in advance to everybody who is willing to help me.
Instead of setText why don't you try append,
Replace this,
textView1.setText("KOSILO: "+sitesList.getDva().get(i));
with,
textView1.append("KOSILO: "+sitesList.getDva().get(i));