0

I Have a button with a top drawable image, onclicking the button a intent is created i want to pass this drawable to the intent. How to do it pls help me out thanks in advance..

Button

My Button

Image to be showed on next activity

Image to be showed on next activity

Prashanth
  • 341
  • 1
  • 4
  • 14

2 Answers2

2

Assuming that your buttons are all atrological signs, and that you know what image resource you are using for each, why don't you just assign all your buttons the same onClick listener, and use a switch case to determine which sign it is.

public void onClick(View v) {
    Intent i = new Intent(this, yourSecondActivity.class);      
    switch(v.getId()){
        case R.id.ariesBtn:
            i.putExra("sign", "aries");
            break;

        case R.id.cancerBtn:
            i.putExtra("sign", "cancer");
            break;

    }

    startActivity(i);
}

Then in your other activity check for extras "sign" and act accordingly

David Ferrand
  • 5,357
  • 1
  • 33
  • 43
erik
  • 4,946
  • 13
  • 70
  • 120
  • hmmm i thought of it but can u help me how to find the image from the drawable resources. For example if its aries image is R.Drawable.ic_aries should i use if statements for all the 12 images – Prashanth Jul 07 '14 at 13:37
  • so i thought if i could find the resource id of the drawable associated with the button i can pass the resource id and set the image accordingly. but i dont know how to find the resource id can u help me – Prashanth Jul 07 '14 at 13:40
  • The Resources class has this method: public int getIdentifier (String name, String defType, String defPackage) Which returns the integer of the specified resource name, type & package. taken from: http://stackoverflow.com/questions/3476430/how-to-get-a-resource-id-with-a-known-resource-name – erik Jul 08 '14 at 13:05
0

Try this:
@drawable/filename

or post the code to better undertastand

ygrunin
  • 325
  • 1
  • 4
  • 15