can BitmapFactory.decodeResource(context,int) take context string - android

I have a method that returns a String(let's say the var sprite) and this string is the name of my bitmap img,and i want to use it with BitmapFactory.decodeResource() but i don't know how to combine this since it must be int R.drawable.sprite
Can this be done?

Yes, it's possible. Take a look at this question:
Android and getting a view with id cast as a string

Related

Handle display of string or string resource

Suppose you apply the following architecture in your Android project:
View - ViewModel - UseCase - Repository and you have to display some text from the UseCase layer to the View layer. However, this text some times is of type String and some times is of type Int (stringResource).
What is the best practice to handle this?
Maybe you can try type Any and set case to decalre type as String || as Int

Android findViewWithTag using int[] as view tag

I want to bind some information by attaching tags to a view. So when the view is clicked I can retrieve this information with its tag. However, I meet problem when using an int array as the tag.
I save two integers by following samples:
int arr=[x,y];
view.setTag(arr);
parentView.addView(view);//parentView is a list/LinearLayout of such views
Later, when I want to find the view using
int tmp=[i, j];//i=x, j=y in values
View target=parentView.findViewWithTag(tmp);
The returned target is null. Why does it not return the correct view?
Edit:
I'm not sure if it's because of object reference and comparison issue. I tried to combine integers x and y into a string like "x,y", and search the view like parentView.findViewWithTag("x,y") and it can find the view.
Although didn't do verification experiments, a guess is mentioned by #CommonsWare. And real objects should solve the problem.

getStringExtra - Public Static Final - The Busy Coder's Guide to Android

I am on page 301 of this book and it is an example of an Activity getting "extras" from the intent that started it. I am fairly new to Java so maybe am missing something pretty obvious but...
I thought that when you declare a variable as "final" it meant that it doesn't change.
There is a line of code initialising a final variable:
public static final String EXTRA_MESSAGE="msg";
and then later in onCreate method:
tv.setText(getIntent().getStringExtra(EXTRA_MESSAGE));
The text displayed in the activity is not "msg" but is the string passed from the intent "I am the other activity". Why do you have to have the variable declaration above for the code to work? I don't understand what its doing.
Thanks
You are getting the extra received from another Activity indexed by the key 'msg'.
Like when you do this with the Intent used to start your Activity:
intent.putExtra("msg", "text going in the TextView");
The key is 'msg', but the value you get for the TextView is 'text going in the TextView'
Yes, final means EXTRA_MESSAGE value won't change, but you're not displaying EXTRA_MESSAGE value, but
getIntent().getStringExtra(EXTRA_MESSAGE)
which actually contains the value put in the previous activity. Regarding your question
Why do you have to have the variable declaration above for the code to work?
You don't actually need that variable for the code to work, but it's a good practice to use constant values instead of just hardcoding string values such in.-
getIntent().getStringExtra("msg")
The parameter you pass to getStringExtra is the key to which the String is mapped. All the extras you put in an Intent are mapped as key-value, so if you want to get a value you have to know the key, which must be the same key you used in the previous activity to save the value (with putStringExtra).
http://developer.android.com/reference/android/content/Intent.html#getStringExtra(java.lang.String)

android: Variable passed for R.drawable.variableValue

iv.setImageResource(R.drawable.icon);
this line is working good for me
can I pass a variable value instead of icon
like if i have 10 images in drawable and i want to decide on runtime which image to show
can i pass the value through a variable and work it like
String variableValue = imageName;
iv.setImageResource(R.drawable.variableValue);
Resources.getIdentifier() can solve your problem.
String variableValue = imageName;
iv.setImageResource(getResources().getIdentifier(variableValue, "drawable", getPackageName()));
You could use an array to define your drawables:
int[] images = new int[2];
images[0] = R.drawables.image1;
images[1] = R.drawables.image2;
And then you can use this array to set a image at runtime:
lv.setImageResource(images[i]);
Where is is in this case either 0 to point to the first image or 1 to point to the second image of the array.
No, it won't work that way.
In your first line icon is a generated static final member of the static final class R.drawable so trying to concatenate it up like it was a String would give you a compiler error.
But you could try using
iv.setImageURI(uri);
where uri is the URI to your image, so you can use a string value here.
Also, according to the api documentation, it would be a better solution to use setImageBitmap: you should consider it based on your resources.
APIDocs:
public void setImageURI (Uri uri)
Since: API Level 1
Sets the content of this ImageView to
the specified Uri.
This does Bitmap reading and decoding
on the UI thread, which can cause a
latency hiccup. If that's a concern,
consider using
setImageDrawable(Drawable) or
setImageBitmap(Bitmap) and
BitmapFactory instead.
Parameters
uri The Uri of an image

Pass Control By Name To findViewById

If you got:
Button btn = (Button) findViewById(R.id.widget28);
where "widget28" is the name of your button, how can you pass this
into the findViewById as a String? or get it from R class as a string?
I want to get the Button instance by string and not by the hard code R.id.widget28 reference.
thanks
Note that findViewById expects an int, not a string, so not sure what you're trying to achieve here.
But the general way to get a string from its resource id is getString(R.string.theId). See the Context API docs.
I don't believe even with reflection that you can get the variable's name, widget28, if you actually need it for some reason. You could look into using the button's android:tag element to store it in the XML file, you'd then retrieve it in code by using yourButton.getTag(), but I'd look into designing it differently.

Categories

Resources