Button issue eclipse - android

I want to create a simple app in eclipse with a button. I want to make it so that after the button is pushed 10 times a message will pop up. The problem is that when I start the app and push the button 10 times , nothing happens. Could you please tell me what I've done wrong?
Here's my activity file:
package com.example.dialog;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
public class Game extends Activity implements android.view.View.OnClickListener{
#Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.game);
Button gamebutton = (Button) findViewById(R.id.gamebutton);
}
#Override
public void onClick(View v){
//TODO Auto-generated method stub
int clicked = 0;
clicked++;
if( clicked==10){
AlertDialog.Builder gamebuild = new AlertDialog.Builder(Game.this);
gamebuild.setMessage("Good");
gamebuild.setCancelable(false);
gamebuild.setPositiveButton("Quit", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
Game.this.finish();
}
});
gamebuild.setNegativeButton("One more!", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
dialog.cancel();
}
});
}
}
}
Thanks for response! I've edited the activity file this way:
package com.example.dialog;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
public class Game extends Activity implements android.view.View.OnClickListener{
int clicked = 0;
#Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.game);
Button gamebutton = (Button) findViewById(R.id.gamebutton);
}
#Override
public void onClick(View v){
//TODO Auto-generated method stub
clicked++;
if( clicked==10){
AlertDialog.Builder gamebuild = new AlertDialog.Builder(Game.this);
gamebuild.setMessage("Good");
gamebuild.setCancelable(false);
gamebuild.setPositiveButton("Quit", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
Game.this.finish();
}
});
gamebuild.setNegativeButton("One more!", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
dialog.cancel();
}
});
}
}
}
Still it's not working in a proper way. Sorry for dumb questions: I'm new to android.
new edit:
package com.example.dialog;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
public class Game extends Activity implements android.view.View.OnClickListener{
int clicked = 0;
#Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.game);
Button gamebutton = (Button) findViewById(R.id.gamebutton);
gamebutton.setOnClickListener(this);
}
#Override
public void onClick(View v){
//TODO Auto-generated method stub
clicked++;
if( clicked==10){
AlertDialog.Builder gamebuild = new AlertDialog.Builder(Game.this);
gamebuild.setMessage("Good");
gamebuild.setCancelable(false);
gamebuild.setPositiveButton("Quit", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
Game.this.finish();
}
});
gamebuild.setNegativeButton("One more!", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
dialog.cancel();
}
});
}
}
}

You put the counter inside the onClick function, causing it to reset every time. So, move
int clicked = 0;
from inside the onClick to before your onCreate such as:
int clicked = 0;
#Override
protected void onCreate....
This will make it set to 0 once, then stay equal to whatever it's last value was as long as the app is open and not killed.

Problem is that variable clicked is declared inside onClick. So it's always zero. You have to declare it globally, inside class Game.

Related

Regarding validation of a paint drawn on a canvas in android

