Saturday, March 23, 2013

Create a Poker Game in Android

Firstly i told you a very true its my first running project. When i create that project in beginning i have no basic idea in android. But devloper.android.com its very helpful for me to understand this topic and that subject.
In that project you have lots import these thing like bundle, handler, Message, Activity, Resources, Draw able, Log, Menu, View,and etc.
In that project you know how to handle of C because if you don't have knowledge of  C then firstly you learn the C, XML, Java.
Then we come to my project and get the random number. And i told you in this project i take 56 cards in that project because it have 52 cads of different suits and four joker because in the champion poker game make a pair joker, four pair of joker and if joker came then payment is double.

First you create a project Poker Game in Eclipse

Go to file section and click on New Android Project

And fill the section.

And then you come on ChampionActivity.java


package com.example.championpoker;

import java.util.Random;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.app.Activity;
import android.content.res.Resources;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.LayerDrawable;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;

public class MainActivity extends Activity
{


Button btnShow;
Button btnShow1;
ImageView ivCard1,ivCard2,ivCard3,ivCard4,ivCard5;
Drawable [] ivCardarray=new Drawable[56];
LayerDrawable layerCard;
Resources g_resource;

//In this place you create suit array & rank array
int suit[]={0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,2,2,2,2,2,2,2,2,2,2,2,2,2,2,3,3,3,3,3,3,3,3,3,3,3,3,3,3};
int rank[]={1,2,3,4,5,6,7,8,9,10,11,12,13,14,1,2,3,4,5,6,7,8,9,10,11,12,13,14,1,2,3,4,5,6,7,8,9,10,11,12,13,14,1,2,3,4,5,6,7,8,9,10,11,12,13,14};

//take 5 final card array because you open this array for display
int FinalCardArr[]= new int[5];

public Handler mHandlerBet=new Handler()
{
public void handleMessage(Message msg)
{
//this function show the cards.
Showfunction();


}
};



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


btnShow=(Button)findViewById(R.id.button1);
btnShow1=(Button)findViewById(R.id.button2);


ivCard1=(ImageView)findViewById(R.id.imageView1);
ivCard2=(ImageView)findViewById(R.id.imageView2);
ivCard3=(ImageView)findViewById(R.id.imageView3);
ivCard4=(ImageView)findViewById(R.id.imageView4);
ivCard5=(ImageView)findViewById(R.id.imageView5);
g_resource=getResources();
//load 52 cards in your mdepi
ivCardarray[0]=g_resource.getDrawable(R.drawable.sa);
ivCardarray[1]=g_resource.getDrawable(R.drawable.s2);
ivCardarray[2]=g_resource.getDrawable(R.drawable.s3);
ivCardarray[3]=g_resource.getDrawable(R.drawable.s4);
ivCardarray[4]=g_resource.getDrawable(R.drawable.s5);
ivCardarray[5]=g_resource.getDrawable(R.drawable.s6);
ivCardarray[6]=g_resource.getDrawable(R.drawable.s7);
ivCardarray[7]=g_resource.getDrawable(R.drawable.s8);
ivCardarray[8]=g_resource.getDrawable(R.drawable.s9);
ivCardarray[9]=g_resource.getDrawable(R.drawable.s10);
ivCardarray[10]=g_resource.getDrawable(R.drawable.sj);
ivCardarray[11]=g_resource.getDrawable(R.drawable.sq);
ivCardarray[12]=g_resource.getDrawable(R.drawable.sk);
ivCardarray[13]=g_resource.getDrawable(R.drawable.joker);
ivCardarray[14]=g_resource.getDrawable(R.drawable.ha);
ivCardarray[15]=g_resource.getDrawable(R.drawable.h2);
ivCardarray[16]=g_resource.getDrawable(R.drawable.h3);
ivCardarray[17]=g_resource.getDrawable(R.drawable.h4);
ivCardarray[18]=g_resource.getDrawable(R.drawable.h5);
ivCardarray[19]=g_resource.getDrawable(R.drawable.h6);
ivCardarray[20]=g_resource.getDrawable(R.drawable.h7);
ivCardarray[21]=g_resource.getDrawable(R.drawable.h8);
ivCardarray[22]=g_resource.getDrawable(R.drawable.h9);
ivCardarray[23]=g_resource.getDrawable(R.drawable.h10);
ivCardarray[24]=g_resource.getDrawable(R.drawable.hj);
ivCardarray[25]=g_resource.getDrawable(R.drawable.hq);
ivCardarray[26]=g_resource.getDrawable(R.drawable.hk);
ivCardarray[27]=g_resource.getDrawable(R.drawable.joker);
ivCardarray[28]=g_resource.getDrawable(R.drawable.da);
ivCardarray[29]=g_resource.getDrawable(R.drawable.d2);
ivCardarray[30]=g_resource.getDrawable(R.drawable.d3);
ivCardarray[31]=g_resource.getDrawable(R.drawable.d4);
ivCardarray[32]=g_resource.getDrawable(R.drawable.d5);
ivCardarray[33]=g_resource.getDrawable(R.drawable.d6);
ivCardarray[34]=g_resource.getDrawable(R.drawable.d7);
ivCardarray[35]=g_resource.getDrawable(R.drawable.d8);
ivCardarray[36]=g_resource.getDrawable(R.drawable.d9);
ivCardarray[37]=g_resource.getDrawable(R.drawable.d10);
ivCardarray[38]=g_resource.getDrawable(R.drawable.dj);
ivCardarray[39]=g_resource.getDrawable(R.drawable.dq);
ivCardarray[40]=g_resource.getDrawable(R.drawable.dk);
ivCardarray[41]=g_resource.getDrawable(R.drawable.joker);
ivCardarray[42]=g_resource.getDrawable(R.drawable.fa);
ivCardarray[43]=g_resource.getDrawable(R.drawable.f2);
ivCardarray[44]=g_resource.getDrawable(R.drawable.f3);
ivCardarray[45]=g_resource.getDrawable(R.drawable.f4);
ivCardarray[46]=g_resource.getDrawable(R.drawable.f5);
ivCardarray[47]=g_resource.getDrawable(R.drawable.f6);
ivCardarray[48]=g_resource.getDrawable(R.drawable.f7);
ivCardarray[49]=g_resource.getDrawable(R.drawable.f8);
ivCardarray[50]=g_resource.getDrawable(R.drawable.f9);
ivCardarray[51]=g_resource.getDrawable(R.drawable.f10);
ivCardarray[52]=g_resource.getDrawable(R.drawable.fj);
ivCardarray[53]=g_resource.getDrawable(R.drawable.fq);
ivCardarray[54]=g_resource.getDrawable(R.drawable.fk);
ivCardarray[55]=g_resource.getDrawable(R.drawable.joker);
layerCard=new LayerDrawable(ivCardarray);


btnShow.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v)
{
// TODO Auto-generated method stub
ivCard1.setImageDrawable(null);
ivCard2.setImageDrawable(null);
ivCard3.setImageDrawable(null);
ivCard4.setImageDrawable(null);
ivCard5.setImageDrawable(null);
//mHandlerBet.obtainMessage(1).sendToTarget();
}

});

