I am new in android game development. Right now developing an Hidden Object game for android phones.
my question is how to remove an object on touch?
if player touch the object how can it will be removed. i tried on click listener with imageview. but not know what to do next..here is my code
setContentView(R.layout.level1);
bowTie = (ImageView) findViewById(R.id.bowtie);
bowTie.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
???? what goes here ???
is it: finisht():
or
destroy();
or
bowTie.remove();
}
});
}
}
Related
I am having difficulty in figuring out what is wrong with my code, My code runs when the onclick listener is not yet implemented but once I implement the onclick listener it crashes.
public class menu extends AppCompatActivity implements View.OnClickListener {
private CardView assess, profile, chatbot, breathing;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.menu);
assess = (CardView) findViewById(R.id.assess);
profile = (CardView) findViewById(R.id.profile);
chatbot = (CardView) findViewById(R.id.chatbot);
breathing = (CardView) findViewById(R.id.breathing);
// assess.setOnClickListener(this);
// profile.setOnClickListener(this);
// chatbot.setOnClickListener(this);
// breathing.setOnClickListener(this);
}
#Override
public void onClick(View v) {
// Intent i;
//
// switch (v.getId()){
//
//
// case R.id.assess :
// i = new Intent(this,depression_assessment.class);
// startActivity(i);
// break;
}
}
//}
When I tried debugging the codes, these lines are the cause of the crash.
// assess.setOnClickListener(this);
// profile.setOnClickListener(this);
// chatbot.setOnClickListener(this);
// breathing.setOnClickListener(this);
It is where the problem is starting because the code works even though the onclick is blank. When I checked the logs it shows this error
java.lang.RuntimeException: Unable to start activity
ComponentInfo{com.example.thesis/com.example.thesis.menu}:
java.lang.NullPointerException: Attempt to invoke virtual method 'void
androidx.cardview.widget.CardView.setOnClickListener(android.view.View$OnClickListener)'
on a null object reference
You haven't shown enough code, but this could be due to serveral reasons:
This line of code breathing = (CardView) findViewById(R.id.breathing); will look for a view with id breathing inside your activity's layout, and according to the error it is null, which means it did not find it within the same activity's layout, so make sure your cardView is in this activity's layout. Another possible reason is that you might have duplicate Ids in your xml files, in this case,find the duplicate and rename the Ids.
I am using the lib bellow to make a qr scanner, I am calling the scan with a button. Everything is ok. I need to add a custom button at the bottom center of capture.
lib: com.journeyapps:zxing-android-embedded:3.1.0#aar
And my code to call the scanner:
scannow.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
IntentIntegrator scanIntegrator = new IntentIntegrator(MainActivity.this);
scanIntegrator.setPrompt("Scanning...");
scanIntegrator.setBeepEnabled(true);
scanIntegrator.setDesiredBarcodeFormats(IntentIntegrator.ALL_CODE_TYPES); scanIntegrator.setCaptureActivity(CaptureActivityAnyOrientation.class);
scanIntegrator.setOrientationLocked(true);
scanIntegrator.setBarcodeImageEnabled(true);
scanIntegrator.initiateScan();
}
});
I wish to play a specific music file from raw folder in Android Studio, on click of a specific button. However I get the following response:
Error message -can not resolve method ' Create(anonymous android.view.View.OnClickListener,int)
My code is as below:
Button t1 = (Button) findViewById(R.id.choose_Tamil1);
t1.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
mediaPlayer =MediaPlayer.create(this,R.raw.melam);
}
});
Try this code:
Button t1 = (Button) findViewById(R.id.choose_Tamil1);
t1.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
mediaPlayer =MediaPlayer.create(CLASSNAME.this,R.raw.melam);
}
});
And replace CLASSNAME with the name of the current class in which you are working in.
The error was because this keyword was referring to the OnClickListener class in general, but it should have referred to the custom OnClickListener class you wrote within your current class.
Hence the change.
TRY THIS
mediaPlayer =MediaPlayer.create(getContext(),R.raw.melam);
public void onClick(View v) {
final Intent intent=new Intent(Intent.ACTION_MAIN,null);
intent.addCategory(Intent.CATEGORY_LAUNCHER);
final ComponentName cn= new ComponentName("com.android.settings","com.android.settings.LOCALE_SETTINGS");
intent.setComponent(cn);
intent.setFlags(intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
}
You can use this, When the user hits the back button, it will return to your app.
startActivityForResult(new Intent(Settings.ACTION_LOCALE_SETTINGS), 0);
It will bring you to the system language menu. Or:
startActivityForResult(new Intent(Settings.ACTION_INPUT_METHOD_SETTINGS), 0);
For keyboards and inputs.
button2.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// button 2 was clicked!
startActivityForResult(new Intent(android.provider.Settings.ACTION_SETTINGS), 0);
}
});
This tutorial can be helpful
http://weimenglee.blogspot.in/2013/06/android-tip-programmatically-displaying.html
If you want to use other constants instead of ACTION_SETTINGS then take a look of android official documentations .http://developer.android.com/reference/android/provider/Settings.html
I have a project with 2 activities, the first one is the "SplashActivity" - where I load some network data - the second one, the MainActivity.
Inside of my MainActivity I have a fragment and inside of this fragment a webview. My first point is, when the user clicks on back button, the SplashScreen is open again.
The back button should behave like:
When the user doesn't navigate inside of my webview, close the app.
When the user navigates in webview, use the back history of the browswer.
I read about back stack here: http://developer.android.com/training/implementing-navigation/temporal.html#back-webviews
I didn't understand at all how it should work, because I have all cases "mixed". Anyone knows what should I do to fix this problem?
Any idea or sample code will be appreciate!
Define Webview wb as a global variable. Then try this;
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if(event.getAction() == KeyEvent.ACTION_DOWN){
switch(keyCode)
{
case KeyEvent.KEYCODE_BACK:
if(wb.canGoBack() == true){
wb.goBack();
}else{
new AlertDialog.Builder(this).setIcon(android.R.drawable.ic_dialog_alert).setTitle("Application will be closed")
.setMessage("Close app?")
.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
finish();
System.exit(0);
}
}).setNegativeButton("No", null).show();
}
return true;
}
}
return super.onKeyDown(keyCode, event);
}
}