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
Related
I'm trying to run the following unit test code snippet and it fails:
final TextView textView = mView.findViewById(R.id.title);
assertEquals(View.TEXT_ALIGNMENT_VIEW_START, textView.getTextAlignment()); // FAILS
The text alignment is set in the layout file like this:
<TextView
...
android:textAlignment="viewStart" />
The test doesn't even succeed when I set the text alignment programmatically like this:
final TextView textView = mView.findViewById(R.id.title);
textView.setTextAlignment(View.TEXT_ALIGNMENT_VIEW_START);
assertEquals(View.TEXT_ALIGNMENT_VIEW_START, textView.getTextAlignment()); // STILL FAILS
What could be the problem? Why is the value different from the constant when I get it from the TextView object by using the getter?
I've checked what the exact int values of the two values are. TEXT_ALIGNMENT_VIEW_START is 5 but when I get the text alignment value from the TextView object I get 1.
Because at runtime this is converted into ALIGN_LEFT or ALIGN_RIGHT, depending on the LTR of the locale. Its not kept as VIEW_START and recalculated.
Is it possible to change at run time id name of a component?
<ImageButton android:id="#+id/ImageButton01"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true" android:background="#drawable/single_square"
android:onClick="itemSelected"
/>
onClick method adds random background image. Later, when it is clicked for the second time I want to know what background image was added. To do that I want to change id name (ImageButton1) of this component at the first click to the image name that was set(for example, key_image). How do I do that?
There's no need to change the id if you just want to keep track of the image name. Simply use a separate String variable, or (possibly preferable) the setTag and getTag methods on the Button.
An object's tag is really just a place to store additional information with that object. So, in your case, if you have a Button btn, and you want to associate a String like imageName with it:
btn.setTag(imageName);
To retrieve the String, use:
String lastImageName = (String) btn.getTag();
Note: A tag can be any Object. It doesn't have to be a String, which is, in fact, why we need to cast the getTag result with (String) in our example.
You can use the setId(id) method of the ImageButton to set an id to your ImageButton..
imagebutton.setId(someId);
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'm new into the "Android World" and I've a problem getting a ImageView value, suppose I've a ImageView somewhere in my layout file and I want to retrieve is value, i.e. the value in the "android:src" tag. How I can I do it?
Thanks in advance.
Once the resource file is compiled the ImageView simply refers to a resource integer in the R file. So the image "src" value will simply be a value once you've retrieved it, and not the string value you placed in your layout file.
Nonetheless, I think you can get that ID through your ImageView's getResources() method.
EDIT: I'm wrong, turns out you can get the string through Resources.getString(int id)
EDIT 2: Looks like getResources might not contain the ID for you either, an Android engineer already answered a similar question: http://groups.google.com/group/android-developers/browse_thread/thread/84d31b244163821d?pli=1
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.