Regarding validation of a paint drawn on a canvas in android - 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;
}
}

Related

Android Application and Observable

i'm developing a simple application on android studio. I'm using "application", "observable", and many more. i got some error like this:
java.lang.RuntimeException: Unable to start activity ComponentInfo{id.wdharmana.doahindu/id.wdharmana.doahindu.MainActivity}: java.lang.ClassCastException: android.app.Application cannot be cast to id.wdharmana.doahindu.app.DoaApplication
2nd error
Caused by: java.lang.ClassCastException: android.app.Application cannot be cast to id.wdharmana.doahindu.app.DoaApplication
3rd error
at id.wdharmana.doahindu.MainActivity.onCreate(MainActivity.java:52)
This is my full MainActivity.java:
package id.wdharmana.doahindu;
import android.app.Activity;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.app.SearchManager;
import android.content.Context;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.SearchView;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import id.wdharmana.doahindu.adapter.ListJudulAdapter;
import id.wdharmana.doahindu.app.DoaApplication;
import id.wdharmana.doahindu.data.DefaultData;
import id.wdharmana.doahindu.helper.DoaHelper;
import id.wdharmana.doahindu.model.DoaModel;
import id.wdharmana.doahindu.model.DoaObserver;
import java.util.ArrayList;
import java.util.Observable;
import java.util.Observer;
public class MainActivity extends AppCompatActivity implements Observer {
private ListView lvJudul;
private ArrayList<DoaModel> listJudul;
private DoaHelper doaHelper;
public ListJudulAdapter listJudulAdapter;
private DoaApplication application;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
lvJudul = (ListView)findViewById(R.id.lvListJudul);
listJudul = new ArrayList<DoaModel>();
application = (DoaApplication) getApplication();
application.getDoaObserver().addObserver(this);
doaHelper = new DoaHelper(MainActivity.this);
doaHelper.open();
listJudul = doaHelper.getAllData();
if (listJudul.size()>0) {
bindData();
}else{
insertDefaultData();
}
lvJudul.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
showMeaningDialog(MainActivity.this, listJudul.get(arg2));
}
});
lvJudul.setOnItemLongClickListener(new OnItemLongClickListener() {
public boolean onItemLongClick(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
deleteDialog(listJudul.get(arg2).getId());
return false;
}
});
}
private void insertDefaultData() {
// TODO Auto-generated method stub
new StoreDefaultData().execute();
}
public void update(Observable observable, Object o) {
if (o.equals(DoaObserver.NEED_TO_REFRESH)){
bindData();
}
}
private class StoreDefaultData extends AsyncTask<Void, Void, Void>{
ProgressDialog mProgressDialog;
#Override
protected void onPreExecute() {
// TODO Auto-generated method stub
super.onPreExecute();
mProgressDialog = new ProgressDialog(MainActivity.this);
mProgressDialog.setTitle(getString(R.string.notify_input_data));
mProgressDialog.setMessage(getString(R.string.text_please_wait));
mProgressDialog.show();
}
#Override
protected Void doInBackground(Void... params) {
// TODO Auto-generated method stub
for (int i = 0; i < DefaultData.defaultData.length; i++) {
doaHelper.insert(DoaModel.getDoaModel(DefaultData.defaultData[i][0],
DefaultData.defaultData[i][1]));
}
listJudul = doaHelper.getAllData();
return null;
}
#Override
protected void onPostExecute(Void result) {
// TODO Auto-generated method stub
super.onPostExecute(result);
mProgressDialog.dismiss();
listJudulAdapter = new ListJudulAdapter(MainActivity.this, listJudul);
lvJudul.setAdapter(listJudulAdapter);
}
}
#Override
protected void onDestroy() {
if (doaHelper != null){
doaHelper.close();
}
super.onDestroy();
}
public static void showMeaningDialog(final Activity activity, final DoaModel item) {
final Dialog dialog = new Dialog(activity, R.style.AppCompatAlertDialogStyle);
dialog.setContentView(R.layout.dialog_konten);
dialog.setCancelable(true);
TextView txtKonten = (TextView)dialog.findViewById(R.id.txtMeaning);
TextView txtJudul = (TextView)dialog.findViewById(R.id.txtWord);
Button btnTutup = (Button)dialog.findViewById(R.id.btnTutup);
Button btnEdit = (Button)dialog.findViewById(R.id.btnEdit);
txtKonten.setText(item.getKonten());
txtJudul.setText(item.getJudul());
btnEdit.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
//FormInputUpdateActivity.toFormInputUpdate(activity, item);
dialog.dismiss();
}
});
btnTutup.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
// TODO Auto-generated method stub
dialog.dismiss();
}
});
dialog.show();
}
private void deleteDialog(final int id) {
// TODO Auto-generated method stub
final Dialog dialog = new Dialog(MainActivity.this, R.style.AppCompatAlertDialogStyle);
dialog.setTitle("Hapus");
//dialog.setContentView(R.layout.dialog_delete);
dialog.setCancelable(true);
// Button btnYes = (Button)dialog.findViewById(R.id.btnDeleteYes);
// Button btnCancel = (Button)dialog.findViewById(R.id.btnDeleteCancel);
// btnYes.setOnClickListener(new OnClickListener() {
// public void onClick(View v) {
// // TODO Auto-generated method stub
// doaHelper.delete(id);
// dialog.dismiss();
// Toast.makeText(MainActivity.this, getString(R.string.text_success_delete), Toast.LENGTH_LONG).show();
// application.getDoaObserver().refresh();
// }
// });
//btnCancel.setOnClickListener(new View.OnClickListener() {
// public void onClick(View arg0) {
// TODO Auto-generated method stub
// dialog.dismiss();
// }
//});
dialog.show();
}
public void bindData(){
if (listJudul.size()>0) {
listJudul.clear();
}
listJudul = doaHelper.getAllData();
listJudulAdapter = new ListJudulAdapter(MainActivity.this, listJudul);
lvJudul.setAdapter(listJudulAdapter);
listJudulAdapter.notifyDataSetChanged();
}
#Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_activity_main, menu);
SearchManager searchManager =
(SearchManager) getSystemService(Context.SEARCH_SERVICE);
SearchView searchView =
(SearchView) menu.findItem(R.id.action_search).getActionView();
searchView.setSearchableInfo(
searchManager.getSearchableInfo(getComponentName()));
return true;
}
}
This is DoaApplication.java
package id.wdharmana.doahindu.app;
import android.app.Application;
import id.wdharmana.doahindu.model.DoaObserver;
/**
* Created by WDHARMANA on 9/18/2015.
*/
public class DoaApplication extends Application {
DoaObserver doaObserver;
#Override
public void onCreate() {
super.onCreate();
doaObserver = new DoaObserver();
}
public DoaObserver getDoaObserver(){
return doaObserver;
}
}
No error when build. Please tell me if you have some suggestions. Thanks in advance.
Put DoaApplication in your manifest, in the <application> node as android:name="id.wdharmana.doahindu.app.DoaApplication"
Please make sure your AndroidManifest.xml like that:
<application
android:name="id.wdharmana.doahindu.app.DoaApplication"
... >
...
</application>
I think you forgot to add your

