How to set Icon to ImageView through code.

Put problem concerning Views, Layouts and other XML-Resources (like AndroidManifest) here.

How to set Icon to ImageView through code.

Postby nbagade » Sun Jan 03, 2010 8:00 am

Hi all,

I have layout file like below.I have two imageView .for this image view i want to set icon through
dynamically.because each time icon is changing.
can u help how to set image for Imageview through my activity.

Syntax: [ Download ] [ Hide ]
Using xml Syntax Highlighting
  1.  
  2. <?xml version="1.0" encoding="utf-8"?>
  3.  
  4. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  5.  
  6.    android:orientation="vertical"
  7.  
  8.    android:layout_width="fill_parent"
  9.  
  10.    android:layout_height="fill_parent"
  11.  
  12.    android:background="@drawable/recipe"
  13.  
  14.    >
  15.  
  16. <ImageView
  17.  
  18.    android:id="@+id/ImageView01"
  19.  
  20.    android:layout_width="wrap_content"
  21.  
  22.    android:layout_height="wrap_content"
  23.  
  24.    android:layout_marginTop="90sp"
  25.  
  26.    android:layout_marginLeft="11sp"
  27.  
  28.    >
  29.  
  30.  </ImageView>
  31.  
  32.  <ImageView android:id="@+id/ImageView02"
  33.  
  34.    android:layout_width="wrap_content"
  35.  
  36.    android:layout_height="wrap_content"
  37.  
  38.    
  39.  
  40.    android:layout_marginLeft="75sp"
  41.  
  42.    android:layout_marginTop="-40sp">
  43.  
  44.     </ImageView>
  45.  
  46.  <TextView  
  47.  
  48.    android:id="@+id/TextView01"
  49.  
  50.    android:layout_width="fill_parent"
  51.  
  52.    android:layout_height="wrap_content"
  53.  
  54.    android:layout_marginTop="-14sp"
  55.  
  56.    android:layout_marginLeft="95sp"
  57.  
  58.    android:layout_marginRight="10sp"
  59.  
  60.    
  61.  
  62.    android:textColor="#ffffff00"/>
  63.  
  64.     <TextView  
  65.  
  66.    android:id="@+id/TextView02"
  67.  
  68.    android:layout_width="fill_parent"
  69.  
  70.    android:layout_height="wrap_content"
  71.  
  72.    android:layout_marginTop="1sp"
  73.  
  74.    android:layout_marginLeft="95sp"
  75.  
  76.    android:layout_marginRight="10sp"
  77.  
  78.    android:text="Ingradients:"
  79.  
  80.    android:textColor="#ffffffff"/>
  81.  
  82.  <TextView  
  83.  
  84.    android:id="@+id/TextView03"
  85.  
  86.    android:layout_width="fill_parent"
  87.  
  88.    android:layout_height="wrap_content"
  89.  
  90.    android:layout_marginTop="10sp"
  91.  
  92.    android:layout_marginLeft="10sp"
  93.  
  94.    android:layout_marginRight="10sp"
  95.  
  96.    android:textSize="12dip"
  97.  
  98.      
  99.  
  100.    />  
  101.  
  102.     <ImageView android:id="@+id/ImageView03"
  103.  
  104.    android:layout_width="wrap_content"
  105.  
  106.    android:layout_height="wrap_content"
  107.  
  108.    android:src="@drawable/serve"
  109.  
  110.    android:layout_marginLeft="10sp"
  111.  
  112.    android:layout_marginTop="4sp">
  113.  
  114.     </ImageView>  
  115.  
  116.     <TextView  
  117.  
  118.    android:id="@+id/TextView04"
  119.  
  120.    android:layout_width="65sp"
  121.  
  122.    android:layout_height="wrap_content"
  123.  
  124.    android:layout_marginTop="-18sp"
  125.  
  126.    android:layout_marginLeft="30sp"
  127.  
  128.    android:layout_marginRight="10sp"
  129.  
  130.    
  131.  
  132.    android:textSize="12dip"/>
  133.  
  134.    
  135.  
  136.     <ImageView android:id="@+id/ImageView04"
  137.  
  138.    android:layout_width="wrap_content"
  139.  
  140.    android:layout_height="wrap_content"
  141.  
  142.    android:src="@drawable/clock"
  143.  
  144.    android:layout_marginLeft="100sp"
  145.  
  146.    android:layout_marginTop="-15sp">
  147.  
  148.     </ImageView>
  149.  
  150.     <TextView  
  151.  
  152.    android:id="@+id/TextView06"
  153.  
  154.    android:layout_width="75sp"
  155.  
  156.    android:layout_height="wrap_content"
  157.  
  158.    android:layout_marginTop="-19sp"
  159.  
  160.    android:layout_marginLeft="123sp"
  161.  
  162.    android:layout_marginRight="10sp"
  163.  
  164.    
  165.  
  166.    android:textSize="12dip"/>
  167.  
  168.      
  169.  
  170.     <TextView  
  171.  
  172.    android:id="@+id/TextView08"
  173.  
  174.    android:layout_width="fill_parent"
  175.  
  176.    android:layout_height="wrap_content"
  177.  
  178.    android:layout_marginTop="4sp"  
  179.  
  180.    android:layout_marginLeft="10sp"
  181.  
  182.    android:layout_marginRight="10sp"
  183.  
  184.    android:text="Method:"
  185.  
  186.    android:textColor="#ffff0000"/>      
  187.  
  188.     <TextView  
  189.  
  190.    android:id="@+id/TextView09"
  191.  
  192.    android:layout_width="fill_parent"
  193.  
  194.    android:layout_height="wrap_content"    
  195.  
  196.    android:layout_marginLeft="10sp"
  197.  
  198.    android:layout_marginRight="10sp"
  199.  
  200.    android:textSize="12dip"    
  201.  
  202.        />
  203.  
  204. </LinearLayout>
  205.  
  206.  
  207.  
  208.  
Parsed in 0.011 seconds, using GeSHi 1.0.8.4



Thanks
nbagade
Experienced Developer
Experienced Developer
 
Posts: 58
Joined: Fri Nov 20, 2009 7:49 am
Location: India

Top

Postby faisalloe » Sun Jan 03, 2010 8:13 pm

findViewbyId(R.id.imageViewId).setImageDrawable(getResource().getDrawable(R.drawable.localimage))
faisalloe
Developer
Developer
 
Posts: 29
Joined: Wed Nov 04, 2009 5:01 am

Postby serkan.dogantekin » Mon Jan 04, 2010 3:33 pm

I think you can also do:

((ImageView)findViewbyId(R.id.imageViewId)).setImageResource(R.drawable.localimage)
serkan.dogantekin
Developer
Developer
 
Posts: 27
Joined: Thu Jun 04, 2009 6:12 pm
Location: Istanbul/Turkey

Call drawable from Content Provider

Postby Exeter » Tue Jan 05, 2010 7:20 pm

Below is sample code to return drawables from a Content Provider.

I haven't been able to test it b/c i'm debugging my Content Provider (which is why it's not included).
However, you might want to fix the do while in CreateArrayInteger method as it likely returns one too many ","

http://www.anddev.org/call_drawable_fro ... 10028.html

Hope it helps.


Cheers,

Exeter
Exeter
Junior Developer
Junior Developer
 
Posts: 18
Joined: Tue Dec 22, 2009 4:17 pm

Top

Return to View, Layout & Resource Problems

Who is online

Users browsing this forum: No registered users and 14 guests