2

I am a new in android programming. I want to set dynamically images in one xml, when this displayed. Specifically, i am storing my project related images in drawable folder. Also I am storing the image names in string variable and dynamically I am trying to set those images to the imageview. But the image is not displaying.

My code:

    protected void onCreate( Bundle savedInstanceState ) 
    {
       super.onCreate( savedInstanceState );
       setContentView( R.layout.stone );    

       imageView = ( ImageView )findViewById( R.id.stone_xxxx );

       Intent intent = getIntent();
       position = intent.getStringExtra( "POSITION" );

       if ( position == "0" )
       {
         int imageResource = getResources().getIdentifier(     "@drawable/stone_a1", null, getPackageName() );
        Drawable res = getResources().getDrawable(imageResource);
        imageView.setImageDrawable( res );
        }
    }
Panagiotis
  • 511
  • 8
  • 26
  • can you log value of imageResource after int imageResource = getResources().getIdentifier( "@drawable/stone_a1", null, getPackageName() ); – RBK Sep 19 '15 at 13:04

5 Answers5

1

set in ImageView

iv.setImageResource(R.drawable.image)
David Buck
  • 3,752
  • 35
  • 31
  • 35
0

Try this

final int sdk = android.os.Build.VERSION.SDK_INT;
if(sdk < android.os.Build.VERSION_CODES.JELLY_BEAN) {
    imageView.setBackgroundDrawable( getResources().getDrawable(R.drawable.stone_a1) );
} else {
    imageView.setBackground( getResources().getDrawable(R.drawable.stone_a1));
}

for more help visit here https://stackoverflow.com/a/12523109/5202007

Community
  • 1
  • 1
Mohammad Tauqir
  • 1,817
  • 1
  • 18
  • 53
0
ImageView imageView = new ImageView(this);
Bitmap bmp = BitmapFactory.decodeResource(getResources(), R.drawable.image);
imageView.setImageBitmap(bmp);
Ziem
  • 6,579
  • 8
  • 53
  • 86
akhil batlawala
  • 1,066
  • 1
  • 10
  • 30
0

Hope it will work for you..

imagev1=(ImageView) findViewById(R.id.imageViewslider22);
            final int []imageArray={R.drawable.img1,R.drawable.img2,R.drawable.img3,R.drawable.img4,R.drawable.slider3,R.drawable.slider4,R.drawable.slider5,R.drawable.slider6,R.drawable.slider7};

            final Handler handler = new Handler();
            Runnable runnable = new Runnable() {
            int i=0;
            public void run() {
                        imagev1.setImageResource(imageArray[i]);
                            i++;
                                if(i>imageArray.length-1)
            {
            i=0;    
            }
            handler.postDelayed(this, 30000);  //for interval...
            }
            };
            handler.postDelayed(runnable, 2000); //for initial delay..
MRP192
  • 7
  • 6
0

I found the problem.

It must be: if ( position.equals( "0" ) ) In addition i am using only: imageView.setImageResource( R.drawable.stone_a1 )

Panagiotis
  • 511
  • 8
  • 26