Button issue eclipse

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.

Animate weapon ontouch

Hello everyone I would like to implement a method to touch on my image of a ak47, I have written the code but the method does not work on touch as it should. I would like that if you touch the gun that starts the repetition in my code. Thanks in advance
package org.realgunshot;
import android.app.Activity;
import android.app.ActionBar.LayoutParams;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnCompletionListener;
import android.os.Bundle;
import android.os.Handler;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.PopupWindow;
import android.widget.TextView;
public class B2 extends Activity {
ImageView bulletshot,bulletempty ;
Boolean click=true;
TextView Display;
Button b1;
int counter = 15 ;
Object mediaPlayerspara,mediaPlayersload;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.b2);
bulletshot=(ImageView)findViewById(R.id.bulletshot);
bulletempty=(ImageView)findViewById(R.id.bulletempty);
b1 = (Button)findViewById(R.id.b1);
Display = (TextView)findViewById(R.id.counter);
final Animation animRotate = AnimationUtils.loadAnimation(this, R.anim.gun);
final Animation animgunshot = AnimationUtils.loadAnimation(this, R.anim.gunshot);
final Animation animbulletempty = AnimationUtils.loadAnimation(this, R.anim.bulletempty);
b1.setOnTouchListener(new OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
counter --;
Display.setText(""+counter);
if(counter <= 0)
{
counter=1;
mediaPlayerspara = null;
mediaPlayersload = MediaPlayer.create(getBaseContext(),R.raw.sload);
((MediaPlayer) mediaPlayersload).setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
#Override
public void onPrepared(MediaPlayer mediaPlayersload) {
// TODO Auto-generated method stub
mediaPlayersload.start();
mediaPlayersload.setOnCompletionListener(new OnCompletionListener() {
public void onCompletion(MediaPlayer mediaPlayersload) {
mediaPlayersload.release();
};
});
}
});
}
else{
bulletshot.setVisibility(View.VISIBLE);
bulletshot.startAnimation(animgunshot);
Handler handlers = new Handler();
handlers.postDelayed(new Runnable() {
public void run() {
bulletshot.setVisibility(View.GONE);
bulletempty.setVisibility(View.GONE);
}
}, 300);
bulletempty.setVisibility(View.VISIBLE);
bulletempty.startAnimation(animbulletempty);
Handler handlers2 = new Handler();
handlers2.postDelayed(new Runnable() {
public void run() {
bulletshot.setVisibility(View.GONE);
bulletempty.setVisibility(View.GONE);
}
}, 2000);
v.startAnimation(animRotate);
final Button button1 = (Button)findViewById(R.id.b1);
button1.setBackgroundResource(R.drawable.ak47_1);
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
public void run() {
button1.setBackgroundResource(R.drawable.ak47_0);
}
}, 100);
mediaPlayerspara = MediaPlayer.create(getBaseContext(),R.raw.ak47);
((MediaPlayer) mediaPlayerspara).setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
#Override
public void onPrepared(MediaPlayer mediaPlayerspara) {
// TODO Auto-generated method stub
mediaPlayerspara.start();
mediaPlayerspara.setOnCompletionListener(new OnCompletionListener() {
public void onCompletion(MediaPlayer mediaPlayerspara) {
mediaPlayerspara.release();
};
});
}
});}
if(event.getAction() == MotionEvent.ACTION_UP){
return true;
}
return false;
}
});
Button apri = (Button)findViewById(R.id.loadoadd);
apri.setOnClickListener(new Button.OnClickListener(){
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
LayoutInflater layoutInflater= (LayoutInflater)getBaseContext().getSystemService(LAYOUT_INFLATER_SERVICE);
final View popupView = layoutInflater.inflate(R.layout.loadoadd, null);
final PopupWindow popupWindow = new PopupWindow(popupView,LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
popupWindow.setAnimationStyle(R.style.Animation);
Button chiudi1 = (Button)popupView.findViewById(R.id.dismiss);
chiudi1.setOnClickListener(new Button.OnClickListener(){
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
popupWindow.dismiss();
}});
Button load = (Button)popupView.findViewById(R.id.load);
load.setOnClickListener(new Button.OnClickListener(){
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
counter = 15;
Display.setText(""+counter);
}});
Button add = (Button)popupView.findViewById(R.id.add);
add.setOnClickListener(new Button.OnClickListener(){
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
counter = 1015;
Display.setText(""+counter);
}});
if (click){
popupWindow.showAtLocation(chiudi1, Gravity.CENTER, 0, 0);
click=false;
}else{
popupWindow.dismiss();
click=true;
}
}});
}
}
I don't think anyone can help you - unless you post your code fully. There are so many things missing...Your OnTouchListener doesn't even check for touch events such as ACTION_DOWN, ACTION_MOVE or anything else.
I would suggest that you take the action when user taps i.e.
public boolean onTouch(View v, MotionEvent event) {
switch(event.getAction())
{
case MotionEvent.ACTION_DOWN:
// Do your firing even
break;
case MotionEvent.ACTION_UP:
//action when user removes finger.
break;
default:
return false;
} // end of switch case
return true;
}
Also, just add a break point in onTouch() and debug to know what is happening and what part is not executing and fix it accordingly.
package org.realgunshot;
import android.app.Activity;
import android.app.ActionBar.LayoutParams;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnCompletionListener;
import android.os.Bundle;
import android.os.Handler;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.PopupWindow;
import android.widget.TextView;
public class B2 extends Activity {
ImageView proiettilesparo,proiettilevuoto ;
Boolean click=true;
TextView Display;
Button b1;
int contatore = 15 ;
Object mediaPlayerspara,mediaPlayerscarica;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.b2);
proiettilesparo=(ImageView)findViewById(R.id.proiettilesparo);
proiettilevuoto=(ImageView)findViewById(R.id.proiettilecade);
b1 = (Button)findViewById(R.id.b1);
Display = (TextView)findViewById(R.id.contatore);
final Animation animRotate = AnimationUtils.loadAnimation(this, R.anim.pistola);
final Animation animsparoproiettile = AnimationUtils.loadAnimation(this, R.anim.sparoproiettile);
final Animation animproiettilecade = AnimationUtils.loadAnimation(this, R.anim.proiettilecade);
b1.setOnTouchListener(new OnTouchListener() {
private Handler mHandler;
#Override
public boolean onTouch(View v, MotionEvent event) {
switch(event.getAction()) {
case MotionEvent.ACTION_DOWN:
if (mHandler != null) return true;
mHandler = new Handler();
mHandler.postDelayed(mAction, 1);
break;
case MotionEvent.ACTION_UP:
if (mHandler == null) return true;
mHandler.removeCallbacks(mAction);
mHandler = null;
break;
}
return false;
}
Runnable mAction = new Runnable() {
#Override public void run() {
contatore --;
Display.setText(""+contatore);
if(contatore <= 0)
{
contatore=1;
mediaPlayerspara = null;
mediaPlayerscarica = MediaPlayer.create(getBaseContext(),R.raw.scarica);
((MediaPlayer) mediaPlayerscarica).setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
#Override
public void onPrepared(MediaPlayer mediaPlayerscarica) {
// TODO Auto-generated method stub
mediaPlayerscarica.start();
mediaPlayerscarica.setOnCompletionListener(new OnCompletionListener() {
public void onCompletion(MediaPlayer mediaPlayerscarica) {
mediaPlayerscarica.release();
};
});
}
});
}
else{
proiettilesparo.setVisibility(View.VISIBLE);
proiettilesparo.startAnimation(animsparoproiettile);
Handler handlers = new Handler();
handlers.postDelayed(new Runnable() {
public void run() {
proiettilesparo.setVisibility(View.GONE);
proiettilevuoto.setVisibility(View.GONE);
}
}, 300);
proiettilevuoto.setVisibility(View.VISIBLE);
proiettilevuoto.startAnimation(animproiettilecade);
Handler handlers2 = new Handler();
handlers2.postDelayed(new Runnable() {
public void run() {
proiettilesparo.setVisibility(View.GONE);
proiettilevuoto.setVisibility(View.GONE);
}
}, 2000);
b1.startAnimation(animRotate);
final Button button1 = (Button)findViewById(R.id.b1);
button1.setBackgroundResource(R.drawable.ak47_1);
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
public void run() {
button1.setBackgroundResource(R.drawable.ak47_0);
}
}, 100);
mediaPlayerspara = MediaPlayer.create(getBaseContext(),R.raw.ak47);
((MediaPlayer) mediaPlayerspara).setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
#Override
public void onPrepared(MediaPlayer mediaPlayerspara) {
// TODO Auto-generated method stub
mediaPlayerspara.start();
mediaPlayerspara.setOnCompletionListener(new OnCompletionListener() {
public void onCompletion(MediaPlayer mediaPlayerspara) {
mediaPlayerspara.release();
};
});
}
});}
mHandler.postDelayed(this, 100);
}
};
});
Button apri = (Button)findViewById(R.id.caricaoaggiungi);
apri.setOnClickListener(new Button.OnClickListener(){
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
LayoutInflater layoutInflater= (LayoutInflater)getBaseContext().getSystemService(LAYOUT_INFLATER_SERVICE);
final View popupView = layoutInflater.inflate(R.layout.caricaoaggiungi, null);
final PopupWindow popupWindow = new PopupWindow(popupView,LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
popupWindow.setAnimationStyle(R.style.Animation);
Button chiudi1 = (Button)popupView.findViewById(R.id.dismiss);
chiudi1.setOnClickListener(new Button.OnClickListener(){
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
popupWindow.dismiss();
}});
Button carica = (Button)popupView.findViewById(R.id.carica);
carica.setOnClickListener(new Button.OnClickListener(){
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
contatore = 15;
Display.setText(""+contatore);
}});
Button aggiungi = (Button)popupView.findViewById(R.id.aggiungi);
aggiungi.setOnClickListener(new Button.OnClickListener(){
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
contatore = 1015;
Display.setText(""+contatore);
}});
if (click){
popupWindow.showAtLocation(chiudi1, Gravity.CENTER, 0, 0);
click=false;
}else{
popupWindow.dismiss();
click=true;
}
}});
}
I changed it this way, but every time the animation starts from 0, there is a way to continue an action and does not stop and start it again?

