Choosing Drawable to show on a imageview programmatically - android

I have say N images that i want to include in my android project resources, say image0 to imageN-1, how do I set an image view to show one of those drawables say imagei when i is known only at runtime?
something like that:
imageView.setImageDrawable(getResources().getDrawable("R.drawable.image"+i));
obviously that wouldnt work but you know what i mean...
Thanks!

You can use Resources.getIdentifier() as in this blog explained

Related

AndroidStudio Adding an image to draw with srcCompat

I am trying to do a drawer activity with Android Studio and I'm wanting to personalize the icons used. I've downloaded the icons I want to use and for the most part, it's okay.
In fact, I can modify all the android:icon icons but I have no idea how to modify the app:srcCompat icons. I don't know how to detect the image I want to use in this case...
Do you know how I can do it? Could you explain this to me?
Thanks a lot!
Edit: The file I'm working on as a base is the generated project with Drawer activity as the main activity. For example, if I wanted to modify the icon in the nav_header_main.xml file, how could I do to have my image (.png) transformed in a way detected by Android Studio as compatible with srcCompat
Ensure your image file is in res/drawable folder(or drawable-xhdpi/xxhdpi, determined by your design)
Refer it by app:srcCompact="#drawable/$IMAGE_FILE_NAME" in your xml.
Then you should see what you want.
Please check the doc to further understand the official usage.
Well... I was blind! I was wondering how I could add my icons but they were, I was just mistaking on the address... #android:drawable/... is obviously not #drawable/... but I didn't notice. So be aware that there is a difference and it'll be okay I think!

How to make a spinner with icons in a form of table layout dynamically?

Anyone know how can i achieve a list like this in the picture? I want to add the images in the project, and create a layout that will pick them up and put them automatically in the layout. Not manually to import all images in the layout.
So like this if i add a new image in the app then it will be picked automatically.
I didnt try anything yet as am not sure how to do it.
All i found in the web is layouts with each row having 1 icon, but thats not what i want.
Any info or a link i can use anyone?
You just can't do that with a Spinner.
You need to use a GridView.
Normally we won't use TableLayout if there are so many images, which will cause Out of Memory Error if too many images loaded. Use GridView instead, very easy and meet your requirements. Look into ApiDemos for some sample codes.
I managed to solve this. Here is the link that helped me out: http://iserveandroid.blogspot.com/2010/12/custom-gridview-in-dialog.html

Custom UI which looks like design image

I have a design Mockup of a remote control with 4 buttons and a Text Display. Now i want to create an Android Activity which looks like this mockup. How can i do this.
I think taking the design image as background for the activity is not the right way to do this (how would i make the buttons on the image clickable, etc...)
Can anybody give me a hint how to start here?
kind regards
You should create 9-patches(or patterns) for each graphic element on your layout for proper layouting on all available devices.
Then apply this patterns to your UI elemnt's(ImageView's, Buttons, TextViews) backgrounds.
Refernces:
http://developer.android.com/tools/help/draw9patch.html - 9-patch documentation
http://android-ui-utils.googlecode.com/hg/asset-studio/dist/index.html - 9-patch creation
http://developer.android.com/guide/topics/ui/declaring-layout.html - Layouting
You can use your background easily but the rest need to be done in Android.
First : download android sdk
second : create your project
third : code :)
If you're familiar with HTML, that should be kinda similar. You separate the cliclable parts from the background (the buttons), and make them ImageView, setting an onClickListener.
You'll need some Photoshop skills to do that, or ask a buddy designer to do that for you :)
Be careful with multiple screen support, since the background may get trickier to make it look right on every supported screen size.

How to style TextViews that look like "cards"?

I want to create an android style that allows me to put TextViews, pictures, and other elements within the "cards" as demonstrated in the link below. I think that design really adds depth and helps separate content from the background and I'd like to use it in my app. However, I don't know how to create that. If you could help that'd be greatly appreciated! :-)
android card design example
Those TextView just have a custom background. This may be
A 9-patch image
A xml-drawable
I suggest reading on 9-patch drawables and on android drawables in general.
The reason why I think it's a 9-patch is that this "card" has a slight drop shadow.
See if this Library helps you
http://www.androidviews.net/2012/12/cardsui/

Android: Use different drawables after choosing a different theme, keeping the same references

Is possible to make the app use different drawables after choosing a theme in Android?
An explanatory example:
i have a layout which background uses a reference to: "#drawable/backgroundsolid", that is image backgroundsolid.png in res/drawable-mdpi.
I want that, if the user choose "Glass" theme, the reference stays to "#drawable/backgroundsolid" but the resources folder is changed to res/drawable-glass, which contains a different backgroundsolid.png image.
Is it possible to set this programmatically? Thanks a lot!
You can set it programmatically by using view.setBackgroundResourceId(). I am not sure if you do it the other way which is let android pick the right theme for you. Maybe someone else has a better answer

Categories

Resources