Hello my friends! Today I will show you how to create a simple counter clicker app for android. The counter clicker is use for count the number of something such as people in concert, cars on the road, etc. This app is easy to create and you can improve your better user interface by yourself. Then, here we go!
First, start eclipse and create a new android project named "counterClicker" and set it to Android 2.2 or API level 8. Next, set the package to "my.test.counter"
Next, open /res/layout/main.xml and edit code like this: (You can enlarge this picture by clicking on it.)
Attention: Oh! my friends!! I forget to change the button text. So, in this below xml code, change the first button (id:button2) from "android:text="Button"" to "android:text="Count"" and the second button (id:button1) from "android:text="Button"" to "android:text="Clear"".
Next, open CounterClickerActivity.java and edit the code:
(look at This Tutorial to find how to add sound to the project with MediaPlayer object)
package my.test.counter;
import android.app.Activity;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class CounterClickerActivity extends Activity {
private TextView txt1, txt2, txt3, txt4;
private Button btnCount, btnClear;
private int i1,i2,i3,i4;
private MediaPlayer mp;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
txt1 = (TextView) findViewById(R.id.txt1);
txt2 = (TextView) findViewById(R.id.txt2);
txt3 = (TextView) findViewById(R.id.txt3);
txt4 = (TextView) findViewById(R.id.txt4);
btnCount = (Button) findViewById(R.id.button2);
btnCount.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
countingNumber();
}
});
btnClear = (Button) findViewById(R.id.button1);
btnClear.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
clearingNumber();
}
});
}
private void countingNumber() {
if(mp != null) mp.release();
try {
mp = MediaPlayer.create(this, R.raw.stapler);
mp.start();
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
if(!(i1==9 && i2==9 && i3==9 && i4==9))i4++;
if(i4>9) {i3++;i4=0;}
if(i3>9) {i2++;i3=0;}
if(i2>9) {i1++;i2=0;}
txt4.setText((i4%10)+"");
txt3.setText((i3%10)+"");
txt2.setText((i2%10)+"");
txt1.setText((i1%10)+"");
}
private void clearingNumber() {
if(mp != null) mp.release();
try {
mp = MediaPlayer.create(this, R.raw.stapler);
mp.start();
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
i1=0;i2=0;i3=0;i4=0;
txt1.setText(i1+"");
txt2.setText(i2+"");
txt3.setText(i3+"");
txt4.setText(i4+"");
}
}
OK!!! It's done! Now, run the project and test it. Click on Count Button to increase the number and Clear Button to reset the counter. Have fun!
-vyppN-
hello, I just read your post about:
ReplyDeletecreate-android-counter-clicker-app
I need to count how many times the user clicks on a especific button in 24 hours, and then reset the counter. The user not always be in the app, So I figure i need to use a small data base to recover the counter every day.
What should I do?
thanks in advance…
Just store it in Sqlite, bro. To store it, you could create a table that store button name, count, and date. When user click a button, update your database if it's in the same date. Otherwise, insert a new one.
ReplyDelete