Exit app when back pressed

sorry for this again button i ant figure out the correct placement here. i just want it on this class only to exit the app when back pressed but i cant figure out what exactly to write or where to write it. thanks for the help. code of class below.
package com.example.whattodo2;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RelativeLayout;
public class Title extends Activity {
Button reset, rts;
ImageView title;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_title);
reset = (Button) findViewById(R.id.reset);
reset.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
double rand = Math.random();
if(rand < 0.5){
Intent reset1 = new Intent(Title.this, MainActivity.class);
startActivity(reset1);
} else {
Intent reset2 = new Intent(Title.this, Question36.class);
startActivity(reset2);
}
}
});
rts = (Button) findViewById(R.id.rts);
rts.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Intent rts=new Intent(Title.this,Rts.class);
startActivity(rts);
}
});
final Animation a = AnimationUtils.loadAnimation(this, R.animator.animation);
a.reset();
final ImageView rImage = (ImageView) findViewById(R.id.title);
RelativeLayout layout = (RelativeLayout) findViewById(R.id.root);
layout.setOnClickListener(new OnClickListener() {
#Override public void onClick(View v) {
rImage.startAnimation(a);
func(); //A call to the function.
}
});
}
protected void func() {
// TODO Auto-generated method stub
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.title, menu);
return true;
}
}
Use like this:
public class Title extends Activity {
#Override
public void onBackPressed() {
// TODO Auto-generated method stub
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_HOME);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
super.onBackPressed();
}
}
hope this will give you some solution.
Before onCreate in all the activities:
public boolean onKeyDown(int keyCode, KeyEvent event) {
if ((keyCode == KeyEvent.KEYCODE_BACK)) {
Intent intent=new Intent (Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_HOME);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
// finish();
}
return true;
}

