android: assigning a number to a specific image -


i creating a card game using standard deck of 52 playing cards. want generate random number between 0 , 52 , depending on number, draw assigned card number. how go doing that?

ok, let's suppose have activity this:

public void oncreate (bundle savedinstancestate){     super.oncreate(savedinstancestate);     setcontentview(r.layout.mainlayout);      imageview imageview = (imageview) findviewbyid(r.id.mycard);      randon ran = new random();     int number = ran.nextint(51);      switch(number){         case (0):             imageview.setimageresource(r.drawable.card0);         case (1):             imageview.setimageresource(r.drawable.card1);         case (2):             imageview.setimageresource(r.drawable.card2);         //rest of cases     } } 

now let's suppose have mainlayout.xml file inside res/layout folder looks this:

<?xml version="1.0" encoding="utf-8"?> <linearlayout xmlns:android="http://schemas.android.com/apk/res/android"     android:orientation="vertical"     android:layout_width="match_parent"     android:layout_height="match_parent">  <imageview     android:id="@+id/mycard"     android:src="@drawable/initialviewofthecard"     android:layout_width="match_parent"     android:layout_width="match_parent"/>  </linearlayout> 

there have! it's simple think you'll got idea!

edit: suggest investigate how use gridlayouts in layouts! make things lot easier in order show lot of cards @ same time!

edit2: can model card pojo drawable id , card number attributes! can have list of card objects , use listadapter in order populate gridview!


Popular posts from this blog

c# - ODP.NET Oracle.ManagedDataAccess causes ORA-12537 network session end of file -

matlab - Compression and Decompression of ECG Signal using HUFFMAN ALGORITHM -

utf 8 - split utf-8 string into bytes in python -