• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps Dynamically Load Images from resources

I know there has to be an easy way to do this, but I cant seem to figure it out.
I'm trying to load a table layout with 2 columns.
The column is a picture, and the 2nd column is text.

I have been able to do this statically by doing something like this...
<​
TableRow>

<ImageView

android:id="@+id/image2"

android:adjustViewBounds="true"

android:maxWidth="100sp"

android:maxHeight="100sp"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:src="@drawable/a_2"
/>

<TextView

android:layout_width="fill_parent"

android:layout_height="fill_parent"

android:layout_gravity="left"

android:text="@string/Picture2Name"
android:singleLine="false"

/>

</TableRow>

However, I am looking for a way to dynamically create this so I dont have to code the xml for every picture.
Yet I cant seem to find a way to do this or dynamically change the image to a picture that is in the resource drawable directory.
Any help or pointers would be appreciated.
 
You could probably try something like this, event though I have not tried it, it's what I would try:

Create a new .xml file containing just the code you showed here and call it eg. tablerow.xml - but give the textview and imageview seperate id's eg. "tablerow_text" and "tablerow_img".

Then, in you code, as you want to add new rows to your TableLayout, use a layout inflater to create new tablerows from tablerow.xml, then add the text and the src to the inflated view's textview and imageview and then add the inflated view to the TableLayout.
 
Why not just create a method in a Utilities class that programmatically creates a new ImageView based on the parameters and, optionally added to a tableView also given in the parameters.
 
Back
Top Bottom