1

I want to change the resource of an image view from a variable in android studio. Let me explain you, i have first activity "selector activity" which has 21 images, on every image click, a variable god_name is changed to the particular god, now i am sending that variable using putExtra, now i want that to change the resource of an ImageView in MainActivity depending on the variable, for example if the variable is "two", then i want to change the resource to "R.drawable.two". Something like that, I have some experience in python so I used f strings there, here can I do something?

MainActivity.class (Note-I have not Included imports so the code is allowed to be uploaded.)

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        String god_name = getIntent().getExtras().getString("chosen_god").toString();

        ImageView god = findViewById(R.id.god);
        god.setImageResource();
    }
}

Activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/black"
    tools:context=".MainActivity">

    <ImageView
        android:id="@+id/god"
        android:layout_width="413dp"
        android:layout_height="496dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.666"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.0"
        app:srcCompat="@mipmap/hanuman" />
</androidx.constraintlayout.widget.ConstraintLayout>

I can use multiple if conditions and switch case statement but that would be so much large as I would have to write it 21 times so it is very difficult! Thanks in advance for anyone kind of help!

1 Answers1

2

That sounds like you need to use the resources.getIdentifier() to make this work.

I wrote once an answer to a related topic: https://stackoverflow.com/a/4865350/180538

Anyway what you need is to pass the name of the resource and then resolve the identifier to use it. So something like this:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    // make sure that god_name is the name of the resource, so AFTER R.mipmap.[resourceName]
    // so for R.mipmap.two you need to pass "two" as "chosen_god" extra
    String god_name = getIntent().getExtras().getString("chosen_god").toString();

    ImageView god = findViewById(R.id.god);
    int resId = getResources().getIdentifier(god_name, "mipmap", getPackageName());
    god.setImageResource(resId);
}
WarrenFaith
  • 57,492
  • 25
  • 134
  • 150
  • Thanks, it worked!, I tried the same thing earlier but it was returning 0, i was mad from tomorrow evening that why isn't it working?, But thanks again :-) – Parvanshu Sharma Feb 09 '21 at 10:13