Dice Emulation - ImageView

Posted by Michelle Harris on Stack Overflow See other posts from Stack Overflow or by Michelle Harris
Published on 2012-06-10T16:31:49Z Indexed on 2012/06/10 16:40 UTC
Read the original article Hit count: 200

Filed under:
|
|
|

I am trying to emulate dice using ImageView. When I click the button, nothing seems to happen. I have hard coded this example to replace the image with imageView4 for debugging purposes (I was making sure the random wasn't fail). Can anyone point out what I am doing wrong? I am new to Java, Eclipse and Android so I'm sure I've probably made more than one mistake.

Java:

import java.util.Random;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.Spinner;
import android.widget.Toast;

public class Yahtzee4Activity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);


        Spinner s = (Spinner) findViewById(R.id.spinner);
        ArrayAdapter adapter = ArrayAdapter.createFromResource(
                this, R.array.score_types, android.R.layout.simple_spinner_dropdown_item);
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        s.setAdapter(adapter); 
    }

    public void onMyButtonClick(View view)  
    {  
        ImageView imageView1 = new ImageView(this);
        Random rand = new Random();     
        int rndInt = 4; //rand.nextInt(6) + 1; // n = the number of images, that start at index 1
        String imgName = "die" + rndInt;
        int id = getResources().getIdentifier(imgName, "drawable", getPackageName());  
        imageView1.setImageResource(id);  
    }  



}

XML for the button:

 <Button
              android:id="@+id/button_roll"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:text="@string/roll"
              android:onClick="onMyButtonClick"   
          />    

© Stack Overflow or respective owner

Related posts about java

Related posts about android