Sir, we are developing an app to teach small children write alphabets.. Here we show a screen with two images where one image is on the right side it is just to show the image of the alphabet, and the the other one is a light image where the user can draw on image for practise. I want to validate the image drawn by the user whether he has drawn correctly on the base image or not.!
(Note: I exactly dont know what is the use of "Scaling" which i used in it.)Can i please get a solution because i tried a lot but cudnt get an answer
The code is:
enter code here
package com.example.androidhive;
import com.example.androidhive.ColorPickerDialog.OnColorSelectedListener;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.os.Bundle;
import android.support.v4.view.MotionEventCompat;
import android.util.Log;
import android.view.Display;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.SeekBar;
import android.widget.Toast;
import android.widget.ViewFlipper;
import android.widget.SeekBar.OnSeekBarChangeListener;
public class FullImgTwoActivity extends Activity implements OnTouchListener {
int pp;
int pickupLinesItemIndex;
int position;
PaintView paintView;
ViewFlipper layout;
int size = 0;
int progress = 0;
ImageView imageView1;
private Bitmap bmp;
private Bitmap operation;
GetScaling scaling;
#SuppressLint("NewApi")
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.full_image);
paintView = new PaintView(this);
scaling=new GetScaling(this);
layout = (ViewFlipper) findViewById(R.id.view1);
Intent i = getIntent();
position = i.getExtras().getInt("id");
final ImageAdapterTwo imageAdapterTwo = new ImageAdapterTwo(this);
imageView1 = (ImageView) findViewById(R.id.imageView1);
imageView1.setImageResource(imageAdapterTwo.mThumbIdsx[position]);
bmp=imageView1.getDrawingCache();
layout.setBackgroundResource(imageAdapterTwo.mThumbIdsy[position]);
paintView.setLayoutParams(new LinearLayout.LayoutParams(500, 500));
layout.addView(paintView);
ImageView back = (ImageView) findViewById(R.id.imageView3);
back.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
// TODO Auto-generated method stub
if (pickupLinesItemIndex < imageAdapterTwo.mThumbIdsw.length) {
if (position <= 0) {
Toast.makeText(getApplicationContext(), "This is the starting alphabet", 90)
.show();
} else {
imageView1
.setImageResource(imageAdapterTwo.mThumbIdsx[--position]);
layout
.setBackgroundResource(imageAdapterTwo.mThumbIdsy[position]);
}
}
}
});
ImageView nexta = (ImageView) findViewById(R.id.imageView4);
nexta.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
if (pickupLinesItemIndex > imageAdapterTwo.mThumbIdsw.length) {
//if (position <=imageAdapter.mThumbIds.length) {
Toast.makeText(getApplicationContext(), "This level is done", 90)
.show();
} else {
imageView1
.setImageResource(imageAdapterTwo.mThumbIdsx[++position]);
layout
.setBackgroundResource(imageAdapterTwo.mThumbIdsy[position]);
//}
}
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// TODO Auto-generated method stub
getMenuInflater().inflate(R.menu.home, menu);
return super.onCreateOptionsMenu(menu);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// TODO Auto-generated method stub
switch (item.getItemId()) {
case R.id.select_color_menu:
showColorPickerDialogDemo();
break;
case R.id.select_clear_menu:
paintView.clear();
paintView.invalidate();
break;
case R.id.pointers_select_menu:
// pointerDialog();
break;
default:
break;
}
return super.onOptionsItemSelected(item);
}
private void pointerDialog() {
// TODO Auto-generated method stub
final Dialog d = new Dialog(FullImgTwoActivity.this);
d.setTitle("Increase pointer size");
d.setContentView(R.layout.pointerssize);
SeekBar bar = (SeekBar) d.findViewById(R.id.seekBar1);
Button save = (Button) d.findViewById(R.id.save);
Button cancel = (Button) d.findViewById(R.id.cancel);
bar.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
#Override
public void onProgressChanged(SeekBar seekBar, int progresValue,
boolean fromUser) {
progress = progresValue;
}
#Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
#Override
public void onStopTrackingTouch(SeekBar seekBar) {
// Display the value in textview
size = seekBar.getMax();
}
});
save.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
paintView.setSize(progress);
d.dismiss();
}
});
cancel.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
d.dismiss();
}
});
d.show();
}
private void showColorPickerDialogDemo() {
int initialColor = Color.WHITE;
ColorPickerDialog colorpick = new ColorPickerDialog(
FullImgTwoActivity.this, initialColor,
new OnColorSelectedListener() {
#Override
public void onColorSelected(int color) {
paintView.setColo(color);
}
});
colorpick.show();
}
public void Checkimage(View v) {
scaling.checktheimage(imageView1);
scaling.checkpaintimage(paintView);
boolean paint=scaling.bmapaint;
boolean image=scaling.bmapimage;
if(paint!=image){
AlertDialog.Builder al=new AlertDialog.Builder(FullImgTwoActivity.this);
al.setTitle("Please Try Again");
al.setIcon(R.drawable.checkno);
al.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
paintView.clear();
paintView.invalidate();
}
}) ;
al.setNegativeButton("NO",new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
Intent in=new Intent(FullImgTwoActivity.this, GridLayoutTwo.class);
startActivity(in);
finish();
}
});
AlertDialog dialog=al.create();
dialog.show();
}else {
AlertDialog.Builder al=new AlertDialog.Builder(FullImgTwoActivity.this);
al.setTitle("Please Go To Next Letter");
al.setIcon(R.drawable.checkno);
al.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
Intent in=new Intent(FullImgTwoActivity.this, GridLayoutTwo.class);
startActivity(in);
finish();
}
}) ;
al.setNegativeButton("NO",new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
paintView.clear();
paintView.invalidate();
}
});
AlertDialog dialog=al.create();
dialog.show();
}
}
//#Override
public boolean onCreateOptionsMenu1(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_full_img_two, menu);
return true;
}
#Override
public boolean onTouch(View arg0, MotionEvent arg1) {
// TODO Auto-generated method stub
return false;
}
}

