I have an imageView that i want to display a little icon of the country that you are currently in. I can get the country code, but problem is i can't dynamically change the imageView resource. My image files are all lowercase
String lowerCountryCode = countryCode.toLowerCase();
String resource = "R.drawable." + lowerCountryCode;
img.setImageResource(resource);
int id = getResources().getIdentifier(lowerCountryCode, "drawable", getPackageName());
setImageResource(id);
abve is my code.
Now, of course this will not work because set Image Resource wants an int, so how can i do this? Thanks in advance
I mean you know the name of files(Drawable) which you wanna use for one and other country.
ImageView imgView=(ImageView)findViewById(R.id.imageView1);
int id = getResources().getIdentifier(lowerCountryCode.toLowerCase(), "drawable", getPackageName());
imgView.setImageDrawable(res.getDrawable(id));
The following code should work well (assuming that you really have a file with name lowerCountryCode in your res/drawable/ folder)
String lowerCountryCode = countryCode.toLowerCase();
int id = getResources().getIdentifier(lowerCountryCode, "drawable", getPackageName());
ivCard[0].setImageResource(id);
i use that code....
ImageView[] ivCard = new ImageView[1];
#override
protected void onCreate(Bundle savedInstanceState)
ivCard[0]=(ImageView)findViewById(R.id.imageView1);
Related
I'm testing my android app with espresso and want to test the ZoomControls like this:
onView(withId(R.id.zoomIn)).perform(click());
But I don't get the zoomIn/zoomOut id, from the Zoom_Controls. In the Layout Component Tree, the zoomIn and zoomOut are visible.
Can someone help me?
Thanks in advance.
Resources res = Resources.getSystem();
int id = res.getIdentifier("btn_zoom_down", "drawable", "android");
Didn't work.
Got it working with:
Resources res = Resources.getSystem();
int id = res.getIdentifier("#id/zoomOut", "drawable", "android");
onView(withId(id)).perform(click());
ImageView test = (ImageView) findViewById(R.id.A1);
gets me an ImageView
instead of A1 I would like to use a string variable to fetch a series of IDs
I have tried
Method Pmethod = Class.getDeclaredMethod("(ImageView) findViewById(Id.R." + dest.toString());
as suggusted in this question : how to call a java method using a variable name?
However I get the error
non-static method getDeclaredMethod cannot be referenced from a static context
Any way to fix this?
My latest attempt :
String ps = "P"+ dest.toString();
String ss = "S"+ dest.toString();
int piecelayertier = getResources().getIdentifier(ps,"id","com.donthaveawebsite.mhy.ocfix");
int selectorlayertier = getResources().getIdentifier(ss,"id","com.donthaveawebsite.mhy.ocfix");
ImageView test =(ImageView)findViewById(piecelayertier);
spot.TieAppearance((ImageView)findViewById(piecelayertier));
spot.TieSelector((ImageView)findViewById(selectorlayertier));
I think this is not a correct way to get id.
You can try following to get id using a string variable :
int id = getResources().getIdentifier("<String id>","id","<package name>");
ImageView test =(ImageView)findViewById(id);
You might also need to use a this reference like so :
int id = this.getResources().getIdentifier("<String id>","id", getPackageName());
I'm fetching some data containing HTML from my server to use in my app, and the html has some links like Go here.
I'm applying the html to a TextView like
text.setText(Html.fromHtml(htmlString));
text.setMovementMethod(LinkMovementMethod.getInstance());
The link works, but causes my app to crash with
No Activity found to handle Intent { act=android.intent.action.VIEW dat=/go/here...`
I'm guessing the crash is because it's a relative link and doesn't have the domain in. Is there any methods, maybe Regex, to search for all <a/> tags and add the domain before it?
I would be tempted to go the simplest way:
final String faultyHtml = "Link: click here etc etc";
final String domain = "http://www.google.fr";
final String fixedHtml = faultyHtml.replace("href=\"/", "href=\"" + domain + "/");
text.setText(Html.fromHtml(fixedHtml));
text.setMovementMethod(LinkMovementMethod.getInstance());
(all occurrences would be added the same domain though)
int index = htmlString.indexOf("<a>");
String part1 = htmlString.subString(0,index+2);
String part2 = htmlString.subString(index+3);
String newHtmlString = part1+"http://"+part2;
You can try like this codes.
TextView tv = (TextView) findViewById(R.id.textView1);
String text = "This is just a test. Click this link here Google to visit google.";
tv.setMovementMethod(LinkMovementMethod.getInstance());
tv.setText(Html.fromHtml(text));
reference How to make links in textview clickable in android?
I have looked everywhere but cannot find an example of programmatically setting a background resource from a string value?
As an example:
Drawable a = getResources().getDrawable( R.drawable.a );
Drawable b = getResources().getDrawable( R.drawable.b );
Drawable c = getResources().getDrawable( R.drawable.c );
abc.setBackgroundResource("b");
Is this possible or would I have to do it as a big switch statement?
you have getResources().getIdentifier for this purpose. It returns the id of the resources from its name.
E.g.:
int resId = getResources().getIdentifier("b", "drawable", getPackageName());
Here you can find the documentation.
Still a Noob at Android Development..
So here's My Code
for(int i=1;i<=6;i++){
for(int j=startat;j<=7;j++){
String constring = "r" + i + "c" + j;
//TextView dtv = (TextView) findViewById(R.id.constring); #commented this out
}
}
Is there a way I could use the string variable constring as an Id?
Is there a way I could use the string variable constring as an Id?
I'm little confused what are trying to make?!
This: TextView dtv = (TextView) findViewById(R.id.constring);
especially R.id.constring you shouldn't, musn't do it in the way you do. Every id is automatic generated from XML resources and you should respect it!
Also R.id.costring is int auto-generated in R.java not String so your approach won't work.
From your code is feel "spaghetti code".
<TextView
android:id=#+id/constring"
... next attributes
/>
If you'll write this, id named as constring will be automatic generated and then you just call R.id so constring is automatic filled.
It a case if you don't want to create XML so you just do it like that:
TextView t = new TextView();
t.setText("Hello World");
setContentView(t);
I recommend to you read some Android tutorials for beginners. Here you can start:
Android - A beginner's
guide
You can use getIdentifier method to obtain resource id from name.