I have a XML tag like "Yes,No,Dontknow " and I am parsing the XML file and getting the data. Now I need to display each option in separate TextView, i.e: 'yes' should be displayed in one TextView, 'No' should be displayed in another TextView, and 'Dontknow' should be displayed in another TextView, but how can I do this, can anyone give me some idea (I am new to Android).
Use setText() method of TextView to load text into it.
You can use string tokenizer:
StringTokenizer tokens = new StringTokenizer(theString, ",");
while( tokens.hasMoreTokens() ){
String token = tokens.nextToken();
// here you must have the reference to the text view...
textView.setText(token);
}
If you are creating the text views programmatically, then you must create or reference those text views inside the loop. Other wise, if the text views are static, you better put each token inside an array or something (words[0] will be Yes, word[1] will be No, etc) and then you set those strings manually.
You can just declare 3 separate TextView in you Activity layout file. Using attribute android:text you can assign the text for the TextView.
Example:
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Yes"
/>
parse xml file store that in a string.take an array like String[] array = parsedstring.split(","); then take 3 text views ,put array[0],array[1],array[2] on to textview
If you want to split comma-separated strings, take a look at using java.util.StringTokenizer. You can tell it to use , as the token separator.
Related
I have a string I'm using in a text view for example
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/tvText"
android:text="#string/yes"/>
I want to be able to get the key "yes" from this Textview , not the value of this string but just the key that leads to it , is there some way I can use the value of the string to get it ?
there is no option for getting string resource id (you are naming it "key") after setting with android:text. TextView is resolving it in its runtime, fetching String and "forgets" way how it was set up. same case when setting e.g. image for ImageView (android:src="#drawable/drawable_name") or some dimensions for all Views (e.g. android:padding="#dimen/some_dimension")
if you really need this reference in code then it would be better to NOT set this text via XML, instead store this reference under some final int variable in code and use it for setting text for TextView programmatically, further use same variable in another place you need
I have a string defined in MainActivity.java:
public String counter1 = String.valueOf(e.getCount());
I would like to use this string in activity_main.xml as:
android:text="#string/counter1"
As you can tell I am very new to this so basic steps would be appreciated.
Thanks
Short answer: you cannot. The resources you set in your xml layouts must be statically defined in resources files, like.-
<string name="counter1">COUNTER VALUE</string>
To dynamically define new strings, you must set them programmatically.-
TextView textView = (TextView) findViewById(R.id.textViewId);
textView.setText(counter1);
What do you want to do if you could get this to work? If you want to set text dynamically, you can use setText:
yourTextView.setText(counter1);
If you want to set something programmatically which you can... you should use
txtview.setText(yourString);
if you want to set a String in XML then you either set it in the XML like you did
android:text="Exercise Name"
or using strings which then you can use programmatically as well
in strings:
<string name="Delete">Delete</string>
in code you can call it with R.string.Delete or getString(R.string.app_name);
of course you can also set from strings in XML with #string...
by the sound of what you are trying to do, you want the TextView to change while program is running so the first option would suit you well
you can not,if you want to acheive this kind of solution:
create string in strings.xml
<string name="counter1">value of counter</string>
and if you want to set the text of counter dynamically then in you activity:
TextView tView = (TextView) findViewById(R.id.textViewId);
tView.setText(counter1);
you should follow this,because all static strings shoul always be defined in strings.xml
How to retrieve the value "topRight" on this function on the layout? I need to get this value in order for me to know the name of the container so that i can set the the arrangement of my child. I tried to use getId() but it returns an integer of course not the value "topRight". Please help guys.
android:id="#+id/topRight
Thanks.
When you define an ID in your layout, it simply modifies the auto-created R file with a new field. The name you specified will be the name of the variable (R.id.topRight).
I guess you might be able to access the name of the variable via some reflection magic, but I feel like you're going about this in a wrong way.
IDs are used to reference the view from the layout and create instances during runtime.
If you want to have some string stored in the layout elements which you can use to determine what view you'd like to use, I would probably go with the "Tag" property rather than ID.
Hope this helps.
Look at this, I have a textView with editTextUserName as an id so I get its value like the one I posted. Try it, I hope it works for you.
EditText editTextUserName = (EditText) findViewById(R.id.editTextUserName);
<EditText
android:id="#+id/editTextUserName"
android:hint="User Name"
android:layout_width="match_parent"
android:layout_height="wrap_content">
I would like to create an array of EditText for android but it seems that finding the id has been a very challenging task.Can someone reach out?
My code for the array:
EdiTText[] mEditText = new EditText[20];
for(int i =0;i<mEditText.length;i++){
mEditText[i] = (EditText) findViewById(i);
}
You cannot set id to view in runtime. Read more about android id here.
Just use constructor to initialize EditText:
mEditText[i] = new EditText(this);
There's two approaches to UI elements like EditText:
a. Implement them in your layout xml like this:
<EditText android:id="#+id/myedittext"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
Here the EditText would have the id "myedittext". You can then do stuff with that EditText by referencing it from your code with
EditText et = (EditText)findViewById(R.id.myedittext);
b. Programmatically create them like
EditText et = new EditText(context)
With this approach you will also have to manually add this EditText to your UI / layout.
You mixed those two approaches in a way that won't work.
If you don't know beforehand how many UI elements (in your case EditTexts) you need, it's cool to create them programmatically, but you should definitely read up on how to do that properly.
I'm not going to ask why you're trying to do this. However...
The resource IDs needed by findViewById will not start at zero as implied by your code snippet which uses the loop index for id.
If the EditText are created in XML, then you need the resource IDs of the form R.id.xxxx. You could create an int[] of the IDs and pass the corresponding id to findViewById.
If you create the EditText in code, then you won't have IDs, so will have to store the EditText objects at creation time.
I'm trying to create a dynamic text field that will take multiple fields of user input during a single activity, do a calculation in the java file, and then display the resulting value within the SAME activity in a text field.
Is there any way of doing this? I just figured out that I can't edit strings.xml dynamically, so are there any structures I can use that will allow me to constantly change the values?
Thanks all.
This will be pretty straight forward:
String yourTextValue = "text";
TextView myTextView = (TextView) findViewById(R.id.textView1);
myTextView.setText(yourTextValue);
Just call setText() with your String value (whatever it may be) each time you want to change the value of your TextView.