I have a set of images in my drawable resources. Now I want to be able to change a placeholder image accordingly with image.setImageResource(R.id.name_of_image) programmatically.
My ImageView is defined as ImageView image = findViewById(R.id.placeholder);
How would I do this? I have tried getIdentifier but that didn't work.
The images have the format Name.jpg
EDIT:
I currently have the following construction:
int resID = getResources().getIdentifier(name.toLowerCase(), "drawable", this.getPackageName());
ImageView image = findViewById(R.id.placeholder);
image.setImageResource(resID);
I have changed the image name to lowercase and it is now a png file.
Your images has to be in png format (prefered by android) and must contain only lowercase letters and digits ([a-z0-9_.].
Then you can use
image.setImageResource(R.drawable.name_of_image);
You can get the name of the drawable with this:
String name = context.getResources().getResourceEntryName(R.drawable.name_of_image);
To get the drawable with the string name:
int id = context.getResources().getIdentifier("name_of_image", "drawable", context.getPackageName());
image.setImageResource(id);
Related
For a scanning ticket app, I want to display full screen image.
The app does sequential operation. You scan your QrCode (first image), the device reach server (second image indicating to wait), then I display a third image if the ticket is validate, else I display the last one.
I'm using glide and the image path is indicated in a second activity, I transmit URI to main activity with a sharedPreferences file.
Here is the issue:
val settings = getSharedPreferences("PrefFile", 0)
if (S_mod){//priority on S_mod
uri_State_init = Uri.parse(settings.getString("mainUri", "none"))
uri_State_waiting = Uri.parse(settings.getString("waitUri", "none"))
uri_State_validated = Uri.parse(settings.getString("okUri", "none"))
uri_State_refused = Uri.parse(settings.getString("errorUri", "none"))
displayUri( uri_State_init, background)
image.setImageResource(R.drawable.empty)
}
where displayUri is
fun displayUri( uri: Uri?, dir:ImageView){//use to display image known by uri, only for full screen
Glide.with(this)
.load(uri)
.error(R.drawable.imagenotfound) // image in case of error
.override(1280, 800) // resizing if user doesn't respect the indicated dim
.centerCrop()//type of resizing
.into(dir)
}
The issue is that only the first image called by displayUri is displayed, the other call show the error image (imagenotfound)
It seem that I've not totally understood the glide extension.
If someone know about this particular issue thanks a lot!!
Here
val settings = getSharedPreferences("PrefFile", 0)
if (S_mod){//priority on S_mod
uri_State_init = Uri.parse(settings.getString("mainUri", "none"))
uri_State_waiting = Uri.parse(settings.getString("waitUri", "none"))
uri_State_validated = Uri.parse(settings.getString("okUri", "none"))
uri_State_refused = Uri.parse(settings.getString("errorUri", "none"))
displayUri( uri_State_init, background)
image.setImageResource(R.drawable.empty)
}
displayUri( uri_State_init, background)
You are just calling the display Image function with the same uri which means if you didn't passed the uri, then uri will be null and you will definitely get errors. What you can do is to send uri like this ,
val settings = getSharedPreferences("PrefFile", 0)
if (S_mod){//priority on S_mod
uri = Uri.parse(settings.getString("main_uri", "none"))
displayUri( uri_State_init, background)
image.setImageResource(R.drawable.empty)
}
Just change the value of main_uri in Shared Preferences whenever you want to change the state. You can also use onSharedPreferencesChanged Listener.
Is there a way to add a drawable in any position within a text view programmatically without having to position it on a particular side of a text view? The following code works when using unicode character but I want to try the same with a vector drawable.
textView.text = getString(R.string.app_settings) + " \u2794 " + getString(R.string.display)
For me, ImageSpan works.
You can put a delimiter and replace it with the drawable. I used a google icon in this example
:
Code with delimiter replacement:
Drawable drawable = ContextCompat.getDrawable(this, R.drawable.google_icon);
drawable.setBounds(0, 0, 100,100);
String text = " Google %google_icon% icon";
String delimiter = "%google_icon%";
int icon_index = text.indexOf("%google_icon%");
text = text.replace(delimiter," ");
Spannable span = new SpannableString(text);
ImageSpan image = new ImageSpan(drawable, ImageSpan.ALIGN_BASELINE);
span.setSpan(image, icon_index, icon_index+1, Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
textView.setText(span);
Or, you can place the drawable on any index like:
span.setSpan(image, start_index, end_index, Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
PS: I used Display1 in text appearance. You need to change drawable bounds according to your own needs.
I have an SVG string
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 60 30" width="1200" height="600"><clipPath id="a"><path d="M30 15h30v15zv15h-30zh-30v-15zv-15h30z"/></clipPath><path d="M0 0v30h60v-30z" fill="#00247d"/><path d="M0 0l60 30m0-30l-60 30" stroke="#fff" stroke-width="6"/><path d="M0 0l60 30m0-30l-60 30" clip-path="url(#a)" stroke="#cf142b" stroke-width="4"/><path d="M30 0v30m-30-15h60" stroke="#fff" stroke-width="10"/><path d="M30 0v30m-30-15h60" stroke="#cf142b" stroke-width="6"/></svg>
How do I render it on ImageView?
If this was .svg file, I can load it with Glide in Android or other SVG library but don't know how to get around it with this svg string.
Try this out I think this may help you.let me know if this works
String yourSvgFile = "Your svg file";
InputStream stream = new ByteArrayInputStream(yourSvgFile.getBytes(StandardCharsets.UTF_8));
Glide.with(this).load(stream).into(yourView)
Accepted Answer does not really help me as Glide was throwing an exception asking to create a ModelLoader for type ByteArrayInputStream.Adding to the #Aweda's answer I used the AndroidSVG library as below.
Dependencies
//AndroidSVG
implementation 'com.caverock:androidsvg-aar:1.4'
//Glide (add only if you're going to use Glide to load the image)
implementation 'com.github.bumptech.glide:glide:4.12.0'
kapt 'com.github.bumptech.glide:compiler:4.12.0'
Code snippet
//SVG string content
val svgString =
"<svg xmlns=\"http://www.w3.org/2000/svg\" viewBox=\"0 0 60 30\" width=\"1200\" height=\"600\"><clipPath id=\"a\"><path d=\"M30 15h30v15zv15h-30zh-30v-15zv-15h30z\"/></clipPath><path d=\"M0 0v30h60v-30z\" fill=\"#00247d\"/><path d=\"M0 0l60 30m0-30l-60 30\" stroke=\"#fff\" stroke-width=\"6\"/><path d=\"M0 0l60 30m0-30l-60 30\" clip-path=\"url(#a)\" stroke=\"#cf142b\" stroke-width=\"4\"/><path d=\"M30 0v30m-30-15h60\" stroke=\"#fff\" stroke-width=\"10\"/><path d=\"M30 0v30m-30-15h60\" stroke=\"#cf142b\" stroke-width=\"6\"/></svg>"
//convert SVG string to an object of type SVG
val svg = SVG.getFromString(svgString)
//create a drawable from svg
val drawable = PictureDrawable(svg.renderToPicture())
//finally load the drawable with Glide.
Glide.with(this)
.load(drawable)
.into(yourImageView)
In case you do not want to use Glide and want to set the SVG image to ImageView directly, do as follow:
yourImageView.setImageDrawable(drawable);
In Android, I have only found answers as to how to open a single specific Drawable from MainActivity.java, but not how to iterate over each Drawable from res/drawables. The Drawables names do not follow any patterns (e.g. being numbered from 0 to 25), so the answer suggested here sadly doesn't solve my problem. Does anyone know how to do the latter?
Thank you in advance :)
First, put your drawables into an arrays
<array name="dashboard_item_menu_drawable">
<item>#drawable/ic_file_green</item>
<item>#drawable/ic_email_green</item>
<item>#drawable/ic_linear_scale_green</item>
<item>#drawable/ic_undo_green</item>
<item>#drawable/ic_check_circle_green</item>
<item>#drawable/ic_archive_green</item>
</array>
Then, iterate your array drawables
val icons = ArrayList<Int>()
val arr = resources.obtainTypedArray(R.array.dashboard_item_menu_drawable)
(0 until arr.length()).forEach {
// get resource id of each drawable
val icon = arr.getResourceId(it, -1)
icons.add(icon)
}
Next, recycles resource
arr.recycle()
Then you can use your drawable
iconView.setImageDrawable(ContextCompat.getDrawable(this, icons[index]))
If you want to iterate through drawables that have similar names like: image1, image2, ..., image10 you can do it like this:
for (int i = 0; i < 10; i++) {
int id = getResources().getIdentifier("image" + i, "drawable", getPackageName());
Drawable d = ContextCompat.getDrawable(this, id);
// your code here
}
The easiest way to do it is to put the names of your drawables in a String array:
String[] symbols = {"first_img", "second_img", "third_img", "fourth_img"};
Then iterate over them like this (I put the images into a GridLayout):
for(String symbol : symbols) {
int id = getResources().getIdentifier(symbol, "drawable", getPackageName());
ImageView img = new ImageView(this);
LinearLayout.LayoutParams imgParams = new LinearLayout.LayoutParams(300, 300);
imgParams.setMargins(30, 30, 30, 30);
img.setLayoutParams(imgParams);
img.setBackgroundResource(id);
symbolGrid.addView(img);
}
For example, the name of the image is got from the following code
Bundle bundle = getIntent().getExtras();
String name = bundle.getString("Name");
And what i would like to do is like
int picId = R.drawable.name;
where name should be replaced by the actual name. How can I do that? Or is there an alternative way so that I could show the picture in ImageView? I tried to put the image in the database which worked, but it makes the database way too large.
Try this
Bundle bundle = getIntent().getExtras();
String name = bundle.getString("Name");
int resId = getResources().getIdentifier(name, "drawable", getPackageName());