fragment activity give error in alertdialog

i create an activity with fragment; then add an OnClickListener() for a button ;every thing work fine. but when i try to add an OnClickListener() for positivebutton to alertdialog eclipse give error before i can run program
it's piece of code that have error and i do not now why :(
AlertDialog.Builder exitDialog=new AlertDialog.Builder(getActivity());
exitDialog.setTitle("Alert");
exitDialog.setMessage("Exit Program");
exitDialog.setPositiveButton("Yes", new OnClickListener()
{
#Override
public void onClick(DialogInterface arg0, int arg1)
{
// TODO Auto-generated method stub
System.exit(1);
}
});
exitDialog.setNegativeButton("NO", null);
exitDialog.show();
and it's whole code of my activity
package com.TB.mylistprojct;
import android.os.Bundle;
import android.app.AlertDialog;
import android.app.Fragment;
import android.content.DialogInterface;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
public class ActFooter extends Fragment
{
View EMyView =null;
Button BtnExit =null;
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
InitialUI();
}
#Override
public View onCreateView(LayoutInflater Inflater,ViewGroup Container,Bundle SavedInstanceState)
{
View MyView=Inflater.inflate(R.layout.actfooter, Container,false);
EMyView=MyView;
return MyView;
}
public void InitialUI()
{
BtnExit=(Button)EMyView.findViewById(R.id.Btn_exit);
BtnExit.setOnClickListener(BtnExit_OnClick);
}
public OnClickListener BtnExit_OnClick=new OnClickListener()
{
#Override
public void onClick(View arg0)
{
// TODO Auto-generated method stub
AlertDialog.Builder exitDialog=new AlertDialog.Builder(getActivity());
exitDialog.setTitle("Alert");
exitDialog.setMessage("Exit Program");
exitDialog.setPositiveButton("Yes", new OnClickListener()
{
#Override
public void onClick(DialogInterface arg0, int arg1)
{
System.exit(1);
}
});
exitDialog.setNegativeButton("NO", null);
exitDialog.show();
}
};
}
anybody can help about this error
Replace new OnClickListener() in your alert dialogs positive button click with: new DialogInterface.OnClickListener()
Please call InitialUI Method in onActivityCreated instead of onCreate Method.
#Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
InitialUI();
}
May be it'll helpful to you. Yeah and replace OnClickListerner with DialogInterface.OnClickListener

AlertDialog has not come out because cannot getText of a button

My Alert dialog message doesn't come out because I cannot get the text of the button. After that the program will directly go to my EventActivity. How can settle this problem?
public void onClick(View v) {
// TODO Auto-generated method stub
if (v.getId() == insertButton.getId()) {
if(colorButton.getText().toString().equals("Color")){
colorAlert.show();
}
}
}
This is variable
AlertDialog colorAlert;
AlertDialog in the OnCreate()
AlertDialog.Builder CA = new AlertDialog.Builder(this);
CA.setTitle("Alert Message!");
CA.setMessage("Please insert the level of important for the event.");
CA.setPositiveButton("OK", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
colorAlert.dismiss();
startActivity(new Intent(this, EventActivity.class));
}
});
colorAlert = CA.create();
When you compare a String in Java, use YOUR_STRING.equals("Color");
Update:
change your while into if , because your are running it once.
The move your
startActivity(new Intent(this, EventActivity.class));
in your alertbox positive button handler.
Comparing Strings in Java.
Ok look like you need more help, here is one solution... i let you deal with the EventActivity ;)
import android.os.Bundle;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Context;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.widget.*;
public class MainActivity extends Activity {
final Context context=this;
public static String EXTRA_MESSAGE;
Button colorButton;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_test);
colorButton = (Button) findViewById(R.id.button1);
colorButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(colorButton.getText().toString().equals("Color")){
AlertDialog.Builder alertBuilder = new AlertDialog.Builder(context);
alertBuilder.setMessage("myDialog").setCancelable(false).setPositiveButton("yes", new DialogInterface.OnClickListener(){
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
Intent intent = new Intent(MainActivity.this, EventActivity.class);
String message = colorButton.getText().toString();
intent.putExtra(EXTRA_MESSAGE, message);
startActivity(intent);
}
}).setNegativeButton("cancel", new DialogInterface.OnClickListener(){
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
AlertDialog myAlert = alertBuilder.create();
myAlert.show();
}
}
});
}
Adding to wtsang02 answer, you shouldn't put your alert.show in a while like this one... I see coming the infinite loop ;)