btnShow1.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub

mHandlerBet.obtainMessage(1).sendToTarget();


}
});

}





public void Showfunction()
{

/*int   i,k;
  int uniqNoArr[] = new int[10];
  int ranNoArr[] = new int [10];
      //int FinalCardArr[] =  new int[5];
  for(i = 0; i <= 9;)
{
 uniqNoArr[i] = ranNoArr[i] = (int) Math.random() % 52;
for(int j= 0; j < i; j++)
{
if(ranNoArr[j] == ranNoArr[i])
{
 uniqNoArr[i] = ranNoArr[i] = (int)Math.random() % 52;
 j= -1;
}
}
 i++;
}
  FinalCardArr [0] = uniqNoArr[0];
  FinalCardArr [1] = uniqNoArr[1];
  FinalCardArr [2] = uniqNoArr[2];
  FinalCardArr [3] = uniqNoArr[3];
  FinalCardArr [4] = uniqNoArr[4];
for(int x=0;i<5 data-blogger-escaped-i="i" data-blogger-escaped-p="p"> Log.d("random number", Integer.toString(FinalCardArr[i]));
*/


int n=0;
Random r=new Random();
int[]number=new int[5];
/*for(int i=0;i<5 data-blogger-escaped-i="i" data-blogger-escaped-p="p"> {
n=r.nextInt(56);
if(i > 0)
{
for(int j=0;j<=i;j++)
{
if (n == number[j])
{
i--;
continue;
}
}

}
number[i]=n;
}*/
int i=0;
boolean data=false;
do
{
data=false;
n=r.nextInt(56);
if(i > 0)
{
for(int j=0;j<=i;j++)
{
if (n == number[j])
data=true;
}

}
if(!data)
{
number[i]=n;
i++;
}

}while(i<5 data-blogger-escaped-p="p">


for(i=0;i<5 data-blogger-escaped-i="i" data-blogger-escaped-p="p"> Log.d("random number", Integer.toString(number[i]));


ivCard1.setImageDrawable(layerCard.getDrawable(number[0]));
ivCard2.setImageDrawable(layerCard.getDrawable(number[1]));
ivCard3.setImageDrawable(layerCard.getDrawable(number[2]));
ivCard4.setImageDrawable(layerCard.getDrawable(number[3]));
ivCard5.setImageDrawable(layerCard.getDrawable(number[4]));

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}

}

Friday, January 18, 2013

Implement A Image Listener In Android

In the ANDROID development you use this code. It's helpful for you. If you use any image like button then by java coding you use that implement. It is call a image listener. Firstly you have a project in android application who called package com.example.imagebutton (example) then you export these things like Bundle, Activity, Menu, Motion event (Because on touch you call the listener) , View (for image) , On touch listener and etc. Then u use that Java code and enjoy you first app using image listener.




package com.example.imagebutton;


import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;

import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

public class Imagebutton extends Activity {

ImageView ivCard;
TextView editText;
boolean isPressed=false;


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

addListenerOnImage();

}


public void addListenerOnImage()
 {
// TODO Auto-generated method stub
ivCard=(ImageView)findViewById(R.id.imageView1);

ivCard.setOnTouchListener(new OnTouchListener()
{

@Override
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
if(isPressed){
ivCard.setBackgroundResource(R.drawable.ic_launcher);

}
else{
ivCard.setBackgroundResource(R.drawable.cancelglow);
Toast.makeText(Imagebutton.this,
"ImageButton is clicked!", Toast.LENGTH_SHORT).show();
}
isPressed=!isPressed;
return false;
}
});
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_imagebutton, menu);
return true;
}


}

