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 );
 }
 }
asked Sep 19, 2015 at 11:28
1
  • can you log value of imageResource after int imageResource = getResources().getIdentifier( "@drawable/stone_a1", null, getPackageName() ); Commented Sep 19, 2015 at 13:04

5 Answers 5

1

set in ImageView

iv.setImageResource(R.drawable.image)
David Buck
3,88640 gold badges54 silver badges74 bronze badges
answered Aug 2, 2020 at 13:36
Sign up to request clarification or add additional context in comments.

Comments

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

answered Sep 19, 2015 at 11:31

2 Comments

@Ezazel, Have you tried debugging it. I think problem lies in if ( position == "0" )
Yes you are right. It must be: if ( position.equals( "0" ) ) In addition i am using only: imageView.setImageResource( R.drawable.stone_a1 )
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..
answered Sep 19, 2015 at 12:05

Comments

0

I found the problem.

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

answered Sep 19, 2015 at 15:46

Comments

0
ImageView imageView = new ImageView(this);
Bitmap bmp = BitmapFactory.decodeResource(getResources(), R.drawable.image);
imageView.setImageBitmap(bmp);
Ziem
6,7278 gold badges57 silver badges86 bronze badges
answered Sep 19, 2015 at 11:37

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.