how to use alertdialog

package com.progme.wallkon;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
public class NextActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.next);
ImageView im1;
im1 = (ImageView)findViewById(R.id.a_01_b);
im1.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
}
});
ImageView im2;
im2 = (ImageView)findViewById(R.id.a_02_b);
im2.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
}
});
ImageView im3;
im3 = (ImageView)findViewById(R.id.a_03_b);
im3.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
showDialog(1);
}
});
}
#Override
protected Dialog onCreateDialog(int id) {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Gmelon");
builder.setMessage("setting?");
builder.setPositiveButton("YES",
new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
Log.i("MyTag" , "Click YES");
}
});
builder.setNegativeButton("NO",
new android.content.DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
Log.i("MyTag", "Click NO");
}
});
return builder.create();
}
}
I wrote code in activity.java like this..
I want to use dialog in im1, im2, im3, and each have to get another event.
Then, I have to write 3 dialog?
and how I can set [//TODO Auto...] here that I use is like..
first dialog for im1,
second dialog for im2,
third dialog for im3..
Please help..
You can write a private variable for the alert dialog and reuse it, but not at the same time
private AlertDialog mDialog = new AlertDialog.Builder(this)
.setTitle("Gmelon")
.setMessage("setting?")
.setPositiveButton("YES",
new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
Log.i("MyTag" , "Click YES");
}
})
.setNegativeButton("NO",
new android.content.DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
Log.i("MyTag", "Click NO");
}
}).create();
now you can show the dialog where ever you want in your code.
It looks like you could just use showDialog(x) to me unless there is more to this question.

AlertDialog crashes application

Can someone explain to me why this AlertDialog crashes?
package com.clicker;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class Clicker extends Activity
{
public int clickerNumber = 0;
private TextView clickerText;
private Button clickerButton;
private Button resetButton;
// Called when the activity is first created.
#SuppressWarnings("null")
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// Declare each of the layout objects
clickerText = (TextView)findViewById(R.id.clickerText);
clickerButton = (Button)findViewById(R.id.clickerButton);
resetButton = (Button)findViewById(R.id.resetButton);
clickerText.setText("0");
final AlertDialog.Builder resetQuestion = null;
resetQuestion.setTitle("Reset?");
resetQuestion.setMessage("Are you sure you want to reset the counter?");
resetQuestion.setPositiveButton("Yes", new OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
clickerNumber = 0;
clickerText.setText("0");
}
});
resetQuestion.setNegativeButton("No", new OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
dialog.dismiss();
}
});
clickerButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
clickerNumber++;
clickerText.setText(Integer.toString(clickerNumber));
}
});
resetButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
resetQuestion.show();
}
});
};
};
This is a great fail:
final AlertDialog.Builder resetQuestion = null;
resetQuestion.setTitle("Reset?");
You are trying to use a null object, and that (of course) will throw a NullPointerException
This is how I create dialogs (and I think it's the best way to do it):
LayoutInflater factory = LayoutInflater.from(this);
final View textEntryView = factory.inflate(R.layout.dialogo_layout, null);
final AlertDialog.Builder resetQuestion = new AlertDialog.Builder(YourActivity.this)
// do whatever you want with the resetQuestion AlertDialog
Here, R.layout.dialogo_layout represent a file called dialogo_layout.xml in the res/layout dir that contains the dialog layout.

Categories

Resources