Tuesday, January 15, 2013

How to Call a Image as a Button in Android

In this project how i call a image as a button . Then i use the  button and in this button i call the touch listener who call the image on the screen then you already use that code it is tested. You believe that.



package com.example.imagebutton;


import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.ImageButton;
import android.widget.ImageView;

public class Imagebutton extends Activity
{
ImageView ivCard;
boolean isPressed=false;


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

addListenerOnImage();

}

public void addListenerOnImage()
 {
// TODO Auto-generated method stub
ivCard=(ImageView)findViewById(R.id.imageView1);

ivCard.setOnTouchListener(new OnTouchListener()
            {

@Override
public boolean onTouch(View v, MotionEvent event)
                {
// TODO Auto-generated method stub
if(isPressed)
                                       {
ivCard.setBackgroundResource(R.drawable.ic_launcher);

}
else{
ivCard.setBackgroundResource(R.drawable.cancelglow);
Toast.makeText(Imagebutton.this,
"ImageButton is clicked!", Toast.LENGTH_SHORT).show();
        }
isPressed=!isPressed;
return false;
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu)
 {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_imagebutton, menu);
return true;
}


}

How to Draw a Image on Android

Hello friends Its very use full code for you when you are starting learner in android tutorial. Because you  never start without How to draw a image on android.

First you create a project Camera testing in Eclipse

Go to file section and click on New Android Project


And fill the section.

Then work on activity.xml.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

   <ImageView
        android:id="@+id/imageView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
         />

<ImageView
        android:id="@+id/imageView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
         />

<ImageView
        android:id="@+id/imageView3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
         />

<ImageView
        android:id="@+id/imageView4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
         />



</LinearLayout>/


Now you come on the his MainActivity. just simple click on his main activity and to call image on onCreate activity and simple you are done your first project in android.

  package com.example.image
import android.app.Activity;
import android.widget.ImageView;


public class MainActivity extends Activity 
 {
    ImageView[] ivImage = new ImageView[4];



@Override
protected void onCreate(Bundle savedInstanceState) 
{
           ivImage[0]=(ImageView)findViewById(R.id.imageView1);
   ivImage[1]=(ImageView)findViewById(R.id.imageView2);
   ivImage[2]=(ImageView)findViewById(R.id.imageView3);
   ivImage[3]=(ImageView)findViewById(R.id.imageView4);
}


@Override

public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}

  }