ListView OnItemClickListener Not working When ListView is inflated to onCreateInputView()

I am developing a Keyboard. When i am inflating a ListView in onCreateInputView() and returning same View(or parent View) to it. I have implemented setOnItemClickListener for the ListView i am not getting callback to it.
What might be the issue?
android framework doesn't work for listview for keyboard(InputMethodService) ?
FY Reference i am attaching the code please have look at it.
NOTE: i am able to get ontouchlistner of it.
package com.listkeyboard;
import java.util.ArrayList;
import android.content.Context;
import android.content.res.Configuration;
import android.inputmethodservice.InputMethodService;
import android.view.LayoutInflater;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.LinearLayout;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.Toast;
public class ListKeyBoardIME extends InputMethodService {
private LinearLayout mainLayout;
#Override
public void onConfigurationChanged(Configuration newConfig) {
// TODO Auto-generated method stub
super.onConfigurationChanged(newConfig);
}
#Override
public void onCreate() {
// TODO Auto-generated method stub
super.onCreate();
System.out.println("onCreate");
}
#Override
public View onCreateCandidatesView() {
// TODO Auto-generated method stub
System.out.println("onCreateCandidatesView");
return super.onCreateCandidatesView();
}
#Override
public void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
System.out.println("onDestroy");
}
#Override
public void onFinishInput() {
// TODO Auto-generated method stub
super.onFinishInput();
System.out.println("onFinishInput");
}
#Override
public void onFinishInputView(boolean finishingInput) {
// TODO Auto-generated method stub
super.onFinishInputView(finishingInput);
System.out.println("onFinishInputView");
}
#Override
public View onCreateInputView() {
// TODO Auto-generated method stub
System.out.println(" onCreateInputView");
LayoutInflater layoutInflater = (LayoutInflater) ListKeyBoardIME.this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View mMainKeyboardLayout = layoutInflater.inflate(R.layout.keyboard_list_view, null);
mainLayout = (LinearLayout)mMainKeyboardLayout.findViewById(R.id.main_layout);
ListView listView = (ListView)mainLayout.findViewById(R.id.list);
ArrayList<Character> alphabets = new ArrayList<Character>();
for (char ch = 'a'; ch <= 'z'; ch++) {
alphabets.add(ch);
if (ch == 'n') {
break;
}
}
ArrayAdapter<Character> adapter = new ArrayAdapter<Character>(ListKeyBoardIME.this, android.R.layout.simple_list_item_1,alphabets);
listView.setAdapter(adapter);
listView.setItemsCanFocus(false);
listView.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
Toast.makeText(getApplicationContext(), "on itemclicked clicked" , Toast.LENGTH_SHORT).show();
}
});
setInputView(mainLayout);
return mainLayout;
}
#Override
public void onStartCandidatesView(EditorInfo info, boolean restarting) {
// TODO Auto-generated method stub
super.onStartCandidatesView(info, restarting);
System.out.println("onStartCandidatesView ");
}
#Override
public void onStartInput(EditorInfo attribute, boolean restarting) {
// TODO Auto-generated method stub
super.onStartInput(attribute, restarting);
System.out.println("onStartInput ");
}
#Override
public void onStartInputView(EditorInfo info, boolean restarting) {
// TODO Auto-generated method stub
super.onStartInputView(info, restarting);
System.out.println("onStartInputView");
}
}
I am also face the same problem & after lot of searching when no answer work for me, i implement below code in my list adapter & its work for me. If anyone find any other better answer please let us know.
view.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
}
});

Categories

Resources