1

Hello I try to explain what I mean but it is as following:

<ImageView
android:id="@+id/imageLogo"    
android:src="@drawable/logo"        
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</ImageView>

I want the src to be an string so it is easy to edit for other users that need to use this. The problem is that the android:src doesn't accept an @string/logo with the file name in it.

Edit: To explain it a bit more: I want my image src be flexible so if a client of mine wants to change his/her logo he/she just need to go to the string.xml and change the value the string has in the name the logo has. But now when I think about it they already need to acces the drawable folder to change their image so thanks for all the help :D

Napokue
  • 105
  • 13

3 Answers3

2
int resourceId = Activity.getResources().getIdentifier(getString(R.string.myComapnyLogo), "drawable", "your.package.name");
image.setImageResource(resourceId);

Here you are using getString(R.string.myComapnyLogo) to get the String from xml file at runtime. Refer this documentation.

Sagar Pilkhwal
  • 3,998
  • 2
  • 25
  • 77
2

You can't have src as string in xml.

but you write your own code in your calling activity to set image source using string resourse. for this you have to put your image file in asset , and you can refer below code

    ImageView view  = findViewById(R.id.imageLogo);
       AssetManager assetManager = getAssets();
        InputStream istr = null;
        try {
            //R.string.yourstring will be like "sample.png"
            istr = assetManager.open(getResources().getString(R.string.yourstring));
        } catch (IOException e) {
            e.printStackTrace();
        }
        Bitmap bitmap = BitmapFactory.decodeStream(istr);
    view.setImageBitmap(bitmap);
1

android:src refers to drawable image resource and you cannot change it as String resource. if required you can change the name of the image as android:src="@drawable/imagename" and rename the file in corresponding directory

  • I'm making this app for a client of mine and a requirement is that you can edit the image src for instance in the strings.xml for easy editting. This app will be a kind of template app so we can integrate it in other apps. EDIT: You are right about what you say and it is possible for me to do that but it is not optimal. – Napokue Sep 10 '14 at 11:50