0

I have an activity where the user can select their character. I have three image buttons on the screen and when the user clicks the character they want, I want the game to load another activity that will then just show an image of the character they selected in the previous screen. Basically this is just the forerunner for what I am going to do later. I found some examples on this website for how to accomplish this but I haven't quite gotten it all ironed out.

This is what I have in my character selection activity:

Button archerButton = (Button) findViewById(R.id.Button_Archer);
archerButton.setOnClickListener(new View.OnClickListener() {
    public void onClick(View v) {
        Intent intent = new Intent(SelectCharacterActivity.this, LevelOneActivity.class);
        intent.putExtra("@drawable/archer", pathToImage);
        startActivity(intent);
        finish();
        }
    });

The pathToImage line is throwing an error. What exactly am I supposed to put here?

The LevelOne activity which is supposed to just display the image chosen has this:

    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
    setContentView(R.layout.level_one);



    String path = getIntent().getStringExtra("imagePath");
    Drawable image = Drawable.createFromPath(path);
    Character_Chosen.setImageDrawable(image);
    }

I'm a bit confused on this section as well. The Character_Chosen entry is the name of the imageview which should house the selected image.

I'm also confused on this line of code:

String path = getIntent().getStringExtra("imagePath");

Does this mean I have to manually enter the image path every time? What if they choose a different image?

Does anyone have a link to an actual working example? Pseudo code doesn't really help me very much when I'm a novice and don't know what needs to stay and what needs to go.

Rhendar
  • 410
  • 7
  • 25

3 Answers3

0

I would take an alternative approach, instead of passing the path to every activity you want to show the image(or say avatar).
I would save it in Global Application Object(by extending Application) either a bitmap loaded into memory or path to image and just access or change it in one place and access that value in all the activities.

sat
  • 40,138
  • 28
  • 93
  • 102
0

First of all convert the resource into id

Button archerButton = (Button) findViewById(R.id.Button_Archer);
archerButton.setOnClickListener(new View.OnClickListener() {
   public void onClick(View v) {

    int id =getResources().getIdentifier("imagename", "drawable", getPackageName());  

    Intent intent = new Intent(SelectCharacterActivity.this, LevelOneActivity.class);
    intent.putExtra("image_id", id);
    startActivity(intent);
    finish();
    }
});

LevelOne.java

public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
setContentView(R.layout.level_one);



int imageId=getIntent().getIntExtra("image_id", 0);
Character_Chosen.setImageResource(imageId)
}

Same way can be done for all the remaining buttons.

Happy Coding :)

moDev
  • 5,248
  • 4
  • 33
  • 63
0

I made it running by doing a different approach.

MainActivity.java

What I did is I pass the R.drawable reference in the intent instead using the string path.

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

    btnArcher = (Button)findViewById(R.id.button_archer);

    btnArcher.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            Intent intent = new Intent(MainActivity.this,LevelOne.class);
            intent.putExtra("archer_drawable", R.drawable.archer);
            startActivity(intent);
        }
     });

}

On levelOne.java is I used the getIntExtra using the name that I specified in the MainActivity.java to get the R.drawable resource reference of the archer image( you can assign a name whatever you want ). Finally use the integer value that you got from getIntExtra and use it on the the image view by calling the method setImageResource(int resId).

ImageView img;
int drwResource;

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

    //find the ImageView fron level_one.xml
    img = (ImageView)findViewById(R.id.character_image);

    drwResource = getIntent().getIntExtra("archer_drawable", -1);   
    img.setImageResource(drwResource);

}

level_one.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >
    <ImageView
        android:id="@+id/character_image"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true" />
</RelativeLayout>

I hope this helps. :)

Ariel Magbanua
  • 3,083
  • 7
  • 37
  • 48