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 );
}
}
-
can you log value of imageResource after int imageResource = getResources().getIdentifier( "@drawable/stone_a1", null, getPackageName() );RBK– RBK2015年09月19日 13:04:05 +00:00Commented Sep 19, 2015 at 13:04
5 Answers 5
set in ImageView
iv.setImageResource(R.drawable.image)
Comments
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
2 Comments
if ( position == "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..
Comments
I found the problem.
It must be: if ( position.equals( "0" ) ) In addition i am using only: imageView.setImageResource( R.drawable.stone_a1 )
Comments
ImageView imageView = new ImageView(this);
Bitmap bmp = BitmapFactory.decodeResource(getResources(), R.drawable.image);
imageView.setImageBitmap(bmp);